diff --git a/Source/lua/LuaParser.cpp b/Source/lua/LuaParser.cpp index 9e77698..0235766 100644 --- a/Source/lua/LuaParser.cpp +++ b/Source/lua/LuaParser.cpp @@ -2,18 +2,27 @@ #include "luaimport.h" -LuaParser::LuaParser(juce::String script) { - // initialization +LuaParser::LuaParser(juce::String script, juce::String fallbackScript) : fallbackScript(fallbackScript) { + reset(script); +} + +LuaParser::~LuaParser() { + lua_close(L); +} + +void LuaParser::reset(juce::String script) { + functionRef = -1; + + if (L != nullptr) { + lua_close(L); + } + L = luaL_newstate(); lua_atpanic(L, panic); luaL_openlibs(L); this->script = script; - parse(); -} - -LuaParser::~LuaParser() { - lua_close(L); + parse(); } void LuaParser::parse() { @@ -23,6 +32,9 @@ void LuaParser::parse() { DBG(error); lua_pop(L, 1); functionRef = -1; + if (script != fallbackScript) { + reset(fallbackScript); + } } else { functionRef = luaL_ref(L, LUA_REGISTRYINDEX); } @@ -31,10 +43,6 @@ void LuaParser::parse() { // only the audio thread runs this fuction std::vector LuaParser::run() { std::vector values; - - if (functionRef == -1) { - return values; - } lua_pushnumber(L, step); lua_setglobal(L, "step"); @@ -61,6 +69,9 @@ std::vector LuaParser::run() { const char* error = lua_tostring(L, -1); DBG(error); functionRef = -1; + if (script != fallbackScript) { + reset(fallbackScript); + } } else if (lua_istable(L, -1)) { auto length = lua_rawlen(L, -1); @@ -73,8 +84,10 @@ std::vector LuaParser::run() { } } } else { - DBG("functionRef is not a function"); functionRef = -1; + if (script != fallbackScript) { + reset(fallbackScript); + } } // clear stack @@ -94,6 +107,14 @@ void LuaParser::setVariable(juce::String variableName, double value) { updateVariables = true; } +bool LuaParser::isFunctionValid() { + return functionRef != -1; +} + +juce::String LuaParser::getScript() { + return script; +} + int LuaParser::panic(lua_State *L) { const char *msg = lua_tostring(L, -1); diff --git a/Source/lua/LuaParser.h b/Source/lua/LuaParser.h index c7f23b8..909801a 100644 --- a/Source/lua/LuaParser.h +++ b/Source/lua/LuaParser.h @@ -5,21 +5,25 @@ struct lua_State; class LuaParser { public: - LuaParser(juce::String script); + LuaParser(juce::String script, juce::String LuaParser = "return { 0.0, 0.0 }"); ~LuaParser(); std::vector run(); void setVariable(juce::String variableName, double value); + bool isFunctionValid(); + juce::String getScript(); private: + void reset(juce::String script); void parse(); static int panic(lua_State* L); int functionRef = -1; long step = 1; - lua_State* L; + lua_State* L = nullptr; juce::String script; + juce::String fallbackScript; std::atomic updateVariables = false; juce::SpinLock variableLock; std::vector variableNames; diff --git a/Source/parser/FileParser.cpp b/Source/parser/FileParser.cpp index 12d411d..104ae0e 100644 --- a/Source/parser/FileParser.cpp +++ b/Source/parser/FileParser.cpp @@ -8,6 +8,10 @@ FileParser::FileParser() {} void FileParser::parse(juce::String extension, std::unique_ptr stream, juce::Font font) { juce::SpinLock::ScopedLockType scope(lock); + if (extension == ".lua" && lua != nullptr && lua->isFunctionValid()) { + fallbackLuaScript = lua->getScript(); + } + object = nullptr; camera = nullptr; svg = nullptr; @@ -23,7 +27,7 @@ void FileParser::parse(juce::String extension, std::unique_ptr(stream->readEntireStreamAsString(), font); } else if (extension == ".lua") { - lua = std::make_shared(stream->readEntireStreamAsString()); + lua = std::make_shared(stream->readEntireStreamAsString(), fallbackLuaScript); } sampleSource = lua != nullptr; diff --git a/Source/parser/FileParser.h b/Source/parser/FileParser.h index 102abf0..8cce95d 100644 --- a/Source/parser/FileParser.h +++ b/Source/parser/FileParser.h @@ -37,4 +37,6 @@ private: std::shared_ptr svg; std::shared_ptr text; std::shared_ptr lua; + + juce::String fallbackLuaScript = "return { 0.0, 0.0 }"; };