diff options
| author | ruki <[email protected]> | 2024-04-02 23:04:57 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-04-02 23:04:57 +0800 |
| commit | df624c36f992f13bb65b8b2777718e1255ed5652 (patch) | |
| tree | cbb777346b564997a09b6054c20a5611aa5fb919 | |
| parent | 15cb9bc95d2d8df1f9b14f3014c275f6b0032a34 (diff) | |
load foo module
5 files changed, 38 insertions, 31 deletions
diff --git a/tests/projects/other/native_module/modules/shared/foo/src/add.cpp b/tests/projects/other/native_module/modules/shared/foo/src/add.cpp deleted file mode 100644 index e19d07e12..000000000 --- a/tests/projects/other/native_module/modules/shared/foo/src/add.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -int main(int argc, char** argv) { - int a = atoi(argv[1]); - int b = atoi(argv[2]); - printf("%d", a + b); - return 0; -} diff --git a/tests/projects/other/native_module/modules/shared/foo/src/foo.c b/tests/projects/other/native_module/modules/shared/foo/src/foo.c new file mode 100644 index 000000000..3024f9380 --- /dev/null +++ b/tests/projects/other/native_module/modules/shared/foo/src/foo.c @@ -0,0 +1,29 @@ +#include <stdlib.h> +#include <lua.h> +#include <lauxlib.h> + +static int add(lua_State* lua) { + int a = lua_tointeger(lua, 1); + int b = lua_tointeger(lua, 2); + lua_pushinteger(lua, a + b); + return 1; +} + +static int sub(lua_State* lua) { + int a = lua_tointeger(lua, 1); + int b = lua_tointeger(lua, 2); + lua_pushinteger(lua, a - b); + return 1; +} + +static const luaL_Reg g_funcs[] = { + {"add", add}, + {"sub", sub}, + {NULL, NULL} +}; + +int luaopen_foo(lua_State* lua) { + lua_newtable(lua); + luaL_setfuncs(lua, g_funcs, 0); + return 1; +} diff --git a/tests/projects/other/native_module/modules/shared/foo/src/sub.cpp b/tests/projects/other/native_module/modules/shared/foo/src/sub.cpp deleted file mode 100644 index 980289d1a..000000000 --- a/tests/projects/other/native_module/modules/shared/foo/src/sub.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -int main(int argc, char** argv) { - int a = atoi(argv[1]); - int b = atoi(argv[2]); - printf("%d", a - b); - return 0; -} - diff --git a/tests/projects/other/native_module/modules/shared/foo/xmake.lua b/tests/projects/other/native_module/modules/shared/foo/xmake.lua index 61c0a8357..e4fcc34ad 100644 --- a/tests/projects/other/native_module/modules/shared/foo/xmake.lua +++ b/tests/projects/other/native_module/modules/shared/foo/xmake.lua @@ -1,14 +1,9 @@ add_rules("mode.debug", "mode.release") -add_requires("lua") +add_requires("lua 5.4", {system = false}) -target("add") +target("foo") add_rules("module.shared") - add_files("src/add.cpp") - add_packages("lua") - -target("sub") - add_rules("module.shared") - add_files("src/sub.cpp") + add_files("src/foo.c") add_packages("lua") diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua index 291598885..ce1ed8fa0 100644 --- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua +++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua @@ -241,16 +241,18 @@ function core_sandbox_module._load_from_shared(module_fullpath, opt) local script local module local module_bindir = core_sandbox_module._build_module(module_fullpath) - local libraryfiles = os.files(path.join(module_bindir, "module_*")) + local libraryfiles = os.files(path.join(module_bindir, "*module_*")) if libraryfiles then - module = {} for _, libraryfile in ipairs(libraryfiles) do - local modulename = path.basename(binaryfile):sub(8) + local modulename = path.basename(libraryfile):match("module_(.+)") script, errors = package.loadlib(libraryfile, "luaopen_" .. modulename) if not script then return nil, errors end - module[modulename] = script() + module = script() + if module then + break + end end end return module, script |
