fix unable to load any native library

something went wrong in snprintf that it skips the first character of
the library name. Not only that, the signature is actually luaopen and
not lua_open.
This commit is contained in:
takase1121 2021-09-25 10:31:15 +08:00
parent 8c32950f4b
commit 27fe185ed4
No known key found for this signature in database
GPG Key ID: 60EEFFC68EB3031B
1 changed files with 25 additions and 24 deletions

View File

@ -696,38 +696,39 @@ static void* api_require(const char* symbol) {
} }
static int f_load_native_plugin(lua_State *L) { static int f_load_native_plugin(lua_State *L) {
size_t sname, namelen, pathlen; char entrypoint_name[512]; entrypoint_name[sizeof(entrypoint_name) - 1] = '\0';
int results; int result;
char olib[512]; olib[sizeof(olib)-1] = 0;
const char* name = luaL_checklstring(L, -2, &namelen); const char *name = luaL_checkstring(L, 1);
const char* path = luaL_checklstring(L, -1, &pathlen); const char *path = luaL_checkstring(L, 2);
void *library = SDL_LoadObject(path); void *library = SDL_LoadObject(path);
if (name == 0 || !library) if (!library)
return luaL_error(L, "Unable to load %s: %s", name, SDL_GetError()); return luaL_error(L, "Unable to load %s: %s", name, SDL_GetError());
lua_getglobal(L, "package"); lua_getglobal(L, "package");
lua_getfield(L, -1, "native_plugins"); lua_getfield(L, -1, "native_plugins");
lua_pushlightuserdata(L, library); lua_pushlightuserdata(L, library);
lua_setfield(L, -2, name); lua_setfield(L, -2, name);
lua_pop(L, 1); lua_pop(L, 1);
for (sname = namelen - 1; sname > 0 && name[sname] != '.'; --sname);
snprintf(olib, sizeof(olib), "lua_open_lite_xl_%s", &name[sname+1]); const char *basename = strrchr(name, '.');
int (*ext_entrypoint)(lua_State* L, void*) = SDL_LoadFunction(library, olib); basename = !basename ? name : basename + 1;
snprintf(entrypoint_name, sizeof(entrypoint_name), "luaopen_lite_xl_%s", basename);
int (*ext_entrypoint) (lua_State *L, void*) = SDL_LoadFunction(library, entrypoint_name);
if (!ext_entrypoint) { if (!ext_entrypoint) {
snprintf(olib, sizeof(olib), "lua_open_%s", &name[sname+1]); snprintf(entrypoint_name, sizeof(entrypoint_name), "luaopen_%s", basename);
int (*entrypoint)(lua_State* L) = SDL_LoadFunction(library, olib); int (*entrypoint)(lua_State *L) = SDL_LoadFunction(library, entrypoint_name);
if (!entrypoint) { if (!entrypoint)
return luaL_error(L, "Unable to load %s: Can't find entrypoint. Requires a " return luaL_error(L, "Unable to load %s: Can't find %s(lua_State *L, void *XL)", name, entrypoint_name);
"function defined as int lua_open_lite_xl_%s(lua_State* L, void* XL)", result = entrypoint(L);
name, &name[sname+1]);
}
results = entrypoint(L);
} else { } else {
results = ext_entrypoint(L, api_require); result = ext_entrypoint(L, api_require);
} }
if (!results)
return luaL_error(L, "Unable to load %s: Your entrypoint must return at " if (!result)
" least one value.", name); return luaL_error(L, "Unable to load %s: entrypoint must return a value", name);
return results;
return result;
} }