From 94f4e1eccce159c2bb6b60e89cde5590c73fb0c5 Mon Sep 17 00:00:00 2001 From: Roman Tsisyk <roman@tsisyk.com> Date: Mon, 19 Jan 2015 18:30:03 +0300 Subject: [PATCH] Fix #631: Command line arguments like vanilla Lua --- src/lua/init.cc | 9 +++++++-- src/lua/init.h | 2 +- src/tarantool.cc | 2 +- test/app/minimal.result | 4 ++++ test/app/minimal.test.lua | 17 +++++++++++++++++ 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/lua/init.cc b/src/lua/init.cc index f01a95f592..775d5ed865 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 57ce8d47a0..8e0489ea99 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 92174466a7..8ee06c3ad4 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 8ab686eafe..e09f5358ed 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 9c35806e20..8d07ecb023 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") -- GitLab