aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_entity.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_entity.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_entity.cpp')
-rw-r--r--src/script/cpp_api/s_entity.cpp75
1 files changed, 32 insertions, 43 deletions
diff --git a/src/script/cpp_api/s_entity.cpp b/src/script/cpp_api/s_entity.cpp
index c9d1bd8e0..8e79f8838 100644
--- a/src/script/cpp_api/s_entity.cpp
+++ b/src/script/cpp_api/s_entity.cpp
@@ -39,7 +39,7 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
lua_gettable(L, -2);
// Should be a table, which we will use as a prototype
//luaL_checktype(L, -1, LUA_TTABLE);
- if(lua_type(L, -1) != LUA_TTABLE){
+ if (lua_type(L, -1) != LUA_TTABLE){
errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
return false;
}
@@ -58,7 +58,7 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
// This should be userdata with metatable ObjectRef
objectrefGet(id);
luaL_checktype(L, -1, LUA_TUSERDATA);
- if(!luaL_checkudata(L, -1, "ObjectRef"))
+ if (!luaL_checkudata(L, -1, "ObjectRef"))
luaL_typerror(L, -1, "ObjectRef");
lua_setfield(L, -2, "object");
@@ -78,10 +78,7 @@ void ScriptApiEntity::luaentity_Activate(u16 id,
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
- verbosestream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
+ verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
// Get minetest.luaentities[id]
luaentity_get(L, id);
@@ -89,25 +86,25 @@ void ScriptApiEntity::luaentity_Activate(u16 id,
// Get on_activate function
lua_getfield(L, -1, "on_activate");
- if(!lua_isnil(L, -1)) {
+ if (!lua_isnil(L, -1)) {
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
lua_pushlstring(L, staticdata.c_str(), staticdata.size());
lua_pushinteger(L, dtime_s);
// Call with 3 arguments, 0 results
- if(lua_pcall(L, 3, 0, errorhandler))
+ if (lua_pcall(L, 3, 0, m_errorhandler))
scriptError();
} else {
lua_pop(L, 1);
}
- lua_pop(L, 2); // Pop object and error handler
+ lua_pop(L, 1); // Pop object
}
void ScriptApiEntity::luaentity_Remove(u16 id)
{
SCRIPTAPI_PRECHECKHEADER
- verbosestream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
+ verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
// Get minetest.luaentities table
lua_getglobal(L, "minetest");
@@ -127,9 +124,6 @@ std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
//infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
// Get minetest.luaentities[id]
@@ -138,19 +132,21 @@ std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
// Get get_staticdata function
lua_getfield(L, -1, "get_staticdata");
- if(lua_isnil(L, -1))
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 2); // Pop entity and get_staticdata
return "";
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
// Call with 1 arguments, 1 results
- if(lua_pcall(L, 1, 1, errorhandler))
+ if (lua_pcall(L, 1, 1, m_errorhandler))
scriptError();
lua_remove(L, object); // Remove object
- lua_remove(L, errorhandler); // Remove error handler
size_t len = 0;
const char *s = lua_tolstring(L, -1, &len);
+ lua_pop(L, 1); // Pop static data
return std::string(s, len);
}
@@ -162,8 +158,7 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
//infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
// Get minetest.luaentities[id]
- luaentity_get(L,id);
- //int object = lua_gettop(L);
+ luaentity_get(L, id);
// Set default values that differ from ObjectProperties defaults
prop->hp_max = 10;
@@ -178,7 +173,7 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
getfloatfield(L, -1, "weight", prop->weight);
lua_getfield(L, -1, "collisionbox");
- if(lua_istable(L, -1))
+ if (lua_istable(L, -1))
prop->collisionbox = read_aabb3f(L, -1, 1.0);
lua_pop(L, 1);
@@ -199,9 +194,6 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
// Get minetest.luaentities[id]
@@ -210,16 +202,17 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
// State: object is at top of stack
// Get step function
lua_getfield(L, -1, "on_step");
- if(lua_isnil(L, -1))
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 2); // Pop on_step and entity
return;
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
lua_pushnumber(L, dtime); // dtime
// Call with 2 arguments, 0 results
- if(lua_pcall(L, 2, 0, errorhandler))
+ if (lua_pcall(L, 2, 0, m_errorhandler))
scriptError();
- lua_remove(L, object); // Remove object
- lua_remove(L, errorhandler); // Remove error handler
+ lua_pop(L, 1); // Pop object
}
// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
@@ -230,9 +223,6 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
// Get minetest.luaentities[id]
@@ -241,19 +231,20 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
// State: object is at top of stack
// Get function
lua_getfield(L, -1, "on_punch");
- if(lua_isnil(L, -1))
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 2); // Pop on_punch and entitu
return;
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
- lua_pushvalue(L, object); // self
- objectrefGetOrCreate(puncher); // Clicker reference
+ lua_pushvalue(L, object); // self
+ objectrefGetOrCreate(puncher); // Clicker reference
lua_pushnumber(L, time_from_last_punch);
push_tool_capabilities(L, *toolcap);
push_v3f(L, dir);
// Call with 5 arguments, 0 results
- if(lua_pcall(L, 5, 0, errorhandler))
+ if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
- lua_remove(L, object); // Remove object
- lua_remove(L, errorhandler); // Remove error handler
+ lua_pop(L, 1); // Pop object
}
// Calls entity:on_rightclick(ObjectRef clicker)
@@ -262,26 +253,24 @@ void ScriptApiEntity::luaentity_Rightclick(u16 id,
{
SCRIPTAPI_PRECHECKHEADER
- lua_pushcfunction(L, script_error_handler);
- int errorhandler = lua_gettop(L);
-
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
// Get minetest.luaentities[id]
- luaentity_get(L,id);
+ luaentity_get(L, id);
int object = lua_gettop(L);
// State: object is at top of stack
// Get function
lua_getfield(L, -1, "on_rightclick");
- if(lua_isnil(L, -1))
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 2); // Pop on_rightclick and entity
return;
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
objectrefGetOrCreate(clicker); // Clicker reference
// Call with 2 arguments, 0 results
- if(lua_pcall(L, 2, 0, errorhandler))
+ if (lua_pcall(L, 2, 0, m_errorhandler))
scriptError();
- lua_remove(L, object); // Remove object
- lua_remove(L, errorhandler); // Remove error handler
+ lua_pop(L, 1); // Pop object
}