summaryrefslogtreecommitdiff
path: root/library/LuaTypes.cpp
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-04-15 19:09:25 +0400
committerAlexander Gavrilov2012-04-15 19:09:25 +0400
commit14709e5d4598e11ddce6f9d2cce734528d842ca5 (patch)
treeb1370d42d766b329b1ce8fc5431b4e7abf04a4b8 /library/LuaTypes.cpp
parentcb27a1d83916b333d1a0d1b5aa24a7f371e120af (diff)
downloaddfhack-14709e5d4598e11ddce6f9d2cce734528d842ca5.tar.gz
dfhack-14709e5d4598e11ddce6f9d2cce734528d842ca5.tar.bz2
dfhack-14709e5d4598e11ddce6f9d2cce734528d842ca5.tar.xz
Add an official core lua context, and allow plugins to send events to it.
- This context requires core suspend lock and asserts it in a few places. - Special 'event' objects are introduced. They can be invoked as functions, in which case they iterate all their fields and call them as functions. Errors are printed and consumed. - When a plugin is opened by the core context, events registered in a special array are linked to it. The system is organized so as to avoid even trying to pass the event to lua if the module isn't loaded.
Diffstat (limited to 'library/LuaTypes.cpp')
-rw-r--r--library/LuaTypes.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/library/LuaTypes.cpp b/library/LuaTypes.cpp
index f5ba565d..c5ffe0f6 100644
--- a/library/LuaTypes.cpp
+++ b/library/LuaTypes.cpp
@@ -1036,7 +1036,9 @@ static int meta_call_function(lua_State *state)
int LuaWrapper::method_wrapper_core(lua_State *state, function_identity_base *id)
{
- if (lua_gettop(state) != id->getNumArgs())
+ if (id->adjustArgs())
+ lua_settop(state, id->getNumArgs());
+ else if (lua_gettop(state) != id->getNumArgs())
field_error(state, UPVAL_METHOD_NAME, "invalid argument count", "invoke");
try {
@@ -1056,10 +1058,10 @@ int LuaWrapper::method_wrapper_core(lua_State *state, function_identity_base *id
}
/**
- * Create a closure invoking the given function, and add it to the field table.
+ * Push a closure invoking the given function.
*/
-static void AddMethodWrapper(lua_State *state, int meta_idx, int field_idx,
- const char *name, function_identity_base *fun)
+void LuaWrapper::PushFunctionWrapper(lua_State *state, int meta_idx,
+ const char *name, function_identity_base *fun)
{
lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_TYPETABLE_TOKEN);
if (meta_idx)
@@ -1069,7 +1071,15 @@ static void AddMethodWrapper(lua_State *state, int meta_idx, int field_idx,
lua_pushfstring(state, "%s()", name);
lua_pushlightuserdata(state, fun);
lua_pushcclosure(state, meta_call_function, 4);
+}
+/**
+ * Create a closure invoking the given function, and add it to the field table.
+ */
+static void AddMethodWrapper(lua_State *state, int meta_idx, int field_idx,
+ const char *name, function_identity_base *fun)
+{
+ PushFunctionWrapper(state, meta_idx, name, fun);
lua_setfield(state, field_idx, name);
}