diff --git a/core/src/objects/script.cpp b/core/src/objects/script.cpp index 4c72dab..994c824 100644 --- a/core/src/objects/script.cpp +++ b/core/src/objects/script.cpp @@ -1,5 +1,6 @@ #include "script.h" #include "common.h" +#include "datatypes/variant.h" #include "lauxlib.h" #include "logger.h" #include "objects/base/instance.h" @@ -31,23 +32,6 @@ void Script::Run() { this->thread = lua_newthread(L); lua_State* Lt = thread; - lua_pushthread(Lt); // Push thread for later* - - // Initialize script globals - scriptContext->NewEnvironment(Lt); // Pushes envtable, metatable - - // Set script in metatable source - InstanceRef(shared_from_this()).PushLuaValue(Lt); - lua_setfield(Lt, -2, "source"); - - lua_pop(Lt, 1); // Pop metatable - - InstanceRef(shared_from_this()).PushLuaValue(Lt); - lua_setfield(Lt, -2, "script"); - - lua_setfenv(Lt, -2); // *Set env of current thread - lua_pop(Lt, 1); // Pop thread - // Push wrapper as thread function lua_getfield(Lt, LUA_REGISTRYINDEX, "LuaPCallWrapper"); @@ -61,6 +45,21 @@ void Script::Run() { return; } + // Initialize script globals + scriptContext->NewEnvironment(Lt); // Pushes envtable, metatable + + // Set script in metatable source + InstanceRef(shared_from_this()).PushLuaValue(Lt); + lua_setfield(Lt, -2, "source"); + + lua_pop(Lt, 1); // Pop metatable + + // Set script in environment + InstanceRef(shared_from_this()).PushLuaValue(Lt); + lua_setfield(Lt, -2, "script"); + + lua_setfenv(Lt, -2); // Set env of loaded function + // Push our error handler and then generate the wrapped function lua_pushcfunction(Lt, script_errhandler); lua_call(Lt, 2, 1); @@ -76,6 +75,34 @@ void Script::Stop() { // TODO: } +static std::shared_ptr