summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-04-03 23:50:20 +0800
committerruki <[email protected]>2024-04-03 23:50:20 +0800
commit70748de03539eec10aafb9cb78178c275d6e9b47 (patch)
tree38d732ed34f3cef05616f270e427400b7247ebee
parentd08d95ac8fc9e1f316ff180aaedd61e21c384e24 (diff)
improve loadxmi
-rw-r--r--core/src/xmake/engine.c13
-rw-r--r--core/src/xmake/package/loadxmi.c72
-rw-r--r--core/src/xmake/package/prefix.h32
-rwxr-xr-xcore/src/xmake/xmake.sh1
-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.lua11
6 files changed, 129 insertions, 4 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 827d46d2e..b63dcf42b 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -294,6 +294,9 @@ tb_int_t xm_libc_setbyte(lua_State* lua);
// the tty functions
tb_int_t xm_tty_term_mode(lua_State* lua);
+// the package functions
+tb_int_t xm_package_loadxmi(lua_State* lua);
+
#ifdef XM_CONFIG_API_HAVE_CURSES
// register curses functions
tb_int_t xm_lua_curses_register(lua_State* lua, tb_char_t const* module);
@@ -571,6 +574,13 @@ static luaL_Reg const g_tty_functions[] =
, { tb_null, tb_null }
};
+// the package functions
+static luaL_Reg const g_package_functions[] =
+{
+ { "loadxmi", xm_package_loadxmi }
+, { tb_null, tb_null }
+};
+
// the lua global instance for signal handler
static lua_State* g_lua = tb_null;
@@ -1158,6 +1168,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c
// bind tty functions
xm_lua_register(engine->lua, "tty", g_tty_functions);
+ // bind package functions
+ xm_lua_register(engine->lua, "package", g_package_functions);
+
#ifdef XM_CONFIG_API_HAVE_CURSES
// bind curses
xm_lua_curses_register(engine->lua, "curses");
diff --git a/core/src/xmake/package/loadxmi.c b/core/src/xmake/package/loadxmi.c
new file mode 100644
index 000000000..fcbecf72b
--- /dev/null
+++ b/core/src/xmake/package/loadxmi.c
@@ -0,0 +1,72 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file loadxmi.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "loadxmi"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * types
+ */
+typedef int (*xm_open_func_t)(lua_State* lua);
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_package_loadxmi(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ tb_char_t const* path = luaL_checkstring(lua, 1);
+ tb_check_return_val(path, 0);
+
+ tb_char_t const* name = luaL_checkstring(lua, 2);
+ tb_check_return_val(name, 0);
+
+ // load module library
+ tb_dynamic_ref_t lib = tb_dynamic_init(path);
+ if (!lib)
+ {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "load %s failed", path);
+ return 2;
+ }
+
+ // get xmiopen_xxx function
+ xm_open_func_t func = (xm_open_func_t)tb_dynamic_func(path, name);
+ if (!func)
+ {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "cannot get symbol %s failed", name);
+ return 2;
+ }
+
+ // load module
+ return func(lua);
+}
diff --git a/core/src/xmake/package/prefix.h b/core/src/xmake/package/prefix.h
new file mode 100644
index 000000000..e710e526c
--- /dev/null
+++ b/core/src/xmake/package/prefix.h
@@ -0,0 +1,32 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file prefix.h
+ *
+ */
+#ifndef XM_PACKAGE_PREFIX_H
+#define XM_PACKAGE_PREFIX_H
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "../prefix.h"
+
+
+#endif
+
+
diff --git a/core/src/xmake/xmake.sh b/core/src/xmake/xmake.sh
index a813cf078..5a3cbe1d0 100755
--- a/core/src/xmake/xmake.sh
+++ b/core/src/xmake/xmake.sh
@@ -63,6 +63,7 @@ target "xmake"
add_files "lz4/*.c"
add_files "os/*.c"
add_files "path/*.c"
+ add_files "package/*.c"
add_files "process/*.c"
add_files "readline/*.c"
add_files "sandbox/*.c"
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 a93fb0dab..29e4a0e73 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
@@ -1,3 +1,4 @@
+#include <stdio.h>
#include <xmi.h>
static int add(lua_State* lua) {
@@ -18,7 +19,8 @@ static int sub(lua_State* lua) {
return 1;
}
-int xmi_luaopen_foo(lua_State* lua) {
+int xmiopen_zoo(lua_State* lua) {
+ printf("xxx\n");
static const luaL_Reg funcs[] = {
{"add", add},
{"sub", sub},
diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
index b1fc0084e..64698a504 100644
--- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua
+++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
@@ -311,12 +311,17 @@ function core_sandbox_module._load_from_shared(module_fullpath, opt)
libraryfiles = os.files(path.join(moduleinfo.buildir, "*module_*"))
end
if #libraryfiles > 0 then
- local errors
+ local errors1, errors2
for _, libraryfile in ipairs(libraryfiles) do
local modulename = path.basename(libraryfile):match("module_(.+)")
- script, errors = package.loadlib(libraryfile, "luaopen_" .. modulename)
+ if package.loadxmi then
+ script, errors1 = package.loadxmi(libraryfile, "xmiopen_" .. modulename)
+ end
if not script then
- return nil, errors
+ 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