summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-04-04 00:24:53 +0800
committerruki <[email protected]>2024-04-04 00:24:53 +0800
commitb0bc1ce397bb4b700c989df01d11b65b72ca800f (patch)
treea8382840fd0c628a8b05b11752f8a8819096fcad
parentfee2d1b5106761728134c179375c73f9283a7ba1 (diff)
fix xmiopen
-rw-r--r--core/src/xmake/package/loadxmi.c13
-rw-r--r--tests/projects/other/native_module/modules/shared/zoo/src/zoo.c4
-rw-r--r--xmake/core/sandbox/modules/import/core/sandbox/module.lua27
-rw-r--r--xmake/scripts/module/xmi.h15
4 files changed, 35 insertions, 24 deletions
diff --git a/core/src/xmake/package/loadxmi.c b/core/src/xmake/package/loadxmi.c
index b79e8cee4..501c5d680 100644
--- a/core/src/xmake/package/loadxmi.c
+++ b/core/src/xmake/package/loadxmi.c
@@ -61,8 +61,8 @@ 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);
@@ -70,8 +70,8 @@ tb_int_t xm_package_loadxmi(lua_State* lua)
}
// 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");
@@ -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);
}
diff --git a/tests/projects/other/native_module/modules/shared/zoo/src/zoo.c b/tests/projects/other/native_module/modules/shared/zoo/src/zoo.c
index 26b35f3bc..02458467c 100644
--- a/tests/projects/other/native_module/modules/shared/zoo/src/zoo.c
+++ b/tests/projects/other/native_module/modules/shared/zoo/src/zoo.c
@@ -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;
}
diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
index 64698a504..1cf2ace5c 100644
--- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua
+++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
@@ -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
diff --git a/xmake/scripts/module/xmi.h b/xmake/scripts/module/xmi.h
index d20f8242f..aa45e9bb1 100644
--- a/xmake/scripts/module/xmi.h
+++ b/xmake/scripts/module/xmi.h
@@ -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) { \
@@ -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;
/* //////////////////////////////////////////////////////////////////////////////////////