Browse Source

Add WIP LuaJitEngine.

luajit
Andrew Belt 5 years ago
parent
commit
0e25c202e3
4 changed files with 79 additions and 8 deletions
  1. +10
    -8
      Makefile
  2. +62
    -0
      src/LuaJitEngine.cpp
  3. +3
    -0
      src/ScriptEngine.hpp
  4. +4
    -0
      tests/test.lua

+ 10
- 8
Makefile View File

@@ -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


+ 62
- 0
src/LuaJitEngine.cpp View File

@@ -0,0 +1,62 @@
#include "ScriptEngine.hpp"
#include <lua.hpp>


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;
}

+ 3
- 0
src/ScriptEngine.hpp View File

@@ -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;
}

+ 4
- 0
tests/test.lua View File

@@ -0,0 +1,4 @@
function process(block)
end

print("Hello, world!")

Loading…
Cancel
Save