refactor(lua): move _G.arg init to nlua_init()

This commit is contained in:
Justin M. Keyes 2023-01-05 09:25:19 +01:00
parent e8ab218716
commit 7089f33144
2 changed files with 6 additions and 11 deletions

View File

@ -335,9 +335,8 @@ static int nlua_thr_api_nvim__get_runtime(lua_State *lstate)
/// @see https://github.com/premake/premake-core/blob/1c1304637f4f5e50ba8c57aae8d1d80ec3b7aaf2/src/host/premake.c#L563-L594 /// @see https://github.com/premake/premake-core/blob/1c1304637f4f5e50ba8c57aae8d1d80ec3b7aaf2/src/host/premake.c#L563-L594
/// ///
/// @returns number of args /// @returns number of args
int nlua_init_argv(char **argv, int argc, int lua_arg0) static int nlua_init_argv(lua_State *const L, char **argv, int argc, int lua_arg0)
{ {
lua_State *const L = global_lstate;
lua_newtable(L); // _G.arg lua_newtable(L); // _G.arg
int i = 0; int i = 0;
for (; lua_arg0 >= 0 && i + lua_arg0 < argc; i++) { for (; lua_arg0 >= 0 && i + lua_arg0 < argc; i++) {
@ -790,10 +789,8 @@ static bool nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
return true; return true;
} }
/// Initialize global lua interpreter /// Initializes global Lua interpreter, or exits Nvim on failure.
/// void nlua_init(char **argv, int argc, int lua_arg0)
/// Crashes Nvim if initialization fails.
void nlua_init(void)
{ {
#ifdef NLUA_TRACK_REFS #ifdef NLUA_TRACK_REFS
const char *env = os_getenv("NVIM_LUA_NOTRACK"); const char *env = os_getenv("NVIM_LUA_NOTRACK");
@ -814,10 +811,9 @@ void nlua_init(void)
} }
luv_set_thread_cb(nlua_thread_acquire_vm, nlua_common_free_all_mem); luv_set_thread_cb(nlua_thread_acquire_vm, nlua_common_free_all_mem);
global_lstate = lstate; global_lstate = lstate;
main_thread = uv_thread_self(); main_thread = uv_thread_self();
nlua_init_argv(lstate, argv, argc, lua_arg0);
} }
static lua_State *nlua_thread_acquire_vm(void) static lua_State *nlua_thread_acquire_vm(void)

View File

@ -279,8 +279,7 @@ int main(int argc, char **argv)
// argument list "global_alist". // argument list "global_alist".
command_line_scan(&params); command_line_scan(&params);
nlua_init(); nlua_init(argv, argc, params.lua_arg0);
nlua_init_argv(argv, argc, params.lua_arg0);
TIME_MSG("init lua interpreter"); TIME_MSG("init lua interpreter");
if (embedded_mode) { if (embedded_mode) {
@ -2147,7 +2146,7 @@ static void mainerr(const char *errstr, const char *str)
static void version(void) static void version(void)
{ {
// TODO(bfred): not like this? // TODO(bfred): not like this?
nlua_init(); nlua_init(NULL, 0, -1);
info_message = true; // use os_msg(), not os_errmsg() info_message = true; // use os_msg(), not os_errmsg()
list_version(); list_version();
msg_putchar('\n'); msg_putchar('\n');