void toluaI_getregistry (lua_State* L, const char* field) { lua_getregistry(L); lua_pushstring(L,field); lua_gettable(L,-2); lua_insert(L,-2); lua_pop(L,1); }
static void hookf (lua_State *L, void *key) { lua_getregistry(L); lua_pushuserdata(L, key); lua_gettable(L, -2); if (lua_isfunction(L, -1)) { lua_pushvalue(L, 1); lua_rawcall(L, 1, 0); } else lua_pop(L, 1); /* pop result from gettable */ lua_pop(L, 1); /* pop table */ }
static void sethook (lua_State *L, void *key, lua_Hook hook, lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) { lua_settop(L, 1); if (lua_isnil(L, 1)) (*sethookf)(L, NULL); else if (lua_isfunction(L, 1)) (*sethookf)(L, hook); else luaL_argerror(L, 1, "function expected"); lua_getregistry(L); lua_pushuserdata(L, key); lua_pushvalue(L, -1); /* dup key */ lua_gettable(L, -3); /* get old value */ lua_pushvalue(L, -2); /* key (again) */ lua_pushvalue(L, 1); lua_settable(L, -5); /* set new value */ }
tLuaCOMClassFactory::tLuaCOMClassFactory(lua_State* L) { CHECKPARAM(L); m_cRef = 0; L_inproc = L; lua_getregistry(L); lua_pushstring(L,"object"); lua_gettable(L,-2); object = (IDispatch*)luaCompat_getPointer(L,-1); object->AddRef(); lua_pop(L,1); lua_pushstring(L,"object"); lua_pushnil(L); lua_settable(L,-3); lua_pop(L,1); }
LUALIB_API int luaopen_base (lua_State *L) { int i; base_open(L); luaL_openlib(L, LUA_COLIBNAME, co_funcs, 0); lua_newtable(L); lua_setglobal(L, REQTAB); lua_getregistry(L); for (i = 0; i < LUA_NTYPES - 1; i++) { lua_pushstring(L, type_names[i]); lua_getdefaultmetatable(L, i); lua_settable(L, -3); } lua_pop(L, 1); return 0; }
static int addoption(lua_State* L) { const char* name = luaL_checkstring(L, 1); const char* desc = luaL_checkstring(L, 2); /* Retrieve the options list from the registry */ lua_getregistry(L); lua_pushstring(L, "options"); lua_gettable(L, -2); /* Create a new table for this new option */ lua_newtable(L); lua_pushstring(L, name); lua_rawseti(L, -2, 1); lua_pushstring(L, desc); lua_rawseti(L, -2, 2); /* Add the option to the end of the registry list */ lua_rawseti(L, -2, luaL_getn(L, -2) + 1); lua_pop(L, 2); return 0; }
static int newpackage(lua_State* L) { int count, i; lua_newtable(L); /* Add this package to the master list in the registry */ lua_getregistry(L); lua_pushstring(L, "packages"); lua_gettable(L, -2); count = luaL_getn(L, -1); lua_pushvalue(L, -3); lua_rawseti(L, -2, count + 1); lua_pop(L, 2); /* Set default values */ if (count == 0) { lua_getglobal(L, "project"); lua_pushstring(L, "name"); lua_pushstring(L, "name"); lua_gettable(L, -3); lua_settable(L, -4); lua_pop(L, 1); } else { lua_pushstring(L, "name"); lua_pushstring(L, "Package"); lua_pushnumber(L, count); lua_concat(L, 2); lua_settable(L, -3); } lua_pushstring(L, "script"); lua_pushstring(L, currentScript); lua_settable(L, -3); lua_pushstring(L, "path"); lua_pushstring(L, path_getdir(currentScript)); lua_settable(L, -3); lua_pushstring(L, "language"); lua_pushstring(L, "c++"); lua_settable(L, -3); lua_pushstring(L, "kind"); lua_pushstring(L, "exe"); lua_settable(L, -3); lua_pushstring(L, "objdir"); lua_pushstring(L, "obj"); lua_settable(L, -3); buildNewConfig(NULL); /* Build list of configurations matching what is in the project, and * which can be indexed by name or number */ lua_pushstring(L, "config"); lua_newtable(L); lua_getglobal(L, "project"); lua_pushstring(L, "configs"); lua_gettable(L, -2); count = luaL_getn(L, -1); for (i = 1; i <= count; ++i) { lua_rawgeti(L, -1, i); lua_newtable(L); buildNewConfig(lua_tostring(L, -2)); lua_pushvalue(L, -1); lua_rawseti(L, -6, i); lua_settable(L, -5); } lua_pop(L, 2); /* Hook the index metamethod so I can dynamically add file configs */ lua_newtable(L); lua_pushstring(L, "__index"); lua_pushcfunction(L, newfileconfig); lua_settable(L, -3); lua_setmetatable(L, -2); /* Set the 'package' global to point to it */ lua_pushvalue(L, -1); lua_setglobal(L, "package"); lua_settable(L, -3); return 1; }
int script_init() { /* Create a script environment and install the standard libraries */ L = lua_open(); luaopen_base(L); luaopen_table(L); luaopen_io(L); luaopen_string(L); luaopen_math(L); luaopen_loadlib(L); lua_atpanic(L, panic); /* Register my extensions to the Lua environment */ lua_register(L, "addoption", addoption); lua_register(L, "_ALERT", alert); lua_register(L, "copyfile", copyfile); lua_register(L, "docommand", docommand); lua_register(L, "dopackage", dopackage); lua_register(L, "findlib", findlib); lua_register(L, "matchfiles", matchfiles); lua_register(L, "newpackage", newpackage); /* Add some extensions to the built-in "os" table */ lua_getglobal(L, "os"); lua_pushstring(L, "chdir"); lua_pushcfunction(L, chdir_lua); lua_settable(L, -3); lua_pushstring(L, "copyfile"); lua_pushcfunction(L, copyfile); lua_settable(L, -3); lua_pushstring(L, "findlib"); lua_pushcfunction(L, findlib); lua_settable(L, -3); lua_pushstring(L, "getcwd"); lua_pushcfunction(L, getcwd_lua); lua_settable(L, -3); lua_pushstring(L, "rmdir"); lua_pushcfunction(L, rmdir_lua); lua_settable(L, -3); lua_pop(L, 1); /* Register some commonly used Lua4 functions */ lua_register(L, "rmdir", rmdir_lua); lua_getglobal(L, "table"); lua_pushstring(L, "insert"); lua_gettable(L, -2); lua_setglobal(L, "tinsert"); lua_pop(L, 1); lua_getglobal(L, "os"); lua_pushstring(L, "remove"); lua_gettable(L, -2); lua_setglobal(L, "remove"); lua_pop(L, 1); /* Set the global OS identifiers */ lua_pushstring(L, os_get()); lua_setglobal(L, "OS"); lua_pushnumber(L, 1); lua_setglobal(L, os_get()); /* Create a list of option descriptions for addoption() */ lua_getregistry(L); lua_pushstring(L, "options"); lua_newtable(L); lua_settable(L, -3); lua_pop(L, 1); /* Create and populate a global "options" table */ buildOptionsTable(); /* Create an empty list of packages */ lua_getregistry(L); lua_pushstring(L, "packages"); lua_newtable(L); lua_settable(L, -3); lua_pop(L, 1); /* Create a default project object */ buildNewProject(); /* Set hook to intercept creation of globals, used to create packages */ lua_pushvalue(L, LUA_GLOBALSINDEX); lua_newtable(L); lua_pushstring(L, "__index"); lua_pushcfunction(L, getglobal); lua_settable(L, -3); lua_setmetatable(L, -2); lua_pop(L, 1); return 1; }