summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-04-22 23:44:03 +0800
committerruki <[email protected]>2020-04-22 16:24:20 +0800
commit05df31effcfc9bc701387e1383b678f0914241be (patch)
treead03fc1658358ed939e1bb479289f4a3e9515130
parenta69f20d2daf4ed61b58b036eee7bf20a3034a30e (diff)
improve engine
-rw-r--r--core/src/demo/xmake.c24
-rw-r--r--core/src/xmake/engine.c45
-rw-r--r--core/src/xmake/engine.h59
-rw-r--r--tests/projects/xmake_cli/hello/src/lni/main.c32
-rw-r--r--tests/projects/xmake_cli/hello/src/lua/main.lua5
-rw-r--r--tests/projects/xmake_cli/hello/xmake.lua2
-rw-r--r--tests/projects/xmake_cli/xmake/src/main.c13
7 files changed, 94 insertions, 86 deletions
diff --git a/core/src/demo/xmake.c b/core/src/demo/xmake.c
index 69141323a..d6605f7d0 100644
--- a/core/src/demo/xmake.c
+++ b/core/src/demo/xmake.c
@@ -8,27 +8,5 @@
*/
tb_int_t main(tb_int_t argc, tb_char_t** argv)
{
- // init ok
- tb_int_t ok = -1;
-
- // init xmake
- if (xm_init())
- {
- // init engine
- xm_engine_ref_t engine = xm_engine_init(tb_null);
- if (engine)
- {
- // do engine main entry
- ok = xm_engine_main(engine, argc, argv);
-
- // exit engine
- xm_engine_exit(engine);
- }
-
- // exit xmake
- xm_exit();
- }
-
- // ok?
- return ok;
+ return xm_engine_run(argc, argv, tb_null);
}
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index f92729297..6ccada916 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -743,7 +743,7 @@ static tb_void_t xm_engine_init_features(xm_engine_t* engine)
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
-xm_engine_ref_t xm_engine_init(xm_engine_lua_initializer_cb_t lua_initalizer)
+xm_engine_ref_t xm_engine_init(xm_engine_lni_initalizer_cb_t lni_initalizer)
{
// done
tb_bool_t ok = tb_false;
@@ -834,7 +834,7 @@ xm_engine_ref_t xm_engine_init(xm_engine_lua_initializer_cb_t lua_initalizer)
* we can get the lni modules for _lni or `import("lib.lni.xxx")` in sandbox
*/
lua_newtable(engine->lua);
- if (lua_initalizer) lua_initalizer((xm_engine_ref_t)engine, engine->lua);
+ if (lni_initalizer) lni_initalizer((xm_engine_ref_t)engine, engine->lua);
lua_setglobal(engine->lua, "_lni");
#ifdef TB_CONFIG_OS_WINDOWS
@@ -960,3 +960,44 @@ tb_void_t xm_engine_register(xm_engine_ref_t self, tb_char_t const* module, luaL
luaL_register(engine->lua, tb_null, funcs);
lua_rawset(engine->lua, -3);
}
+tb_int_t xm_engine_run(tb_int_t argc, tb_char_t** argv, xm_engine_lni_initalizer_cb_t lni_initalizer)
+{
+ tb_int_t ok = -1;
+ if (xm_init())
+ {
+ xm_engine_ref_t engine = xm_engine_init(lni_initalizer);
+ if (engine)
+ {
+ ok = xm_engine_main(engine, argc, argv);
+ xm_engine_exit(engine);
+ }
+ xm_exit();
+ }
+ return ok;
+}
+tb_int_t xm_engine_run_lua(tb_int_t argc, tb_char_t** argv, xm_engine_lni_initalizer_cb_t lni_initalizer, tb_char_t const* luaopts)
+{
+ if (luaopts)
+ {
+ tb_int_t argc2 = argc + 3;
+ tb_char_t* argv2[argc2 + 1];
+ argv2[0] = argv[0];
+ argv2[1] = "lua";
+ argv2[2] = (tb_char_t*)luaopts;
+ argv2[3] = "lua.main";
+ if (argc > 1) tb_memcpy(argv2 + 4, argv + 1, (argc - 1) * sizeof(tb_char_t*));
+ argv2[argc2] = tb_null;
+ return xm_engine_run(argc2, argv2, lni_initalizer);
+ }
+ else
+ {
+ tb_int_t argc2 = argc + 2;
+ tb_char_t* argv2[argc2 + 1];
+ argv2[0] = argv[0];
+ argv2[1] = "lua";
+ argv2[2] = "lua.main";
+ if (argc > 1) tb_memcpy(argv2 + 3, argv + 1, (argc - 1) * sizeof(tb_char_t*));
+ argv2[argc2] = tb_null;
+ return xm_engine_run(argc2, argv2, lni_initalizer);
+ }
+}
diff --git a/core/src/xmake/engine.h b/core/src/xmake/engine.h
index 9ccd2adb0..7b0042dfc 100644
--- a/core/src/xmake/engine.h
+++ b/core/src/xmake/engine.h
@@ -36,10 +36,10 @@ __tb_extern_c_enter__
*/
/// the xmake engine type
-typedef struct {tb_int_t dummy;} const* xm_engine_ref_t;
+typedef struct {tb_int_t dummy;} const* xm_engine_ref_t;
-/// the lua initializer callback type
-typedef tb_bool_t (*xm_engine_lua_initializer_cb_t)(xm_engine_ref_t engine, lua_State* lua);
+/// the lni initializer callback type
+typedef tb_bool_t (*xm_engine_lni_initalizer_cb_t)(xm_engine_ref_t engine, lua_State* lua);
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
@@ -47,33 +47,58 @@ typedef tb_bool_t (*xm_engine_lua_initializer_cb_t)(xm_engine_ref_t engine, lua_
/*! init the engine
*
- * @return the engine
+ * @param lni_initalizer the lni initializer
+ *
+ * @return the engine
*/
-xm_engine_ref_t xm_engine_init(xm_engine_lua_initializer_cb_t lua_initalizer);
+xm_engine_ref_t xm_engine_init(xm_engine_lni_initalizer_cb_t lni_initalizer);
/*! exit the engine
*
- * @param engine the engine
+ * @param engine the engine
*/
-tb_void_t xm_engine_exit(xm_engine_ref_t engine);
+tb_void_t xm_engine_exit(xm_engine_ref_t engine);
/*! do the main entry of the engine
*
- * @param engine the engine
- * @param argc the argument count of the console
- * @param argv the argument list of the console
+ * @param engine the engine
+ * @param argc the argument count of the console
+ * @param argv the argument list of the console
+ *
+ * @return the error code of main()
+ */
+tb_int_t xm_engine_main(xm_engine_ref_t engine, tb_int_t argc, tb_char_t** argv);
+
+/*! register lni modules in the engine, @note we need call it in lni_initalizer()
+ *
+ * @param engine the engine
+ * @param module the lni module name
+ * @param funcs the lni module functions
+ */
+tb_void_t xm_engine_register(xm_engine_ref_t engine, tb_char_t const* module, luaL_Reg const funcs[]);
+
+/*! run main entry of the engine singleton
+ *
+ * @param argc the argument count of the console
+ * @param argv the argument list of the console
+ * @param lni_initalizer the lni initializer
*
- * @return the error code of main()
+ * @return the error code of main()
*/
-tb_int_t xm_engine_main(xm_engine_ref_t engine, tb_int_t argc, tb_char_t** argv);
+tb_int_t xm_engine_run(tb_int_t argc, tb_char_t** argv, xm_engine_lni_initalizer_cb_t lni_initalizer);
-/*! register lni modules in the engine, @note we need call it in lua_initalizer()
+/*! run lua action entry of the engine singleton for `xmake lua [-vD] lua.main`
+ *
+ * @note we need set $XMAKE_MODULES_DIR with the directory of "lua\\*.lua"
+ *
+ * @param argc the argument count of the console
+ * @param argv the argument list of the console
+ * @param lni_initalizer the lni initializer
+ * @param luaopts the lua options, e.g. "-vD"
*
- * @param engine the engine
- * @param module the lni module name
- * @param funcs the lni module functions
+ * @return the error code of main()
*/
-tb_void_t xm_engine_register(xm_engine_ref_t engine, tb_char_t const* module, luaL_Reg const funcs[]);
+tb_int_t xm_engine_run_lua(tb_int_t argc, tb_char_t** argv, xm_engine_lni_initalizer_cb_t lni_initalizer, tb_char_t const* luaopts);
/* //////////////////////////////////////////////////////////////////////////////////////
* extern
diff --git a/tests/projects/xmake_cli/hello/src/lni/main.c b/tests/projects/xmake_cli/hello/src/lni/main.c
index a2601687a..d1ab0b0fb 100644
--- a/tests/projects/xmake_cli/hello/src/lni/main.c
+++ b/tests/projects/xmake_cli/hello/src/lni/main.c
@@ -16,34 +16,10 @@ static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State* lua)
}
tb_int_t main(tb_int_t argc, tb_char_t** argv)
{
- tb_int_t ok = -1;
- if (xm_init())
- {
- xm_engine_ref_t engine = xm_engine_init(lni_initalizer);
- if (engine)
- {
-#if 0
- tb_int_t argc2 = argc + 2;
- tb_char_t* argv2[argc2 + 1];
- argv2[0] = argv[0];
- argv2[1] = "lua";
- argv2[2] = "lua.main";
- if (argc > 1) tb_memcpy(argv2 + 3, argv + 1, (argc - 1) * sizeof(tb_char_t*));
- argv2[argc2] = tb_null;
+#ifdef __tb_debug__
+ tb_char_t const* luaopts = "-vD";
#else
- tb_int_t argc2 = argc + 3;
- tb_char_t* argv2[argc2 + 1];
- argv2[0] = argv[0];
- argv2[1] = "lua";
- argv2[2] = "-vD";
- argv2[3] = "lua.main";
- if (argc > 1) tb_memcpy(argv2 + 4, argv + 1, (argc - 1) * sizeof(tb_char_t*));
- argv2[argc2] = tb_null;
+ tb_char_t const* luaopts = "-D";
#endif
- ok = xm_engine_main(engine, argc2, argv2);
- xm_engine_exit(engine);
- }
- xm_exit();
- }
- return ok;
+ return xm_engine_run_lua(argc, argv, lni_initalizer, luaopts);
}
diff --git a/tests/projects/xmake_cli/hello/src/lua/main.lua b/tests/projects/xmake_cli/hello/src/lua/main.lua
index 0a1590543..d87daf102 100644
--- a/tests/projects/xmake_cli/hello/src/lua/main.lua
+++ b/tests/projects/xmake_cli/hello/src/lua/main.lua
@@ -1,9 +1,8 @@
import("core.base.option")
-import("lib.lni")
+import("lib.lni.test")
function main ()
- print(lni)
- print("hello xmake!")
+ print(test.hello())
local argv = option.get("arguments")
if argv then
print(argv)
diff --git a/tests/projects/xmake_cli/hello/xmake.lua b/tests/projects/xmake_cli/hello/xmake.lua
index e2e410e03..e4c0aba32 100644
--- a/tests/projects/xmake_cli/hello/xmake.lua
+++ b/tests/projects/xmake_cli/hello/xmake.lua
@@ -1,5 +1,5 @@
add_rules("mode.debug", "mode.release")
-add_requires("libxmake")
+add_requires("libxmake", {debug = is_mode("debug")})
target("hello")
add_rules("xmake.cli")
add_files("src/lni/*.c")
diff --git a/tests/projects/xmake_cli/xmake/src/main.c b/tests/projects/xmake_cli/xmake/src/main.c
index b84c5b500..68403c6c4 100644
--- a/tests/projects/xmake_cli/xmake/src/main.c
+++ b/tests/projects/xmake_cli/xmake/src/main.c
@@ -2,16 +2,5 @@
tb_int_t main(tb_int_t argc, tb_char_t** argv)
{
- tb_int_t ok = -1;
- if (xm_init())
- {
- xm_engine_ref_t engine = xm_engine_init(tb_null);
- if (engine)
- {
- ok = xm_engine_main(engine, argc, argv);
- xm_engine_exit(engine);
- }
- xm_exit();
- }
- return ok;
+ return xm_engine_run(argc, argv, tb_null);
}