Add Lua fallback script if it doesn't run correctly

pull/170/head
James Ball 2023-09-14 20:21:08 +01:00
rodzic 60e7e2082d
commit 059d436261
4 zmienionych plików z 46 dodań i 15 usunięć

Wyświetl plik

@ -2,18 +2,27 @@
#include "luaimport.h" #include "luaimport.h"
LuaParser::LuaParser(juce::String script) { LuaParser::LuaParser(juce::String script, juce::String fallbackScript) : fallbackScript(fallbackScript) {
// initialization reset(script);
}
LuaParser::~LuaParser() {
lua_close(L);
}
void LuaParser::reset(juce::String script) {
functionRef = -1;
if (L != nullptr) {
lua_close(L);
}
L = luaL_newstate(); L = luaL_newstate();
lua_atpanic(L, panic); lua_atpanic(L, panic);
luaL_openlibs(L); luaL_openlibs(L);
this->script = script; this->script = script;
parse(); parse();
}
LuaParser::~LuaParser() {
lua_close(L);
} }
void LuaParser::parse() { void LuaParser::parse() {
@ -23,6 +32,9 @@ void LuaParser::parse() {
DBG(error); DBG(error);
lua_pop(L, 1); lua_pop(L, 1);
functionRef = -1; functionRef = -1;
if (script != fallbackScript) {
reset(fallbackScript);
}
} else { } else {
functionRef = luaL_ref(L, LUA_REGISTRYINDEX); functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
} }
@ -31,10 +43,6 @@ void LuaParser::parse() {
// only the audio thread runs this fuction // only the audio thread runs this fuction
std::vector<float> LuaParser::run() { std::vector<float> LuaParser::run() {
std::vector<float> values; std::vector<float> values;
if (functionRef == -1) {
return values;
}
lua_pushnumber(L, step); lua_pushnumber(L, step);
lua_setglobal(L, "step"); lua_setglobal(L, "step");
@ -61,6 +69,9 @@ std::vector<float> LuaParser::run() {
const char* error = lua_tostring(L, -1); const char* error = lua_tostring(L, -1);
DBG(error); DBG(error);
functionRef = -1; functionRef = -1;
if (script != fallbackScript) {
reset(fallbackScript);
}
} else if (lua_istable(L, -1)) { } else if (lua_istable(L, -1)) {
auto length = lua_rawlen(L, -1); auto length = lua_rawlen(L, -1);
@ -73,8 +84,10 @@ std::vector<float> LuaParser::run() {
} }
} }
} else { } else {
DBG("functionRef is not a function");
functionRef = -1; functionRef = -1;
if (script != fallbackScript) {
reset(fallbackScript);
}
} }
// clear stack // clear stack
@ -94,6 +107,14 @@ void LuaParser::setVariable(juce::String variableName, double value) {
updateVariables = true; updateVariables = true;
} }
bool LuaParser::isFunctionValid() {
return functionRef != -1;
}
juce::String LuaParser::getScript() {
return script;
}
int LuaParser::panic(lua_State *L) { int LuaParser::panic(lua_State *L) {
const char *msg = lua_tostring(L, -1); const char *msg = lua_tostring(L, -1);

Wyświetl plik

@ -5,21 +5,25 @@
struct lua_State; struct lua_State;
class LuaParser { class LuaParser {
public: public:
LuaParser(juce::String script); LuaParser(juce::String script, juce::String LuaParser = "return { 0.0, 0.0 }");
~LuaParser(); ~LuaParser();
std::vector<float> run(); std::vector<float> run();
void setVariable(juce::String variableName, double value); void setVariable(juce::String variableName, double value);
bool isFunctionValid();
juce::String getScript();
private: private:
void reset(juce::String script);
void parse(); void parse();
static int panic(lua_State* L); static int panic(lua_State* L);
int functionRef = -1; int functionRef = -1;
long step = 1; long step = 1;
lua_State* L; lua_State* L = nullptr;
juce::String script; juce::String script;
juce::String fallbackScript;
std::atomic<bool> updateVariables = false; std::atomic<bool> updateVariables = false;
juce::SpinLock variableLock; juce::SpinLock variableLock;
std::vector<juce::String> variableNames; std::vector<juce::String> variableNames;

Wyświetl plik

@ -8,6 +8,10 @@ FileParser::FileParser() {}
void FileParser::parse(juce::String extension, std::unique_ptr<juce::InputStream> stream, juce::Font font) { void FileParser::parse(juce::String extension, std::unique_ptr<juce::InputStream> stream, juce::Font font) {
juce::SpinLock::ScopedLockType scope(lock); juce::SpinLock::ScopedLockType scope(lock);
if (extension == ".lua" && lua != nullptr && lua->isFunctionValid()) {
fallbackLuaScript = lua->getScript();
}
object = nullptr; object = nullptr;
camera = nullptr; camera = nullptr;
svg = nullptr; svg = nullptr;
@ -23,7 +27,7 @@ void FileParser::parse(juce::String extension, std::unique_ptr<juce::InputStream
} else if (extension == ".txt") { } else if (extension == ".txt") {
text = std::make_shared<TextParser>(stream->readEntireStreamAsString(), font); text = std::make_shared<TextParser>(stream->readEntireStreamAsString(), font);
} else if (extension == ".lua") { } else if (extension == ".lua") {
lua = std::make_shared<LuaParser>(stream->readEntireStreamAsString()); lua = std::make_shared<LuaParser>(stream->readEntireStreamAsString(), fallbackLuaScript);
} }
sampleSource = lua != nullptr; sampleSource = lua != nullptr;

Wyświetl plik

@ -37,4 +37,6 @@ private:
std::shared_ptr<SvgParser> svg; std::shared_ptr<SvgParser> svg;
std::shared_ptr<TextParser> text; std::shared_ptr<TextParser> text;
std::shared_ptr<LuaParser> lua; std::shared_ptr<LuaParser> lua;
juce::String fallbackLuaScript = "return { 0.0, 0.0 }";
}; };