diff options
| author | ruki <[email protected]> | 2024-04-02 23:00:07 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-04-02 23:00:07 +0800 |
| commit | 15cb9bc95d2d8df1f9b14f3014c275f6b0032a34 (patch) | |
| tree | 86cead731f5dafd2f3c2845eeb51e33cad4dffd3 | |
| parent | d2c877aa407931a0a385d7937cd06e13b15ecfea (diff) | |
add module.shared test
6 files changed, 55 insertions, 4 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 new file mode 100644 index 000000000..e19d07e12 --- /dev/null +++ b/tests/projects/other/native_module/modules/shared/foo/src/add.cpp @@ -0,0 +1,9 @@ +#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/sub.cpp b/tests/projects/other/native_module/modules/shared/foo/src/sub.cpp new file mode 100644 index 000000000..980289d1a --- /dev/null +++ b/tests/projects/other/native_module/modules/shared/foo/src/sub.cpp @@ -0,0 +1,10 @@ +#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 new file mode 100644 index 000000000..61c0a8357 --- /dev/null +++ b/tests/projects/other/native_module/modules/shared/foo/xmake.lua @@ -0,0 +1,14 @@ +add_rules("mode.debug", "mode.release") + +add_requires("lua") + +target("add") + add_rules("module.shared") + add_files("src/add.cpp") + add_packages("lua") + +target("sub") + add_rules("module.shared") + add_files("src/sub.cpp") + add_packages("lua") + diff --git a/tests/projects/other/native_module/test.lua b/tests/projects/other/native_module/test.lua new file mode 100644 index 000000000..b57362078 --- /dev/null +++ b/tests/projects/other/native_module/test.lua @@ -0,0 +1,3 @@ +function main(t) + t:build() +end diff --git a/tests/projects/other/native_module/xmake.lua b/tests/projects/other/native_module/xmake.lua index e5564cf4f..396921051 100644 --- a/tests/projects/other/native_module/xmake.lua +++ b/tests/projects/other/native_module/xmake.lua @@ -6,10 +6,10 @@ target("test") set_kind("binary") add_files("src/*.cpp") on_config(function (target) --- import("shared.foo") + import("shared.foo") import("binary.bar") --- print("shared: 1 + 1 = %d", foo.add(1, 1)) --- print("shared: 1 - 1 = %d", foo.sub(1, 1)) + print("shared: 1 + 1 = %d", foo.add(1, 1)) + print("shared: 1 - 1 = %d", foo.sub(1, 1)) print("binary: 1 + 1 = %s", bar.add(1, 1)) print("binary: 1 - 1 = %s", bar.sub(1, 1)) end) diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua index fc38747ba..291598885 100644 --- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua +++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua @@ -238,7 +238,22 @@ end -- load module from the shared module function core_sandbox_module._load_from_shared(module_fullpath, opt) - core_sandbox_module._build_module(module_fullpath) + local script + local module + local module_bindir = core_sandbox_module._build_module(module_fullpath) + 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) + script, errors = package.loadlib(libraryfile, "luaopen_" .. modulename) + if not script then + return nil, errors + end + module[modulename] = script() + end + end + return module, script end -- load module |
