summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2020-04-22 23:23:37 +0800
committerruki <[email protected]>2020-04-22 14:21:42 +0800
commit9407f3893ca3f71578febcabdf6a022a80dae209 (patch)
treeed5e08bf206b406028b840a7f036af4c9a4d96c7 /core/src
parent8567065df7d23e7c51880d1e36a9895a13cd4b89 (diff)
add lni module
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/engine.c21
-rw-r--r--core/src/xmake/engine.h12
2 files changed, 29 insertions, 4 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
*/