summaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_base.cpp
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2014-04-15 13:30:46 -0400
committerShadowNinja <shadowninja@minetest.net>2014-04-27 16:15:53 -0400
commitdb4ea4658c58772ee447ff0eff8bb39b692081ec (patch)
tree0ef394ea43e667bff1660bb576fe4f9013ce404b /src/script/cpp_api/s_base.cpp
parent1838a3fd696782b1733a435bbb25accf3e40d1f3 (diff)
downloadminetest-db4ea4658c58772ee447ff0eff8bb39b692081ec.tar.gz
minetest-db4ea4658c58772ee447ff0eff8bb39b692081ec.tar.bz2
minetest-db4ea4658c58772ee447ff0eff8bb39b692081ec.zip
Only push the Lua error handler once
Diffstat (limited to 'src/script/cpp_api/s_base.cpp')
-rw-r--r--src/script/cpp_api/s_base.cpp27
1 files changed, 13 insertions, 14 deletions
diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp
index 932cc5015..774b3f51d 100644
--- a/src/script/cpp_api/s_base.cpp
+++ b/src/script/cpp_api/s_base.cpp
@@ -72,6 +72,10 @@ ScriptApiBase::ScriptApiBase()
m_luastack = luaL_newstate();
assert(m_luastack);
+ // Add and save an error handler
+ lua_pushcfunction(m_luastack, script_error_handler);
+ m_errorhandler = lua_gettop(m_luastack);
+
// Make the ScriptApiBase* accessible to ModApiBase
lua_pushlightuserdata(m_luastack, this);
lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
@@ -125,23 +129,18 @@ bool ScriptApiBase::loadScript(const std::string &scriptpath)
lua_State *L = getStack();
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
- int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, errorhandler);
- if(ret){
- errorstream<<"========== ERROR FROM LUA ==========="<<std::endl;
- errorstream<<"Failed to load and run script from "<<std::endl;
- errorstream<<scriptpath<<":"<<std::endl;
- errorstream<<std::endl;
- errorstream<<lua_tostring(L, -1)<<std::endl;
- errorstream<<std::endl;
- errorstream<<"======= END OF ERROR FROM LUA ========"<<std::endl;
+ int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, m_errorhandler);
+ if (ret) {
+ errorstream << "========== ERROR FROM LUA ===========" << std::endl;
+ errorstream << "Failed to load and run script from " << std::endl;
+ errorstream << scriptpath << ":" << std::endl;
+ errorstream << std::endl;
+ errorstream << lua_tostring(L, -1) << std::endl;
+ errorstream << std::endl;
+ errorstream << "======= END OF ERROR FROM LUA ========" << std::endl;
lua_pop(L, 1); // Pop error message from stack
- lua_pop(L, 1); // Pop the error handler from stack
return false;
}
- lua_pop(L, 1); // Pop the error handler from stack
return true;
}