robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e533ec5160
master
1//! "nlua" is an abbreviation for nvim flavored lua, i e lua with the 2//! extended standard library functionality added by nvim such as json, mpack 3//! and libuv and a range of vim.* utility functions. 4//! 5//! nlua0 is an interpreter for the "bootstrap" lua code we need to run before 6//! nvim can be built, in order to run lua scripts which process and generate 7//! more .c code, which still need these extensions. 8const std =@import ( "std" ); 9const ziglua =@import ( "ziglua" ); 10const options =@import ( "options" ); 11 12const embedded_data =@import ( "embedded_data" ); 13 14// these are common dependencies used by many generators 15const hashy =@embedFile ( "gen/hashy.lua" ); 16const keyset =@embedFile ( "gen/keyset.lua" ); 17const c_grammar =@embedFile ( "gen/c_grammar.lua" ); 18 19const Lua =ziglua . Lua ; 20 21extern "c" fn luaopen_mpack ( ptr : * anyopaque ) c_int ; 22extern "c" fn luaopen_lpeg ( ptr : * anyopaque ) c_int ; 23extern "c" fn luaopen_bit ( ptr : * anyopaque ) c_int ; 24extern "c" fn luaopen_luv ( ptr : * anyopaque ) c_int ; 25 26fn init_lua () ! * Lua { 27// Initialize the Lua vm 28var lua =try Lua . init ( std . heap . c_allocator ); 29lua . openLibs (); 30 31// this sets _G.vim by itself, so we don't need to 32try lua . loadBuffer ( embedded_data . shared_module , "shared.lua" ); 33lua . call (.{ . results =1 }); 34 35try lua . loadBuffer ( embedded_data . inspect_module , "inspect.lua" ); 36lua . call (.{ . results =1 }); 37lua . setField ( - 2 , "inspect" ); 38 39try lua . loadBuffer ( embedded_data . iter_module , "iter.lua" ); 40lua . call (.{ . results =1 }); 41lua . setField ( - 2 , "iter" ); 42 43_ = lua . getGlobal ( "package" ); 44_ = lua . getField ( - 1 , "preload" ); 45try lua . loadBuffer ( hashy , "hashy.lua" ); // [package, preload, hashy] 46lua . setField ( - 2 , "gen.hashy" ); 47try lua . loadBuffer ( keyset , "keyset.lua" ); // [package, preload, keyset] 48lua . setField ( - 2 , "gen.keyset" ); 49try lua . loadBuffer ( c_grammar , "c_grammar.lua" ); // [package, preload, c_grammar] 50lua . setField ( - 2 , "gen.c_grammar" ); 51lua . pop ( 2 ); 52 53const retval =luaopen_mpack ( lua ); 54if ( retval != 1 ) return error . LoadError ; 55_ = lua . getField ( - 1 , "NIL" ); // [vim, mpack, NIL] 56lua . setField ( - 3 , "NIL" ); // vim.NIL = mpack.NIL (wow BOB wow) 57lua . setField ( - 2 , "mpack" ); 58 59const retval2 =luaopen_lpeg ( lua ); 60if ( retval2 != 1 ) return error . LoadError ; 61lua . setField ( - 3 , "lpeg" ); 62 63const retval3 =luaopen_luv ( lua ); 64if ( retval3 != 1 ) return error . LoadError ; 65lua . setField ( - 3 , "uv" ); 66 67lua . pop ( 2 ); 68 69if ( ! options . use_luajit ) { 70lua . pop ( luaopen_bit ( lua )); 71} 72return lua ; 73} 74 75pub fn main ( init : std . process . Init ) ! void { 76const args =init . minimal . args ; 77 78const lua =try init_lua (); 79defer lua . deinit (); 80 81if ( args . vector . len < 2 ) { 82std . debug . ( "USAGE: nlua0 script.lua args...\n\n" , .{}); 83return ; 84} 85lua . createTable ( @intCast ( args . vector . len - 2 ), 1 ); 86 87var iter =try init . minimal . args . iterateAllocator ( init . arena . allocator ()); 88_ = iter . skip (); 89var i : u32 =0 ; 90var firstarg : [: 0 ] const u8 =undefined ; 91while ( iter . next ()) | val | : ( i += 1 ) { 92_ = lua . pushString ( val ); 93if ( i == 0 ) { 94firstarg = try lua . toString ( - 1 ); // preserved on lua heap.. 95} 96lua . setIndexRaw ( - 2 , @intCast ( i )); 97} 98lua . setGlobal ( "arg" ); 99 100_ = lua . getGlobal ( "debug" ); 101_ = lua . getField ( - 1 , "traceback" ); 102try lua . loadFile ( firstarg ); 103lua . protectedCall (.{ . msg_handler =- 2 }) catch | e | { 104if ( e == error . LuaRuntime ) { 105const msg =try lua . toString ( - 1 ); 106std . debug . ( " { s } \n" , .{ msg }); 107} 108return e ; 109}; 110} 111 112fn do_ret1 ( lua : * Lua , str : [: 0 ] const u8 ) ! void { 113try lua . loadString ( str ); 114try lua . protectedCall (.{ . results =1 }); 115} 116 117test "simple test" { 118const lua =try init_lua (); 119defer lua . deinit (); 120 121try do_ret1 ( lua , "return vim.isarray( { 2,3 } )" ); 122try std . testing . expectEqual ( true , lua . toBoolean ( - 1 )); 123lua . pop ( 1 ); 124 125try do_ret1 ( lua , "return vim.isarray( { a=2,b=3 } )" ); 126try std . testing . expectEqual ( false , lua . toBoolean ( - 1 )); 127lua . pop ( 1 ); 128 129try do_ret1 ( lua , "return vim.inspect(vim.mpack.decode('\\146\\42\\69'))" ); 130try std . testing . expectEqualStrings ( " { 42, 69 } " , try lua . toString ( - 1 )); 131lua . pop ( 1 ); 132 133try do_ret1 ( lua , "return require'bit'.band(7,12)" ); 134try std . testing . expectEqualStrings ( "4" , try lua . toString ( - 1 )); 135lua . pop ( 1 ); 136}