"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/Diluculum/LuaVariable.cpp" (12 May 2020, 7368 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 * LuaVariable.cpp                                                              *
    3 * A variable living in a Lua interpreter.                                      *
    4 *                                                                              *
    5 *                                                                              *
    6 * Copyright (C) 2005-2010 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 <boost/lexical_cast.hpp>
   29 #include <Diluculum/LuaState.hpp>
   30 #include <Diluculum/LuaUserData.hpp>
   31 #include <Diluculum/LuaUtils.hpp>
   32 #include <Diluculum/LuaVariable.hpp>
   33 #include "InternalUtils.hpp"
   34 
   35 
   36 namespace Diluculum
   37 {
   38    // - LuaVariable::LuaVariable -----------------------------------------------
   39    LuaVariable::LuaVariable (lua_State* state, const LuaValue& key,
   40                              const KeyList& predKeys)
   41       : state_(state), keys_(predKeys)
   42    {
   43       keys_.push_back (key);
   44    }
   45 
   46 
   47 
   48    // - LuaVariable::operator= -------------------------------------------------
   49    const LuaValue& LuaVariable::operator= (const LuaValue& rhs)
   50    {
   51       pushLastTable();
   52       PushLuaValue (state_, keys_.back());
   53       PushLuaValue (state_, rhs);
   54       lua_settable (state_, -3);
   55       lua_pop (state_, 1);
   56 
   57       return rhs;
   58    }
   59 
   60 
   61 
   62    // - LuaVariable::value -----------------------------------------------------
   63    LuaValue LuaVariable::value() const
   64    {
   65       pushTheReferencedValue();
   66       LuaValue ret = ToLuaValue (state_, -1);
   67       lua_pop (state_, 1);
   68       return ret;
   69    }
   70 
   71 
   72 
   73    // - LuaVariable::operator[] ------------------------------------------------
   74    LuaVariable LuaVariable::operator[] (const LuaValue& key) const
   75    {
   76       return LuaVariable (state_, key, keys_);
   77    }
   78 
   79 
   80 
   81    // - LuaVariable::operator() ------------------------------------------------
   82    LuaValueList LuaVariable::operator() (const LuaValueList& params)
   83    {
   84       pushTheReferencedValue();
   85       return Impl::CallFunctionOnTop (state_, params);
   86    }
   87 
   88    LuaValueList LuaVariable::operator()()
   89    {
   90       return (*this)(LuaValueList());
   91    }
   92 
   93    LuaValueList LuaVariable::operator() (const LuaValue& param)
   94    {
   95       LuaValueList params;
   96       params.push_back (param);
   97       return (*this)(params);
   98    }
   99 
  100    LuaValueList LuaVariable::operator() (const LuaValue& param1,
  101                                          const LuaValue& param2)
  102    {
  103       LuaValueList params;
  104       params.push_back (param1);
  105       params.push_back (param2);
  106       return (*this)(params);
  107    }
  108 
  109    LuaValueList LuaVariable::operator() (const LuaValue& param1,
  110                                          const LuaValue& param2,
  111                                          const LuaValue& param3)
  112    {
  113       LuaValueList params;
  114       params.push_back (param1);
  115       params.push_back (param2);
  116       params.push_back (param3);
  117       return (*this)(params);
  118    }
  119 
  120    LuaValueList LuaVariable::operator() (const LuaValue& param1,
  121                                          const LuaValue& param2,
  122                                          const LuaValue& param3,
  123                                          const LuaValue& param4)
  124    {
  125       LuaValueList params;
  126       params.push_back (param1);
  127       params.push_back (param2);
  128       params.push_back (param3);
  129       params.push_back (param4);
  130       return (*this)(params);
  131    }
  132 
  133    LuaValueList LuaVariable::operator() (const LuaValue& param1,
  134                                          const LuaValue& param2,
  135                                          const LuaValue& param3,
  136                                          const LuaValue& param4,
  137                                          const LuaValue& param5)
  138    {
  139       LuaValueList params;
  140       params.push_back (param1);
  141       params.push_back (param2);
  142       params.push_back (param3);
  143       params.push_back (param4);
  144       params.push_back (param5);
  145       return (*this)(params);
  146    }
  147 
  148 
  149 
  150    // - LuaVariable::pushLastTable ---------------------------------------------
  151    void LuaVariable::pushLastTable()
  152    {
  153       // Push the globals table onto the stack
  154 #if LUA_VERSION_NUM >= 502
  155       lua_pushglobaltable (state_);
  156 #else
  157       lua_pushstring (state_, "_G");
  158       lua_gettable (state_, LUA_GLOBALSINDEX);
  159 #endif
  160       // Reach the "final" table (and leave it at the stack top)
  161       typedef KeyList::const_iterator iter_t;
  162 
  163       assert (keys_.size() > 0 && "At least one key should be present here.");
  164 
  165       iter_t end = keys_.end();
  166       --end;
  167 
  168       for (iter_t p = keys_.begin(); p != end; ++p)
  169       {
  170          PushLuaValue (state_, *p);
  171          lua_gettable (state_, -2);
  172          if (!lua_istable (state_, -1))
  173             throw TypeMismatchError ("table", luaL_typename (state_, -1));
  174          lua_remove (state_, -2);
  175       }
  176    }
  177 
  178 
  179 
  180    // - LuaVariable::pushTheReferencedValue ------------------------------------
  181    void LuaVariable::pushTheReferencedValue() const
  182    {
  183       assert (keys_.size() > 0 && "There should be at least one key here.");
  184 
  185 #if LUA_VERSION_NUM >= 502
  186       lua_rawgeti (state_, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
  187 #else
  188       int index = LUA_GLOBALSINDEX;
  189 #endif
  190       typedef std::vector<LuaValue>::const_iterator iter_t;
  191       for (iter_t p = keys_.begin(); p != keys_.end(); ++p)
  192       {
  193          PushLuaValue (state_, *p);
  194 #if LUA_VERSION_NUM >= 502
  195       lua_gettable (state_, -2);
  196 #else
  197          lua_gettable (state_, index);
  198 #endif
  199          if (keys_.size() > 1 && p != keys_.end()-1 && !lua_istable(state_, -1))
  200             throw TypeMismatchError ("table", p->typeName());
  201 #if LUA_VERSION_NUM >= 502
  202       lua_remove (state_, -2);
  203 #else
  204          if (index != LUA_GLOBALSINDEX)
  205             lua_remove (state_, -2);
  206          else
  207             index = -2;
  208 #endif
  209       }
  210    }
  211 
  212 } // namespace Diluculum