diff options
| author | ruki <[email protected]> | 2020-04-22 23:23:37 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-04-22 14:21:42 +0800 |
| commit | 9407f3893ca3f71578febcabdf6a022a80dae209 (patch) | |
| tree | ed5e08bf206b406028b840a7f036af4c9a4d96c7 | |
| parent | 8567065df7d23e7c51880d1e36a9895a13cd4b89 (diff) | |
add lni module
| -rw-r--r-- | core/src/xmake/engine.c | 21 | ||||
| -rw-r--r-- | core/src/xmake/engine.h | 12 | ||||
| -rw-r--r-- | tests/projects/xmake_cli/hello/src/lni/main.c | 16 | ||||
| -rw-r--r-- | tests/projects/xmake_cli/hello/src/lua/main.lua | 3 |
4 files changed, 47 insertions, 5 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index b7ef783d1..f92729297 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -829,8 +829,13 @@ xm_engine_ref_t xm_engine_init(xm_engine_lua_initializer_cb_t lua_initalizer) lua_newtable(engine->lua); lua_setglobal(engine->lua, "xmake"); - // do lua initializer - if (lua_initalizer) lua_initalizer(engine->lua); + /* do lua initializer and init namespace: _lni + * + * 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); + lua_setglobal(engine->lua, "_lni"); #ifdef TB_CONFIG_OS_WINDOWS // enable terminal colors output for windows cmd @@ -943,3 +948,15 @@ tb_int_t xm_engine_main(xm_engine_ref_t self, tb_int_t argc, tb_char_t** argv) // get the error code return (tb_int_t)lua_tonumber(engine->lua, -1); } +tb_void_t xm_engine_register(xm_engine_ref_t self, tb_char_t const* module, luaL_Reg const funcs[]) +{ + // check + xm_engine_t* engine = (xm_engine_t*)self; + tb_assert_and_check_return(engine && engine->lua && module && funcs); + + // do register + lua_pushstring(engine->lua, module); + lua_newtable(engine->lua); + luaL_register(engine->lua, tb_null, funcs); + lua_rawset(engine->lua, -3); +} diff --git a/core/src/xmake/engine.h b/core/src/xmake/engine.h index d4e117e51..9ccd2adb0 100644 --- a/core/src/xmake/engine.h +++ b/core/src/xmake/engine.h @@ -39,7 +39,7 @@ __tb_extern_c_enter__ 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)(lua_State* lua); +typedef tb_bool_t (*xm_engine_lua_initializer_cb_t)(xm_engine_ref_t engine, lua_State* lua); /* ////////////////////////////////////////////////////////////////////////////////////// * interfaces @@ -57,7 +57,7 @@ xm_engine_ref_t xm_engine_init(xm_engine_lua_initializer_cb_t lua_initalizer */ tb_void_t xm_engine_exit(xm_engine_ref_t engine); -/*! done the engine +/*! do the main entry of the engine * * @param engine the engine * @param argc the argument count of the console @@ -67,6 +67,14 @@ tb_void_t xm_engine_exit(xm_engine_ref_t engine); */ 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 lua_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[]); + /* ////////////////////////////////////////////////////////////////////////////////////// * extern */ diff --git a/tests/projects/xmake_cli/hello/src/lni/main.c b/tests/projects/xmake_cli/hello/src/lni/main.c index dfef99794..1d574dc1b 100644 --- a/tests/projects/xmake_cli/hello/src/lni/main.c +++ b/tests/projects/xmake_cli/hello/src/lni/main.c @@ -1,11 +1,25 @@ #include <xmake/xmake.h> +static tb_int_t lni_test_hello(lua_State* lua) +{ + lua_pushliteral(lua, "hello xmake!"); + return 1; +} +static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State* lua) +{ + static luaL_Reg const lni_test_funcs[] = + { + {"hello", lni_test_hello} + , {tb_null, tb_null} + }; + xm_engine_register(engine, "test", lni_test_funcs); +} 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); + xm_engine_ref_t engine = xm_engine_init(lni_initalizer); if (engine) { tb_int_t argc2 = argc + 2; diff --git a/tests/projects/xmake_cli/hello/src/lua/main.lua b/tests/projects/xmake_cli/hello/src/lua/main.lua index 1ce83e728..26ba586a2 100644 --- a/tests/projects/xmake_cli/hello/src/lua/main.lua +++ b/tests/projects/xmake_cli/hello/src/lua/main.lua @@ -1,5 +1,8 @@ import("core.base.option") +import("lib.lni.test") + function main () + print(test) print("hello xmake!") local argv = option.get("arguments") if argv then |
