summaryrefslogtreecommitdiff
path: root/library/LuaTypes.cpp
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-04-05 19:55:59 +0400
committerAlexander Gavrilov2012-04-05 19:55:59 +0400
commit9eed9f0d246f44a51266a05e0107ea22fea54e73 (patch)
tree72eb99e5ca85311b7a486f8e4036b9dd703ae3d4 /library/LuaTypes.cpp
parent28a741082f8b0981806b8a63589279627bd8e39e (diff)
downloaddfhack-9eed9f0d246f44a51266a05e0107ea22fea54e73.tar.gz
dfhack-9eed9f0d246f44a51266a05e0107ea22fea54e73.tar.bz2
dfhack-9eed9f0d246f44a51266a05e0107ea22fea54e73.tar.xz
Wrap a few utility functions defined on the c++ side for lua.
Diffstat (limited to 'library/LuaTypes.cpp')
-rw-r--r--library/LuaTypes.cpp33
1 files changed, 28 insertions, 5 deletions
diff --git a/library/LuaTypes.cpp b/library/LuaTypes.cpp
index 2cc929ee..4213499e 100644
--- a/library/LuaTypes.cpp
+++ b/library/LuaTypes.cpp
@@ -30,6 +30,7 @@ distribution.
#include "MemAccess.h"
#include "Core.h"
+#include "Error.h"
#include "VersionInfo.h"
#include "tinythread.h"
// must be last due to MS stupidity
@@ -1022,18 +1023,29 @@ static int meta_call_function(lua_State *state)
auto id = (function_identity_base*)lua_touserdata(state, UPVAL_CONTAINER_ID);
if (lua_gettop(state) != id->getNumArgs())
field_error(state, UPVAL_METHOD_NAME, "invalid argument count", "invoke");
- id->invoke(state, 1);
+
+ try {
+ id->invoke(state, 1);
+ }
+ catch (Error::NullPointer &e) {
+ const char *vn = e.varname();
+ std::string tmp = stl_sprintf("NULL pointer: %s", vn ? vn : "?");
+ field_error(state, UPVAL_METHOD_NAME, tmp.c_str(), "invoke");
+ }
+ catch (std::exception &e) {
+ std::string tmp = stl_sprintf("C++ exception: %s", e.what());
+ field_error(state, UPVAL_METHOD_NAME, tmp.c_str(), "invoke");
+ }
+
return 1;
}
/**
* Create a closure invoking the given function, and add it to the field table.
*/
-void LuaWrapper::AddMethodWrapper(lua_State *state, int meta_idx, int field_idx,
- const char *name, function_identity_base *fun)
+static void AddMethodWrapper(lua_State *state, int meta_idx, int field_idx,
+ const char *name, function_identity_base *fun)
{
- field_idx = lua_absindex(state, field_idx);
-
lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_TYPETABLE_TOKEN);
if (meta_idx)
lua_pushvalue(state, meta_idx);
@@ -1047,6 +1059,17 @@ void LuaWrapper::AddMethodWrapper(lua_State *state, int meta_idx, int field_idx,
}
/**
+ * Wrap functions and add them to the table on the top of the stack.
+ */
+void LuaWrapper::SetFunctionWrappers(lua_State *state, const FunctionReg *reg)
+{
+ int base = lua_gettop(state);
+
+ for (; reg && reg->name; ++reg)
+ AddMethodWrapper(state, 0, base, reg->name, reg->identity);
+}
+
+/**
* Add fields in the array to the UPVAL_FIELDTABLE candidates on the stack.
*/
static void IndexFields(lua_State *state, int base, struct_identity *pstruct)