@@ -16,7 +16,7 @@ include $(RACK_DIR)/arch.mk | |||||
DUKTAPE ?= 0 | DUKTAPE ?= 0 | ||||
QUICKJS ?= 1 | QUICKJS ?= 1 | ||||
LUAJIT ?= 1 | LUAJIT ?= 1 | ||||
PYTHON ?= 1 | |||||
PYTHON ?= 0 | |||||
# Entropia File System Watcher | # Entropia File System Watcher | ||||
efsw := dep/lib/libefsw-static-release.a | efsw := dep/lib/libefsw-static-release.a | ||||
@@ -217,6 +217,7 @@ struct DuktapeEngine : ScriptEngine { | |||||
}; | }; | ||||
ScriptEngine* createDuktapeEngine() { | |||||
return new DuktapeEngine; | |||||
} | |||||
__attribute__((constructor(1000))) | |||||
static void constructor() { | |||||
addScriptEngine<DuktapeEngine>("js"); | |||||
} |
@@ -317,6 +317,7 @@ struct LuaJITEngine : ScriptEngine { | |||||
}; | }; | ||||
ScriptEngine* createLuaJITEngine() { | |||||
return new LuaJITEngine; | |||||
} | |||||
__attribute__((constructor(1000))) | |||||
static void constructor() { | |||||
addScriptEngine<LuaJITEngine>("lua"); | |||||
} |
@@ -13,6 +13,18 @@ using namespace rack; | |||||
Plugin* pluginInstance; | Plugin* pluginInstance; | ||||
// Don't bother deleting this with a destructor. | |||||
__attribute((init_priority(999))) | |||||
std::map<std::string, ScriptEngineFactory*> scriptEngineFactories; | |||||
ScriptEngine* createScriptEngine(std::string extension) { | |||||
auto it = scriptEngineFactories.find(extension); | |||||
if (it == scriptEngineFactories.end()) | |||||
return NULL; | |||||
return it->second->createScriptEngine(); | |||||
} | |||||
// Global warning message for script security | // Global warning message for script security | ||||
bool securityRequested = false; | bool securityRequested = false; | ||||
bool securityAccepted = false; | bool securityAccepted = false; | ||||
@@ -253,10 +265,10 @@ struct Prototype : Module { | |||||
this->script = script; | this->script = script; | ||||
// Create script engine from path extension | // Create script engine from path extension | ||||
std::string ext = string::filenameExtension(string::filename(path)); | |||||
scriptEngine = createScriptEngine(ext); | |||||
std::string extension = string::filenameExtension(string::filename(path)); | |||||
scriptEngine = createScriptEngine(extension); | |||||
if (!scriptEngine) { | if (!scriptEngine) { | ||||
message = string::f("No engine for .%s extension", ext.c_str()); | |||||
message = string::f("No engine for .%s extension", extension.c_str()); | |||||
return; | return; | ||||
} | } | ||||
scriptEngine->module = this; | scriptEngine->module = this; | ||||
@@ -247,6 +247,7 @@ struct PythonEngine : ScriptEngine { | |||||
}; | }; | ||||
ScriptEngine* createPythonEngine() { | |||||
return new PythonEngine; | |||||
} | |||||
__attribute__((constructor(1000))) | |||||
static void constructor() { | |||||
addScriptEngine<PythonEngine>("py"); | |||||
} |
@@ -327,6 +327,7 @@ struct QuickJSEngine : ScriptEngine { | |||||
}; | }; | ||||
ScriptEngine* createQuickJSEngine() { | |||||
return new QuickJSEngine(); | |||||
__attribute__((constructor(1000))) | |||||
static void constructor() { | |||||
addScriptEngine<QuickJSEngine>("js"); | |||||
} | } |
@@ -48,22 +48,20 @@ struct ScriptEngine { | |||||
}; | }; | ||||
// List of ScriptEngines | |||||
// Add your createMyEngine() function here. | |||||
ScriptEngine* createDuktapeEngine(); | |||||
ScriptEngine* createQuickJSEngine(); | |||||
ScriptEngine* createPythonEngine(); | |||||
ScriptEngine* createLuaJITEngine(); | |||||
struct ScriptEngineFactory { | |||||
virtual ScriptEngine* createScriptEngine() = 0; | |||||
}; | |||||
extern std::map<std::string, ScriptEngineFactory*> scriptEngineFactories; | |||||
inline ScriptEngine* createScriptEngine(std::string ext) { | |||||
ext = rack::string::lowercase(ext); | |||||
if (ext == "js") | |||||
return createQuickJSEngine(); | |||||
else if (ext == "lua") | |||||
return createLuaJITEngine(); | |||||
else if (ext == "py") | |||||
return createPythonEngine(); | |||||
// Add your file extension check here. | |||||
return NULL; | |||||
/** Called from functions with | |||||
__attribute__((constructor(1000))) | |||||
*/ | |||||
template<typename TScriptEngine> | |||||
void addScriptEngine(std::string extension) { | |||||
struct TScriptEngineFactory : ScriptEngineFactory { | |||||
ScriptEngine* createScriptEngine() { | |||||
return new TScriptEngine; | |||||
} | |||||
}; | |||||
scriptEngineFactories[extension] = new TScriptEngineFactory; | |||||
} | } |