/** * Evaluate a Lua script. * @param script String with Lua code. * @return Non-zero if successful, zero on error. */ int LuaEngine::eval(const char* script) { lua_State* L = (lua_State*) mLuaState; // Evaluate Lua script. int result = luaL_dostring(L, script); // Was there an error? if (0 != result) { MAUtil::String errorMessage; if (lua_isstring(L, -1)) { errorMessage = lua_tostring(L, -1); // Pop the error message. lua_pop(L, 1); } else { errorMessage = "There was a Lua error condition, but no error message."; } lprintfln("Lua Error: %s\n", errorMessage.c_str()); // Print size of Lua stack (debug info). lprintfln("Lua stack size: %i\n", lua_gettop(L)); reportLuaError(errorMessage.c_str()); } return result == 0; }
/** * Evaluate a Lua script. * @param script String with Lua code. * @return Non-zero if successful, zero on error. */ int LuaEngine::eval(const char* script) { lua_State* L = (lua_State*) mLuaState; // Create temporary script string with an ending space. // There seems to be a bug in Lua when evaluating // statements like: // "return 10" // "x = 10" // But this works: // "return 10 " // "return (10)" // "x = 10 " int length = strlen(script); char* s = (char*) malloc(length + 2); strcpy(s, script); s[length] = ' '; s[length + 1] = 0; // Evaluate Lua script. int result = luaL_dostring(L, s); // Free temporary script string. free(s); // Was there an error? if (0 != result) { MAUtil::String errorMessage; if (lua_isstring(L, -1)) { errorMessage = lua_tostring(L, -1); // Pop the error message. lua_pop(L, 1); } else { errorMessage = "There was a Lua error condition, but no error message."; } lprintfln("Lua Error: %s\n", errorMessage.c_str()); // Print size of Lua stack (debug info). lprintfln("Lua stack size: %i\n", lua_gettop(L)); reportLuaError(errorMessage.c_str()); } return result == 0; }