From 0e25c202e32184c1a9932e906f242bc481dc1208 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 16 Sep 2019 05:10:37 -0400 Subject: [PATCH] Add WIP LuaJitEngine. --- Makefile | 18 +++++++------ src/LuaJitEngine.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ src/ScriptEngine.hpp | 3 +++ tests/test.lua | 4 +++ 4 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 src/LuaJitEngine.cpp create mode 100644 tests/test.lua diff --git a/Makefile b/Makefile index c0423d9..7a6f838 100644 --- a/Makefile +++ b/Makefile @@ -22,14 +22,16 @@ $(duktape): $(SHA256) duktape-2.4.0.tar.xz 86a89307d1633b5cedb2c6e56dc86e92679fc34b05be551722d8cc69ab0771fc cd dep && $(UNTAR) ../duktape-2.4.0.tar.xz -# # LuaJIT -# luajit := dep/lib/luajit.a -# DEPS += $(luajit) -# $(luajit): -# cd dep && $(WGET) "http://luajit.org/download/LuaJIT-2.0.5.tar.gz" -# cd dep && $(SHA256) LuaJIT-2.0.5.tar.gz 874b1f8297c697821f561f9b73b57ffd419ed8f4278c82e05b48806d30c1e979 -# cd dep && $(UNTAR) LuaJIT-2.0.5.tar.gz -# cd dep/LuaJIT-2.0.5 && $(MAKE) +# LuaJIT +luajit := dep/lib/libluajit-5.1.a +DEPS += $(luajit) +OBJECTS += $(luajit) +FLAGS += -Idep/include/luajit-2.0 +$(luajit): + $(WGET) "http://luajit.org/download/LuaJIT-2.0.5.tar.gz" + $(SHA256) LuaJIT-2.0.5.tar.gz 874b1f8297c697821f561f9b73b57ffd419ed8f4278c82e05b48806d30c1e979 + cd dep && $(UNTAR) ../LuaJIT-2.0.5.tar.gz + cd dep/LuaJIT-2.0.5 && $(MAKE) PREFIX="$(DEP_PATH)" install # # Julia # julia := dep/lib/libjulia.a diff --git a/src/LuaJitEngine.cpp b/src/LuaJitEngine.cpp new file mode 100644 index 0000000..3f9ff4e --- /dev/null +++ b/src/LuaJitEngine.cpp @@ -0,0 +1,62 @@ +#include "ScriptEngine.hpp" +#include + + +struct LuaJitEngine : ScriptEngine { + lua_State* L = NULL; + + ~LuaJitEngine() { + if (L) + lua_close(L); + } + + std::string getEngineName() override { + return "Lua"; + } + + int run(const std::string& path, const std::string& script) override { + L = luaL_newstate(); + if (!L) { + setMessage("Could not create LuaJIT context"); + return -1; + } + + // Use only "safe" libraries + luaopen_math(L); + luaopen_string(L); + luaopen_table(L); + + if (luaL_loadbuffer(L, script.c_str(), script.size(), path.c_str())) { + const char* s = lua_tostring(L, -1); + rack::WARN("LuaJIT: %s", s); + setMessage(s); + lua_pop(L, 1); + return -1; + } + + if (lua_pcall(L, 0, 0, 0)) { + const char* s = lua_tostring(L, -1); + rack::WARN("LuaJIT: %s", s); + setMessage(s); + lua_pop(L, 1); + return -1; + } + + return 0; + } + + int process() override { + return 0; + } + + static int native_print(lua_State* L) { + const char* s = lua_tostring(L, 1); + rack::INFO("LuaJIT: %s", s); + return 1; + } +}; + + +ScriptEngine* createLuaJitEngine() { + return new LuaJitEngine; +} \ No newline at end of file diff --git a/src/ScriptEngine.hpp b/src/ScriptEngine.hpp index 49aecd9..e72bb7c 100644 --- a/src/ScriptEngine.hpp +++ b/src/ScriptEngine.hpp @@ -51,11 +51,14 @@ struct ScriptEngine { // Add your createMyEngine() function here. ScriptEngine* createDuktapeEngine(); +ScriptEngine* createLuaJitEngine(); inline ScriptEngine* createScriptEngine(std::string ext) { ext = rack::string::lowercase(ext); if (ext == "js") return createDuktapeEngine(); + else if (ext == "lua") + return createLuaJitEngine(); // Add your file extension check here. return NULL; } diff --git a/tests/test.lua b/tests/test.lua new file mode 100644 index 0000000..4f957a2 --- /dev/null +++ b/tests/test.lua @@ -0,0 +1,4 @@ +function process(block) +end + +print("Hello, world!") \ No newline at end of file