Skip to content

Commit

Permalink
fix xmiopen
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Apr 3, 2024
1 parent fee2d1b commit b0bc1ce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
13 changes: 7 additions & 6 deletions core/src/xmake/package/loadxmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ tb_int_t xm_package_loadxmi(lua_State* lua)
}

// get xmiopen_xxx function
xm_open_func_t luaopen = (xm_open_func_t)tb_dynamic_func(lib, name);
if (!luaopen)
xm_open_func_t luaopen_func = (xm_open_func_t)tb_dynamic_func(lib, name);
if (!luaopen_func)
{
lua_pushnil(lua);
lua_pushfstring(lua, "cannot get symbol %s failed", name);
return 2;
}

// get xmisetup function
xm_setup_func_t xmisetup = (xm_setup_func_t)tb_dynamic_func(lib, "xmisetup");
if (!xmisetup)
xm_setup_func_t xmisetup_func = (xm_setup_func_t)tb_dynamic_func(lib, "xmisetup");
if (!xmisetup_func)
{
lua_pushnil(lua);
lua_pushfstring(lua, "cannot get symbol xmisetup failed");
Expand All @@ -81,8 +81,9 @@ tb_int_t xm_package_loadxmi(lua_State* lua)
// setup lua interfaces
xmi_lua_ops_t luaops = {0};
luaops._lua_createtable = &lua_createtable;
xmisetup(&luaops);
luaops._luaL_setfuncs = &luaL_setfuncs;
xmisetup_func(&luaops);

// load module
return luaopen(lua);
return luaopen_func(lua);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ static int sub(lua_State* lua) {
return 1;
}

int xmiopen(zoo, lua_State* lua) {
int luaopen(zoo, lua_State* lua) {
static const luaL_Reg funcs[] = {
{"add", add},
{"sub", sub},
{NULL, NULL}
};
lua_newtable(lua);
#if 0
luaL_setfuncs(lua, funcs, 0);
#endif
return 1;
}
27 changes: 14 additions & 13 deletions xmake/core/sandbox/modules/import/core/sandbox/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -305,26 +305,27 @@ function core_sandbox_module._load_from_shared(module_fullpath, opt)
end
libraryfiles = {}
end
local script
local module
if #libraryfiles == 0 then
libraryfiles = os.files(path.join(moduleinfo.buildir, "*module_*"))
end
if #libraryfiles > 0 then
local errors1, errors2
local script, errors1, errors2
for _, libraryfile in ipairs(libraryfiles) do
local modulename = path.basename(libraryfile):match("module_(.+)")
if package.loadxmi then
script, errors1 = package.loadxmi(libraryfile, "xmiopen_" .. modulename)
end
if not script then
script, errors2 = package.loadlib(libraryfile, "luaopen_" .. modulename)
end
if not script then
return nil, errors1 or errors2 or string.format("xmiopen_%s and luaopen_%s not found!", modulename, modulename)
end
module = script()
if module then
if modulename then
if package.loadxmi then
module, errors1 = package.loadxmi(libraryfile, "xmiopen_" .. modulename)
end
if not module then
script, errors2 = package.loadlib(libraryfile, "luaopen_" .. modulename)
if script then
module = script()
end
end
if not module then
return nil, errors1 or errors2 or string.format("xmiopen_%s and luaopen_%s not found!", modulename, modulename)
end
break
end
end
Expand Down
15 changes: 13 additions & 2 deletions xmake/scripts/module/xmi.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,29 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/

// lua interfaces
#define xmi_lua_createtable(lua, narr, nrec) (g_lua_ops)->_lua_createtable(lua, narr, nrec)
#define xmi_lua_newtable(lua) xmi_lua_createtable(lua, 0, 0)

// luaL interfaces
#define xmi_luaL_setfuncs(lua, narr, nrec) (g_lua_ops)->_luaL_setfuncs(lua, narr, nrec)

/* we cannot redefine lua functions in loadxmi.c,
* because original lua.h has been included
*/
#ifndef LUA_VERSION
# define lua_createtable xmi_lua_createtable
# define lua_newtable xmi_lua_newtable

# define luaL_setfuncs xmi_luaL_setfuncs

# define luaL_Reg xmi_luaL_Reg
# define lua_State xmi_lua_State
#endif

// define lua module entry function
#define xmiopen(name, lua) \
#define luaopen(name, lua) \
__dummy = 1; \
xmi_lua_ops_t* g_lua_ops; \
int xmisetup(xmi_lua_ops_t* ops) { \
Expand All @@ -64,7 +74,8 @@ typedef struct xmi_luaL_Reg_ {
}xmi_luaL_Reg;

typedef struct xmi_lua_ops_t_ {
void (*_lua_createtable)(lua_State* lua, int narr, int nrec);
void (*_lua_createtable)(lua_State* lua, int narr, int nrec);
void (*_luaL_setfuncs)(lua_State* lua, const luaL_Reg* l, int nup);
}xmi_lua_ops_t;

/* //////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit b0bc1ce

Please sign in to comment.