robin/maivi
a tiny cute lua web framework
git clone https://git.robinwobin.dev/robin/maivi
03ca8ad
main
1const std =@import ( "std" ); 2const Io =std . Io ; 3 4const fern =@import ( "fern" ); 5const options =@import ( "options" ); 6const zlua =@import ( "zlua" ); 7pub const Lua =zlua . Lua ; 8 9const maivi =@import ( "../root.zig" ); 10const helpers =maivi . helpers ; 11pub const read_data =@import ( "./data.zig" ). read ; 12const embed =@import ( "embed.zig" ); 13const lua_display =@import ( "lua_display.zig" ); 14const lua_md4c =if ( options . md4c ) @import ( "lua_md4c.zig" ); 15const lua_print =@import ( "lua_print.zig" ). lua_print ; 16const lua_read =@import ( "lua_read.zig" ). lua_read ; 17const lua_str =@import ( "lua_str.zig" ); 18pub const pcall =@import ( "pcall.zig" ). pcall ; 19 20pub var lua : * Lua =undefined ; 21 22pub fn init () ! void { 23fern . trace (). ctx ( "lua.init" ). log (); 24 25lua = try lua_init (); 26} 27 28pub fn thread () * Lua { 29return lua . newThread (); 30} 31 32pub fn lua_init () ! * Lua { 33const l =try Lua . init ( maivi . allocator ); 34errdefer l . deinit (); 35l . openBase (); 36l . openOS (); 37l . openDebug (); 38l . openString (); 39l . openTable (); 40l . openMath (); 41l . openBit32 (); 42 43// package ================================================================ 44 45l . openPackage (); 46 47_ = l . getGlobal ( "package" ); 48 49{ 50const path =try std . fmt . allocPrint ( maivi . allocator , " { s } /?.lua; { s } /?/init.lua" , .{ maivi . config . luaroot , maivi . config . luaroot }); 51defer maivi . allocator . free ( path ); 52_ = l . pushString ( path ); 53l . setField ( - 2 , "path" ); 54} 55 56if ( options . runtime ) { 57const path =try std . fmt . allocPrint ( maivi . allocator , " { s } /?.lua; { s } /?/init.lua" , .{ maivi . config . runtime , maivi . config . runtime }); 58defer maivi . allocator . free ( path ); 59 60_ = l . getField ( - 1 , "path" ); 61_ = l . pushString ( ";" ); 62_ = l . pushString ( path ); 63_ = l . concat ( 3 ); 64l . setField ( - 2 , "path" ); 65} 66 67l . pop ( 1 ); // pops "package" 68 69// global fn ============================================================== 70 71l . register ( "print" , zlua . wrap ( lua_print )); 72l . register ( "read" , zlua . wrap ( lua_read )); 73 74// maivi ====================================================================== 75 76if ( options . runtime ) 77try do_require ( l , "maivi" ) 78else 79try do_buffer ( l , embed . maivi_module , "@runtime/maivi.lua" ); 80l . setGlobal ( "maivi" ); 81 82_ = l . getGlobal ( "maivi" ); 83 84_ = try lua_str . lua_openstr ( l ); 85l . setField ( - 2 , "str" ); 86 87_ = try lua_display . lua_opendisplay ( l ); 88l . setField ( - 2 , "display" ); 89 90try do_buffer ( l , embed . inspect_module , "@inspect_lua" ); 91l . setField ( - 2 , "inspect" ); 92 93_ = try maivi . Router . lua_openrouter ( l ); 94l . setField ( - 2 , "router" ); 95 96l . pop ( 1 ); // pops "maivi" 97 98// h ========================================================================== 99 100if ( options . runtime ) 101try do_require ( l , "h" ) 102else 103try do_buffer ( l , embed . h_module , "@runtime/h.lua" ); 104l . setGlobal ( "h" ); 105 106// this ======================================================================= 107 108if ( options . runtime ) 109try do_require ( l , "this" ) 110else 111try do_buffer ( l , embed . this_module , "@runtime/this.lua" ); 112 113_ = l . pushString ( maivi . config . base_url ); 114l . setField ( - 2 , "base_url" ); 115_ = l . pushString ( maivi . rev orelse "dev" ); 116l . setField ( - 2 , "rev" ); 117_ = l . pushBoolean ( maivi . dev ); 118l . setField ( - 2 , "dev" ); 119 120l . setGlobal ( "this" ); 121 122// iter ======================================================================= 123 124if ( options . runtime ) 125try do_require ( l , "iter" ) 126else 127try do_buffer ( l , embed . iter_module , "@runtime/iter.lua" ); 128l . setGlobal ( "Iter" ); 129 130// preload ================================================================ 131 132if ( options . md4c ) { 133_ = l . getGlobal ( "package" ); 134_ = l . getField ( - 1 , "loaded" ); 135defer l . pop ( 2 ); // pops loaded, package 136_ = try lua_md4c . lua_openmd4c ( l ); 137l . setField ( - 2 , "md4c" ); 138} 139 140if ( l . getTop () != 0 ) return error . UnexpectedStackSize ; 141 142return l ; 143} 144 145pub fn deinit () void { 146lua . deinit (); 147} 148 149/// * Pops from Stack: `0` 150/// * Pushes to Stack: `1` 151fn do_buffer ( l : * Lua , buf : [: 0 ] const u8 , chunkname : [: 0 ] const u8 ) ! void { 152try l . loadBuffer ( buf , chunkname ); 153try pcall ( l , .{ . results =1 }); 154} 155 156/// * Pops from Stack: `0` 157/// * Pushes to Stack: `1` 158fn do_require ( l : * Lua , modname : [] const u8 ) ! void { 159_ = l . getGlobal ( "require" ); 160_ = l . pushString ( modname ); 161try pcall ( l , .{ . args =1 , . results =1 }); 162} 163 164pub fn setup ( io : Io ) ! void { 165fern . info (). ctx ( "lua.setup" ). msg ( "loading plugins" ). log (); 166// load plugins 167blk : { 168const dir =try Io . Dir . openDirAbsolute ( io , maivi . config . root , .{}); 169defer dir . close ( io ); 170const plugindir =dir . openDir ( io , "plugins" , .{ . iterate =true }) catch | err | switch ( err ) { 171error . FileNotFound =>break : blk , 172else =>return err , 173}; 174defer plugindir . close ( io ); 175var it =plugindir . iterate (); 176while ( try it . next ( io )) | entry | { 177if ( entry . kind != . file ) continue ; 178if ( ! std . mem . endsWith ( u8 , entry . name , ".lua" )) continue ; 179const contents =try helpers . read_file ( io , maivi . allocator , plugindir , entry . name ); 180defer maivi . allocator . free ( contents ); 181const filename =try maivi . allocator . dupeZ ( u8 , entry . name ); 182defer maivi . allocator . free ( filename ); 183try lua . loadBuffer ( contents , filename ); 184try pcall ( lua , .{}); 185} 186} 187 188fern . info (). ctx ( "lua.setup" ). msg ( "running lua init module" ). log (); 189try do_require ( lua , "init" ); 190lua . pop ( 1 ); 191} 192 193pub fn toRef ( l : * Lua , i : ? i32 ) i32 { 194if ( i ) | idx | 195l . pushValue ( idx ); 196return l . ref ( zlua . registry_index ); 197} 198 199pub fn setMethodTable ( l : * Lua , comptime T : type ) ! void { 200const type_info =@typeInfo ( T ); 201comptime { 202if ( type_info != . @ "struct" ) @compileError ( "non-struct type passed as method table" ); 203const fields =type_info . @ "struct" . fields ; 204if ( fields . len > 0 ) @compileError ( "method table cannot contain fields" ); 205} 206 207l . createTable ( 0 , 1 ); 208 209{ 210const decls =type_info . @ "struct" . decls ; 211l . createTable ( 0 , decls . len ); 212inline for ( decls ) |field |{ 213try l . pushAny ( field . name ); 214l . pushFunction ( zlua . wrap ( @field ( T , field . name ))); 215l . setTable ( - 3 ); 216} 217l . setField ( - 2 , "__index" ); 218} 219 220l . setMetatable ( - 2 ); 221} 222 223pub fn pushCtx ( l : * Lua , comptime T : type , ctx : * T ) ! void { 224{ 225const stack_size_on_entry =l . getTop (); 226 227// ctx 228 229const useref =@hasField ( T , "ref" ) and @FieldType ( T , "ref" ) == ? i32 ; 230 231if ( useref and ctx . ref != null ) { 232fern . debug (). msg ( "reusing ctx ref" ). str ( "name" , @typeName ( T )). log (); 233_ = l . getIndexRaw ( zlua . registry_index , ctx . ref .? ); 234} else try l . pushAny ( ctx .* ); 235 236if ( useref and ctx . ref == null ) { 237ctx . ref = maivi . lua . toRef ( l , - 1 ); 238} 239 240if ( l . getTop () != stack_size_on_entry + 1 ) return error . UnexpectedStackSize ; 241} 242 243l . pushLightUserdata ( @ptrCast ( ctx )); 244l . setField ( - 2 , "ptr" ); 245} 246 247pub const Event =enum { 248handle , 249read_data , 250 251pub fn str ( self : @This ()) [] const u8 { 252return switch ( self ) { 253else =>@tagName ( self ), 254}; 255} 256}; 257 258pub fn trigger ( l : * Lua , comptime T : type , ctx : * T , event : Event , props : anytype ) ! void { 259// trigger 260 261_ = l . getGlobal ( "maivi" ); 262defer l . pop ( 1 ); 263_ = l . getField ( - 1 , "trigger" ); 264// -1 => f, -2 => maivi 265errdefer l . pop ( 2 ); 266 267try pushCtx ( l , T , ctx ); 268// -1 => ctx, -2 => f, -3 => maivi 269 270const hook_str =event . str (); 271_ = l . pushString ( hook_str ); 272try l . pushAny ( props ); 273 274try pcall ( l , .{ . args =3 }); 275}