Lua_icxx  1.02 (Aug 2011)
LuaInterpreter.h
00001 /*
00002 This file is part of the Lua_icxx library. 
00003 Copyright 2010 (c) by Oliver Schoenborn. 
00004 License terms in LICENSE.txt.
00005 */
00006 
00007 
00008 #ifndef LUA_ICXX_INTERP_INCLUDED
00009 #define LUA_ICXX_INTERP_INCLUDED
00010 
00011 
00012 // Only tested with Lua 5.1
00013 
00014 #include <string>
00015 #include <cassert>
00016 #include <lua.hpp>
00017 
00018 #include "lua_icxx_export.h"
00019 #include "LuaTempResult.h"
00020 #include "LuaObjRef.h"
00021 class LuaFuncRef;
00022 
00023 
00043 class LUA_ICXX_CPP_API 
00044 LuaInterpreter
00045 {
00046 public: // construct/destroy
00047 
00054     LuaInterpreter();
00055 
00059     LuaInterpreter(lua_State* lua): mLua(lua), mOwner(false) {}
00060     ~LuaInterpreter();
00061 
00062 public: // methods
00063 
00065 
00069     LuaTempResult eval(const std::string& expr);
00070     LuaTempResult operator()(const std::string& expr);
00071 
00072     LuaTempResult eval(const std::string& expr, const LuaTableRef& globalEnv);
00073     LuaTempResult operator()(const std::string& expr, const LuaTableRef& globalEnv);
00075 
00076 private:
00077     LuaTempResult doStringCommon( const std::string& script, const LuaTableRef * globalEnv = NULL);
00078     LuaTempResult doFileCommon( const std::string& script, const LuaTableRef * globalEnv = NULL);
00079 
00080 public:
00081 
00083 
00089     LuaTempResult doString(const std::string& script);
00090     LuaTempResult doFile(const std::string& filename) { return doFileCommon(filename); }
00091     LuaTempResult doString(const std::string& script, const LuaTableRef& globalEnv);
00092     LuaTempResult doFile(const std::string& filename, const LuaTableRef& globalEnv);
00094 
00096     LuaTempResult chunkFromString(const std::string& script);
00097     LuaTempResult chunkFromFile(const std::string& filename);
00098     //LuaTempResult loadString(const std::string& script, const LuaTableRef& globalEnv);
00099     //LuaTempResult loadFile(const std::string& script, const LuaTableRef& globalEnv);
00101 
00103     template <typename TT> void setGlobal(const std::string& name, const TT& item);
00105     LuaTempResult getGlobal(const std::string& name);
00107     LuaTempResult newTable();
00108 
00110     LuaTempResult openDynLib(const std::string& libPath, const std::string& entryPoint);
00112     LuaTempResult require(const std::string& moduleName);
00113 
00114 public: // methods to use Lua C API directly
00115 
00117 
00118     lua_State* getLuaState() const { return mLua; }
00119     operator lua_State*() const { return mLua; }
00121 
00122 private: // methods
00123 
00124     LuaErrCode tryOpenDynLib( 
00125         const LuaFuncRef&, const std::string&, const std::string&, std::string& errMsg );
00126 
00127 private: // data
00128 
00129     lua_State * mLua;
00130     bool mOwner;
00131 };
00132 
00133 
00134 inline LuaTempResult 
00135 LuaInterpreter::eval( const std::string& expr )
00136 {
00137     return doStringCommon("return " + expr);
00138 }
00139 
00140 
00141 inline LuaTempResult 
00142 LuaInterpreter::eval( const std::string& expr, const LuaTableRef& globalEnv )
00143 {
00144     return doStringCommon("return " + expr, & globalEnv);
00145 }
00146 
00147 
00148 inline LuaTempResult 
00149 LuaInterpreter::operator()( const std::string& expr )
00150 {
00151     return eval(expr);
00152 }
00153 
00154 
00155 inline LuaTempResult 
00156 LuaInterpreter::operator()( const std::string& expr, const LuaTableRef& globalEnv )
00157 {
00158     return eval(expr, globalEnv);
00159 }
00160 
00161 
00162 inline LuaTempResult 
00163 LuaInterpreter::doString( const std::string& script )
00164 {
00165     return doStringCommon(script);
00166 }
00167 
00168 
00169 inline LuaTempResult 
00170 LuaInterpreter::doString( const std::string& script, const LuaTableRef& globalEnv )
00171 {
00172     return doStringCommon(script, & globalEnv);
00173 }
00174 
00175 
00176 inline LuaTempResult 
00177 LuaInterpreter::doFile( const std::string& filename, const LuaTableRef& globalEnv )
00178 {
00179     return doFileCommon(filename, & globalEnv);
00180 }
00181 
00182 
00183 inline LuaTempResult 
00184 LuaInterpreter::require( const std::string& moduleName )
00185 {
00186     return eval( "require('" + moduleName + "')" );
00187 }
00188 
00189 
00190 template <typename TT>
00191 inline void 
00192 LuaInterpreter::setGlobal( const std::string& name, const TT& item )
00193 {
00194     pushValToStack(mLua, item);
00195     lua_setglobal(mLua, name.c_str());
00196 }
00197 
00198 
00199 inline LuaTempResult 
00200 LuaInterpreter::getGlobal( const std::string& name )
00201 {
00202     const int stackTop = lua_gettop(mLua);
00203     assert( lua_checkstack(mLua, 1) ); 
00204     lua_getglobal(mLua, name.c_str());
00205     return LuaTempResult(mLua, stackTop);
00206 }
00207 
00208 
00209 inline LuaTempResult 
00210 LuaInterpreter::newTable()
00211 {
00212     const int stackTop = lua_gettop(mLua);
00213     assert( lua_checkstack(mLua, 1) ); 
00214     lua_newtable(mLua);
00215     return LuaTempResult(mLua, stackTop);
00216 }
00217 
00218 #endif // LUA_ICXX_INTERP_INCLUDED
00219