How to filter out user defined globals in Lua from C++?
snippet in lua

How to filter out user defined globals in Lua from C++?

user6862

// The hash table storing global names
std::set<unsigned int> Blacklist;

// Create hash table "Blacklist"
void BlacklistSnapshot(lua_State *L) {

  lua_pushglobaltable(L);
  lua_pushnil(L);
  while (lua_next(L,-2) != 0) {                     // pop NIL, push name,value
    Blacklist.insert(HashName(lua_tostring(L,-2))); // insert to hash table
    lua_pop(L,1);                                   // remove value
  }
  lua_pop(L,1); // Remove global table
}


// Display user defined globals only
void PrintGlobals(lua_State *L) {

  lua_pushglobaltable(L);
  lua_pushnil(L);
  while (lua_next(L,-2) != 0) { // pop NIL, push name,value

    // Check if the global is present in our blacklist
    if (Blacklist.find(HashName(lua_tostring(L,-2))) == Blacklist.end()) {
      // Not present, print it...
      PrintFormattedVariable(lua_type(L,-1),lua_tostring(L,-2));
    }
    lua_pop(L,1); // remove value
  }
  lua_pop(L,1);   // remove global table
}


void RunScript(void) {

  // Create new Lua state
  L = luaL_newstate();

  // Load all Lua libraries
  luaL_openlibs(L);

  // Create co-routine
  CO = lua_newthread(L);

  BlacklistSnapshot(CO);

  // Load and compile script
  AnsiString script(Frame->Script_Edit->Text);
  if (luaL_loadbuffer(CO,script.c_str(),script.Length(),"Test") == LUA_OK) {
    lua_resume(CO,NULL,0);
  } else {
    cs_error(CO, "Compiler error: ");    // Print compiler error
  }
}