refactor(lua): pre-parse pcall wrapper ahead of time
This commit is contained in:
parent
5c8c39cc33
commit
c63e91285b
3 changed files with 8 additions and 6 deletions
|
@ -7,7 +7,6 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
extern const char* WRAPPER_SRC; // TODO: Move this to a shared header
|
|
||||||
int script_errhandler(lua_State*); // extern
|
int script_errhandler(lua_State*); // extern
|
||||||
|
|
||||||
SignalSource::SignalSource() : std::shared_ptr<Signal>(std::make_shared<Signal>()) {}
|
SignalSource::SignalSource() : std::shared_ptr<Signal>(std::make_shared<Signal>()) {}
|
||||||
|
@ -62,7 +61,7 @@ void LuaSignalConnection::Call(std::vector<Variant> args) {
|
||||||
lua_State* thread = lua_newthread(state);
|
lua_State* thread = lua_newthread(state);
|
||||||
|
|
||||||
// Push wrapepr as thread function
|
// Push wrapepr as thread function
|
||||||
luaL_loadbuffer(thread, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER");
|
lua_getfield(thread, LUA_REGISTRYINDEX, "LuaPCallWrapper");
|
||||||
|
|
||||||
// Push function as upvalue for wrapper
|
// Push function as upvalue for wrapper
|
||||||
lua_rawgeti(thread, LUA_REGISTRYINDEX, function);
|
lua_rawgeti(thread, LUA_REGISTRYINDEX, function);
|
||||||
|
|
|
@ -16,9 +16,6 @@ int script_wait(lua_State*);
|
||||||
int script_delay(lua_State*);
|
int script_delay(lua_State*);
|
||||||
int script_errhandler(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) {
|
Script::Script(): Instance(&TYPE) {
|
||||||
source = "print(\"Hello, world!\")";
|
source = "print(\"Hello, world!\")";
|
||||||
}
|
}
|
||||||
|
@ -59,7 +56,7 @@ void Script::Run() {
|
||||||
lua_pop(Lt, 1); // _G
|
lua_pop(Lt, 1); // _G
|
||||||
|
|
||||||
// Push wrapper as thread function
|
// Push wrapper as thread function
|
||||||
luaL_loadbuffer(Lt, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER");
|
lua_getfield(Lt, LUA_REGISTRYINDEX, "LuaPCallWrapper");
|
||||||
|
|
||||||
// Load source code and push onto thread as upvalue for wrapper
|
// Load source code and push onto thread as upvalue for wrapper
|
||||||
int status = luaL_loadbuffer(Lt, source.c_str(), source.size(), this->GetFullName().c_str());
|
int status = luaL_loadbuffer(Lt, source.c_str(), source.size(), this->GetFullName().c_str());
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "luaapis.h" // IWYU pragma: keep
|
#include "luaapis.h" // IWYU pragma: keep
|
||||||
|
|
||||||
|
const char* WRAPPER_SRC = "local func, errhandler = ... return function(...) local args = {...} xpcall(function() func(unpack(args)) end, errhandler) end";
|
||||||
|
|
||||||
static int g_print(lua_State*);
|
static int g_print(lua_State*);
|
||||||
static int g_require(lua_State*);
|
static int g_require(lua_State*);
|
||||||
static const struct luaL_Reg luaglobals [] = {
|
static const struct luaL_Reg luaglobals [] = {
|
||||||
|
@ -48,6 +50,10 @@ void ScriptContext::InitService() {
|
||||||
Color3::PushLuaLibrary(state);
|
Color3::PushLuaLibrary(state);
|
||||||
Instance::PushLuaLibrary(state);
|
Instance::PushLuaLibrary(state);
|
||||||
|
|
||||||
|
// Add wrapper function
|
||||||
|
luaL_loadbuffer(state, WRAPPER_SRC, strlen(WRAPPER_SRC), "=PCALL_WRAPPER");
|
||||||
|
lua_setfield(state, LUA_REGISTRYINDEX, "LuaPCallWrapper");
|
||||||
|
|
||||||
// TODO: custom os library
|
// TODO: custom os library
|
||||||
|
|
||||||
// Override print
|
// Override print
|
||||||
|
|
Loading…
Add table
Reference in a new issue