From 03b467d9d98ef0e7d9c369b92c7b5a49a0d462cf Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Thu, 16 Sep 2021 16:08:21 -0400 Subject: [PATCH] Cleaned up, added utility API. --- resources/lite_xl_plugin_api.h | 6 ++-- src/api/system.c | 62 +++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/resources/lite_xl_plugin_api.h b/resources/lite_xl_plugin_api.h index 7ac9c5e8..bd2d3dba 100644 --- a/resources/lite_xl_plugin_api.h +++ b/resources/lite_xl_plugin_api.h @@ -20,7 +20,8 @@ LUA_HEADERS=`pkg-config --cflags lua5.2 | sed 's/^-I//' | sed 's/$/\/*.h/'` grep -h "^LUA\(LIB\)*_API" $LUA_HEADERS | sed "s/LUA\(LIB\)*_API //" | sed "s/(lua/(*lua/" | grep -v ",\s*$" | sed "s/^/static /" grep -h "#define luaL*_" $LUA_HEADERS | grep -v "\\\s*$" | grep -v "\(assert\|lock\)" | grep -v "\(asm\|int32\)" | grep -v "#define lua_number2integer(i,n)\s*lua_number2int(i, n)" echo "#define IMPORT_SYMBOL(name, ret, ...) name = (ret (*)(__VA_ARGS__))symbol(#name)" -echo "static void lite_init_plugin(void* (*symbol(const char*))) {" +echo "static void lite_xl_plugin_init(void* XL) {" +echo "\tvoid* (*symbol)(const char*) = (void* (*)(const char*))XL;" grep -h "^LUA\(LIB\)*_API" $LUA_HEADERS | sed "s/LUA\(LIB\)*_API //" | sed "s/(lua/(*lua/" | grep -v ",\s*$" | sed "s/^\([^)]*\)(\*\(lua\w*\))\s*(/\tIMPORT_SYMBOL(\2, \1,/" echo "}" echo "#endif" @@ -197,7 +198,8 @@ static void (*luaL_openlibs) (lua_State *L); #define lua_str2number(s,p) strtod((s), (p)) #define lua_strx2number(s,p) strtod((s), (p)) #define IMPORT_SYMBOL(name, ret, ...) name = (ret (*)(__VA_ARGS__))symbol(#name) -static void lite_init_plugin(void* (*symbol(const char*))) { +static void lite_xl_plugin_init(void* XL) { + void* (*symbol)(const char*) = (void* (*)(const char*))XL; IMPORT_SYMBOL(luaL_checkversion_, void ,lua_State *L, lua_Number ver); IMPORT_SYMBOL(luaL_getmetafield, int ,lua_State *L, int obj, const char *e); IMPORT_SYMBOL(luaL_callmeta, int ,lua_State *L, int obj, const char *e); diff --git a/src/api/system.c b/src/api/system.c index 78b3e700..01cf72a7 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -645,6 +645,7 @@ typedef struct { } lua_function_node; #define P(FUNC) { "lua_" #FUNC, (void*)(lua_##FUNC) } +#define U(FUNC) { "luaL_" #FUNC, (void*)(luaL_##FUNC) } static void* api_require(const char* symbol) { static lua_function_node nodes[] = { P(absindex), P(arith), P(atpanic), P(callk), P(checkstack), @@ -652,21 +653,26 @@ static void* api_require(const char* symbol) { P(error), P(gc), P(getallocf), P(getctx), P(getfield), P(getglobal), P(gethook), P(gethookcount), P(gethookmask), P(getinfo), P(getlocal), P(getmetatable), P(getstack), P(gettable), P(gettop), P(getupvalue), - P(getuservalue), P(insert), P(isnumber), - P(isstring), P(isuserdata), P(len), P(load), - P(newstate), P(newthread), P(newuserdata), P(next), - P(pcallk), P(pushboolean), P(pushcclosure), - P(pushfstring), P(pushinteger), P(pushlightuserdata), - P(pushlstring), P(pushnil), P(pushnumber), P(pushstring), - P(pushthread), P(pushunsigned), P(pushvalue), P(pushvfstring), P(rawequal), - P(rawget), P(rawgeti), P(rawgetp), P(rawlen), P(rawset), P(rawseti), - P(rawsetp), P(remove), P(replace), P(resume), P(setallocf), - P(setfield), P(setglobal), P(sethook), P(setlocal), P(setmetatable), - P(settable), P(settop), P(setupvalue), P(setuservalue), P(status), - P(tocfunction), P(tointegerx), P(tolstring), P(toboolean), - P(tonumberx), P(topointer), P(tothread), - P(tounsignedx), P(touserdata), P(type), P(typename), P(upvalueid), - P(upvaluejoin), P(version), P(xmove), P(yieldk) + P(getuservalue), P(insert), P(isnumber), P(isstring), P(isuserdata), + P(len), P(load), P(newstate), P(newthread), P(newuserdata), P(next), + P(pcallk), P(pushboolean), P(pushcclosure), P(pushfstring), P(pushinteger), + P(pushlightuserdata), P(pushlstring), P(pushnil), P(pushnumber), + P(pushstring), P(pushthread), P(pushunsigned), P(pushvalue), + P(pushvfstring), P(rawequal), P(rawget), P(rawgeti), P(rawgetp), P(rawlen), + P(rawset), P(rawseti), P(rawsetp), P(remove), P(replace), P(resume), + P(setallocf), P(setfield), P(setglobal), P(sethook), P(setlocal), + P(setmetatable), P(settable), P(settop), P(setupvalue), P(setuservalue), + P(status), P(tocfunction), P(tointegerx), P(tolstring), P(toboolean), + P(tonumberx), P(topointer), P(tothread), P(tounsignedx), P(touserdata), + P(type), P(typename), P(upvalueid), P(upvaluejoin), P(version), P(xmove), + P(yieldk), U(checkversion_), U(getmetafield), U(callmeta), U(tolstring), + U(argerror), U(checknumber), U(optnumber), U(checkinteger), + U(checkunsigned), U(checkstack), U(checktype), U(checkany), + U(newmetatable), U(setmetatable), U(testudata), U(checkudata), U(where), + U(error), U(fileresult), U(execresult), U(ref), U(unref), U(loadstring), + U(newstate), U(len), U(setfuncs), U(getsubtable), U(buffinit), + U(prepbuffsize), U(addlstring), U(addstring), U(addvalue), U(pushresult), + U(pushresultsize), U(buffinitsize) }; for (int i = 0; i < sizeof(nodes) / sizeof(lua_function_node); ++i) { if (strcmp(nodes[i].symbol, symbol) == 0) @@ -676,26 +682,26 @@ static void* api_require(const char* symbol) { } static int f_load_native_plugin(lua_State* L) { - size_t sname, modlen, pathlen; - char buffer[512] = "lua_open_"; - const char* modname = luaL_checklstring(L, -2, &modlen); + size_t sname, namelen, pathlen; + char olib[512]; + const char* name = luaL_checklstring(L, -2, &namelen); const char* path = luaL_checklstring(L, -1, &pathlen); void* library = SDL_LoadObject(path); - if (modlen == 0 || !library) - return luaL_error(L, "Unable to load %s: %s", modname, SDL_GetError()); - for (sname = modlen - 1; sname > 0 && modname[sname] != '.'; --sname); - strcat(buffer, &modname[sname+1]); - int (*entrypoint)(lua_State* L, void*) = SDL_LoadFunction(library, buffer); + if (name == 0 || !library) + return luaL_error(L, "Unable to load %s: %s", name, SDL_GetError()); + for (sname = namelen - 1; sname > 0 && name[sname] != '.'; --sname); + snprintf(olib, sizeof(olib), "lua_open_%s", &name[sname+1]); olib[511] = 0; + int (*entrypoint)(lua_State* L, void*) = SDL_LoadFunction(library, olib); if (!entrypoint) { return luaL_error(L, "Unable to load %s: Can't find entrypoint. Requires a " - "function defined as int %s(lua_State* L, void* (*symbol)(const char*))", - modname, buffer); + "function defined as int %s(lua_State* L, void* XL)", + name, olib); } lua_pushlightuserdata(L, library); - lua_setfield(L, LUA_REGISTRYINDEX, modname); + lua_setfield(L, LUA_REGISTRYINDEX, name); if (entrypoint(L, api_require) == 0) - return luaL_error(L, "Unable to load %s: Your entrypoint must return at\ - least one argument."); + return luaL_error(L, "Unable to load %s: Your entrypoint must return at " + " least one value.", name); return 1; }