Advertisement
C0BRA

Untested LuaMeta crap

Apr 9th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. lua_newtable(L);
  2.     lua_pushcstring("SWEP");
  3.         lua_newtable(L);
  4.             lua_settable(L, -3);
  5.     lua_newtable(L);
  6.         // stack is now env, meta
  7.         lua_pushcstring(L, "__index");
  8.             lua_getglobal(L, "_G");
  9.                 lua_settable(L, -3);
  10.         lua_pushcstring(L, "__newindex");
  11.             lua_getglobal(L, "_G");
  12.                 lua_settable(L, -3);
  13.         lua_setmetatable(L -1); // meta is set, env now on top
  14.     // stack is env
  15.     if(lua_loadfile(L, "file.lua"))
  16.     {
  17.         std::cerr << errormsg << "\n";
  18.         lua_pop(L, 2); // pop err str AND env tbl
  19.         abort(); // or continue or something
  20.     }
  21.        
  22.         // set the env
  23.         lua_pushvalue(L, -2); // the copy the env table to the top
  24.             lua_setfenv(L, -2); // pops -1 and sets as env for -2
  25.        
  26.         // now the env is set up, invoke it
  27.         if(lua_pcall(L, 0, 0))
  28.         {
  29.             std::cerr << errormsg << "\n";
  30.             lua_pop(L, 2);
  31.             abort(); // or continue or something
  32.         }
  33.         lua_pop(L, 1); // was called above, don't need the loaded func anymore
  34.  
  35.     // the table is now on the top of the stack (-1)
  36.     // get the SWEP table using lua_gettable
  37.     // ....
  38.    
  39.     // we've done all now, don't need the env table anymore, let's return
  40.     lua_pop(L, 1);
  41. // stack is now clean
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement