diff --git a/core/src/datatypes/signal.cpp b/core/src/datatypes/signal.cpp index fe41106..4dfda6c 100644 --- a/core/src/datatypes/signal.cpp +++ b/core/src/datatypes/signal.cpp @@ -7,7 +7,7 @@ #include #include -int signalcall_wrapper(lua_State*); +extern const char* WRAPPER_SRC; // TODO: Move this to a shared header int script_errhandler(lua_State*); // extern SignalSource::SignalSource() : std::shared_ptr(std::make_shared()) {} @@ -56,9 +56,15 @@ static void stackdump(lua_State* L) { void LuaSignalConnection::Call(std::vector args) { lua_State* thread = lua_newthread(state); - // Push function + // Push wrapepr as thread function + luaL_loadbuffer(thread, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER"); + + // Push function as upvalue for wrapper lua_rawgeti(thread, LUA_REGISTRYINDEX, function); - lua_pushcclosure(thread, signalcall_wrapper, 1); // Push our own wrapper + + // Also push our error handler and generate wrapped function + lua_pushcfunction(thread, script_errhandler); + lua_call(thread, 2, 1); for (Variant arg : args) { arg.PushLuaValue(thread); @@ -69,22 +75,6 @@ void LuaSignalConnection::Call(std::vector args) { lua_pop(state, 1); // Pop thread } -int signalcall_wrapper(lua_State* L) { - int nargs = lua_gettop(L); - - // Push error handler and move to bottom - lua_pushcfunction(L, script_errhandler); - lua_insert(L, 1); - - // Push function and move to bottom (after error handler) - lua_pushvalue(L, lua_upvalueindex(1)); - lua_insert(L, 2); - - lua_pcall(L, nargs, 0, 1); - - return 0; -} - // CSignalConnection::CSignalConnection(std::function)> func, std::weak_ptr parent) : SignalConnection(parent) { diff --git a/core/src/objects/script.cpp b/core/src/objects/script.cpp index 9988f1b..9a823e8 100644 --- a/core/src/objects/script.cpp +++ b/core/src/objects/script.cpp @@ -16,6 +16,7 @@ int script_wait(lua_State*); int script_delay(lua_State*); int script_errhandler(lua_State*); +// TODO: Move this to a shared header const char* WRAPPER_SRC = "local func, errhandler = ... return function(...) local args = {...} xpcall(function() func(unpack(args)) end, errhandler) end"; Script::Script(): Instance(&TYPE) {