Skip to content

Commit 51c38e8

Browse files
committed
lua: use require() to pull built-in modules
It is planned to allow to overload a built-in module by an external one. The implementation assumes storing of the built-in modules in a table that is different from `package.loaded`. It means that accessing a module as `package.loaded[<module_name>]` may fail. However we still have a guarantee that a registered module is accessible using `require()`. Use it for accessing built-in modules from our code. Notes on different modules: * `console` Reorder the print/log callbacks to place the print callback first. If something is going very wrong (say, someone removes the internal.print module), things will be consistent: both callback will not be set. * `session` `box` is accessible as `require('box')` and as `_G.box`. Use the latter. src/lua/serializer.c and src/lua/digest.c also access `package.loaded` directly. I left them out of scope here, it requires a bit more in-depth look. Part of tarantool#7774 NO_DOC=user visible behavior is unchanged, pure refactoring change NO_TEST=see NO_DOC NO_CHANGELOG=see NO_DOC
1 parent 76ee57b commit 51c38e8

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

src/box/lua/console.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,18 +421,19 @@ luaT_console_setup_write_cb(struct lua_State *L)
421421
if (!console_hide_show_prompt_is_enabled())
422422
return;
423423

424-
say_set_stderr_callback(console_hide_prompt, console_show_prompt);
425-
426-
lua_getfield(L, LUA_GLOBALSINDEX, "package");
427-
lua_getfield(L, -1, "loaded");
428-
lua_getfield(L, -1, "internal.print");
424+
lua_getfield(L, LUA_GLOBALSINDEX, "require");
425+
lua_pushstring(L, "internal.print");
426+
if (luaT_call(L, 1, 1) != 0)
427+
return;
429428

430429
lua_rawgeti(L, LUA_REGISTRYINDEX, console_hide_prompt_ref);
431430
lua_setfield(L, -2, "before_cb");
432431
lua_rawgeti(L, LUA_REGISTRYINDEX, console_show_prompt_ref);
433432
lua_setfield(L, -2, "after_cb");
434433

435-
lua_pop(L, 3);
434+
lua_pop(L, 1);
435+
436+
say_set_stderr_callback(console_hide_prompt, console_show_prompt);
436437
}
437438

438439
static void
@@ -441,18 +442,19 @@ luaT_console_cleanup_write_cb(struct lua_State *L)
441442
if (!console_hide_show_prompt_is_enabled())
442443
return;
443444

444-
say_set_stderr_callback(NULL, NULL);
445-
446-
lua_getfield(L, LUA_GLOBALSINDEX, "package");
447-
lua_getfield(L, -1, "loaded");
448-
lua_getfield(L, -1, "internal.print");
445+
lua_getfield(L, LUA_GLOBALSINDEX, "require");
446+
lua_pushstring(L, "internal.print");
447+
if (luaT_call(L, 1, 1) != 0)
448+
return;
449449

450450
lua_pushnil(L);
451451
lua_setfield(L, -2, "before_cb");
452452
lua_pushnil(L);
453453
lua_setfield(L, -2, "after_cb");
454454

455-
lua_pop(L, 3);
455+
lua_pop(L, 1);
456+
457+
say_set_stderr_callback(NULL, NULL);
456458
}
457459

458460
/* }}} Show/hide prompt */

src/box/lua/session.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,7 @@ session_storage_cleanup(int sid)
509509
int top = lua_gettop(L);
510510

511511
if (ref == LUA_REFNIL) {
512-
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
513-
if (!lua_istable(L, -1))
514-
goto exit;
515-
lua_getfield(L, -1, "box");
512+
lua_getfield(L, LUA_GLOBALSINDEX, "box");
516513
if (!lua_istable(L, -1))
517514
goto exit;
518515
lua_getfield(L, -1, "session");

src/lua/init.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -977,9 +977,10 @@ run_script_f(va_list ap)
977977
* Execute scripts or modules pointed by TT_PRELOAD
978978
* environment variable.
979979
*/
980-
lua_getfield(L, LUA_GLOBALSINDEX, "package");
981-
lua_getfield(L, -1, "loaded");
982-
lua_getfield(L, -1, "tarantool");
980+
lua_getfield(L, LUA_GLOBALSINDEX, "require");
981+
lua_pushstring(L, "tarantool");
982+
if (luaT_call(L, 1, 1) != 0)
983+
goto error;
983984
lua_getfield(L, -1, "_internal");
984985
lua_getfield(L, -1, "run_preload");
985986
if (luaT_call(L, 0, 0) != 0)
@@ -1057,12 +1058,13 @@ run_script_f(va_list ap)
10571058
if (interactive) {
10581059
say_crit("%s %s\ntype 'help' for interactive help",
10591060
tarantool_package(), tarantool_version());
1060-
/* get console.start from package.loaded */
1061-
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
1062-
lua_getfield(L, -1, "console");
1061+
/* get console.start */
1062+
lua_getfield(L, LUA_GLOBALSINDEX, "require");
1063+
lua_pushstring(L, "console");
1064+
if (luaT_call(L, 1, 1) != 0)
1065+
goto error;
10631066
lua_getfield(L, -1, "start");
1064-
lua_remove(L, -2); /* remove package.loaded.console */
1065-
lua_remove(L, -2); /* remove package.loaded */
1067+
lua_remove(L, -2); /* remove console */
10661068
start_loop = false;
10671069
if (lua_main(L, argc, argv) != 0)
10681070
goto error;

0 commit comments

Comments
 (0)