Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lua_newtable(L);
- lua_pushcstring("SWEP");
- lua_newtable(L);
- lua_settable(L, -3);
- lua_newtable(L);
- // stack is now env, meta
- lua_pushcstring(L, "__index");
- lua_getglobal(L, "_G");
- lua_settable(L, -3);
- lua_pushcstring(L, "__newindex");
- lua_getglobal(L, "_G");
- lua_settable(L, -3);
- lua_setmetatable(L -1); // meta is set, env now on top
- // stack is env
- if(lua_loadfile(L, "file.lua"))
- {
- std::cerr << errormsg << "\n";
- lua_pop(L, 2); // pop err str AND env tbl
- abort(); // or continue or something
- }
- // set the env
- lua_pushvalue(L, -2); // the copy the env table to the top
- lua_setfenv(L, -2); // pops -1 and sets as env for -2
- // now the env is set up, invoke it
- if(lua_pcall(L, 0, 0))
- {
- std::cerr << errormsg << "\n";
- lua_pop(L, 2);
- abort(); // or continue or something
- }
- lua_pop(L, 1); // was called above, don't need the loaded func anymore
- // the table is now on the top of the stack (-1)
- // get the SWEP table using lua_gettable
- // ....
- // we've done all now, don't need the env table anymore, let's return
- lua_pop(L, 1);
- // stack is now clean
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement