Lua_icxx  1.02 (Aug 2011)
LuaInterpreter Class Reference

Lua Interpreter. More...

#include <LuaInterpreter.h>

List of all members.

Public Member Functions

 LuaInterpreter ()
 Create a Lua interpreter from a new Lua state.
 LuaInterpreter (lua_State *lua)
 Create a Lua interpreter from an existing Lua state.
template<typename TT >
void setGlobal (const std::string &name, const TT &item)
 set global variable to reference given item, which could be POD or a Lua ref
LuaTempResult getGlobal (const std::string &name)
 get global variable; this is same as eval(name), but faster
LuaTempResult newTable ()
 create a new table (shortcut for eval("{}"))
LuaTempResult openDynLib (const std::string &libPath, const std::string &entryPoint)
 load a DLL/SO and run its entryPoint function
LuaTempResult require (const std::string &moduleName)
 do same as require(moduleName) from Lua; there is currently no way to do this sandboxed
LuaTempResult eval (const std::string &expr)
 Execute given string as a Lua expression and return result.
LuaTempResult operator() (const std::string &expr)
 Execute given string as a Lua expression and return result.
LuaTempResult eval (const std::string &expr, const LuaTableRef &globalEnv)
 Execute given string as a Lua expression and return result.
LuaTempResult operator() (const std::string &expr, const LuaTableRef &globalEnv)
 Execute given string as a Lua expression and return result.
LuaTempResult doString (const std::string &script)
 Run the given item as a Lua script.
LuaTempResult doFile (const std::string &filename)
 Run the given item as a Lua script.
LuaTempResult doString (const std::string &script, const LuaTableRef &globalEnv)
 Run the given item as a Lua script.
LuaTempResult doFile (const std::string &filename, const LuaTableRef &globalEnv)
 Run the given item as a Lua script.
LuaTempResult chunkFromString (const std::string &script)
LuaTempResult chunkFromFile (const std::string &filename)
lua_State * getLuaState () const
 get the lua_State for this LuaInterpreter
 operator lua_State * () const
 get the lua_State for this LuaInterpreter

Detailed Description

Lua Interpreter.

Can evaluate Lua expressions (eval()), run Lua statements from strings (doString) or files (doFile), get and set global variables, and open Lua libraries and extensions. Examples:

    Lua lua;

    LuaTempResult res = lua.doFile('someInitScript.lua');
    if ( ! res.ok )
        ... decide what to do: log error, throw exception, etc...

    LuaObjRef obj = lua.eval("someTable[someGlobal].child");
    ...

    // create new table:
    LuaTableRef tt = lua.eval( "{}" ); 
    ...

Definition at line 43 of file LuaInterpreter.h.


Constructor & Destructor Documentation

LuaInterpreter::LuaInterpreter ( )

Create a Lua interpreter from a new Lua state.

Makes all the builtin Lua libraries (table, string, debug, etc) available to scripts that will be run via this interpreter. Note: memory overhead for this is negligible: the code is already present in memory, it is just the symbols that are made available in scripts.

Definition at line 18 of file LuaInterpreter.cpp.

LuaInterpreter::LuaInterpreter ( lua_State *  lua) [inline]

Create a Lua interpreter from an existing Lua state.

Used as-is (no libs loaded), and caller retains ownership of state.

Definition at line 59 of file LuaInterpreter.h.


Member Function Documentation

LuaTempResult LuaInterpreter::doFile ( const std::string &  filename,
const LuaTableRef globalEnv 
) [inline]

Run the given item as a Lua script.

If the string contains a return statement, the return values are available in the returned LuaTempResult. Some of the methods take a table used as the global environment of the file/chunk (useful to run it sandboxed).

Definition at line 177 of file LuaInterpreter.h.

LuaTempResult LuaInterpreter::doFile ( const std::string &  filename) [inline]

Run the given item as a Lua script.

If the string contains a return statement, the return values are available in the returned LuaTempResult. Some of the methods take a table used as the global environment of the file/chunk (useful to run it sandboxed).

Definition at line 90 of file LuaInterpreter.h.

LuaTempResult LuaInterpreter::doString ( const std::string &  script) [inline]

Run the given item as a Lua script.

If the string contains a return statement, the return values are available in the returned LuaTempResult. Some of the methods take a table used as the global environment of the file/chunk (useful to run it sandboxed).

Definition at line 163 of file LuaInterpreter.h.

LuaTempResult LuaInterpreter::doString ( const std::string &  script,
const LuaTableRef globalEnv 
) [inline]

Run the given item as a Lua script.

If the string contains a return statement, the return values are available in the returned LuaTempResult. Some of the methods take a table used as the global environment of the file/chunk (useful to run it sandboxed).

Definition at line 170 of file LuaInterpreter.h.

LuaTempResult LuaInterpreter::eval ( const std::string &  expr) [inline]

Execute given string as a Lua expression and return result.

To run the expression sandboxed, give it a ref to a Lua table, it will be used as the global environment for the expression.

Definition at line 135 of file LuaInterpreter.h.

LuaTempResult LuaInterpreter::eval ( const std::string &  expr,
const LuaTableRef globalEnv 
) [inline]

Execute given string as a Lua expression and return result.

To run the expression sandboxed, give it a ref to a Lua table, it will be used as the global environment for the expression.

Definition at line 142 of file LuaInterpreter.h.

LuaTempResult LuaInterpreter::operator() ( const std::string &  expr,
const LuaTableRef globalEnv 
) [inline]

Execute given string as a Lua expression and return result.

To run the expression sandboxed, give it a ref to a Lua table, it will be used as the global environment for the expression.

Definition at line 156 of file LuaInterpreter.h.

LuaTempResult LuaInterpreter::operator() ( const std::string &  expr) [inline]

Execute given string as a Lua expression and return result.

To run the expression sandboxed, give it a ref to a Lua table, it will be used as the global environment for the expression.

Definition at line 149 of file LuaInterpreter.h.