/******************************************************************************/
void push_items(lua_State *L, const std::vector<ItemStack> &items)
{
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
// Get the table insert function
lua_getglobal(L, "table");
lua_getfield(L, -1, "insert");
lua_pushvalue(L, table_insert);
lua_pushvalue(L, table);
LuaItemStack::create(L, item);
- if(lua_pcall(L, 2, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 2, 0, errorhandler))
+ script_error(L);
}
- lua_remove(L, -2); // Remove table
lua_remove(L, -2); // Remove insert
+ lua_remove(L, -2); // Remove table
+ lua_remove(L, -2); // Remove error handler
}
/******************************************************************************/
std::string script_get_backtrace(lua_State *L)
{
std::string s;
- lua_getfield(L, LUA_GLOBALSINDEX, "debug");
+ lua_getglobal(L, "debug");
if(lua_istable(L, -1)){
lua_getfield(L, -1, "traceback");
- if(lua_isfunction(L, -1)){
+ if(lua_isfunction(L, -1)) {
lua_call(L, 0, 1);
if(lua_isstring(L, -1)){
- s += lua_tostring(L, -1);
+ s = lua_tostring(L, -1);
}
- lua_pop(L, 1);
- }
- else{
- lua_pop(L, 1);
}
+ lua_pop(L, 1);
}
lua_pop(L, 1);
return s;
}
-void script_error(lua_State *L, const char *fmt, ...)
+int script_error_handler(lua_State *L) {
+ lua_getglobal(L, "debug");
+ if (!lua_istable(L, -1)) {
+ lua_pop(L, 1);
+ return 1;
+ }
+ lua_getfield(L, -1, "traceback");
+ if (!lua_isfunction(L, -1)) {
+ lua_pop(L, 2);
+ return 1;
+ }
+ lua_pushvalue(L, 1);
+ lua_pushinteger(L, 2);
+ lua_call(L, 2, 1);
+ return 1;
+}
+
+void script_error(lua_State *L)
{
- va_list argp;
- va_start(argp, fmt);
- char buf[10000];
- vsnprintf(buf, 10000, fmt, argp);
- va_end(argp);
- throw LuaError(L, buf);
+ throw LuaError(NULL, lua_tostring(L, -1));
}
// Push the list of callbacks (a lua table).
{
// Insert the return value into the lua stack, below the table
assert(lua_gettop(L) >= nargs + 1);
+
lua_pushnil(L);
- lua_insert(L, -(nargs + 1) - 1);
+ int rv = lua_gettop(L) - nargs - 1;
+ lua_insert(L, rv);
+
+ // Insert error handler after return value
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = rv + 1;
+ lua_insert(L, errorhandler);
+
// Stack now looks like this:
- // ... <return value = nil> <table> <arg#1> <arg#2> ... <arg#n>
+ // ... <return value = nil> <error handler> <table> <arg#1> <arg#2> ... <arg#n>
- int rv = lua_gettop(L) - nargs - 1;
- int table = rv + 1;
+ int table = errorhandler + 1;
int arg = table + 1;
luaL_checktype(L, table, LUA_TTABLE);
// Call function
for(int i = 0; i < nargs; i++)
lua_pushvalue(L, arg+i);
- if(lua_pcall(L, nargs, 1, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, nargs, 1, errorhandler))
+ script_error(L);
// Move return value to designated space in stack
// Or pop it
// are converted by lua_toboolean to true or false, respectively.
};
-std::string script_get_backtrace (lua_State *L);
-void script_error (lua_State *L, const char *fmt, ...);
-void script_run_callbacks (lua_State *L, int nargs,
- RunCallbacksMode mode);
+std::string script_get_backtrace(lua_State *L);
+int script_error_handler(lua_State *L);
+void script_error(lua_State *L);
+void script_run_callbacks(lua_State *L, int nargs,
+ RunCallbacksMode mode);
#endif /* C_INTERNAL_H_ */
LuaError::LuaError(lua_State *L, const std::string &s)
{
- m_s = "LuaError: ";
- m_s += s + "\n";
- m_s += script_get_backtrace(L);
+ m_s = "LuaError: " + s;
+ if (L) m_s += '\n' + script_get_backtrace(L);
}
struct EnumString es_ItemType[] =
}
};
-static int loadScript_ErrorHandler(lua_State *L) {
- lua_getfield(L, LUA_GLOBALSINDEX, "debug");
- if (!lua_istable(L, -1)) {
- lua_pop(L, 1);
- return 1;
- }
- lua_getfield(L, -1, "traceback");
- if (!lua_isfunction(L, -1)) {
- lua_pop(L, 2);
- return 1;
- }
- lua_pushvalue(L, 1);
- lua_pushinteger(L, 2);
- lua_call(L, 2, 1);
- return 1;
-}
-
/*
ScriptApiBase
lua_State *L = getStack();
- lua_pushcfunction(L, loadScript_ErrorHandler);
+ 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);
errorstream<<std::endl;
errorstream<<lua_tostring(L, -1)<<std::endl;
errorstream<<std::endl;
- errorstream<<"=======END OF ERROR FROM LUA ========"<<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;
if(top >= 30){
dstream<<"Stack is over 30:"<<std::endl;
stackDump(dstream);
- scriptError("Stack is over 30 (reality check)");
+ throw LuaError(m_luastack, "Stack is over 30 (reality check)");
}
}
-void ScriptApiBase::scriptError(const char *fmt, ...)
+void ScriptApiBase::scriptError()
{
- va_list argp;
- va_start(argp, fmt);
- char buf[10000];
- vsnprintf(buf, 10000, fmt, argp);
- va_end(argp);
- //errorstream<<"SCRIPT ERROR: "<<buf;
- throw LuaError(m_luastack, buf);
+ throw LuaError(NULL, lua_tostring(m_luastack, -1));
}
void ScriptApiBase::stackDump(std::ostream &o)
#include "jthread/jmutex.h"
#include "jthread/jmutexautolock.h"
#include "common/c_types.h"
+#include "common/c_internal.h"
#define SCRIPTAPI_LOCK_DEBUG
{ return m_luastack; }
void realityCheck();
- void scriptError(const char *fmt, ...);
+ void scriptError();
void stackDump(std::ostream &o);
Server* getServer() { return m_server; }
{
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]
- luaentity_get(L,id);
+ luaentity_get(L, id);
int object = lua_gettop(L);
// Get on_activate function
- lua_pushvalue(L, object);
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, 0))
- scriptError("error running function on_activate: %s\n",
- lua_tostring(L, -1));
+ if(lua_pcall(L, 3, 0, errorhandler))
+ scriptError();
+ } else {
+ lua_pop(L, 1);
}
+ lua_pop(L, 2); // Pop object and error handler
}
void ScriptApiEntity::luaentity_Remove(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]
- luaentity_get(L,id);
+ luaentity_get(L, id);
int object = lua_gettop(L);
// Get get_staticdata function
- lua_pushvalue(L, object);
lua_getfield(L, -1, "get_staticdata");
if(lua_isnil(L, -1))
return "";
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
// Call with 1 arguments, 1 results
- if(lua_pcall(L, 1, 1, 0))
- scriptError("error running function get_staticdata: %s\n",
- lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 1, errorhandler))
+ scriptError();
+ lua_remove(L, object); // Remove object
+ lua_remove(L, errorhandler); // Remove error handler
- size_t len=0;
+ size_t len = 0;
const char *s = lua_tolstring(L, -1, &len);
return std::string(s, len);
}
{
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 step function
lua_pushvalue(L, object); // self
lua_pushnumber(L, dtime); // dtime
// Call with 2 arguments, 0 results
- if(lua_pcall(L, 2, 0, 0))
- scriptError("error running function 'on_step': %s\n", lua_tostring(L, -1));
+ if(lua_pcall(L, 2, 0, errorhandler))
+ scriptError();
+ lua_remove(L, object); // Remove object
+ lua_remove(L, errorhandler); // Remove error handler
}
// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
{
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]
push_tool_capabilities(L, *toolcap);
push_v3f(L, dir);
// Call with 5 arguments, 0 results
- if(lua_pcall(L, 5, 0, 0))
- scriptError("error running function 'on_punch': %s\n", lua_tostring(L, -1));
+ if(lua_pcall(L, 5, 0, errorhandler))
+ scriptError();
+ lua_remove(L, object); // Remove object
+ lua_remove(L, errorhandler); // Remove error handler
}
// Calls entity:on_rightclick(ObjectRef clicker)
{
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]
lua_pushvalue(L, object); // self
objectrefGetOrCreate(clicker); // Clicker reference
// Call with 2 arguments, 0 results
- if(lua_pcall(L, 2, 0, 0))
- scriptError("error running function 'on_rightclick': %s\n", lua_tostring(L, -1));
+ if(lua_pcall(L, 2, 0, errorhandler))
+ scriptError();
+ lua_remove(L, object); // Remove object
+ lua_remove(L, errorhandler); // Remove error handler
}
{
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;
InventoryLocation loc;
loc.setDetached(name);
InvRef::create(L, loc);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 7, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ lua_pushstring(L, from_list.c_str()); // from_list
+ lua_pushinteger(L, from_index + 1); // from_index
+ lua_pushstring(L, to_list.c_str()); // to_list
+ lua_pushinteger(L, to_index + 1); // to_index
+ lua_pushinteger(L, count); // count
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 7, 1, errorhandler))
+ scriptError();
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_move should return a number");
- return luaL_checkinteger(L, -1);
+ int ret = luaL_checkinteger(L, -1);
+ lua_pop(L, 2); // Pop integer and error handler
+ return ret;
}
// Return number of accepted items to be put
{
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
// Call function(inv, listname, index, stack, player)
- // inv
InventoryLocation loc;
loc.setDetached(name);
- InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ InvRef::create(L, loc); // inv
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 1, errorhandler))
+ scriptError();
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_put should return a number");
- return luaL_checkinteger(L, -1);
+ int ret = luaL_checkinteger(L, -1);
+ lua_pop(L, 2); // Pop integer and error handler
+ return ret;
}
// Return number of accepted items to be taken
{
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
// Call function(inv, listname, index, stack, player)
- // inv
InventoryLocation loc;
loc.setDetached(name);
- InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ InvRef::create(L, loc); // inv
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 1, errorhandler))
+ scriptError();
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_take should return a number");
- return luaL_checkinteger(L, -1);
+ int ret = luaL_checkinteger(L, -1);
+ lua_pop(L, 2); // Pop integer and error handler
+ return ret;
}
// Report moved items
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
// Push callback function on stack
if(!getDetachedInventoryCallback(name, "on_move"))
return;
InventoryLocation loc;
loc.setDetached(name);
InvRef::create(L, loc);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 7, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ lua_pushstring(L, from_list.c_str()); // from_list
+ lua_pushinteger(L, from_index + 1); // from_index
+ lua_pushstring(L, to_list.c_str()); // to_list
+ lua_pushinteger(L, to_index + 1); // to_index
+ lua_pushinteger(L, count); // count
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 7, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
// Report put items
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
// Push callback function on stack
if(!getDetachedInventoryCallback(name, "on_put"))
return;
InventoryLocation loc;
loc.setDetached(name);
InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
// Report taken items
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
// Push callback function on stack
if(!getDetachedInventoryCallback(name, "on_take"))
return;
InventoryLocation loc;
loc.setDetached(name);
InvRef::create(L, loc);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
// Retrieves minetest.detached_inventories[name][callbackname]
{
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;
LuaItemStack::create(L, item);
objectrefGetOrCreate(dropper);
pushFloatPos(L, pos);
- if(lua_pcall(L, 3, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 3, 1, errorhandler))
+ scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
+ lua_pop(L, 2); // Pop item and error handler
return true;
}
{
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;
LuaItemStack::create(L, item);
objectrefGetOrCreate(placer);
pushPointedThing(pointed);
- if(lua_pcall(L, 3, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 3, 1, errorhandler))
+ scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
+ lua_pop(L, 2); // Pop item and error handler
return true;
}
{
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;
LuaItemStack::create(L, item);
objectrefGetOrCreate(user);
pushPointedThing(pointed);
- if(lua_pcall(L, 3, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 3, 1, errorhandler))
+ scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
+ lua_pop(L, 2); // Pop item and error handler
return true;
}
{
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);
push_items(L, items);
InvRef::create(L, craft_inv);
- if(lua_pcall(L, 4, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 4, 1, errorhandler))
+ scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
+ lua_pop(L, 2); // Pop item and error handler
return true;
}
{
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);
objectrefGetOrCreate(user);
-
+
//Push inventory list
std::vector<ItemStack> items;
for(u32 i=0; i<old_craft_grid->getSize(); i++)
push_items(L, items);
InvRef::create(L, craft_inv);
- if(lua_pcall(L, 4, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 4, 1, errorhandler))
+ scriptError();
if(!lua_isnil(L, -1))
item = read_item(L,-1, getServer());
+ lua_pop(L, 2); // Pop item and error handler
return true;
}
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "registered_items");
- lua_remove(L, -2);
+ lua_remove(L, -2); // Remove minetest
luaL_checktype(L, -1, LUA_TTABLE);
lua_getfield(L, -1, name);
- lua_remove(L, -2);
+ lua_remove(L, -2); // Remove registered_items
// Should be a table
if(lua_type(L, -1) != LUA_TTABLE)
{
// Report error and clean up
- errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
+ errorstream << "Item \"" << name << "\" not defined" << std::endl;
lua_pop(L, 1);
// Try minetest.nodedef_default instead
luaL_checktype(L, -1, LUA_TTABLE);
}
lua_getfield(L, -1, callbackname);
- lua_remove(L, -2);
+ lua_remove(L, -2); // Remove item def
// Should be a function or nil
- if(lua_type(L, -1) == LUA_TFUNCTION)
- {
+ if (lua_type(L, -1) == LUA_TFUNCTION) {
return true;
+ } else if (!lua_isnil(L, -1)) {
+ errorstream << "Item \"" << name << "\" callback \""
+ << callbackname << "\" is not a function" << std::endl;
}
- else if(lua_isnil(L, -1))
- {
- lua_pop(L, 1);
- return false;
- }
- else
- {
- errorstream<<"Item \""<<name<<"\" callback \""
- <<callbackname<<" is not a function"<<std::endl;
- lua_pop(L, 1);
- return false;
- }
+ lua_pop(L, 1);
+ return false;
}
void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
{
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");
- if(lua_isnil(L, -1))
+ lua_remove(L, -2); // Remove engine
+ if(lua_isnil(L, -1)) {
+ lua_pop(L, 1); // Pop event_handler
return;
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
// Call it
lua_pushstring(L, text.c_str());
- if(lua_pcall(L, 1, 0, 0))
- scriptError("error running function engine.event_handler: %s\n",
- lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, 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");
- if(lua_isnil(L, -1))
+ lua_remove(L, -2); // Remove engine
+ if(lua_isnil(L, -1)) {
+ lua_pop(L, 1); // Pop button handler
return;
+ }
luaL_checktype(L, -1, LUA_TFUNCTION);
// Convert fields to lua table
}
// Call it
- if(lua_pcall(L, 1, 0, 0))
- scriptError("error running function engine.button_handler: %s\n",
- lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
push_v3s16(L, p);
pushnode(L, node, ndef);
objectrefGetOrCreate(puncher);
- if(lua_pcall(L, 3, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 3, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
return true;
}
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
push_v3s16(L, p);
pushnode(L, node, ndef);
objectrefGetOrCreate(digger);
- if(lua_pcall(L, 3, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 3, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
return true;
}
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
// Call function
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, 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
// Call function
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, 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
// Call function
push_v3s16(L, p);
pushnode(L, node, ndef);
- if(lua_pcall(L, 2, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 2, 0, 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
// Call function
push_v3s16(L, p);
lua_pushnumber(L,dtime);
- if(lua_pcall(L, 2, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
- if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
- return true;
-
- return false;
+ if(lua_pcall(L, 2, 1, errorhandler))
+ scriptError();
+ lua_remove(L, errorhandler); // Remove error handler
+ return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
}
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
return;
// Call function
- // param 1
- push_v3s16(L, p);
- // param 2
- lua_pushstring(L, formname.c_str());
- // param 3
- lua_newtable(L);
+ 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;
lua_pushlstring(L, value.c_str(), value.size());
lua_settable(L, -3);
}
- // param 4
- objectrefGetOrCreate(sender);
- if(lua_pcall(L, 4, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ objectrefGetOrCreate(sender); // player
+ if(lua_pcall(L, 4, 0, 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, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, 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, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
+
{
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
return count;
// function(pos, from_list, from_index, to_list, to_index, count, player)
- // pos
- push_v3s16(L, p);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 7, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, from_list.c_str()); // from_list
+ lua_pushinteger(L, from_index + 1); // from_index
+ lua_pushstring(L, to_list.c_str()); // to_list
+ lua_pushinteger(L, to_index + 1); // to_index
+ lua_pushinteger(L, count); // count
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 7, 1, errorhandler))
+ scriptError();
+ lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_metadata_inventory_move should return a number");
- return luaL_checkinteger(L, -1);
+ int num = luaL_checkinteger(L, -1);
+ lua_pop(L, 1); // Pop integer
+ return num;
}
// Return number of accepted items to be put
{
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
return stack.count;
// Call function(pos, listname, index, stack, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 1, errorhandler))
+ scriptError();
+ lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_metadata_inventory_put should return a number");
- return luaL_checkinteger(L, -1);
+ int num = luaL_checkinteger(L, -1);
+ lua_pop(L, 1); // Pop integer
+ return num;
}
// Return number of accepted items to be taken
{
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
return stack.count;
// Call function(pos, listname, index, count, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 1, errorhandler))
+ scriptError();
+ lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_metadata_inventory_take should return a number");
- return luaL_checkinteger(L, -1);
+ int num = luaL_checkinteger(L, -1);
+ lua_pop(L, 1); // Pop integer
+ return num;
}
// Report moved items
{
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
return;
// function(pos, from_list, from_index, to_list, to_index, count, player)
- // pos
- push_v3s16(L, p);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 7, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, from_list.c_str()); // from_list
+ lua_pushinteger(L, from_index + 1); // from_index
+ lua_pushstring(L, to_list.c_str()); // to_list
+ lua_pushinteger(L, to_index + 1); // to_index
+ lua_pushinteger(L, count); // count
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 7, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
// Report put items
{
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
return;
// Call function(pos, listname, index, stack, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
// Report taken items
{
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
return;
// Call function(pos, listname, index, stack, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
ScriptApiNodemeta::ScriptApiNodemeta() {
{
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(L, "Authentication handler missing get_auth");
lua_pushstring(L, playername.c_str());
- if(lua_pcall(L, 1, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 1, 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))
lua_getfield(L, -1, "privileges");
if(!lua_istable(L, -1))
- throw LuaError(L,
- "Authentication handler didn't return privilege table");
+ throw LuaError(L, "Authentication handler didn't return privilege table");
if(dst_privs)
readPrivileges(-1, *dst_privs);
lua_pop(L, 1);
lua_pop(L, 1);
lua_getfield(L, -1, "builtin_auth_handler");
}
+ lua_remove(L, -2); // Remove minetest
if(lua_type(L, -1) != LUA_TTABLE)
throw LuaError(L, "Authentication handler table not valid");
}
{
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
if(lua_type(L, -1) != LUA_TFUNCTION)
throw LuaError(L, "Authentication handler missing create_auth");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
- if(lua_pcall(L, 2, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 2, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
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
if(lua_type(L, -1) != LUA_TFUNCTION)
throw LuaError(L, "Authentication handler missing set_password");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
- if(lua_pcall(L, 2, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 2, 1, errorhandler))
+ scriptError();
+ lua_remove(L, -2); // Remove error handler
return lua_toboolean(L, -1);
}
#define L_BASE_H_
#include "common/c_types.h"
+#include "common/c_internal.h"
extern "C" {
#include <lua.h>
lua_pushstring(L, &tmpout.item[0]);
lua_setfield(L, -2, "output");
if (lua_pcall(L, 2, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
+ script_error(L);
}
}
return 1;
assert(lua_checkstack(L, 20));
StackUnroller stack_unroller(L);
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
// Get minetest.registered_abms
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "registered_abms");
luaL_checktype(L, -1, LUA_TTABLE);
- int registered_abms = lua_gettop(L);
+ lua_remove(L, -2); // Remove "minetest"
// Get minetest.registered_abms[m_id]
lua_pushnumber(L, m_id);
- lua_gettable(L, registered_abms);
+ lua_gettable(L, -2);
if(lua_isnil(L, -1))
assert(0);
+ lua_remove(L, -2); // Remove "registered_abms"
// Call action
luaL_checktype(L, -1, LUA_TTABLE);
lua_getfield(L, -1, "action");
luaL_checktype(L, -1, LUA_TFUNCTION);
+ lua_remove(L, -2); // Remove "registered_abms[m_id]"
push_v3s16(L, p);
pushnode(L, n, env->getGameDef()->ndef());
lua_pushnumber(L, active_object_count);
lua_pushnumber(L, active_object_count_wider);
- if(lua_pcall(L, 4, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 4, 0, errorhandler))
+ script_error(L);
+ lua_pop(L, 1); // Pop error handler
}
// Exported functions
ItemStack item = read_item(L, 2,getServer(L));
if(item.empty() || !item.isKnown(getServer(L)->idef()))
return 0;
+
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
// Use minetest.spawn_item to spawn a __builtin:item
lua_getglobal(L, "minetest");
lua_getfield(L, -1, "spawn_item");
+ lua_remove(L, -2); // Remove minetest
if(lua_isnil(L, -1))
return 0;
lua_pushvalue(L, 1);
lua_pushstring(L, item.getItemString().c_str());
- if(lua_pcall(L, 2, 1, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 2, 1, errorhandler))
+ script_error(L);
+ lua_remove(L, errorhandler); // Remove error handler
return 1;
/*lua_pushvalue(L, 1);
lua_pushstring(L, "__builtin:item");
lua_pushvalue(L, table);
getScriptApiBase(L)->objectrefGetOrCreate(obj);
if(lua_pcall(L, 2, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
+ script_error(L);
}
return 1;
}
lua_pushvalue(L, table);
push_v3s16(L, p);
if(lua_pcall(L, 2, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
+ script_error(L);
}
}
return 1;
throw LuaError(L, "PseudoRandom.next(): max < min");
}
if(max - min != 32767 && max - min > 32767/5)
- throw LuaError(L, "PseudoRandom.next() max-min is not 32767 and is > 32768/5. This is disallowed due to the bad random distribution the implementation would otherwise make.");
+ throw LuaError(L, "PseudoRandom.next() max-min is not 32767"
+ " and is > 32768/5. This is disallowed due to"
+ " the bad random distribution the"
+ " implementation would otherwise make.");
PseudoRandom &pseudo = o->m_pseudo;
int val = pseudo.next();
val = (val % (max-min+1)) + min;
lua_pushvalue(L, table);
lua_pushstring(L, i->c_str());
if(lua_pcall(L, 2, 0, 0))
- script_error(L, "error: %s", lua_tostring(L, -1));
+ script_error(L);
}
lua_remove(L, -2); // Remove table
lua_remove(L, -2); // Remove insert
int ModApiServer::l_get_modnames(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
+
// Get a list of mods
std::list<std::string> mods_unsorted, mods_sorted;
getServer(L)->getModNames(mods_unsorted);
lua_pushstring(L, (*i).c_str());
if(lua_pcall(L, 2, 0, 0) != 0)
{
- script_error(L, "error: %s", lua_tostring(L, -1));
+ script_error(L);
}
++i;
}