Skip to content

Commit

Permalink
fix init luaops
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Apr 3, 2024
1 parent b0bc1ce commit c04f30a
Show file tree
Hide file tree
Showing 5 changed files with 830 additions and 19 deletions.
21 changes: 14 additions & 7 deletions core/src/xmake/package/loadxmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* types
*/
typedef int (*xm_open_func_t)(lua_State* lua);
typedef int (*xm_setup_func_t)(xmi_lua_ops_t* ops);

/* //////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -61,7 +60,7 @@ tb_int_t xm_package_loadxmi(lua_State* lua)
}

// get xmiopen_xxx function
xm_open_func_t luaopen_func = (xm_open_func_t)tb_dynamic_func(lib, name);
lua_CFunction luaopen_func = (lua_CFunction)tb_dynamic_func(lib, name);
if (!luaopen_func)
{
lua_pushnil(lua);
Expand All @@ -79,11 +78,19 @@ tb_int_t xm_package_loadxmi(lua_State* lua)
}

// setup lua interfaces
xmi_lua_ops_t luaops = {0};
luaops._lua_createtable = &lua_createtable;
luaops._luaL_setfuncs = &luaL_setfuncs;
xmisetup_func(&luaops);
static xmi_lua_ops_t s_luaops = {0};
static tb_bool_t s_luaops_inited = tb_false;
if (!s_luaops_inited)
{
s_luaops._lua_createtable = &lua_createtable;
s_luaops._lua_tointegerx = &lua_tointegerx;
s_luaops._lua_pushinteger = &lua_pushinteger;
s_luaops._luaL_setfuncs = &luaL_setfuncs;
s_luaops_inited = tb_true;
}
xmisetup_func(&s_luaops);

// load module
return luaopen_func(lua);
lua_pushcfunction(lua, luaopen_func);
return 1;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#include <xmi.h>

static int add(lua_State* lua) {
#if 0
int a = lua_tointeger(lua, 1);
int b = lua_tointeger(lua, 2);
lua_pushinteger(lua, a + b);
#endif
return 1;
}

static int sub(lua_State* lua) {
#if 0
int a = lua_tointeger(lua, 1);
int b = lua_tointeger(lua, 2);
lua_pushinteger(lua, a - b);
#endif
return 1;
}

Expand Down
11 changes: 5 additions & 6 deletions xmake/core/sandbox/modules/import/core/sandbox/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,14 @@ function core_sandbox_module._load_from_shared(module_fullpath, opt)
local modulename = path.basename(libraryfile):match("module_(.+)")
if modulename then
if package.loadxmi then
module, errors1 = package.loadxmi(libraryfile, "xmiopen_" .. modulename)
script, errors1 = package.loadxmi(libraryfile, "xmiopen_" .. modulename)
end
if not module then
if not script then
script, errors2 = package.loadlib(libraryfile, "luaopen_" .. modulename)
if script then
module = script()
end
end
if not module then
if script then
module = script()
else
return nil, errors1 or errors2 or string.format("xmiopen_%s and luaopen_%s not found!", modulename, modulename)
end
break
Expand Down
Loading

0 comments on commit c04f30a

Please sign in to comment.