#define CMAKE_USE_FREETYPE @USE_FREETYPE@
#define CMAKE_STATIC_SHAREDIR "@SHAREDIR@"
#define CMAKE_USE_LEVELDB @USE_LEVELDB@
+#define CMAKE_USE_LUAJIT @USE_LUAJIT@
#ifdef NDEBUG
#define CMAKE_BUILD_TYPE "Release"
#define USE_FREETYPE 0
#define STATIC_SHAREDIR ""
#define USE_LEVELDB 0
+#define USE_LUAJIT 0
#ifdef USE_CMAKE_CONFIG_H
#include "cmake_config.h"
#define STATIC_SHAREDIR CMAKE_STATIC_SHAREDIR
#undef USE_LEVELDB
#define USE_LEVELDB CMAKE_USE_LEVELDB
+ #undef USE_LUAJIT
+ #define USE_LUAJIT CMAKE_USE_LUAJIT
#endif
#endif
#define EXCEPTIONS_HEADER
#include <exception>
+#include <string>
+
class BaseException : public std::exception
{
public:
- BaseException(const char *s)
+ BaseException(const std::string s) throw()
{
m_s = s;
}
+ ~BaseException() throw() {}
virtual const char * what() const throw()
{
- return m_s;
+ return m_s.c_str();
}
- const char *m_s;
+protected:
+ std::string m_s;
};
-class AsyncQueuedException : public BaseException
-{
+class AsyncQueuedException : public BaseException {
public:
- AsyncQueuedException(const char *s):
- BaseException(s)
- {}
+ AsyncQueuedException(std::string s): BaseException(s) {}
};
-class NotImplementedException : public BaseException
-{
+class NotImplementedException : public BaseException {
public:
- NotImplementedException(const char *s):
- BaseException(s)
- {}
+ NotImplementedException(std::string s): BaseException(s) {}
};
-class AlreadyExistsException : public BaseException
-{
+class AlreadyExistsException : public BaseException {
public:
- AlreadyExistsException(const char *s):
- BaseException(s)
- {}
+ AlreadyExistsException(std::string s): BaseException(s) {}
};
-class VersionMismatchException : public BaseException
-{
+class VersionMismatchException : public BaseException {
public:
- VersionMismatchException(const char *s):
- BaseException(s)
- {}
+ VersionMismatchException(std::string s): BaseException(s) {}
};
-class FileNotGoodException : public BaseException
-{
+class FileNotGoodException : public BaseException {
public:
- FileNotGoodException(const char *s):
- BaseException(s)
- {}
+ FileNotGoodException(std::string s): BaseException(s) {}
};
-class SerializationError : public BaseException
-{
+class SerializationError : public BaseException {
public:
- SerializationError(const char *s):
- BaseException(s)
- {}
+ SerializationError(std::string s): BaseException(s) {}
};
-class LoadError : public BaseException
-{
+class LoadError : public BaseException {
public:
- LoadError(const char *s):
- BaseException(s)
- {}
+ LoadError(std::string s): BaseException(s) {}
};
-class ContainerFullException : public BaseException
-{
+class ContainerFullException : public BaseException {
public:
- ContainerFullException(const char *s):
- BaseException(s)
- {}
+ ContainerFullException(std::string s): BaseException(s) {}
};
-class SettingNotFoundException : public BaseException
-{
+class SettingNotFoundException : public BaseException {
public:
- SettingNotFoundException(const char *s):
- BaseException(s)
- {}
+ SettingNotFoundException(std::string s): BaseException(s) {}
};
-class InvalidFilenameException : public BaseException
-{
+class InvalidFilenameException : public BaseException {
public:
- InvalidFilenameException(const char *s):
- BaseException(s)
- {}
+ InvalidFilenameException(std::string s): BaseException(s) {}
};
-class ProcessingLimitException : public BaseException
-{
+class ProcessingLimitException : public BaseException {
public:
- ProcessingLimitException(const char *s):
- BaseException(s)
- {}
+ ProcessingLimitException(std::string s): BaseException(s) {}
};
-class CommandLineError : public BaseException
-{
+class CommandLineError : public BaseException {
public:
- CommandLineError(const char *s):
- BaseException(s)
- {}
+ CommandLineError(std::string s): BaseException(s) {}
};
-class ItemNotFoundException : public BaseException
-{
+class ItemNotFoundException : public BaseException {
public:
- ItemNotFoundException(const char *s):
- BaseException(s)
- {}
+ ItemNotFoundException(std::string s): BaseException(s) {}
+};
+
+class ServerError : public BaseException {
+public:
+ ServerError(std::string s): BaseException(s) {}
};
// Only used on Windows (SEH)
-class FatalSystemException : public BaseException
-{
+class FatalSystemException : public BaseException {
public:
- FatalSystemException(const char *s):
- BaseException(s)
- {}
+ FatalSystemException(std::string s): BaseException(s) {}
};
/*
InvalidPositionException():
BaseException("Somebody tried to get/set something in a nonexistent position.")
{}
- InvalidPositionException(const char *s):
+ InvalidPositionException(std::string s):
BaseException(s)
{}
};
L" running a different version of Minetest.";
errorstream<<wide_to_narrow(error_message)<<std::endl;
}
- catch(ServerError &e)
- {
+ catch(ServerError &e) {
error_message = narrow_to_wide(e.what());
- errorstream<<wide_to_narrow(error_message)<<std::endl;
+ errorstream << "ServerError: " << e.what() << std::endl;
}
- catch(ModError &e)
- {
- errorstream<<e.what()<<std::endl;
+ catch(ModError &e) {
+ errorstream << "ModError: " << e.what() << std::endl;
error_message = narrow_to_wide(e.what()) + wgettext("\nCheck debug.txt for details.");
}
}
else
{
- throw LuaError(L, "Expecting itemstack, itemstring, table or nil");
+ throw LuaError(NULL, "Expecting itemstack, itemstring, table or nil");
}
}
return 1;
}
+int script_exception_wrapper(lua_State *L, lua_CFunction f)
+{
+ try {
+ return f(L); // Call wrapped function and return result.
+ } catch (const char *s) { // Catch and convert exceptions.
+ lua_pushstring(L, s);
+ } catch (LuaError& e) {
+ lua_pushstring(L, e.what());
+ }
+ return lua_error(L); // Rethrow as a Lua error.
+}
+
void script_error(lua_State *L)
{
const char *s = lua_tostring(L, -1);
std::string script_get_backtrace(lua_State *L);
int script_error_handler(lua_State *L);
+int script_exception_wrapper(lua_State *L, lua_CFunction f);
void script_error(lua_State *L);
void script_run_callbacks(lua_State *L, int nargs,
RunCallbacksMode mode);
#include "common/c_internal.h"
#include "itemdef.h"
-LuaError::LuaError(lua_State *L, const std::string &s)
+LuaError::LuaError(lua_State *L, const std::string &s) :
+ ServerError(s)
{
- m_s = "LuaError: " + s;
- if (L) m_s += '\n' + script_get_backtrace(L);
+ if (L) {
+ m_s += '\n' + script_get_backtrace(L);
+ }
}
struct EnumString es_ItemType[] =
#include <iostream>
+#include "exceptions.h"
+
struct EnumString
{
int num;
}
};
-class LuaError : public std::exception
+class LuaError : public ServerError
{
public:
LuaError(lua_State *L, const std::string &s);
{
return m_s.c_str();
}
- std::string m_s;
};
extern "C" {
#include "lualib.h"
+#if USE_LUAJIT
+ #include "luajit.h"
+#endif
}
#include <stdio.h>
lua_pushlightuserdata(m_luastack, this);
lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
+ // If we are using LuaJIT add a C++ wrapper function to catch
+ // exceptions thrown in Lua -> C++ calls
+#if USE_LUAJIT
+ lua_pushlightuserdata(m_luastack, (void*) script_exception_wrapper);
+ luaJIT_setmode(m_luastack, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
+ lua_pop(m_luastack, 1);
+#endif
+
m_server = 0;
m_environment = 0;
m_guiengine = 0;
if(lua_pcall(L, 7, 1, errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_move should return a number");
+ throw LuaError(NULL, "allow_move should return a number");
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return ret;
if(lua_pcall(L, 5, 1, errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_put should return a number");
+ throw LuaError(NULL, "allow_put should return a number");
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return ret;
if(lua_pcall(L, 5, 1, errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_take should return a number");
+ throw LuaError(NULL, "allow_take should return a number");
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
return ret;
scriptError();
lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_metadata_inventory_move should return a number");
+ throw LuaError(NULL, "allow_metadata_inventory_move should return a number");
int num = luaL_checkinteger(L, -1);
lua_pop(L, 1); // Pop integer
return num;
scriptError();
lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_metadata_inventory_put should return a number");
+ throw LuaError(NULL, "allow_metadata_inventory_put should return a number");
int num = luaL_checkinteger(L, -1);
lua_pop(L, 1); // Pop integer
return num;
scriptError();
lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
- throw LuaError(L, "allow_metadata_inventory_take should return a number");
+ throw LuaError(NULL, "allow_metadata_inventory_take should return a number");
int num = luaL_checkinteger(L, -1);
lua_pop(L, 1); // Pop integer
return num;
getAuthHandler();
lua_getfield(L, -1, "get_auth");
if(lua_type(L, -1) != LUA_TFUNCTION)
- throw LuaError(L, "Authentication handler missing get_auth");
+ throw LuaError(NULL, "Authentication handler missing get_auth");
lua_pushstring(L, playername.c_str());
if(lua_pcall(L, 1, 1, errorhandler))
scriptError();
std::string password;
bool found = getstringfield(L, -1, "password", password);
if(!found)
- throw LuaError(L, "Authentication handler didn't return password");
+ throw LuaError(NULL, "Authentication handler didn't return password");
if(dst_password)
*dst_password = password;
lua_getfield(L, -1, "privileges");
if(!lua_istable(L, -1))
- throw LuaError(L, "Authentication handler didn't return privilege table");
+ throw LuaError(NULL, "Authentication handler didn't return privilege table");
if(dst_privs)
readPrivileges(-1, *dst_privs);
lua_pop(L, 1);
}
lua_remove(L, -2); // Remove minetest
if(lua_type(L, -1) != LUA_TTABLE)
- throw LuaError(L, "Authentication handler table not valid");
+ throw LuaError(NULL, "Authentication handler table not valid");
}
void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
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");
+ throw LuaError(NULL, "Authentication handler missing create_auth");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
if(lua_pcall(L, 2, 0, errorhandler))
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");
+ throw LuaError(NULL, "Authentication handler missing set_password");
lua_pushstring(L, playername.c_str());
lua_pushstring(L, password.c_str());
if(lua_pcall(L, 2, 1, errorhandler))
ScriptApiBase *scriptIface = getScriptApiBase(L);
T *scriptIfaceDowncast = dynamic_cast<T*>(scriptIface);
if (!scriptIfaceDowncast) {
- throw LuaError(L, "Requested unavailable ScriptApi - core engine bug!");
+ throw LuaError(NULL, "Requested unavailable ScriptApi - core engine bug!");
}
return scriptIfaceDowncast;
}
if(type == "shaped"){
std::string output = getstringfield_default(L, table, "output", "");
if(output == "")
- throw LuaError(L, "Crafting definition is missing an output");
+ throw LuaError(NULL, "Crafting definition is missing an output");
int width = 0;
std::vector<std::string> recipe;
lua_getfield(L, table, "recipe");
if(lua_isnil(L, -1))
- throw LuaError(L, "Crafting definition is missing a recipe"
+ throw LuaError(NULL, "Crafting definition is missing a recipe"
" (output=\"" + output + "\")");
if(!readCraftRecipeShaped(L, -1, width, recipe))
- throw LuaError(L, "Invalid crafting recipe"
+ throw LuaError(NULL, "Invalid crafting recipe"
" (output=\"" + output + "\")");
CraftReplacements replacements;
if(!lua_isnil(L, -1))
{
if(!readCraftReplacements(L, -1, replacements))
- throw LuaError(L, "Invalid replacements"
+ throw LuaError(NULL, "Invalid replacements"
" (output=\"" + output + "\")");
}
else if(type == "shapeless"){
std::string output = getstringfield_default(L, table, "output", "");
if(output == "")
- throw LuaError(L, "Crafting definition (shapeless)"
+ throw LuaError(NULL, "Crafting definition (shapeless)"
" is missing an output");
std::vector<std::string> recipe;
lua_getfield(L, table, "recipe");
if(lua_isnil(L, -1))
- throw LuaError(L, "Crafting definition (shapeless)"
+ throw LuaError(NULL, "Crafting definition (shapeless)"
" is missing a recipe"
" (output=\"" + output + "\")");
if(!readCraftRecipeShapeless(L, -1, recipe))
- throw LuaError(L, "Invalid crafting recipe"
+ throw LuaError(NULL, "Invalid crafting recipe"
" (output=\"" + output + "\")");
CraftReplacements replacements;
if(!lua_isnil(L, -1))
{
if(!readCraftReplacements(L, -1, replacements))
- throw LuaError(L, "Invalid replacements"
+ throw LuaError(NULL, "Invalid replacements"
" (output=\"" + output + "\")");
}
else if(type == "cooking"){
std::string output = getstringfield_default(L, table, "output", "");
if(output == "")
- throw LuaError(L, "Crafting definition (cooking)"
+ throw LuaError(NULL, "Crafting definition (cooking)"
" is missing an output");
std::string recipe = getstringfield_default(L, table, "recipe", "");
if(recipe == "")
- throw LuaError(L, "Crafting definition (cooking)"
+ throw LuaError(NULL, "Crafting definition (cooking)"
" is missing a recipe"
" (output=\"" + output + "\")");
if(!lua_isnil(L, -1))
{
if(!readCraftReplacements(L, -1, replacements))
- throw LuaError(L, "Invalid replacements"
+ throw LuaError(NULL, "Invalid replacements"
" (cooking output=\"" + output + "\")");
}
else if(type == "fuel"){
std::string recipe = getstringfield_default(L, table, "recipe", "");
if(recipe == "")
- throw LuaError(L, "Crafting definition (fuel)"
+ throw LuaError(NULL, "Crafting definition (fuel)"
" is missing a recipe");
float burntime = getfloatfield_default(L, table, "burntime", 1.0);
if(!lua_isnil(L, -1))
{
if(!readCraftReplacements(L, -1, replacements))
- throw LuaError(L, "Invalid replacements"
+ throw LuaError(NULL, "Invalid replacements"
" (fuel recipe=\"" + recipe + "\")");
}
}
else
{
- throw LuaError(L, "Unknown crafting definition type: \"" + type + "\"");
+ throw LuaError(NULL, "Unknown crafting definition type: \"" + type + "\"");
}
lua_pop(L, 1);
name = lua_tostring(L, -1);
verbosestream<<"register_item_raw: "<<name<<std::endl;
} else {
- throw LuaError(L, "register_item_raw: name is not defined or not a string");
+ throw LuaError(NULL, "register_item_raw: name is not defined or not a string");
}
// Check if on_use is defined
content_t id = ndef->set(f.name, f);
if(id > MAX_REGISTERED_CONTENT){
- throw LuaError(L, "Number of registerable nodes ("
+ throw LuaError(NULL, "Number of registerable nodes ("
+ itos(MAX_REGISTERED_CONTENT+1)
+ ") exceeded (" + name + ")");
}
max = luaL_checkinteger(L, 3);
if(max < min){
errorstream<<"PseudoRandom.next(): max="<<max<<" min="<<min<<std::endl;
- throw LuaError(L, "PseudoRandom.next(): max < min");
+ throw LuaError(NULL, "PseudoRandom.next(): max < min");
}
if(max - min != 32767 && max - min > 32767/5)
- throw LuaError(L, "PseudoRandom.next() max-min is not 32767"
+ throw LuaError(NULL, "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.");
struct SimpleSoundSpec;
-class ServerError : public std::exception
-{
-public:
- ServerError(const std::string &s)
- {
- m_s = "ServerError: ";
- m_s += s;
- }
- virtual ~ServerError() throw()
- {}
- virtual const char * what() const throw()
- {
- return m_s.c_str();
- }
- std::string m_s;
-};
-
/*
Some random functions
*/