Only push the Lua error handler once

This commit is contained in:
ShadowNinja 2014-04-15 13:30:46 -04:00
parent 1838a3fd69
commit db4ea4658c
9 changed files with 154 additions and 279 deletions

View File

@ -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,10 +129,7 @@ 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);
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;
@ -138,10 +139,8 @@ bool ScriptApiBase::loadScript(const std::string &scriptpath)
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;
}

View File

@ -82,9 +82,12 @@ class ScriptApiBase {
void objectrefGet(u16 id);
JMutex m_luastackmutex;
// Stack index of Lua error handler
int m_errorhandler;
#ifdef SCRIPTAPI_LOCK_DEBUG
bool m_locked;
#endif
private:
lua_State* m_luastack;

View File

@ -78,9 +78,6 @@ 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;
// Get minetest.luaentities[id]
@ -95,12 +92,12 @@ void ScriptApiEntity::luaentity_Activate(u16 id,
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)
@ -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);
}
@ -163,7 +159,6 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
// Get minetest.luaentities[id]
luaentity_get(L, id);
//int object = lua_gettop(L);
// Set default values that differ from ObjectProperties defaults
prop->hp_max = 10;
@ -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,8 +231,10 @@ 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
@ -250,10 +242,9 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
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,9 +253,6 @@ 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]
@ -273,15 +261,16 @@ void ScriptApiEntity::luaentity_Rightclick(u16 id,
// 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
}

View File

@ -33,9 +33,6 @@ int ScriptApiDetached::detached_inventory_AllowMove(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if (!getDetachedInventoryCallback(name, "allow_move"))
return count;
@ -51,12 +48,12 @@ int ScriptApiDetached::detached_inventory_AllowMove(
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 7, 1, errorhandler))
if (lua_pcall(L, 7, 1, m_errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
throw LuaError("allow_move should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
lua_pop(L, 1); // Pop integer
return ret;
}
@ -68,9 +65,6 @@ int ScriptApiDetached::detached_inventory_AllowPut(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if (!getDetachedInventoryCallback(name, "allow_put"))
return stack.count; // All will be accepted
@ -83,12 +77,12 @@ int ScriptApiDetached::detached_inventory_AllowPut(
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 1, errorhandler))
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
if (!lua_isnumber(L, -1))
throw LuaError("allow_put should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
lua_pop(L, 1); // Pop integer
return ret;
}
@ -100,9 +94,6 @@ int ScriptApiDetached::detached_inventory_AllowTake(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if (!getDetachedInventoryCallback(name, "allow_take"))
return stack.count; // All will be accepted
@ -115,12 +106,12 @@ int ScriptApiDetached::detached_inventory_AllowTake(
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 1, errorhandler))
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
if (!lua_isnumber(L, -1))
throw LuaError("allow_take should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
lua_pop(L, 1); // Pop integer
return ret;
}
@ -133,9 +124,6 @@ void ScriptApiDetached::detached_inventory_OnMove(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if (!getDetachedInventoryCallback(name, "on_move"))
return;
@ -151,9 +139,8 @@ void ScriptApiDetached::detached_inventory_OnMove(
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 7, 0, errorhandler))
if (lua_pcall(L, 7, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
// Report put items
@ -164,9 +151,6 @@ void ScriptApiDetached::detached_inventory_OnPut(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if (!getDetachedInventoryCallback(name, "on_put"))
return;
@ -180,9 +164,8 @@ void ScriptApiDetached::detached_inventory_OnPut(
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 0, errorhandler))
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
// Report taken items
@ -193,9 +176,6 @@ void ScriptApiDetached::detached_inventory_OnTake(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if (!getDetachedInventoryCallback(name, "on_take"))
return;
@ -209,9 +189,8 @@ void ScriptApiDetached::detached_inventory_OnTake(
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 0, errorhandler))
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
// Retrieves minetest.detached_inventories[name][callbackname]

View File

@ -34,9 +34,6 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if (!getItemCallback(item.name.c_str(), "on_drop"))
return false;
@ -45,7 +42,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
LuaItemStack::create(L, item);
objectrefGetOrCreate(dropper);
pushFloatPos(L, pos);
if(lua_pcall(L, 3, 1, errorhandler))
if (lua_pcall(L, 3, 1, m_errorhandler))
scriptError();
if (!lua_isnil(L, -1)) {
try {
@ -54,7 +51,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
lua_pop(L, 1); // Pop item
return true;
}
@ -63,9 +60,6 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if (!getItemCallback(item.name.c_str(), "on_place"))
return false;
@ -74,7 +68,7 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
LuaItemStack::create(L, item);
objectrefGetOrCreate(placer);
pushPointedThing(pointed);
if(lua_pcall(L, 3, 1, errorhandler))
if (lua_pcall(L, 3, 1, m_errorhandler))
scriptError();
if (!lua_isnil(L, -1)) {
try {
@ -83,7 +77,7 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
lua_pop(L, 1); // Pop item
return true;
}
@ -92,9 +86,6 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if (!getItemCallback(item.name.c_str(), "on_use"))
return false;
@ -103,7 +94,7 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
LuaItemStack::create(L, item);
objectrefGetOrCreate(user);
pushPointedThing(pointed);
if(lua_pcall(L, 3, 1, errorhandler))
if (lua_pcall(L, 3, 1, m_errorhandler))
scriptError();
if(!lua_isnil(L, -1)) {
try {
@ -112,7 +103,7 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
lua_pop(L, 1); // Pop item
return true;
}
@ -121,9 +112,6 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "on_craft");
LuaItemStack::create(L, item);
@ -131,12 +119,13 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
// Push inventory list
std::vector<ItemStack> items;
for(u32 i=0; i<old_craft_grid->getSize(); i++)
for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
items.push_back(old_craft_grid->getItem(i));
}
push_items(L, items);
InvRef::create(L, craft_inv);
if(lua_pcall(L, 4, 1, errorhandler))
if (lua_pcall(L, 4, 1, m_errorhandler))
scriptError();
if (!lua_isnil(L, -1)) {
try {
@ -145,7 +134,7 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
lua_pop(L, 1); // Pop item
return true;
}
@ -154,9 +143,6 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "craft_predict");
LuaItemStack::create(L, item);
@ -164,12 +150,13 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
//Push inventory list
std::vector<ItemStack> items;
for(u32 i=0; i<old_craft_grid->getSize(); i++)
for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
items.push_back(old_craft_grid->getItem(i));
}
push_items(L, items);
InvRef::create(L, craft_inv);
if(lua_pcall(L, 4, 1, errorhandler))
if (lua_pcall(L, 4, 1, m_errorhandler))
scriptError();
if (!lua_isnil(L, -1)) {
try {
@ -178,7 +165,7 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
throw LuaError(std::string(e.what()) + ". item=" + item.name);
}
}
lua_pop(L, 2); // Pop item and error handler
lua_pop(L, 1); // Pop item
return true;
}
@ -252,4 +239,3 @@ void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
}
}

View File

@ -37,9 +37,6 @@ void ScriptApiMainMenu::handleMainMenuEvent(std::string text)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Get handler function
lua_getglobal(L, "engine");
lua_getfield(L, -1, "event_handler");
@ -52,18 +49,14 @@ void ScriptApiMainMenu::handleMainMenuEvent(std::string text)
// Call it
lua_pushstring(L, text.c_str());
if(lua_pcall(L, 1, 0, errorhandler))
if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
void ScriptApiMainMenu::handleMainMenuButtons(std::map<std::string, std::string> fields)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Get handler function
lua_getglobal(L, "engine");
lua_getfield(L, -1, "button_handler");
@ -74,19 +67,19 @@ void ScriptApiMainMenu::handleMainMenuButtons(std::map<std::string, std::string>
}
luaL_checktype(L, -1, LUA_TFUNCTION);
// Convert fields to lua table
// Convert fields to a Lua table
lua_newtable(L);
for(std::map<std::string, std::string>::const_iterator
i = fields.begin(); i != fields.end(); i++){
const std::string &name = i->first;
const std::string &value = i->second;
std::map<std::string, std::string>::const_iterator it;
for (it = fields.begin(); it != fields.end(); it++){
const std::string &name = it->first;
const std::string &value = it->second;
lua_pushstring(L, name.c_str());
lua_pushlstring(L, value.c_str(), value.size());
lua_settable(L, -3);
}
// Call it
if(lua_pcall(L, 1, 0, errorhandler))
if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}

View File

@ -92,9 +92,6 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@ -106,9 +103,8 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
pushnode(L, node, ndef);
objectrefGetOrCreate(puncher);
pushPointedThing(pointed);
if(lua_pcall(L, 4, 0, errorhandler))
if (lua_pcall(L, 4, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
return true;
}
@ -117,9 +113,6 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@ -130,9 +123,8 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
push_v3s16(L, p);
pushnode(L, node, ndef);
objectrefGetOrCreate(digger);
if(lua_pcall(L, 3, 0, errorhandler))
if (lua_pcall(L, 3, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
return true;
}
@ -140,9 +132,6 @@ void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@ -151,18 +140,14 @@ void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
// Call function
push_v3s16(L, p);
if(lua_pcall(L, 1, 0, errorhandler))
if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@ -171,18 +156,14 @@ void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
// Call function
push_v3s16(L, p);
if(lua_pcall(L, 1, 0, errorhandler))
if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@ -192,18 +173,14 @@ void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
// Call function
push_v3s16(L, p);
pushnode(L, node, ndef);
if(lua_pcall(L, 2, 0, errorhandler))
if (lua_pcall(L, 2, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@ -213,9 +190,8 @@ bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
// Call function
push_v3s16(L, p);
lua_pushnumber(L,dtime);
if(lua_pcall(L, 2, 1, errorhandler))
if (lua_pcall(L, 2, 1, m_errorhandler))
scriptError();
lua_remove(L, errorhandler); // Remove error handler
return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
}
@ -226,9 +202,6 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
@ -244,45 +217,36 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
push_v3s16(L, p); // pos
lua_pushstring(L, formname.c_str()); // formname
lua_newtable(L); // fields
for(std::map<std::string, std::string>::const_iterator
i = fields.begin(); i != fields.end(); i++){
const std::string &name = i->first;
const std::string &value = i->second;
std::map<std::string, std::string>::const_iterator it;
for (it = fields.begin(); it != fields.end(); it++){
const std::string &name = it->first;
const std::string &value = it->second;
lua_pushstring(L, name.c_str());
lua_pushlstring(L, value.c_str(), value.size());
lua_settable(L, -3);
}
objectrefGetOrCreate(sender); // player
if(lua_pcall(L, 4, 0, errorhandler))
if (lua_pcall(L, 4, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_falling_update(v3s16 p)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
lua_getglobal(L, "nodeupdate");
push_v3s16(L, p);
if(lua_pcall(L, 1, 0, errorhandler))
if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_falling_update_single(v3s16 p)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
lua_getglobal(L, "nodeupdate_single");
push_v3s16(L, p);
if(lua_pcall(L, 1, 0, errorhandler))
if (lua_pcall(L, 1, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}

View File

@ -34,9 +34,6 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
@ -57,9 +54,8 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 7, 1, errorhandler))
if (lua_pcall(L, 7, 1, m_errorhandler))
scriptError();
lua_remove(L, errorhandler); // Remove error handler
if (!lua_isnumber(L, -1))
throw LuaError("allow_metadata_inventory_move should"
" return a number, guilty node: " + nodename);
@ -75,9 +71,6 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
@ -96,9 +89,8 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 1, errorhandler))
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
throw LuaError("allow_metadata_inventory_put should"
" return a number, guilty node: " + nodename);
@ -114,9 +106,6 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
@ -135,9 +124,8 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 1, errorhandler))
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
lua_remove(L, errorhandler); // Remove error handler
if (!lua_isnumber(L, -1))
throw LuaError("allow_metadata_inventory_take should"
" return a number, guilty node: " + nodename);
@ -154,9 +142,6 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
@ -177,9 +162,8 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 7, 0, errorhandler))
if (lua_pcall(L, 7, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
// Report put items
@ -189,9 +173,6 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
@ -210,9 +191,8 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 0, errorhandler))
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
// Report taken items
@ -222,9 +202,6 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
@ -243,9 +220,8 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 0, errorhandler))
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
ScriptApiNodemeta::ScriptApiNodemeta() {
@ -254,5 +230,3 @@ ScriptApiNodemeta::ScriptApiNodemeta() {
ScriptApiNodemeta::~ScriptApiNodemeta() {
}

View File

@ -27,18 +27,14 @@ bool ScriptApiServer::getAuth(const std::string &playername,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
getAuthHandler();
lua_getfield(L, -1, "get_auth");
if (lua_type(L, -1) != LUA_TFUNCTION)
throw LuaError("Authentication handler missing get_auth");
lua_pushstring(L, playername.c_str());
if(lua_pcall(L, 1, 1, errorhandler))
if (lua_pcall(L, 1, 1, m_errorhandler))
scriptError();
lua_remove(L, -2); // Remove auth handler
lua_remove(L, errorhandler); // Remove error handler
// nil = login not allowed
if (lua_isnil(L, -1))
@ -101,9 +97,6 @@ void ScriptApiServer::createAuth(const std::string &playername,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
getAuthHandler();
lua_getfield(L, -1, "create_auth");
lua_remove(L, -2); // Remove auth handler
@ -111,9 +104,8 @@ void ScriptApiServer::createAuth(const std::string &playername,
throw LuaError("Authentication handler missing create_auth");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
if(lua_pcall(L, 2, 0, errorhandler))
if (lua_pcall(L, 2, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
bool ScriptApiServer::setPassword(const std::string &playername,
@ -121,9 +113,6 @@ bool ScriptApiServer::setPassword(const std::string &playername,
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
getAuthHandler();
lua_getfield(L, -1, "set_password");
lua_remove(L, -2); // Remove auth handler
@ -131,9 +120,8 @@ bool ScriptApiServer::setPassword(const std::string &playername,
throw LuaError("Authentication handler missing set_password");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
if(lua_pcall(L, 2, 1, errorhandler))
if (lua_pcall(L, 2, 1, m_errorhandler))
scriptError();
lua_remove(L, -2); // Remove error handler
return lua_toboolean(L, -1);
}