Răsfoiți Sursa

Add execute_string()

Daniele Bartolini 12 ani în urmă
părinte
comite
d73215ef74
2 a modificat fișierele cu 24 adăugiri și 0 ștergeri
  1. 22 0
      engine/lua/LuaEnvironment.cpp
  2. 2 0
      engine/lua/LuaEnvironment.h

+ 22 - 0
engine/lua/LuaEnvironment.cpp

@@ -150,6 +150,28 @@ bool LuaEnvironment::load_and_execute(const char* res_name)
 	return false;
 }
 
+//-----------------------------------------------------------------------------
+bool LuaEnvironment::execute_string(const char* s)
+{
+	CE_ASSERT_NOT_NULL(s);
+
+	lua_getglobal(m_state, "debug");
+	lua_getfield(m_state, -1, "traceback");
+	if (luaL_loadstring(m_state, s) == 0)
+	{
+		if (lua_pcall(m_state, 0, 0, -2) == 0)
+		{
+			// Unloading is OK since the script data has been copied to Lua
+			lua_pop(m_state, 2); // Pop debug.traceback
+			return true;
+		}
+	}
+
+	error();
+	lua_pop(m_state, 2); // Pop debug.traceback
+	return false;
+}
+
 //-----------------------------------------------------------------------------
 void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
 {

+ 2 - 0
engine/lua/LuaEnvironment.h

@@ -57,6 +57,8 @@ public:
 	/// true if success, false otherwise.
 	bool					load_and_execute(const char* res_name);
 
+	bool					execute_string(const char* s);
+
 	/// Load a function which will be used in Lua. @a module is the name of table contenitor,
 	/// @a name is the name of function in module and @func is the pointer to the function.
 	/// _func_ must be a C/lua function (__int32_t function_name(lua_State* L)__)