diff --git a/src/lua/init.cc b/src/lua/init.cc index f01a95f5921bd48018b2ee0a2a48bb069f40d02e..775d5ed865f13f7634d0292f5f141a1f4e9ffa4f 100644 --- a/src/lua/init.cc +++ b/src/lua/init.cc @@ -386,6 +386,8 @@ run_script(va_list ap) { struct lua_State *L = va_arg(ap, struct lua_State *); const char *path = va_arg(ap, const char *); + int argc = va_arg(ap, int); + char **argv = va_arg(ap, char **); /* * Return control to tarantool_lua_run_script. @@ -413,6 +415,9 @@ run_script(va_list ap) lua_remove(L, -2); /* remove package.loaded */ } try { + lua_checkstack(L, argc - 1); + for (int i = 1; i < argc; i++) + lua_pushstring(L, argv[i]); lbox_call(L, lua_gettop(L) - 1, 0); } catch (Exception *e) { panic("%s", e->errmsg()); @@ -428,7 +433,7 @@ run_script(va_list ap) } void -tarantool_lua_run_script(char *path) +tarantool_lua_run_script(char *path, int argc, char **argv) { const char *title = path ? basename(path) : "interactive"; /* @@ -439,7 +444,7 @@ tarantool_lua_run_script(char *path) * a separate fiber. */ script_fiber = fiber_new(title, run_script); - fiber_call(script_fiber, tarantool_L, path); + fiber_call(script_fiber, tarantool_L, path, argc, argv); /* * Run an auxiliary event loop to re-schedule run_script fiber. diff --git a/src/lua/init.h b/src/lua/init.h index 57ce8d47a0b99768886370318434741d8d269426..8e0489ea99694808e4e3c97a9e8e899a5b1e461d 100644 --- a/src/lua/init.h +++ b/src/lua/init.h @@ -74,7 +74,7 @@ tarantool_lua_tostring(struct lua_State *L, int index); * @param L is a Lua State. */ void -tarantool_lua_run_script(char *path); +tarantool_lua_run_script(char *path, int argc, char **argv); void tarantool_lua(struct lua_State *L, diff --git a/src/tarantool.cc b/src/tarantool.cc index 92174466a75f27124e4032c083d5759df402e310..8ee06c3ad41c3f323430012ae97097d83bce314d 100644 --- a/src/tarantool.cc +++ b/src/tarantool.cc @@ -648,7 +648,7 @@ main(int argc, char **argv) * is why script must run only after the server was fully * initialized. */ - tarantool_lua_run_script(script); + tarantool_lua_run_script(script, main_argc, main_argv); /* * Start event loop after executing Lua script if signal_cb() * wasn't triggered and there is some new events. Initial value diff --git a/test/app/minimal.result b/test/app/minimal.result index 8ab686eafeb1f44702738c8b0f24f2567c36da6d..e09f5358edb2ee140741e5f751f8445782876bf8 100644 --- a/test/app/minimal.result +++ b/test/app/minimal.result @@ -1 +1,5 @@ +arg[-1] tarantool +arg[0] ./script.lua +arg 1 2 3 +... 1 2 3 Hello, World! diff --git a/test/app/minimal.test.lua b/test/app/minimal.test.lua index 9c35806e20a9f15c2b987302ef8013f2a7dc3f0a..8d07ecb0234f8759a1053be986e125a07e68934e 100755 --- a/test/app/minimal.test.lua +++ b/test/app/minimal.test.lua @@ -1,3 +1,20 @@ #!/usr/bin/env tarantool print('Hello, World!') + +-- +-- Command-line argument handling +-- +local script = io.open('script.lua', 'w') +script:write([[ +-- Tarantool binary +print('arg[-1]', arg[-1]:match('tarantool')) +-- Script name +print('arg[0] ', arg[0]) +-- Command-line arguments +print('arg', arg[1], arg[2], arg[3]) +print('...', ...) +]]) +script:close() + +os.execute("tarantool ./script.lua 1 2 3")