From 36f835f313e02c70f184ca7d4f38b46480950ecd Mon Sep 17 00:00:00 2001 From: Tom van Dijck Date: Sun, 25 Jun 2017 08:57:53 -0700 Subject: [PATCH] Proof of concept for binary modules. --- Bootstrap.mak | 15 +- binmodules/example/main.c | 28 ++ binmodules/example/premake5.lua | 28 ++ contrib/luashim/lua.txt | 134 +++++ contrib/luashim/luashim.c | 861 ++++++++++++++++++++++++++++++++ contrib/luashim/luashim.h | 152 ++++++ contrib/luashim/premake5.lua | 20 + premake5.lua | 9 +- src/_premake_main.lua | 4 + src/host/lua_shimtable.h | 147 ++++++ src/host/premake.c | 8 +- tests/_tests.lua | 1 + tests/base/test_binmodules.lua | 23 + 13 files changed, 1419 insertions(+), 11 deletions(-) create mode 100644 binmodules/example/main.c create mode 100644 binmodules/example/premake5.lua create mode 100644 contrib/luashim/lua.txt create mode 100644 contrib/luashim/luashim.c create mode 100644 contrib/luashim/luashim.h create mode 100644 contrib/luashim/premake5.lua create mode 100644 src/host/lua_shimtable.h create mode 100644 tests/base/test_binmodules.lua diff --git a/Bootstrap.mak b/Bootstrap.mak index f9bdf6cf..00cb6c70 100644 --- a/Bootstrap.mak +++ b/Bootstrap.mak @@ -1,5 +1,6 @@ -MSDEV = vs2012 -LUA_DIR = contrib/lua/src +MSDEV = vs2012 +LUA_DIR = contrib/lua/src +LUASHIM_DIR = contrib/luashim SRC = src/host/*.c \ $(LUA_DIR)/lapi.c \ @@ -53,7 +54,7 @@ mingw: $(SRC) $(SILENT) rm -rf ./build $(SILENT) rm -rf ./obj mkdir -p build/bootstrap - $(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -I"$(LUA_DIR)" $? -lole32 + $(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -I"$(LUA_DIR)" -I"$(LUASHIM_DIR)" $? -lole32 ./build/bootstrap/premake_bootstrap embed ./build/bootstrap/premake_bootstrap --os=windows --to=build/bootstrap gmake $(MAKE) -C build/bootstrap @@ -63,7 +64,7 @@ osx: $(SRC) $(SILENT) rm -rf ./build $(SILENT) rm -rf ./obj mkdir -p build/bootstrap - $(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_USE_MACOSX -I"$(LUA_DIR)" -framework CoreServices -framework Foundation -framework Security -lreadline $? + $(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_USE_MACOSX -I"$(LUA_DIR)" -I"$(LUASHIM_DIR)" -framework CoreServices -framework Foundation -framework Security -lreadline $? ./build/bootstrap/premake_bootstrap embed ./build/bootstrap/premake_bootstrap --to=build/bootstrap gmake $(MAKE) -C build/bootstrap -j`getconf _NPROCESSORS_ONLN` @@ -73,7 +74,7 @@ linux: $(SRC) $(SILENT) rm -rf ./build $(SILENT) rm -rf ./obj mkdir -p build/bootstrap - $(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_USE_POSIX -DLUA_USE_DLOPEN -I"$(LUA_DIR)" $? -lm -ldl -lrt + $(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_USE_POSIX -DLUA_USE_DLOPEN -I"$(LUA_DIR)" -I"$(LUASHIM_DIR)" $? -lm -ldl -lrt ./build/bootstrap/premake_bootstrap embed ./build/bootstrap/premake_bootstrap --to=build/bootstrap gmake $(MAKE) -C build/bootstrap -j`getconf _NPROCESSORS_ONLN` @@ -83,7 +84,7 @@ bsd: $(SRC) $(SILENT) rm -rf ./build $(SILENT) rm -rf ./obj mkdir -p build/bootstrap - $(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_USE_POSIX -DLUA_USE_DLOPEN -I"$(LUA_DIR)" $? -lm + $(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_USE_POSIX -DLUA_USE_DLOPEN -I"$(LUA_DIR)" -I"$(LUASHIM_DIR)" $? -lm ./build/bootstrap/premake_bootstrap embed ./build/bootstrap/premake_bootstrap --to=build/bootstrap gmake $(MAKE) -C build/bootstrap -j`getconf _NPROCESSORS_ONLN` @@ -93,7 +94,7 @@ windows-base: $(SRC) $(SILENT) if exist .\build rmdir /s /q .\build $(SILENT) if exist .\obj rmdir /s /q .\obj if not exist build\bootstrap (mkdir build\bootstrap) - cl /Fo.\build\bootstrap\ /Fe.\build\bootstrap\premake_bootstrap.exe /DPREMAKE_NO_BUILTIN_SCRIPTS /I"$(LUA_DIR)" user32.lib ole32.lib advapi32.lib $** + cl /Fo.\build\bootstrap\ /Fe.\build\bootstrap\premake_bootstrap.exe /DPREMAKE_NO_BUILTIN_SCRIPTS /I"$(LUA_DIR)" /I"$(LUASHIM_DIR)" user32.lib ole32.lib advapi32.lib $** .\build\bootstrap\premake_bootstrap.exe embed .\build\bootstrap\premake_bootstrap --to=build/bootstrap $(MSDEV) diff --git a/binmodules/example/main.c b/binmodules/example/main.c new file mode 100644 index 00000000..d6d48065 --- /dev/null +++ b/binmodules/example/main.c @@ -0,0 +1,28 @@ +#include + + +static int example_test(lua_State* L) +{ + const char* text = luaL_checkstring(L, 1); + printf("%s\n", text); + lua_pushboolean(L, 1); + return 1; +} + + +static const luaL_Reg example_functions[] = { + { "test", example_test }, + { NULL, NULL } +}; + + +#ifdef _WIN32 +__declspec(dllexport) int luaopen_example(lua_State *L) +#else +int luaopen_example(lua_State *L) +#endif +{ + shimInitialize(L); + luaL_register(L, "example", example_functions); + return 0; +} diff --git a/binmodules/example/premake5.lua b/binmodules/example/premake5.lua new file mode 100644 index 00000000..0be5c17c --- /dev/null +++ b/binmodules/example/premake5.lua @@ -0,0 +1,28 @@ +project "example" + language "C" + kind "SharedLib" + warnings "extra" + + includedirs { + "../../contrib/lua/src", + "../../contrib/luashim" + } + + links { 'luashim-lib' } + + files + { + "*.c", + "*.lua" + } + + filter "system:not windows" + targetprefix "" + targetextension ".so" + pic "on" + + filter "configurations:Release" + targetdir "../../bin/release" + + filter "configurations:Debug" + targetdir "../../bin/debug" diff --git a/contrib/luashim/lua.txt b/contrib/luashim/lua.txt new file mode 100644 index 00000000..30dfa0b2 --- /dev/null +++ b/contrib/luashim/lua.txt @@ -0,0 +1,134 @@ +lua_State* (lua_newstate) (lua_Alloc f, void* ud); +void (lua_close) (lua_State* L); +lua_State* (lua_newthread) (lua_State* L); +lua_CFunction (lua_atpanic) (lua_State* L, lua_CFunction panicf); +const lua_Number* (lua_version) (lua_State* L); +int (lua_absindex) (lua_State* L, int idx); +int (lua_gettop) (lua_State* L); +void (lua_settop) (lua_State* L, int idx); +void (lua_pushvalue) (lua_State* L, int idx); +void (lua_rotate) (lua_State* L, int idx, int n); +void (lua_copy) (lua_State* L, int fromidx, int toidx); +int (lua_checkstack) (lua_State* L, int n); +void (lua_xmove) (lua_State* from, lua_State* to, int n); +int (lua_isnumber) (lua_State* L, int idx); +int (lua_isstring) (lua_State* L, int idx); +int (lua_iscfunction) (lua_State* L, int idx); +int (lua_isinteger) (lua_State* L, int idx); +int (lua_isuserdata) (lua_State* L, int idx); +int (lua_type) (lua_State* L, int idx); +const char* (lua_typename) (lua_State* L, int tp); +lua_Number (lua_tonumberx) (lua_State* L, int idx, int* isnum); +lua_Integer (lua_tointegerx) (lua_State* L, int idx, int* isnum); +int (lua_toboolean) (lua_State* L, int idx); +const char* (lua_tolstring) (lua_State* L, int idx, size_t* len); +size_t (lua_rawlen) (lua_State* L, int idx); +lua_CFunction (lua_tocfunction) (lua_State* L, int idx); +void* (lua_touserdata) (lua_State* L, int idx); +lua_State* (lua_tothread) (lua_State* L, int idx); +const void* (lua_topointer) (lua_State* L, int idx); +void (lua_arith) (lua_State* L, int op); +int (lua_rawequal) (lua_State* L, int idx1, int idx2); +int (lua_compare) (lua_State* L, int idx1, int idx2, int op); +void (lua_pushnil) (lua_State* L); +void (lua_pushnumber) (lua_State* L, lua_Number n); +void (lua_pushinteger) (lua_State* L, lua_Integer n); +const char* (lua_pushlstring) (lua_State* L, const char* s, size_t len); +const char* (lua_pushstring) (lua_State* L, const char* s); +const char* (lua_pushvfstring) (lua_State* L, const char* fmt, va_list argp); +const char* (lua_pushfstring) (lua_State* L, const char* fmt, ...); +void (lua_pushcclosure) (lua_State* L, lua_CFunction fn, int n); +void (lua_pushboolean) (lua_State* L, int b); +void (lua_pushlightuserdata) (lua_State* L, void* p); +int (lua_pushthread) (lua_State* L); +int (lua_getglobal) (lua_State* L, const char* name); +int (lua_gettable) (lua_State* L, int idx); +int (lua_getfield) (lua_State* L, int idx, const char* k); +int (lua_geti) (lua_State* L, int idx, lua_Integer n); +int (lua_rawget) (lua_State* L, int idx); +int (lua_rawgeti) (lua_State* L, int idx, lua_Integer n); +int (lua_rawgetp) (lua_State* L, int idx, const void* p); +void (lua_createtable) (lua_State* L, int narr, int nrec); +void* (lua_newuserdata) (lua_State* L, size_t sz); +int (lua_getmetatable) (lua_State* L, int objindex); +int (lua_getuservalue) (lua_State* L, int idx); +void (lua_setglobal) (lua_State* L, const char* name); +void (lua_settable) (lua_State* L, int idx); +void (lua_setfield) (lua_State* L, int idx, const char* k); +void (lua_seti) (lua_State* L, int idx, lua_Integer n); +void (lua_rawset) (lua_State* L, int idx); +void (lua_rawseti) (lua_State* L, int idx, lua_Integer n); +void (lua_rawsetp) (lua_State* L, int idx, const void* p); +int (lua_setmetatable) (lua_State* L, int objindex); +void (lua_setuservalue) (lua_State* L, int idx); +void (lua_callk) (lua_State* L, int nargs, int nresults,lua_KContext ctx, lua_KFunction k); +int (lua_pcallk) (lua_State* L, int nargs, int nresults, int errfunc,lua_KContext ctx, lua_KFunction k); +int (lua_load) (lua_State* L, lua_Reader reader, void* dt,const char* chunkname, const char* mode); +int (lua_dump) (lua_State* L, lua_Writer writer, void* data, int strip); +int (lua_yieldk) (lua_State* L, int nresults, lua_KContext ctx,lua_KFunction k); +int (lua_resume) (lua_State* L, lua_State* from, int narg); +int (lua_status) (lua_State* L); +int (lua_isyieldable) (lua_State* L); +int (lua_gc) (lua_State* L, int what, int data); +int (lua_error) (lua_State* L); +int (lua_next) (lua_State* L, int idx); +void (lua_concat) (lua_State* L, int n); +void (lua_len) (lua_State* L, int idx); +size_t (lua_stringtonumber) (lua_State* L, const char* s); +lua_Alloc (lua_getallocf) (lua_State* L, void** ud); +void (lua_setallocf) (lua_State* L, lua_Alloc f, void* ud); +int (lua_getstack) (lua_State* L, int level, lua_Debug* ar); +int (lua_getinfo) (lua_State* L, const char* what, lua_Debug* ar); +const char* (lua_getlocal) (lua_State* L, const lua_Debug* ar, int n); +const char* (lua_setlocal) (lua_State* L, const lua_Debug* ar, int n); +const char* (lua_getupvalue) (lua_State* L, int funcindex, int n); +const char* (lua_setupvalue) (lua_State* L, int funcindex, int n); +void* (lua_upvalueid) (lua_State* L, int fidx, int n); +void (lua_upvaluejoin) (lua_State* L, int fidx1, int n1,int fidx2, int n2); +void (lua_sethook) (lua_State* L, lua_Hook func, int mask, int count); +lua_Hook (lua_gethook) (lua_State* L); +int (lua_gethookmask) (lua_State* L); +int (lua_gethookcount) (lua_State* L); +void (luaL_checkversion_) (lua_State* L, lua_Number ver, size_t sz); +int (luaL_getmetafield) (lua_State* L, int obj, const char* e); +int (luaL_callmeta) (lua_State* L, int obj, const char* e); +const char* (luaL_tolstring) (lua_State* L, int idx, size_t* len); +int (luaL_argerror) (lua_State* L, int arg, const char* extramsg); +const char* (luaL_checklstring) (lua_State* L, int arg,size_t* l); +const char* (luaL_optlstring) (lua_State* L, int arg,const char* def, size_t* l); +lua_Number (luaL_checknumber) (lua_State* L, int arg); +lua_Number (luaL_optnumber) (lua_State* L, int arg, lua_Number def); +lua_Integer (luaL_checkinteger) (lua_State* L, int arg); +lua_Integer (luaL_optinteger) (lua_State* L, int arg,lua_Integer def); +void (luaL_checkstack) (lua_State* L, int sz, const char* msg); +void (luaL_checktype) (lua_State* L, int arg, int t); +void (luaL_checkany) (lua_State* L, int arg); +int (luaL_newmetatable) (lua_State* L, const char* tname); +void (luaL_setmetatable) (lua_State* L, const char* tname); +void* (luaL_testudata) (lua_State* L, int ud, const char* tname); +void* (luaL_checkudata) (lua_State* L, int ud, const char* tname); +void (luaL_where) (lua_State* L, int lvl); +int (luaL_error) (lua_State* L, const char* fmt, ...); +int (luaL_checkoption) (lua_State* L, int arg, const char* def,const char* const lst[]); +int (luaL_fileresult) (lua_State* L, int stat, const char* fname); +int (luaL_execresult) (lua_State* L, int stat); +int (luaL_ref) (lua_State* L, int t); +void (luaL_unref) (lua_State* L, int t, int ref); +int (luaL_loadfilex) (lua_State* L, const char* filename,const char* mode); +int (luaL_loadbufferx) (lua_State* L, const char* buff, size_t sz,const char* name, const char* mode); +int (luaL_loadstring) (lua_State* L, const char* s); +lua_State* (luaL_newstate) (); +lua_Integer (luaL_len) (lua_State* L, int idx); +const char* (luaL_gsub) (lua_State* L, const char* s, const char* p,const char* r); +void (luaL_setfuncs) (lua_State* L, const luaL_Reg* l, int nup); +int (luaL_getsubtable) (lua_State* L, int idx, const char* fname); +void (luaL_traceback) (lua_State* L, lua_State* L1,const char* msg, int level); +void (luaL_requiref) (lua_State* L, const char* modname,lua_CFunction openf, int glb); +void (luaL_buffinit) (lua_State* L, luaL_Buffer* B); +char* (luaL_prepbuffsize) (luaL_Buffer* B, size_t sz); +void (luaL_addlstring) (luaL_Buffer* B, const char* s, size_t l); +void (luaL_addstring) (luaL_Buffer* B, const char* s); +void (luaL_addvalue) (luaL_Buffer* B); +void (luaL_pushresult) (luaL_Buffer* B); +void (luaL_pushresultsize) (luaL_Buffer* B, size_t sz); +char* (luaL_buffinitsize) (lua_State* L, luaL_Buffer* B, size_t sz); diff --git a/contrib/luashim/luashim.c b/contrib/luashim/luashim.c new file mode 100644 index 00000000..205c3ad0 --- /dev/null +++ b/contrib/luashim/luashim.c @@ -0,0 +1,861 @@ +/** +* \file luashim.c +* \brief Lua shim for premake binary modules. +* \author Copyright (c) 2017 Tom van Dijck and the Premake project +*/ +#include "luashim.h" +#include +#include + +static const LuaFunctionTable_t* g_shimTable; + +void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_register(L, libname, l); +} + +lua_State* lua_newstate(lua_Alloc f, void* ud) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_newstate(f, ud); +} + +void lua_close(lua_State* L) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_close(L); +} + +lua_State* lua_newthread(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_newthread(L); +} + +lua_CFunction lua_atpanic(lua_State* L, lua_CFunction panicf) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_atpanic(L, panicf); +} + +const lua_Number* lua_version(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_version(L); +} + +int lua_absindex(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_absindex(L, idx); +} + +int lua_gettop(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_gettop(L); +} + +void lua_settop(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_settop(L, idx); +} + +void lua_pushvalue(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_pushvalue(L, idx); +} + +void lua_rotate(lua_State* L, int idx, int n) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_rotate(L, idx, n); +} + +void lua_copy(lua_State* L, int fromidx, int toidx) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_copy(L, fromidx, toidx); +} + +int lua_checkstack(lua_State* L, int n) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_checkstack(L, n); +} + +void lua_xmove(lua_State* from, lua_State* to, int n) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_xmove(from, to, n); +} + +int lua_isnumber(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_isnumber(L, idx); +} + +int lua_isstring(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_isstring(L, idx); +} + +int lua_iscfunction(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_iscfunction(L, idx); +} + +int lua_isinteger(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_isinteger(L, idx); +} + +int lua_isuserdata(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_isuserdata(L, idx); +} + +int lua_type(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_type(L, idx); +} + +const char* lua_typename(lua_State* L, int tp) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_typename(L, tp); +} + +lua_Number lua_tonumberx(lua_State* L, int idx, int* isnum) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_tonumberx(L, idx, isnum); +} + +lua_Integer lua_tointegerx(lua_State* L, int idx, int* isnum) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_tointegerx(L, idx, isnum); +} + +int lua_toboolean(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_toboolean(L, idx); +} + +const char* lua_tolstring(lua_State* L, int idx, size_t* len) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_tolstring(L, idx, len); +} + +size_t lua_rawlen(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_rawlen(L, idx); +} + +lua_CFunction lua_tocfunction(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_tocfunction(L, idx); +} + +void* lua_touserdata(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_touserdata(L, idx); +} + +lua_State* lua_tothread(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_tothread(L, idx); +} + +const void* lua_topointer(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_topointer(L, idx); +} + +void lua_arith(lua_State* L, int op) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_arith(L, op); +} + +int lua_rawequal(lua_State* L, int idx1, int idx2) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_rawequal(L, idx1, idx2); +} + +int lua_compare(lua_State* L, int idx1, int idx2, int op) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_compare(L, idx1, idx2, op); +} + +void lua_pushnil(lua_State* L) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_pushnil(L); +} + +void lua_pushnumber(lua_State* L, lua_Number n) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_pushnumber(L, n); +} + +void lua_pushinteger(lua_State* L, lua_Integer n) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_pushinteger(L, n); +} + +const char* lua_pushlstring(lua_State* L, const char* s, size_t len) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_pushlstring(L, s, len); +} + +const char* lua_pushstring(lua_State* L, const char* s) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_pushstring(L, s); +} + +const char* lua_pushvfstring(lua_State* L, const char* fmt, va_list argp) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_pushvfstring(L, fmt, argp); +} + +const char* lua_pushfstring(lua_State* L, const char* fmt, ...) +{ + const char* ret; + va_list argp; + va_start(argp, fmt); + ret = lua_pushvfstring(L, fmt, argp); + va_end(argp); + return ret; +} + +void lua_pushcclosure(lua_State* L, lua_CFunction fn, int n) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_pushcclosure(L, fn, n); +} + +void lua_pushboolean(lua_State* L, int b) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_pushboolean(L, b); +} + +void lua_pushlightuserdata(lua_State* L, void* p) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_pushlightuserdata(L, p); +} + +int lua_pushthread(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_pushthread(L); +} + +int lua_getglobal(lua_State* L, const char* name) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_getglobal(L, name); +} + +int lua_gettable(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_gettable(L, idx); +} + +int lua_getfield(lua_State* L, int idx, const char* k) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_getfield(L, idx, k); +} + +int lua_geti(lua_State* L, int idx, lua_Integer n) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_geti(L, idx, n); +} + +int lua_rawget(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_rawget(L, idx); +} + +int lua_rawgeti(lua_State* L, int idx, lua_Integer n) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_rawgeti(L, idx, n); +} + +int lua_rawgetp(lua_State* L, int idx, const void* p) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_rawgetp(L, idx, p); +} + +void lua_createtable(lua_State* L, int narr, int nrec) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_createtable(L, narr, nrec); +} + +void* lua_newuserdata(lua_State* L, size_t sz) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_newuserdata(L, sz); +} + +int lua_getmetatable(lua_State* L, int objindex) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_getmetatable(L, objindex); +} + +int lua_getuservalue(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_getuservalue(L, idx); +} + +void lua_setglobal(lua_State* L, const char* name) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_setglobal(L, name); +} + +void lua_settable(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_settable(L, idx); +} + +void lua_setfield(lua_State* L, int idx, const char* k) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_setfield(L, idx, k); +} + +void lua_seti(lua_State* L, int idx, lua_Integer n) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_seti(L, idx, n); +} + +void lua_rawset(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_rawset(L, idx); +} + +void lua_rawseti(lua_State* L, int idx, lua_Integer n) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_rawseti(L, idx, n); +} + +void lua_rawsetp(lua_State* L, int idx, const void* p) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_rawsetp(L, idx, p); +} + +int lua_setmetatable(lua_State* L, int objindex) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_setmetatable(L, objindex); +} + +void lua_setuservalue(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_setuservalue(L, idx); +} + +void lua_callk(lua_State* L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_callk(L, nargs, nresults, ctx, k); +} + +int lua_pcallk(lua_State* L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_pcallk(L, nargs, nresults, errfunc, ctx, k); +} + +int lua_load(lua_State* L, lua_Reader reader, void* dt, const char* chunkname, const char* mode) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_load(L, reader, dt, chunkname, mode); +} + +int lua_dump(lua_State* L, lua_Writer writer, void* data, int strip) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_dump(L, writer, data, strip); +} + +int lua_yieldk(lua_State* L, int nresults, lua_KContext ctx, lua_KFunction k) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_yieldk(L, nresults, ctx, k); +} + +int lua_resume(lua_State* L, lua_State* from, int narg) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_resume(L, from, narg); +} + +int lua_status(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_status(L); +} + +int lua_isyieldable(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_isyieldable(L); +} + +int lua_gc(lua_State* L, int what, int data) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_gc(L, what, data); +} + +int lua_error(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_error(L); +} + +int lua_next(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_next(L, idx); +} + +void lua_concat(lua_State* L, int n) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_concat(L, n); +} + +void lua_len(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_len(L, idx); +} + +size_t lua_stringtonumber(lua_State* L, const char* s) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_stringtonumber(L, s); +} + +lua_Alloc lua_getallocf(lua_State* L, void** ud) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_getallocf(L, ud); +} + +void lua_setallocf(lua_State* L, lua_Alloc f, void* ud) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_setallocf(L, f, ud); +} + +int lua_getstack(lua_State* L, int level, lua_Debug* ar) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_getstack(L, level, ar); +} + +int lua_getinfo(lua_State* L, const char* what, lua_Debug* ar) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_getinfo(L, what, ar); +} + +const char* lua_getlocal(lua_State* L, const lua_Debug* ar, int n) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_getlocal(L, ar, n); +} + +const char* lua_setlocal(lua_State* L, const lua_Debug* ar, int n) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_setlocal(L, ar, n); +} + +const char* lua_getupvalue(lua_State* L, int funcindex, int n) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_getupvalue(L, funcindex, n); +} + +const char* lua_setupvalue(lua_State* L, int funcindex, int n) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_setupvalue(L, funcindex, n); +} + +void* lua_upvalueid(lua_State* L, int fidx, int n) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_upvalueid(L, fidx, n); +} + +void lua_upvaluejoin(lua_State* L, int fidx1, int n1, int fidx2, int n2) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_upvaluejoin(L, fidx1, n1, fidx2, n2); +} + +void lua_sethook(lua_State* L, lua_Hook func, int mask, int count) +{ + assert(g_shimTable != NULL); + g_shimTable->shim_sethook(L, func, mask, count); +} + +lua_Hook lua_gethook(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_gethook(L); +} + +int lua_gethookmask(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_gethookmask(L); +} + +int lua_gethookcount(lua_State* L) +{ + assert(g_shimTable != NULL); + return g_shimTable->shim_gethookcount(L); +} + +void luaL_checkversion_(lua_State* L, lua_Number ver, size_t sz) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_checkversion_(L, ver, sz); +} + +int luaL_getmetafield(lua_State* L, int obj, const char* e) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_getmetafield(L, obj, e); +} + +int luaL_callmeta(lua_State* L, int obj, const char* e) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_callmeta(L, obj, e); +} + +const char* luaL_tolstring(lua_State* L, int idx, size_t* len) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_tolstring(L, idx, len); +} + +int luaL_argerror(lua_State* L, int arg, const char* extramsg) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_argerror(L, arg, extramsg); +} + +const char* luaL_checklstring(lua_State* L, int arg, size_t* l) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_checklstring(L, arg, l); +} + +const char* luaL_optlstring(lua_State* L, int arg, const char* def, size_t* l) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_optlstring(L, arg, def, l); +} + +lua_Number luaL_checknumber(lua_State* L, int arg) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_checknumber(L, arg); +} + +lua_Number luaL_optnumber(lua_State* L, int arg, lua_Number def) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_optnumber(L, arg, def); +} + +lua_Integer luaL_checkinteger(lua_State* L, int arg) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_checkinteger(L, arg); +} + +lua_Integer luaL_optinteger(lua_State* L, int arg, lua_Integer def) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_optinteger(L, arg, def); +} + +void luaL_checkstack(lua_State* L, int sz, const char* msg) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_checkstack(L, sz, msg); +} + +void luaL_checktype(lua_State* L, int arg, int t) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_checktype(L, arg, t); +} + +void luaL_checkany(lua_State* L, int arg) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_checkany(L, arg); +} + +int luaL_newmetatable(lua_State* L, const char* tname) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_newmetatable(L, tname); +} + +void luaL_setmetatable(lua_State* L, const char* tname) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_setmetatable(L, tname); +} + +void* luaL_testudata(lua_State* L, int ud, const char* tname) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_testudata(L, ud, tname); +} + +void* luaL_checkudata(lua_State* L, int ud, const char* tname) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_checkudata(L, ud, tname); +} + +void luaL_where(lua_State* L, int lvl) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_where(L, lvl); +} + +int luaL_error(lua_State* L, const char* fmt, ...) +{ + va_list argp; + va_start(argp, fmt); + luaL_where(L, 1); + lua_pushvfstring(L, fmt, argp); + va_end(argp); + lua_concat(L, 2); + return lua_error(L); +} + +int luaL_checkoption(lua_State* L, int arg, const char* def, const char* const lst[]) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_checkoption(L, arg, def, lst); +} + +int luaL_fileresult(lua_State* L, int stat, const char* fname) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_fileresult(L, stat, fname); +} + +int luaL_execresult(lua_State* L, int stat) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_execresult(L, stat); +} + +int luaL_ref(lua_State* L, int t) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_ref(L, t); +} + +void luaL_unref(lua_State* L, int t, int ref) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_unref(L, t, ref); +} + +int luaL_loadfilex(lua_State* L, const char* filename, const char* mode) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_loadfilex(L, filename, mode); +} + +int luaL_loadbufferx(lua_State* L, const char* buff, size_t sz, const char* name, const char* mode) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_loadbufferx(L, buff, sz, name, mode); +} + +int luaL_loadstring(lua_State* L, const char* s) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_loadstring(L, s); +} + +lua_State* luaL_newstate() +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_newstate(); +} + +lua_Integer luaL_len(lua_State* L, int idx) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_len(L, idx); +} + +const char* luaL_gsub(lua_State* L, const char* s, const char* p, const char* r) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_gsub(L, s, p, r); +} + +void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_setfuncs(L, l, nup); +} + +int luaL_getsubtable(lua_State* L, int idx, const char* fname) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_getsubtable(L, idx, fname); +} + +void luaL_traceback(lua_State* L, lua_State* L1, const char* msg, int level) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_traceback(L, L1, msg, level); +} + +void luaL_requiref(lua_State* L, const char* modname, lua_CFunction openf, int glb) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_requiref(L, modname, openf, glb); +} + +void luaL_buffinit(lua_State* L, luaL_Buffer* B) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_buffinit(L, B); +} + +char* luaL_prepbuffsize(luaL_Buffer* B, size_t sz) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_prepbuffsize(B, sz); +} + +void luaL_addlstring(luaL_Buffer* B, const char* s, size_t l) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_addlstring(B, s, l); +} + +void luaL_addstring(luaL_Buffer* B, const char* s) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_addstring(B, s); +} + +void luaL_addvalue(luaL_Buffer* B) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_addvalue(B); +} + +void luaL_pushresult(luaL_Buffer* B) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_pushresult(B); +} + +void luaL_pushresultsize(luaL_Buffer* B, size_t sz) +{ + assert(g_shimTable != NULL); + g_shimTable->shimL_pushresultsize(B, sz); +} + +char* luaL_buffinitsize(lua_State* L, luaL_Buffer* B, size_t sz) +{ + assert(g_shimTable != NULL); + return g_shimTable->shimL_buffinitsize(L, B, sz); +} + +inline const Node* hashpow2(const Table* t, int n) { + int i = lmod(n, sizenode(t)); + return &t->node[i]; +} + +inline const Node* findNode(const Table* t, int key) { + const Node* n = hashpow2(t, key); + while (n->i_key.tvk.value_.i != key) + { + int nx = n->i_key.nk.next; + if (nx == 0) + return NULL; + n += nx; + } + return n; +} + +void shimInitialize(lua_State* L) +{ + lua_lock(L); + + // Find the 'SHIM' entry in the registry. + const Table* reg = hvalue(&G(L)->l_registry); + const Node* n = findNode(reg, 'SHIM'); + assert(n != NULL); + + g_shimTable = (const LuaFunctionTable_t*)n->i_val.value_.p; + assert(g_shimTable != NULL); + + lua_unlock(L); +} \ No newline at end of file diff --git a/contrib/luashim/luashim.h b/contrib/luashim/luashim.h new file mode 100644 index 00000000..2c28f9e9 --- /dev/null +++ b/contrib/luashim/luashim.h @@ -0,0 +1,152 @@ +/** +* \file luashim.h +* \brief Lua shim for premake binary modules. +* \author Copyright (c) 2017 Tom van Dijck and the Premake project +*/ +#ifndef HEADER_luashim_H +#define HEADER_luashim_H + +#include +#include + +// premake specific helper methods. +void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l); +void shimInitialize(lua_State* L); + +typedef struct { + void (*shimL_register)(lua_State *L, const char *libname, const luaL_Reg *l); + lua_State* (*shim_newstate)(lua_Alloc f, void* ud); + void (*shim_close)(lua_State* L); + lua_State* (*shim_newthread)(lua_State* L); + lua_CFunction (*shim_atpanic)(lua_State* L, lua_CFunction panicf); + const lua_Number* (*shim_version)(lua_State* L); + int (*shim_absindex)(lua_State* L, int idx); + int (*shim_gettop)(lua_State* L); + void (*shim_settop)(lua_State* L, int idx); + void (*shim_pushvalue)(lua_State* L, int idx); + void (*shim_rotate)(lua_State* L, int idx, int n); + void (*shim_copy)(lua_State* L, int fromidx, int toidx); + int (*shim_checkstack)(lua_State* L, int n); + void (*shim_xmove)(lua_State* from, lua_State* to, int n); + int (*shim_isnumber)(lua_State* L, int idx); + int (*shim_isstring)(lua_State* L, int idx); + int (*shim_iscfunction)(lua_State* L, int idx); + int (*shim_isinteger)(lua_State* L, int idx); + int (*shim_isuserdata)(lua_State* L, int idx); + int (*shim_type)(lua_State* L, int idx); + const char* (*shim_typename)(lua_State* L, int tp); + lua_Number (*shim_tonumberx)(lua_State* L, int idx, int* isnum); + lua_Integer (*shim_tointegerx)(lua_State* L, int idx, int* isnum); + int (*shim_toboolean)(lua_State* L, int idx); + const char* (*shim_tolstring)(lua_State* L, int idx, size_t* len); + size_t (*shim_rawlen)(lua_State* L, int idx); + lua_CFunction (*shim_tocfunction)(lua_State* L, int idx); + void* (*shim_touserdata)(lua_State* L, int idx); + lua_State* (*shim_tothread)(lua_State* L, int idx); + const void* (*shim_topointer)(lua_State* L, int idx); + void (*shim_arith)(lua_State* L, int op); + int (*shim_rawequal)(lua_State* L, int idx1, int idx2); + int (*shim_compare)(lua_State* L, int idx1, int idx2, int op); + void (*shim_pushnil)(lua_State* L); + void (*shim_pushnumber)(lua_State* L, lua_Number n); + void (*shim_pushinteger)(lua_State* L, lua_Integer n); + const char* (*shim_pushlstring)(lua_State* L, const char* s, size_t len); + const char* (*shim_pushstring)(lua_State* L, const char* s); + const char* (*shim_pushvfstring)(lua_State* L, const char* fmt, va_list argp); + void (*shim_pushcclosure)(lua_State* L, lua_CFunction fn, int n); + void (*shim_pushboolean)(lua_State* L, int b); + void (*shim_pushlightuserdata)(lua_State* L, void* p); + int (*shim_pushthread)(lua_State* L); + int (*shim_getglobal)(lua_State* L, const char* name); + int (*shim_gettable)(lua_State* L, int idx); + int (*shim_getfield)(lua_State* L, int idx, const char* k); + int (*shim_geti)(lua_State* L, int idx, lua_Integer n); + int (*shim_rawget)(lua_State* L, int idx); + int (*shim_rawgeti)(lua_State* L, int idx, lua_Integer n); + int (*shim_rawgetp)(lua_State* L, int idx, const void* p); + void (*shim_createtable)(lua_State* L, int narr, int nrec); + void* (*shim_newuserdata)(lua_State* L, size_t sz); + int (*shim_getmetatable)(lua_State* L, int objindex); + int (*shim_getuservalue)(lua_State* L, int idx); + void (*shim_setglobal)(lua_State* L, const char* name); + void (*shim_settable)(lua_State* L, int idx); + void (*shim_setfield)(lua_State* L, int idx, const char* k); + void (*shim_seti)(lua_State* L, int idx, lua_Integer n); + void (*shim_rawset)(lua_State* L, int idx); + void (*shim_rawseti)(lua_State* L, int idx, lua_Integer n); + void (*shim_rawsetp)(lua_State* L, int idx, const void* p); + int (*shim_setmetatable)(lua_State* L, int objindex); + void (*shim_setuservalue)(lua_State* L, int idx); + void (*shim_callk)(lua_State* L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k); + int (*shim_pcallk)(lua_State* L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k); + int (*shim_load)(lua_State* L, lua_Reader reader, void* dt, const char* chunkname, const char* mode); + int (*shim_dump)(lua_State* L, lua_Writer writer, void* data, int strip); + int (*shim_yieldk)(lua_State* L, int nresults, lua_KContext ctx, lua_KFunction k); + int (*shim_resume)(lua_State* L, lua_State* from, int narg); + int (*shim_status)(lua_State* L); + int (*shim_isyieldable)(lua_State* L); + int (*shim_gc)(lua_State* L, int what, int data); + int (*shim_error)(lua_State* L); + int (*shim_next)(lua_State* L, int idx); + void (*shim_concat)(lua_State* L, int n); + void (*shim_len)(lua_State* L, int idx); + size_t (*shim_stringtonumber)(lua_State* L, const char* s); + lua_Alloc (*shim_getallocf)(lua_State* L, void** ud); + void (*shim_setallocf)(lua_State* L, lua_Alloc f, void* ud); + int (*shim_getstack)(lua_State* L, int level, lua_Debug* ar); + int (*shim_getinfo)(lua_State* L, const char* what, lua_Debug* ar); + const char* (*shim_getlocal)(lua_State* L, const lua_Debug* ar, int n); + const char* (*shim_setlocal)(lua_State* L, const lua_Debug* ar, int n); + const char* (*shim_getupvalue)(lua_State* L, int funcindex, int n); + const char* (*shim_setupvalue)(lua_State* L, int funcindex, int n); + void* (*shim_upvalueid)(lua_State* L, int fidx, int n); + void (*shim_upvaluejoin)(lua_State* L, int fidx1, int n1, int fidx2, int n2); + void (*shim_sethook)(lua_State* L, lua_Hook func, int mask, int count); + lua_Hook (*shim_gethook)(lua_State* L); + int (*shim_gethookmask)(lua_State* L); + int (*shim_gethookcount)(lua_State* L); + void (*shimL_checkversion_)(lua_State* L, lua_Number ver, size_t sz); + int (*shimL_getmetafield)(lua_State* L, int obj, const char* e); + int (*shimL_callmeta)(lua_State* L, int obj, const char* e); + const char* (*shimL_tolstring)(lua_State* L, int idx, size_t* len); + int (*shimL_argerror)(lua_State* L, int arg, const char* extramsg); + const char* (*shimL_checklstring)(lua_State* L, int arg, size_t* l); + const char* (*shimL_optlstring)(lua_State* L, int arg, const char* def, size_t* l); + lua_Number (*shimL_checknumber)(lua_State* L, int arg); + lua_Number (*shimL_optnumber)(lua_State* L, int arg, lua_Number def); + lua_Integer (*shimL_checkinteger)(lua_State* L, int arg); + lua_Integer (*shimL_optinteger)(lua_State* L, int arg, lua_Integer def); + void (*shimL_checkstack)(lua_State* L, int sz, const char* msg); + void (*shimL_checktype)(lua_State* L, int arg, int t); + void (*shimL_checkany)(lua_State* L, int arg); + int (*shimL_newmetatable)(lua_State* L, const char* tname); + void (*shimL_setmetatable)(lua_State* L, const char* tname); + void* (*shimL_testudata)(lua_State* L, int ud, const char* tname); + void* (*shimL_checkudata)(lua_State* L, int ud, const char* tname); + void (*shimL_where)(lua_State* L, int lvl); + int (*shimL_checkoption)(lua_State* L, int arg, const char* def, const char* const lst[]); + int (*shimL_fileresult)(lua_State* L, int stat, const char* fname); + int (*shimL_execresult)(lua_State* L, int stat); + int (*shimL_ref)(lua_State* L, int t); + void (*shimL_unref)(lua_State* L, int t, int ref); + int (*shimL_loadfilex)(lua_State* L, const char* filename, const char* mode); + int (*shimL_loadbufferx)(lua_State* L, const char* buff, size_t sz, const char* name, const char* mode); + int (*shimL_loadstring)(lua_State* L, const char* s); + lua_State* (*shimL_newstate)(); + lua_Integer (*shimL_len)(lua_State* L, int idx); + const char* (*shimL_gsub)(lua_State* L, const char* s, const char* p, const char* r); + void (*shimL_setfuncs)(lua_State* L, const luaL_Reg* l, int nup); + int (*shimL_getsubtable)(lua_State* L, int idx, const char* fname); + void (*shimL_traceback)(lua_State* L, lua_State* L1, const char* msg, int level); + void (*shimL_requiref)(lua_State* L, const char* modname, lua_CFunction openf, int glb); + void (*shimL_buffinit)(lua_State* L, luaL_Buffer* B); + char* (*shimL_prepbuffsize)(luaL_Buffer* B, size_t sz); + void (*shimL_addlstring)(luaL_Buffer* B, const char* s, size_t l); + void (*shimL_addstring)(luaL_Buffer* B, const char* s); + void (*shimL_addvalue)(luaL_Buffer* B); + void (*shimL_pushresult)(luaL_Buffer* B); + void (*shimL_pushresultsize)(luaL_Buffer* B, size_t sz); + char* (*shimL_buffinitsize)(lua_State* L, luaL_Buffer* B, size_t sz); +} LuaFunctionTable_t; + +#endif diff --git a/contrib/luashim/premake5.lua b/contrib/luashim/premake5.lua new file mode 100644 index 00000000..39fd0557 --- /dev/null +++ b/contrib/luashim/premake5.lua @@ -0,0 +1,20 @@ +project "luashim-lib" + language "C" + kind "StaticLib" + warnings "extra" + pic "on" + + includedirs { "../lua/src" } + + files + { + "*.c", + "*.h", + "*.lua" + } + + filter "system:linux or bsd or hurd or aix" + defines { "LUA_USE_POSIX", "LUA_USE_DLOPEN" } + + filter "system:macosx" + defines { "LUA_USE_MACOSX" } diff --git a/premake5.lua b/premake5.lua index 75966215..db258e60 100644 --- a/premake5.lua +++ b/premake5.lua @@ -127,7 +127,7 @@ targetname "premake5" language "C" kind "ConsoleApp" - includedirs { "contrib/lua/src" } + includedirs { "contrib/lua/src", "contrib/luashim" } links { "lua-lib" } -- optional 3rd party libraries @@ -149,7 +149,8 @@ excludes { - "contrib/**.*" + "contrib/**.*", + "binmodules/**.*" } filter "configurations:Debug" @@ -194,6 +195,7 @@ -- optional 3rd party libraries group "contrib" include "contrib/lua" + include "contrib/luashim" if not _OPTIONS["no-zlib"] then include "contrib/zlib" include "contrib/libzip" @@ -203,6 +205,9 @@ include "contrib/curl" end + group "Binary Modules" + include "binmodules/example" + -- -- A more thorough cleanup. -- diff --git a/src/_premake_main.lua b/src/_premake_main.lua index 0551c7de..ea99a754 100644 --- a/src/_premake_main.lua +++ b/src/_premake_main.lua @@ -41,6 +41,10 @@ --- function m.installModuleLoader() + if not os.ishost('windows') then + local premakeDir = path.getdirectory(_PREMAKE_COMMAND) + package.cpath = package.cpath .. ';' .. premakeDir .. '/?.so' + end table.insert(package.searchers, 2, m.moduleLoader) end diff --git a/src/host/lua_shimtable.h b/src/host/lua_shimtable.h new file mode 100644 index 00000000..948d53f5 --- /dev/null +++ b/src/host/lua_shimtable.h @@ -0,0 +1,147 @@ +/** +* \file lua_shimtable.h +* \brief Lua shim for premake binary modules. +* \author Copyright (c) 2017 Tom van Dijck and the Premake project +*/ +#ifndef HEADER_lua_shimtable_H +#define HEADER_lua_shimtable_H + +#include "luashim.h" + +static LuaFunctionTable_t s_shimTable = { + &luaL_register, + &lua_newstate, + &lua_close, + &lua_newthread, + &lua_atpanic, + &lua_version, + &lua_absindex, + &lua_gettop, + &lua_settop, + &lua_pushvalue, + &lua_rotate, + &lua_copy, + &lua_checkstack, + &lua_xmove, + &lua_isnumber, + &lua_isstring, + &lua_iscfunction, + &lua_isinteger, + &lua_isuserdata, + &lua_type, + &lua_typename, + &lua_tonumberx, + &lua_tointegerx, + &lua_toboolean, + &lua_tolstring, + &lua_rawlen, + &lua_tocfunction, + &lua_touserdata, + &lua_tothread, + &lua_topointer, + &lua_arith, + &lua_rawequal, + &lua_compare, + &lua_pushnil, + &lua_pushnumber, + &lua_pushinteger, + &lua_pushlstring, + &lua_pushstring, + &lua_pushvfstring, + &lua_pushcclosure, + &lua_pushboolean, + &lua_pushlightuserdata, + &lua_pushthread, + &lua_getglobal, + &lua_gettable, + &lua_getfield, + &lua_geti, + &lua_rawget, + &lua_rawgeti, + &lua_rawgetp, + &lua_createtable, + &lua_newuserdata, + &lua_getmetatable, + &lua_getuservalue, + &lua_setglobal, + &lua_settable, + &lua_setfield, + &lua_seti, + &lua_rawset, + &lua_rawseti, + &lua_rawsetp, + &lua_setmetatable, + &lua_setuservalue, + &lua_callk, + &lua_pcallk, + &lua_load, + &lua_dump, + &lua_yieldk, + &lua_resume, + &lua_status, + &lua_isyieldable, + &lua_gc, + &lua_error, + &lua_next, + &lua_concat, + &lua_len, + &lua_stringtonumber, + &lua_getallocf, + &lua_setallocf, + &lua_getstack, + &lua_getinfo, + &lua_getlocal, + &lua_setlocal, + &lua_getupvalue, + &lua_setupvalue, + &lua_upvalueid, + &lua_upvaluejoin, + &lua_sethook, + &lua_gethook, + &lua_gethookmask, + &lua_gethookcount, + &luaL_checkversion_, + &luaL_getmetafield, + &luaL_callmeta, + &luaL_tolstring, + &luaL_argerror, + &luaL_checklstring, + &luaL_optlstring, + &luaL_checknumber, + &luaL_optnumber, + &luaL_checkinteger, + &luaL_optinteger, + &luaL_checkstack, + &luaL_checktype, + &luaL_checkany, + &luaL_newmetatable, + &luaL_setmetatable, + &luaL_testudata, + &luaL_checkudata, + &luaL_where, + &luaL_checkoption, + &luaL_fileresult, + &luaL_execresult, + &luaL_ref, + &luaL_unref, + &luaL_loadfilex, + &luaL_loadbufferx, + &luaL_loadstring, + &luaL_newstate, + &luaL_len, + &luaL_gsub, + &luaL_setfuncs, + &luaL_getsubtable, + &luaL_traceback, + &luaL_requiref, + &luaL_buffinit, + &luaL_prepbuffsize, + &luaL_addlstring, + &luaL_addstring, + &luaL_addvalue, + &luaL_pushresult, + &luaL_pushresultsize, + &luaL_buffinitsize, +}; + +#endif \ No newline at end of file diff --git a/src/host/premake.c b/src/host/premake.c index 450ff63c..7f247fb1 100644 --- a/src/host/premake.c +++ b/src/host/premake.c @@ -8,6 +8,7 @@ #include #include #include "premake.h" +#include "lua_shimtable.h" #if PLATFORM_MACOSX #include @@ -152,7 +153,7 @@ static void lua_getorcreate_table(lua_State *L, const char *modname) } -static void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l) +void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l) { lua_getorcreate_table(L, libname); luaL_setfuncs(L, l, 0); @@ -183,6 +184,9 @@ int premake_init(lua_State* L) luaL_register(L, "zip", zip_functions); #endif + lua_pushlightuserdata(L, &s_shimTable); + lua_rawseti(L, LUA_REGISTRYINDEX, 'SHIM'); + /* push the application metadata */ lua_pushstring(L, LUA_COPYRIGHT); lua_setglobal(L, "_COPYRIGHT"); @@ -231,7 +235,7 @@ static int getErrorColor(lua_State* L) lua_getglobal(L, "term"); lua_pushstring(L, "errorColor"); lua_gettable(L, -2); - color = luaL_checkinteger(L, -1); + color = (int)luaL_checkinteger(L, -1); lua_pop(L, 2); return color; diff --git a/tests/_tests.lua b/tests/_tests.lua index b0a8fe46..3b2e9d3e 100644 --- a/tests/_tests.lua +++ b/tests/_tests.lua @@ -2,6 +2,7 @@ return { -- Base API tests "test_string.lua", "base/test_aliasing.lua", + "base/test_binmodules.lua", "base/test_configset.lua", "base/test_context.lua", "base/test_criteria.lua", diff --git a/tests/base/test_binmodules.lua b/tests/base/test_binmodules.lua new file mode 100644 index 00000000..ad52a24d --- /dev/null +++ b/tests/base/test_binmodules.lua @@ -0,0 +1,23 @@ +-- +-- tests/base/test_binmodules.lua +-- Verify handling of binary modules. +-- Copyright (c) 2017 Tom van Dijck and the Premake project +-- + + local suite = test.declare("premake_binmodules") + local p = premake + + + function suite.setup() + test.print('-------') + test.print(package.path) + test.print('-------') + test.print(package.cpath) + test.print('-------') + require("example") + end + + + function suite.testExample() + test.istrue(example.test("hello world")); + end