static int ll_seeall (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); if (!lua_getmetatable(L, 1)) { lua_createtable(L, 0, 1); /* create new metatable */ lua_pushvalue(L, -1); lua_setmetatable(L, 1); } lua_pushglobaltable(L); lua_setfield(L, -2, "__index"); /* mt.__index = _G */ return 0; }
static int luaopen_rpm(lua_State *L) /*@modifies L @*/ { #if defined(LUA_GLOBALSINDEX) lua_pushvalue(L, LUA_GLOBALSINDEX); #else lua_pushglobaltable(L); #endif luaL_newlib(L, rpmlib); return 1; }
void l2dbus_getGlobalField ( lua_State* L, const char* name ) { lua_pushglobaltable(L); lua_getfield(L, -1, name); lua_remove(L, -2); }
static int lua_rl_isglobaltable(lua_State *L, int idx) { #if LUA_VERSION_NUM == 501 return lua_rawequal(L, idx, LUA_GLOBALSINDEX); #else idx = lua_absindex(L, idx); lua_pushglobaltable(L); int same = lua_rawequal(L, idx, -1); lua_pop(L, 1); return same; #endif }
LUAMOD_API int luaopen_base (lua_State *L) { /* open lib into global table */ lua_pushglobaltable(L); luaL_setfuncs(L, base_funcs, 0); /* set global _G */ lua_pushvalue(L, -1); lua_setfield(L, -2, "_G"); /* set global _VERSION */ lua_pushliteral(L, LUA_VERSION); lua_setfield(L, -2, "_VERSION"); return 1; }
NUMLUA_API int luaopen_numlua (lua_State *L) { const luaL_Reg *mod; for (mod = numlua_modules; mod->func; mod++) { nl_require(L, mod->name, mod->func, 1); lua_pop(L, 1); } lua_pushglobaltable(L); lua_getfield(L, -1, "require"); lua_pushliteral(L, "numlua.matrix"); lua_call(L, 1, 0); return 0; }
static int do_set_package_preload(lua_State* L) { // Note: Use ordinary set/get instead of the raw variants, because this // function should not be performance sensitive anyway. lua_pushglobaltable(L); lua_getfield(L, -1, "package"); lua_remove(L, -2); // Remove global table. lua_getfield(L, -1, "preload"); lua_remove(L, -2); // Remove package table. lua_insert(L, -3); // Move package.preload beneath key and value. lua_settable(L, -3); // package.preload[modulename] = loader return 0; }
void luaL_pushmodule (lua_State *L, const char *modname, int sizehint) { luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */ if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no _LOADED[modname]? */ lua_pop(L, 1); /* remove previous result */ /* try global variable (and create one if it does not exist) */ lua_pushglobaltable(L); if (luaL_findtable(L, 0, modname, sizehint) != NULL) luaL_error(L, "name conflict for module '%s'", modname); lua_pushvalue(L, -1); lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */ } lua_remove(L, -2); /* remove _LOADED table */ }
int luas_reg_const(luas_state* s, const char* mod, const luas_const* consts, int count) { lua_State* L = s->l; if (mod == NULL || mod[0] == '\0') lua_pushglobaltable(L); else luaL_newmetatable(L, mod); for (int i=0; i<count; ++i) { lua_pushnumber(L, consts[i].value); lua_setfield(L, -2, consts[i].name); } lua_pop(L, 1); return 0; }
rpmlua rpmluaNew() { rpmlua lua = (rpmlua) xcalloc(1, sizeof(*lua)); struct stat st; const luaL_Reg *lib; char *initlua = rpmGenPath(rpmConfigDir(), "init.lua", NULL); static const luaL_Reg extlibs[] = { {"posix", luaopen_posix}, {"rex", luaopen_rex}, {"rpm", luaopen_rpm}, {"os", luaopen_rpm_os}, {NULL, NULL}, }; lua_State *L = lua_open(); luaL_openlibs(L); lua->L = L; for (lib = extlibs; lib->name; lib++) { lua_pushcfunction(L, lib->func); lua_pushstring(L, lib->name); lua_call(L, 1, 0); lua_settop(L, 0); } #ifndef LUA_GLOBALSINDEX lua_pushglobaltable(L); #endif lua_pushliteral(L, "LUA_PATH"); lua_pushfstring(L, "%s/%s", rpmConfigDir(), "/lua/?.lua"); #ifdef LUA_GLOBALSINDEX lua_rawset(L, LUA_GLOBALSINDEX); #else lua_settable(L, -3); #endif lua_pushliteral(L, "print"); lua_pushcfunction(L, rpm_print); #ifdef LUA_GLOBALSINDEX lua_rawset(L, LUA_GLOBALSINDEX); #else lua_settable(L, -3); #endif #ifndef LUA_GLOBALSINDEX lua_pop(L, 1); #endif rpmluaSetData(lua, "lua", lua); if (stat(initlua, &st) != -1) (void)rpmluaRunScriptFile(lua, initlua); free(initlua); return lua; }
void interpreter_init (void) { L = lua_open(); if (L == NULL) { std::cerr << "Internal error: could not create Lua state." << std::endl; exit(EXIT_FAILURE); } luaL_openlibs(L); //opens all standart lua libs // replace string functions with ICU versions utf8_lua_init(L); // Move all members of math to the top level (i.e. become global funtions, vars, etc) lua_pushglobaltable(L); lua_getfield(L, -1, "math"); lua_remove(L,-2); //global table int t = lua_gettop(L); for (lua_pushnil(L) ; lua_next(L,t)!=0 ; lua_pop(L,1)) { int key = lua_gettop(L) - 1; int val = lua_gettop(L); lua_pushglobaltable(L); lua_pushvalue(L, key); lua_pushvalue(L, val); lua_settable(L, -3); lua_remove(L, -1); //global table } //and fmod to mod alias for compat with lua5.1 code lua_getfield(L, -1, "fmod"); lua_setglobal(L, "mod"); lua_pop(L,1); // math table luaL_register(L, "_G", global); lua_pop(L, 1); lua_gc(L, LUA_GCSETSTEPMUL, 100000000); lua_wrappers_image_init(L); }
/* Begin module * It pushes the module (or class) table on the stack */ TOLUA_API void tolua_beginmodule(lua_State *L, const char *name) { if (name) { lua_pushstring(L, name); lua_rawget(L, -2); } else #if LUA_VERSION_NUM > 501 lua_pushglobaltable(L); #else lua_pushvalue(L, LUA_GLOBALSINDEX); #endif }
static int dolibrary (lua_State *L, const char *name) { int status; lua_pushglobaltable(L); lua_getfield(L, -1, "require"); lua_pushstring(L, name); status = docall(L, 1, 1); if (status == LUA_OK) { lua_setfield(L, -2, name); /* global[name] = require return */ lua_pop(L, 1); /* remove global table */ } else lua_remove(L, -2); /* remove global table (below error msg.) */ return report(L, status); }
static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { int top = lua_gettop(L); lua_getinfo(L, "f", ar); /* push function */ lua_pushglobaltable(L); if (findfield(L, top + 1, 2)) { lua_copy(L, -1, top + 1); /* move name to proper place */ lua_pop(L, 2); /* remove pushed values */ return 1; } else { lua_settop(L, top); /* remove function and global table */ return 0; } }
bool Value::loadFromString(const std::string& source) { LuaState L; if (luaL_dostring(L, source.c_str()) == 1) { return false; } lua_pushglobaltable(L); fillTable(L, -1, *this); return true; }
void LuaEnv_Input::exposeToLua(Game *game) { const luaL_Reg inputFuncs[] = { { "GetMousePos", GetMousePos }, { "IsMousePressed", IsMousePressed }, { "IsKeyPressed", IsKeyPressed }, { NULL, NULL } }; lua_State *state = game->getLua().getRaw(); // Register input functions to global environment lua_pushglobaltable(state); luaL_setfuncs(state, inputFuncs, 0); }
int set_pm3_libraries(lua_State *L) { static const luaL_Reg libs[] = { {"SendCommand", l_SendCommand}, {"WaitForResponseTimeout", l_WaitForResponseTimeout}, {"nonce2key", l_nonce2key}, //{"PrintAndLog", l_PrintAndLog}, {"foobar", l_foobar}, {"ukbhit", l_ukbhit}, {"clearCommandBuffer", l_clearCommandBuffer}, {"console", l_CmdConsole}, {"iso15693_crc", l_iso15693_crc}, {"iso14443b_crc", l_iso14443b_crc}, {"aes128_decrypt", l_aes128decrypt_cbc}, {"aes128_decrypt_ecb", l_aes128decrypt_ecb}, {"aes128_encrypt", l_aes128encrypt_cbc}, {"aes128_encrypt_ecb", l_aes128encrypt_ecb}, {"crc16", l_crc16}, {"crc64", l_crc64}, {"sha1", l_sha1}, {"reveng_models", l_reveng_models}, {"reveng_runmodel", l_reveng_RunModel}, {NULL, NULL} }; lua_pushglobaltable(L); // Core library is in this table. Contains ' //this is 'pm3' table lua_newtable(L); //Put the function into the hash table. for (int i = 0; libs[i].name; i++) { lua_pushcfunction(L, libs[i].func); lua_setfield(L, -2, libs[i].name);//set the name, pop stack } //Name of 'core' lua_setfield(L, -2, "core"); //-- remove the global environment table from the stack lua_pop(L, 1); //-- Last but not least, add to the LUA_PATH (package.path in lua) // so we can load libraries from the ./lualib/ - directory setLuaPath(L,"./lualibs/?.lua"); return 1; }
int heart_luaB_register_task_graph(lua_State* L) { luaL_newmetatable(L, "Heart.TaskGraphRef"); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); luaL_setfuncs(L, heartTaskGraphMethods, 0); lua_pop(L, 1); // remove the metatable luaL_newmetatable(L, "Heart.TaskGraphHandle"); lua_pop(L, 1); // remove the metatable // set create in the global table lua_pushglobaltable(L); luaL_setfuncs(L, heartTaskGraphLib, 0); return 0; }
LUALIB_API int luaopen_ext (lua_State *L) { lua_pushglobaltable(L); luaL_register(L, NULL, extlib); lua_pop(L, 1); lua_getglobal(L, "math"); luaL_register(L, NULL, extmath); lua_pop(L, 1); lua_getglobal(L, "table"); luaL_register(L, NULL, exttable); lua_pushliteral(L, "unpack"); lua_pushvalue(L, -1); lua_gettable(L, LUA_GLOBALSINDEX); lua_settable(L, -3); lua_pop(L, 1); return 0; }
/* ** stripped-down 'require'. Calls 'openf' to open a module, ** registers the result in 'package.loaded' table and, if 'glb' ** is true, also registers the result in the global table. ** Leaves resulting module on the top. */ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) { lua_pushcfunction(L, openf); lua_pushstring(L, modname); /* argument to open function */ lua_call(L, 1, 1); /* open module */ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); lua_pushvalue(L, -2); /* make copy of module (call result) */ lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ lua_pop(L, 1); /* remove _LOADED table */ if (glb) { lua_pushglobaltable(L); lua_pushvalue(L, -2); /* copy of 'mod' */ lua_setfield(L, -2, modname); /* _G[modname] = module */ lua_pop(L, 1); /* remove _G table */ } }
int luaopen_iso_fs(lua_State *L) { lua_settop(L, 1); if(!lua_tostring(L, 1)) { lua_pushliteral(L, "ISO_FS"); lua_replace(L, 1); } // Metatable lua_createtable(L, 0, 2); lua_pushvalue(L, -1); lua_replace(L, luaT_environindex); lua_pushcclosure(L, luaT_stdgc<IsoFilesystem, luaT_environindex>, 0); lua_setfield(L, -2, "__gc"); // Methods table luaT_pushcclosuretable(L, l_isofs_new, 0); lua_pushvalue(L, -1); lua_setfield(L, -3, "__index"); lua_pushcfunction(L, l_isofs_set_path_separator); lua_setfield(L, -2, "setPathSeparator"); lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); lua_pushcclosure(L, l_isofs_set_root, 1); lua_setfield(L, -2, "setRoot"); lua_pushcfunction(L, l_isofs_read_contents); lua_setfield(L, -2, "readContents"); lua_pushcfunction(L, l_isofs_list_files); lua_setfield(L, -2, "listFiles"); lua_pushvalue(L, 1); lua_pushvalue(L, 2); #ifndef LUA_GLOBALSINDEX lua_pushglobaltable(L); lua_insert(L, -3); lua_settable(L, -3); lua_pop(L, 1); #else lua_settable(L, LUA_GLOBALSINDEX); #endif return 1; }
LUAMOD_API int luaopen_upackage(lua_State *L) { int i; /* create new type _LOADLIB */ luaL_newmetatable(L, "_LOADLIB"); lua_pushcfunction(L, gctm); lua_setfield(L, -2, "__gc"); /* create `package' table */ luaL_newlib(L, pk_funcs); /* create 'searchers' table */ lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); /* fill it with pre-defined searchers */ for(i=0; searchers[i] != NULL; i++) { lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ lua_pushcclosure(L, searchers[i], 1); lua_rawseti(L, -2, i+1); } #if defined(LUA_COMPAT_LOADERS) lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ lua_setfield(L, -3, "loaders"); /* put it in field `loaders' */ #endif lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ /* set field 'path' */ setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT); /* set field 'cpath' */ setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT); /* store config information */ push_utf8_string(L, LUA_DIRSEP L"\n" LUA_PATH_SEP L"\n" LUA_PATH_MARK L"\n" LUA_EXEC_DIR L"\n" LUA_IGMARK L"\n", -1); lua_setfield(L, -2, "config"); /* set field `loaded' */ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED"); lua_setfield(L, -2, "loaded"); /* set field `preload' */ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); lua_setfield(L, -2, "preload"); lua_pushglobaltable(L); lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ lua_pop(L, 1); /* pop global table */ return 1; /* return 'package' table */ }
bool luaW_getglobal(lua_State *L, const std::vector<std::string>& path) { lua_pushglobaltable(L); for (const std::string& s : path) { if (!lua_istable(L, -1)) goto discard; lua_pushlstring(L, s.c_str(), s.size()); lua_rawget(L, -2); lua_remove(L, -2); } if (lua_isnil(L, -1)) { discard: lua_pop(L, 1); return false; } return true; }
LUAMOD_API int luaopen_base (lua_State *L) { int i; /* open lib into global table */ lua_pushglobaltable(L); luaL_setfuncs(L, base_funcs, 0); /* set global _G */ lua_pushvalue(L, -1); lua_setfield(L, -2, "_G"); /* set global _VERSION */ lua_pushliteral(L, LUA_VERSION); lua_setfield(L, -2, "_VERSION"); /* set function 'type' with proper upvalues */ for (i = 0; i < LUA_NUMTAGS; i++) /* push all type names as upvalues */ lua_pushstring(L, lua_typename(L, i)); lua_pushcclosure(L, luaB_type, LUA_NUMTAGS); lua_setfield(L, -2, "type"); return 1; }
//----------------------------------------------------------------// void MOAILuaRuntime::ReportLeaksFormatted ( FILE *f ) { this->ForceGarbageCollection (); lua_State* L = this->mMainState; // First, correlate leaks by identical stack traces. LeakStackMap stacks; for ( LeakMap::const_iterator i = this->mLeaks.begin (); i != this->mLeaks.end (); ++i ) { stacks [ i->second ].push_back ( i->first ); } fprintf ( f, "-- BEGIN LUA OBJECT LEAKS --\n" ); // Then, print out each unique allocation spot along with all references // (including multiple references) followed by the alloction stack int top = lua_gettop ( L ); UNUSED ( top ); for ( LeakStackMap::const_iterator i = stacks.begin (); i != stacks.end (); ++i ) { const LeakPtrList& list = i->second; MOAILuaObject *o = list.front (); fprintf ( f, "Allocation: %lu x %s\n", list.size (), o->TypeName ()); for( LeakPtrList::const_iterator j = list.begin (); j != list.end (); ++j ) { fprintf ( f, "\t(%6d) %p\n", ( *j )->GetRefCount (), *j ); } // A table to use as a traversal set. lua_newtable ( L ); // And the table to use as seed lua_pushglobaltable ( L ); this->FindAndPrintLuaRefs ( -2, "_G", f, list ); lua_pop ( L, 2 ); // Pop the 'done' set and our globals table fputs ( i->first.c_str (), f ); fputs ( "\n", f ); fflush ( f ); } assert ( top == lua_gettop ( L )); fprintf ( f, "-- END LUA LEAKS --\n" ); }
/** * expect a MainWindow class * and some optional parameters * * dans l'idéal, on récupère des chunks, ou plutôt des objets, qui sont * des componsants * si le composant existe, on le vire et on le remplace par le code * s'il n'existe pas ?... * **/ int reload(lua_State *L) { int top = lua_gettop(L); LUA::liveCoding(true); // check what we have if(!lua_isfunction(L,2)) { lua_pushnil(L); lua_pushfstring(L,"luceLiveReload: expected function, got '%s'", lua_typename(L, lua_type(L,2))); return 2; } // TODO: get and remove params // call function // push result to luceLiveReload // // meanwhile: //lua_pop(L, lua_gettop(L)-2); // defined in luce.h for Lua5.1/5.2 compatibility lua_pushglobaltable(L); lua_pushstring(L, "LUCE_LIVE_CODING"); lua_pushnumber(L, 1); lua_settable(L, -3); if(top>2) { lua_pushstring(L, "LPATH"); lua_pushstring(L, luaL_checkstring(L,3)); lua_settable(L, -3); lua_remove(L,3); } lua_pop(L,1); int nb_args = 0; if ( lua_pcall(L, nb_args, 1, 0) != 0 ) { lua_pushnil(L); lua_pushvalue(L, -2); return 2; } if(lua_isnoneornil(L,-1)) { lua_pushnil(L); lua_pushfstring(L, "Wrong or malformed MainWindow: expected Component, got nil"); return 2; } int res = mainClass->luceLiveReload(L); return res; }
void rpmluaGetVar(rpmlua _lua, rpmluav var) { INITSTATE(_lua, lua); lua_State *L = lua->L; if (!var->listmode) { if (lua->pushsize == 0) lua_pushglobaltable(L); if (pushvar(L, var->keyType, &var->key) != -1) { lua_rawget(L, -2); popvar(L, &var->valueType, &var->value); } if (lua->pushsize == 0) lua_pop(L, 1); } else if (lua->pushsize > 0) { (void) pushvar(L, var->keyType, &var->key); if (lua_next(L, -2) != 0) popvar(L, &var->valueType, &var->value); } }
void LuaRobot::LuaInit() { //cout << "Begin stuff...\n"; //luaConsole->init(); luaL_openlibs(LuaRobot::luastate); luaopen_WPILib(LuaRobot::luastate); lua_pushglobaltable(LuaRobot::luastate); lua_pushlightuserdata(luastate, this); luaL_setfuncs(luastate, LuaRobot::stateFunctions, 1); lua_pushlightuserdata(luastate, this); luaL_setfuncs(luastate, LuaRobot::interactionFunctions, 1); lua_getfield(luastate, -1, "package"); lua_remove(luastate, -2); lua_getfield(luastate, -1, "preload"); lua_pushcfunction(luastate, luaopen_socket_core); lua_setfield(luastate, -2, "socket.core"); lua_remove(luastate, -1); int errorcode = luaL_dofile(LuaRobot::luastate, coreStartupName.c_str()); if (errorcode > 0) { cout << "file name: " << coreStartupName << "\n"; cout << "error code: " << errorcode << "\n"; cout << "error message: " << lua_tolstring(luastate, -1, NULL) << "\n"; lua_settop(luastate, 0); } cout << "begin userInit\n"; if (crashCount <= 2) { userStartupName = defaultUserStartupName; } if (crashCount > 2 && crashCount <= 4) { userStartupName = defaultStableStartupName; } if (crashCount > 4) { userStartupName = defaultSafemodeStartupName; } errorcode = luaL_dofile(LuaRobot::luastate, userStartupName.c_str()); if (errorcode > 0) { cout << "file name: " << userStartupName << "\n"; cout << "error code: " << errorcode << "\n"; cout << "error message: " << lua_tolstring(luastate, -1, NULL) << "\n"; lua_settop(luastate, 0); } cout << "end userInit\n"; }
void LUAHandler::init(ONScripter *ons, ScriptHandler *sh, int screen_ratio1, int screen_ratio2) { this->ons = ons; this->sh = sh; state = luaL_newstate(); luaL_openlibs(state); #if LUA_VERSION_NUM >= 502 lua_pushglobaltable(state); luaL_setfuncs(state, lua_lut, 0); #else lua_pushvalue(state, LUA_GLOBALSINDEX); luaL_register(state, NULL, lua_lut); #endif lua_pushlightuserdata(state, this); lua_setglobal(state, ONS_LUA_HANDLER_PTR); }
void tenshi_open_actor(lua_State *L) { // Initialize the actor object metatable (contains implementations of the // actor object API) lua_pushstring(L, RIDX_ACTORLIB_METATABLE); lua_newtable(L); luaL_setfuncs(L, actor_metatable_funcs, 0); // Also load mailbox metatable functions. We can do this because actors // also contain a __mbox field. luaL_setfuncs(L, mbox_metatable_funcs, 0); // Set __index to this table lua_pushstring(L, "__index"); lua_pushvalue(L, -2); lua_settable(L, -3); lua_settable(L, LUA_REGISTRYINDEX); // Initialize the functions we load into the global scope. lua_pushglobaltable(L); luaL_setfuncs(L, actor_global_funcs, 0); lua_pop(L, 1); }