robin/maivi
a tiny cute lua web framework
git clone https://git.robinwobin.dev/robin/maivi
6e07b35
push-srxvuswpkqxx
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 { 165// load plugins 166const dir =try Io . Dir . openDirAbsolute ( io , maivi . config . root , .{}); 167defer dir . close ( io ); 168const plugindir =dir . openDir ( io , "plugins" , .{ . iterate =true }) catch | err | switch ( err ) { 169error . FileNotFound =>return , 170else =>return err , 171}; 172defer plugindir . close ( io ); 173var it =plugindir . iterate (); 174while ( try it . next ( io )) | entry | { 175if ( entry . kind != . file ) continue ; 176if ( ! std . mem . endsWith ( u8 , entry . name , ".lua" )) continue ; 177const contents =try helpers . read_file ( io , maivi . allocator , plugindir , entry . name ); 178defer maivi . allocator . free ( contents ); 179const filename =try maivi . allocator . dupeZ ( u8 , entry . name ); 180defer maivi . allocator . free ( filename ); 181try lua . loadBuffer ( contents , filename ); 182try pcall ( lua , .{}); 183} 184 185// explicitly import `init.lua` 186try do_require ( lua , "init" ); 187lua . pop ( 1 ); 188} 189 190pub fn toRef ( l : * Lua , i : ? i32 ) i32 { 191if ( i ) | idx | 192l . pushValue ( idx ); 193return l . ref ( zlua . registry_index ); 194} 195 196pub fn setMethodTable ( l : * Lua , comptime T : type ) ! void { 197const type_info =@typeInfo ( T ); 198comptime { 199if ( type_info != . @ "struct" ) @compileError ( "non-struct type passed as method table" ); 200const fields =type_info . @ "struct" . fields ; 201if ( fields . len > 0 ) @compileError ( "method table cannot contain fields" ); 202} 203 204l . createTable ( 0 , 1 ); 205 206{ 207const decls =type_info . @ "struct" . decls ; 208l . createTable ( 0 , decls . len ); 209inline for ( decls ) |field |{ 210try l . pushAny ( field . name ); 211l . pushFunction ( zlua . wrap ( @field ( T , field . name ))); 212l . setTable ( - 3 ); 213} 214l . setField ( - 2 , "__index" ); 215} 216 217l . setMetatable ( - 2 ); 218} 219 220pub fn pushCtx ( l : * Lua , comptime T : type , ctx : * T ) ! void { 221{ 222const stack_size_on_entry =l . getTop (); 223 224// ctx 225 226const useref =@hasField ( T , "ref" ) and @FieldType ( T , "ref" ) == ? i32 ; 227 228if ( useref and ctx . ref != null ) { 229fern . debug (). msg ( "reusing ctx ref" ). str ( "name" , @typeName ( T )). log (); 230_ = l . getIndexRaw ( zlua . registry_index , ctx . ref .? ); 231} else try l . pushAny ( ctx .* ); 232 233if ( useref and ctx . ref == null ) { 234ctx . ref = maivi . lua . toRef ( l , - 1 ); 235} 236 237if ( l . getTop () != stack_size_on_entry + 1 ) return error . UnexpectedStackSize ; 238} 239 240l . pushLightUserdata ( @ptrCast ( ctx )); 241l . setField ( - 2 , "ptr" ); 242} 243 244pub const Event =enum { 245handle , 246read_data , 247 248pub fn str ( self : @This ()) [] const u8 { 249return switch ( self ) { 250else =>@tagName ( self ), 251}; 252} 253}; 254 255pub fn trigger ( l : * Lua , comptime T : type , ctx : * T , event : Event , props : anytype ) ! void { 256// trigger 257 258_ = l . getGlobal ( "maivi" ); 259defer l . pop ( 1 ); 260_ = l . getField ( - 1 , "trigger" ); 261// -1 => f, -2 => maivi 262errdefer l . pop ( 2 ); 263 264try pushCtx ( l , T , ctx ); 265// -1 => ctx, -2 => f, -3 => maivi 266 267const hook_str =event . str (); 268_ = l . pushString ( hook_str ); 269try l . pushAny ( props ); 270 271try pcall ( l , .{ . args =3 }); 272}