summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-04-04 00:22:19 +0800
committerruki <[email protected]>2024-04-04 00:22:19 +0800
commitfee2d1b5106761728134c179375c73f9283a7ba1 (patch)
tree5aecd901ed3691dfe9b2fc75c5c2739b5442d004
parentf716446fa869ef4dc9663d69a3e4364ef2dcbb6b (diff)
improve xmiopen
-rw-r--r--core/src/xmake/package/loadxmi.c26
-rw-r--r--tests/projects/other/native_module/modules/shared/zoo/src/zoo.c2
-rw-r--r--xmake/scripts/module/xmi.h38
3 files changed, 52 insertions, 14 deletions
diff --git a/core/src/xmake/package/loadxmi.c b/core/src/xmake/package/loadxmi.c
index db4091431..b79e8cee4 100644
--- a/core/src/xmake/package/loadxmi.c
+++ b/core/src/xmake/package/loadxmi.c
@@ -34,7 +34,8 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* types
*/
-typedef int (*xm_open_func_t)(xmi_lua_State* lua);
+typedef int (*xm_open_func_t)(lua_State* lua);
+typedef int (*xm_setup_func_t)(xmi_lua_ops_t* ops);
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
@@ -60,17 +61,28 @@ tb_int_t xm_package_loadxmi(lua_State* lua)
}
// get xmiopen_xxx function
- xm_open_func_t func = (xm_open_func_t)tb_dynamic_func(lib, name);
- if (!func)
+ xm_open_func_t luaopen = (xm_open_func_t)tb_dynamic_func(lib, name);
+ if (!luaopen)
{
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)
+ {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "cannot get symbol xmisetup failed");
+ return 2;
+ }
+
+ // setup lua interfaces
+ xmi_lua_ops_t luaops = {0};
+ luaops._lua_createtable = &lua_createtable;
+ xmisetup(&luaops);
+
// load module
- xmi_lua_State xmi_lua = {0};
- xmi_lua._lua = lua;
- xmi_lua._lua_createtable = &lua_createtable;
- return func(&xmi_lua);
+ return luaopen(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 6b278fd25..26b35f3bc 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,7 +18,7 @@ static int sub(lua_State* lua) {
return 1;
}
-int xmiopen_zoo(lua_State* lua) {
+int xmiopen(zoo, lua_State* lua) {
static const luaL_Reg funcs[] = {
{"add", add},
{"sub", sub},
diff --git a/xmake/scripts/module/xmi.h b/xmake/scripts/module/xmi.h
index c9a2d0a6f..d20f8242f 100644
--- a/xmake/scripts/module/xmi.h
+++ b/xmake/scripts/module/xmi.h
@@ -29,7 +29,7 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
-#define xmi_lua_createtable(lua, narr, nrec) lua->_lua_createtable(lua->_lua, narr, nrec)
+#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)
#ifndef LUA_VERSION
@@ -37,24 +37,50 @@
# define lua_newtable xmi_lua_newtable
# define luaL_Reg xmi_luaL_Reg
-# define lua_State struct xmi_lua_State_
+# define lua_State xmi_lua_State
#endif
+// define lua module entry function
+#define xmiopen(name, lua) \
+__dummy = 1; \
+xmi_lua_ops_t* g_lua_ops; \
+int xmisetup(xmi_lua_ops_t* ops) { \
+ g_lua_ops = ops; \
+ return __dummy; \
+} \
+int xmiopen_##name(lua)
+
/* //////////////////////////////////////////////////////////////////////////////////////
* types
*/
-struct xmi_lua_State_;
+typedef struct xmi_lua_State_ {
+ int dummy;
+}xmi_lua_State;
+
typedef struct xmi_luaL_Reg_ {
char const* name;
int (*func)(struct xmi_lua_State_* lua);
}xmi_luaL_Reg;
-typedef struct xmi_lua_State_ {
- void* _lua;
+typedef struct xmi_lua_ops_t_ {
void (*_lua_createtable)(lua_State* lua, int narr, int nrec);
-}xmi_lua_State;
+}xmi_lua_ops_t;
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * interfaces
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// setup lua interfaces
+int xmisetup(xmi_lua_ops_t* ops);
+
+#ifdef __cplusplus
+}
+#endif
#endif