summaryrefslogtreecommitdiff
path: root/indra/llcommon/lua_function.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'indra/llcommon/lua_function.cpp')
-rw-r--r--indra/llcommon/lua_function.cpp35
1 files changed, 24 insertions, 11 deletions
diff --git a/indra/llcommon/lua_function.cpp b/indra/llcommon/lua_function.cpp
index 906a3f379c..a5f1f582d9 100644
--- a/indra/llcommon/lua_function.cpp
+++ b/indra/llcommon/lua_function.cpp
@@ -41,16 +41,9 @@ namespace
int lluau::dostring(lua_State* L, const std::string& desc, const std::string& text)
{
- {
- size_t bytecodeSize = 0;
- // The char* returned by luau_compile() must be freed by calling free().
- // Use unique_ptr so the memory will be freed even if luau_load() throws.
- std::unique_ptr<char[], freer> bytecode{
- luau_compile(text.data(), text.length(), nullptr, &bytecodeSize)};
- auto r = luau_load(L, desc.data(), bytecode.get(), bytecodeSize, 0);
- if (r != LUA_OK)
- return r;
- } // free bytecode
+ auto r = loadstring(L, desc, text);
+ if (r != LUA_OK)
+ return r;
// It's important to pass LUA_MULTRET as the expected number of return
// values: if we pass any fixed number, we discard any returned values
@@ -58,6 +51,16 @@ int lluau::dostring(lua_State* L, const std::string& desc, const std::string& te
return lua_pcall(L, 0, LUA_MULTRET, 0);
}
+int lluau::loadstring(lua_State *L, const std::string &desc, const std::string &text)
+{
+ size_t bytecodeSize = 0;
+ // The char* returned by luau_compile() must be freed by calling free().
+ // Use unique_ptr so the memory will be freed even if luau_load() throws.
+ std::unique_ptr<char[], freer> bytecode{
+ luau_compile(text.data(), text.length(), nullptr, &bytecodeSize)};
+ return luau_load(L, desc.data(), bytecode.get(), bytecodeSize, 0);
+}
+
/*****************************************************************************
* Lua <=> C++ conversions
*****************************************************************************/
@@ -426,8 +429,18 @@ void lua_pushllsd(lua_State* L, const LLSD& data)
*****************************************************************************/
LuaState::LuaState(script_finished_fn cb):
mCallback(cb),
- mState(luaL_newstate())
+ mState(nullptr)
{
+ initLuaState();
+}
+
+void LuaState::initLuaState()
+{
+ if (mState)
+ {
+ lua_close(mState);
+ }
+ mState = luaL_newstate();
luaL_openlibs(mState);
LuaFunction::init(mState);
// Try to make print() write to our log.