"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/Diluculum/LuaState.cpp" (12 May 2020, 5813 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /******************************************************************************\
    2 * LuaState                                                                     *
    3 * A pleasant way to use a Lua state in C++.                                    *
    4 *                                                                              *
    5 *                                                                              *
    6 * Copyright (C) 2005-2013 by Leandro Motta Barros.                             *
    7 *                                                                              *
    8 * Permission is hereby granted, free of charge, to any person obtaining a copy *
    9 * of this software and associated documentation files (the "Software"), to     *
   10 * deal in the Software without restriction, including without limitation the   *
   11 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or  *
   12 * sell copies of the Software, and to permit persons to whom the Software is   *
   13 * furnished to do so, subject to the following conditions:                     *
   14 *                                                                              *
   15 * The above copyright notice and this permission notice shall be included in   *
   16 * all copies or substantial portions of the Software.                          *
   17 *                                                                              *
   18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR   *
   19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,     *
   20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE *
   21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER       *
   22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      *
   23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS *
   24 * IN THE SOFTWARE.                                                             *
   25 \******************************************************************************/
   26 
   27 #include <cassert>
   28 #include <cstring>
   29 #include <typeinfo>
   30 #include <boost/lexical_cast.hpp>
   31 #include <Diluculum/LuaState.hpp>
   32 #include <Diluculum/LuaUtils.hpp>
   33 #include "InternalUtils.hpp"
   34 
   35 
   36 namespace Diluculum
   37 {
   38    // - LuaState::LuaState -----------------------------------------------------
   39    LuaState::LuaState (bool loadStdLib)
   40       : state_(0), ownsState_(true)
   41    {
   42       state_ = luaL_newstate();
   43       if (state_ == 0)
   44          throw LuaError ("Error opening Lua state.");
   45 
   46       if (loadStdLib)
   47          luaL_openlibs (state_);
   48    }
   49 
   50 
   51    LuaState::LuaState (lua_State* state, bool loadStdLib)
   52       : state_(state), ownsState_(false)
   53    {
   54       if (state_ == 0)
   55          throw LuaError ("Constructor of 'LuaState' got a NULL pointer.");
   56 
   57       if (loadStdLib)
   58          luaL_openlibs (state_);
   59    }
   60 
   61 
   62 
   63    // - LuaState::~LuaState ----------------------------------------------------
   64    LuaState::~LuaState()
   65    {
   66       if (ownsState_ && state_ != 0)
   67          lua_close (state_);
   68    }
   69 
   70 
   71 
   72    // - LuaState::doStringOrFile -----------------------------------------------
   73    LuaValueList LuaState::doStringOrFile (bool isString, const std::string& str)
   74    {
   75       const int stackSizeAtBeginning = lua_gettop (state_);
   76 
   77       if (isString)
   78       {
   79          Impl::ThrowOnLuaError (state_, luaL_loadbuffer (state_, str.c_str(),
   80                                                          str.length(), "line"));
   81       }
   82       else
   83       {
   84          Impl::ThrowOnLuaError (state_, luaL_loadfile (state_, str.c_str()));
   85       }
   86 
   87       Impl::ThrowOnLuaError (state_, lua_pcall (state_, 0, LUA_MULTRET, 0));
   88 
   89       const int numResults = lua_gettop (state_) - stackSizeAtBeginning;
   90 
   91       LuaValueList results;
   92 
   93       for (int i = numResults; i > 0; --i)
   94          results.push_back (ToLuaValue (state_, -i));
   95 
   96       lua_pop (state_, numResults);
   97 
   98       return results;
   99    }
  100 
  101 
  102 
  103    // - LuaState::call ---------------------------------------------------------
  104    LuaValueList LuaState::call (LuaFunction& func,
  105                                 const LuaValueList& params,
  106                                 const std::string& chunkName)
  107    {
  108       func.setReaderFlag (false);
  109       PushLuaValue (state_, LuaValue (func));
  110       return Impl::CallFunctionOnTop (state_, params);
  111    }
  112 
  113 
  114    // - LuaState::operator[] ---------------------------------------------------
  115    LuaVariable LuaState::operator[] (const std::string& variable)
  116    {
  117       assert(variable != "_G" && "Can't access '_G'; use LuaState::globals().");
  118 
  119       return LuaVariable (state_, variable);
  120    }
  121 
  122 
  123    // - LuaState::globals ------------------------------------------------------
  124    LuaValueMap LuaState::globals()
  125    {
  126       // Traverse the globals table adding the key/value pairs to 'ret'
  127       LuaValueMap ret;
  128 
  129 #if LUA_VERSION_NUM >= 502
  130       // Obtain global table
  131       lua_rawgeti (state_, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
  132 #endif
  133       
  134       lua_pushnil (state_);
  135       
  136 #if LUA_VERSION_NUM >= 502
  137       while (lua_next (state_, -2) != 0)
  138 #else
  139       while (lua_next (state_, LUA_GLOBALSINDEX) != 0)
  140 #endif
  141       {
  142          // Exclude from the results the tables that would result in infinite
  143          // recursion
  144          if (lua_type (state_, -2) == LUA_TSTRING)
  145          {
  146             const char* key = lua_tostring(state_, -2);
  147             if (strcmp(key, "_G") == 0
  148                 || strcmp(key, "package") == 0)
  149             {
  150                lua_pop (state_, 1);
  151                continue;
  152             }
  153          }
  154 
  155          ret[ToLuaValue (state_, -2)] = ToLuaValue (state_, -1);
  156 
  157          lua_pop (state_, 1);
  158       }
  159       
  160 #if LUA_VERSION_NUM >= 502
  161       // Remove global table
  162       lua_remove (state_, -2);
  163 #endif
  164       // Alright, return the result
  165       return ret;
  166    }
  167 
  168 } // namespace Diluculum