コード例 #1
0
ファイル: LuaState.cpp プロジェクト: LuaDist/diluculum
   // - LuaState::globals ------------------------------------------------------
   LuaValueMap LuaState::globals()
   {
      // Traverse the globals table adding the key/value pairs to 'ret'
      LuaValueMap ret;

      lua_pushnil (state_);
      while (lua_next (state_, LUA_GLOBALSINDEX) != 0)
      {
         // Exclude from the results the tables that would result in infinite
         // recursion
         if (lua_type (state_, -2) == LUA_TSTRING)
         {
            const char* key = lua_tostring(state_, -2);
            if (strcmp(key, "_G") == 0
                || strcmp(key, "package") == 0)
            {
               lua_pop (state_, 1);
               continue;
            }
         }

         ret[ToLuaValue (state_, -2)] = ToLuaValue (state_, -1);

         lua_pop (state_, 1);
      }

      // Alright, return the result
      return ret;
   }
コード例 #2
0
   // - LuaState::doStringOrFile -----------------------------------------------
   LuaValueList LuaState::doStringOrFile (bool isString, const std::string& str)
   {
      const int stackSizeAtBeginning = lua_gettop (state_);

      if (isString)
      {
         Impl::ThrowOnLuaError (state_, luaL_loadbuffer (state_, str.c_str(),
                                                         str.length(), "line"));
      }
      else
      {
         Impl::ThrowOnLuaError (state_, luaL_loadfile (state_, str.c_str()));
      }

      Impl::ThrowOnLuaError (state_, lua_pcall (state_, 0, LUA_MULTRET, 0));

      const int numResults = lua_gettop (state_) - stackSizeAtBeginning;

      LuaValueList results;

      for (int i = numResults; i > 0; --i)
         results.push_back (ToLuaValue (state_, -i));

      lua_pop (state_, numResults);

      return results;
   }
コード例 #3
0
ファイル: LuaVariable.cpp プロジェクト: lmbarros/Diluculum
 // - LuaVariable::value -----------------------------------------------------
 LuaValue LuaVariable::value() const
 {
    pushTheReferencedValue();
    LuaValue ret = ToLuaValue (state_, -1);
    lua_pop (state_, 1);
    return ret;
 }
コード例 #4
0
   // - LuaState::globals ------------------------------------------------------
   LuaValueMap LuaState::globals()
   {
      // Traverse the globals table adding the key/value pairs to 'ret'
      LuaValueMap ret;

#if LUA_VERSION_NUM >= 502
      // Obtain global table
      lua_rawgeti (state_, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
#endif
      
      lua_pushnil (state_);
      
#if LUA_VERSION_NUM >= 502
      while (lua_next (state_, -2) != 0)
#else
      while (lua_next (state_, LUA_GLOBALSINDEX) != 0)
#endif
      {
         // Exclude from the results the tables that would result in infinite
         // recursion
         if (lua_type (state_, -2) == LUA_TSTRING)
         {
            const char* key = lua_tostring(state_, -2);
            if (strcmp(key, "_G") == 0
                || strcmp(key, "package") == 0)
            {
               lua_pop (state_, 1);
               continue;
            }
         }

         ret[ToLuaValue (state_, -2)] = ToLuaValue (state_, -1);

         lua_pop (state_, 1);
      }
      
#if LUA_VERSION_NUM >= 502
      // Remove global table
      lua_remove (state_, -2);
#endif
      // Alright, return the result
      return ret;
   }