kopia lustrzana https://github.com/jameshball/osci-render
Add Lua fallback script if it doesn't run correctly
rodzic
60e7e2082d
commit
059d436261
|
@ -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<float> LuaParser::run() {
|
||||
std::vector<float> values;
|
||||
|
||||
if (functionRef == -1) {
|
||||
return values;
|
||||
}
|
||||
|
||||
lua_pushnumber(L, step);
|
||||
lua_setglobal(L, "step");
|
||||
|
@ -61,6 +69,9 @@ std::vector<float> 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<float> 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);
|
||||
|
|
|
@ -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<float> 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<bool> updateVariables = false;
|
||||
juce::SpinLock variableLock;
|
||||
std::vector<juce::String> variableNames;
|
||||
|
|
|
@ -8,6 +8,10 @@ FileParser::FileParser() {}
|
|||
void FileParser::parse(juce::String extension, std::unique_ptr<juce::InputStream> 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<juce::InputStream
|
|||
} else if (extension == ".txt") {
|
||||
text = std::make_shared<TextParser>(stream->readEntireStreamAsString(), font);
|
||||
} else if (extension == ".lua") {
|
||||
lua = std::make_shared<LuaParser>(stream->readEntireStreamAsString());
|
||||
lua = std::make_shared<LuaParser>(stream->readEntireStreamAsString(), fallbackLuaScript);
|
||||
}
|
||||
|
||||
sampleSource = lua != nullptr;
|
||||
|
|
|
@ -37,4 +37,6 @@ private:
|
|||
std::shared_ptr<SvgParser> svg;
|
||||
std::shared_ptr<TextParser> text;
|
||||
std::shared_ptr<LuaParser> lua;
|
||||
|
||||
juce::String fallbackLuaScript = "return { 0.0, 0.0 }";
|
||||
};
|
||||
|
|
Ładowanie…
Reference in New Issue