📄 interpreter.cpp
字号:
#include "Interpreter.hpp"namespace lua{OPT_INLINEInterpreter::Interpreter(LuaLib whichLibs): m_vm(lua_open()) { if (m_vm == NULL) throw std::runtime_error("Could not initialize LUA interpreter"); #if HAVE_LUA51 if (whichLibs == LIB_ALL) luaL_openlibs(m_vm); else #endif { if (whichLibs & LIB_BASE) luaopen_base(m_vm); #if HAVE_LUA51 if (whichLibs & LIB_PACKAGE)luaopen_package(m_vm); #endif if (whichLibs & LIB_IO) luaopen_io(m_vm); if (whichLibs & LIB_STRING) luaopen_string(m_vm); if (whichLibs & LIB_MATH) luaopen_math(m_vm); if (whichLibs & LIB_TABLE) luaopen_table(m_vm); if (whichLibs & LIB_DEBUG) luaopen_debug(m_vm); }} OPT_INLINE/// Execute a file in protected modevoid Interpreter::doFile(const std::string& filename) { checkOk( luaL_loadfile(m_vm, filename.c_str()), "Error interpreting file" ); const int nargs = 0, errFnIdx = 0; checkOk( lua_pcall(m_vm, nargs, LUA_MULTRET, errFnIdx), "Error running script" );}OPT_INLINE/// Execute a string in protected modevoid Interpreter::doString(const std::string& cmd) { //lua_dostring(m_vm, cmd.c_str()); // deprecated checkOk( luaL_loadbuffer(m_vm, cmd.c_str(), cmd.size(), cmd.c_str()), "Error interpreting string" ); const int nargs = 0, errFnIdx = 0; checkOk( lua_pcall(m_vm, nargs, LUA_MULTRET, errFnIdx), "Error running string" );} OPT_INLINE/** Adapted from lua auxlib. If the status indicates an error, the error message is saved (get with errMsg()), and if a lua function refered by global _ALERT variable is defined, just call it; otherwise throw a std::runtime exception containing the error message. */void Interpreter::checkOk(int status, const char* msg){ if (status != ERR_NONE) { m_errMsg = lua_tostring(m_vm, -1); lua_getglobal(m_vm, "_ALERT"); if (lua_isfunction(m_vm, -1)) { lua_insert(m_vm, -2); lua_call(m_vm, 1, 0); } else // no _ALERT function; throw exception { lua_pop(m_vm, 2); // remove error message and _ALERT throw std::runtime_error(msg); } }}} // namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -