Skip to content
Snippets Groups Projects
0001-picodata-make-tarantool-callable-over-ffi-with-a-c-c.patch 3.28 KiB
Newer Older
From 8e1caeae48316c8464a769a1d224f18a930f4570 Mon Sep 17 00:00:00 2001
From: Georgy Moshkin <gmoshkin@picodata.io>
Date: Thu, 14 Jul 2022 17:06:36 +0300
Subject: [PATCH] picodata: make tarantool callable over ffi with a c callback
 for setup

- tarantool is now a static library
- main(argc, argv) -> tarantool_main(argc, argv, cb, cb_data)
- callback is called before running lua script
---
 src/CMakeLists.txt |  2 +-
 src/lua/init.c     | 12 ++++++++++--
 src/lua/init.h     |  3 ++-
 src/main.cc        |  5 +++--
 4 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 6fca3a888..a7800f0e7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -331,7 +331,7 @@ add_custom_command(
             ${EXPORT_LIST}
 )
 
-add_executable(
+add_library(
     tarantool main.cc
     ${LIBUTIL_FREEBSD_SRC}/flopen.c
     ${LIBUTIL_FREEBSD_SRC}/pidfile.c)
diff --git a/src/lua/init.c b/src/lua/init.c
index d3a265eb8..e066141b4 100644
--- a/src/lua/init.c
+++ b/src/lua/init.c
@@ -871,9 +871,16 @@ run_script_f(va_list ap)
 	 * never really dead. It never returns from its function.
 	 */
 	struct diag *diag = va_arg(ap, struct diag *);
+	void (*cb)(void *) = va_arg(ap, void (*)(void *));
+	void *cb_data = va_arg(ap, void *);
 	bool aux_loop_is_run = false;
 	bool is_option_e_ran = false;
 
+	if (cb) {
+		cb(cb_data);
+		lua_settop(L, 0);
+	}
+
 	/*
 	 * Load libraries and execute chunks passed by -l and -e
 	 * command line options
@@ -988,7 +995,8 @@ error:
 
 int
 tarantool_lua_run_script(char *path, bool interactive,
-			 int optc, const char **optv, int argc, char **argv)
+			 int optc, const char **optv, int argc, char **argv,
+			 void (*cb)(void *), void *cb_data)
 {
 	const char *title = path ? basename(path) : "interactive";
 	/*
@@ -1012,7 +1020,7 @@ tarantool_lua_run_script(char *path, bool interactive,
 	struct diag script_diag;
 	diag_create(&script_diag);
 	fiber_start(script_fiber, tarantool_L, path, interactive,
-		    optc, optv, argc, argv, &script_diag);
+		    optc, optv, argc, argv, &script_diag, cb, cb_data);
 
 	/*
 	 * Run an auxiliary event loop to re-schedule run_script fiber.
diff --git a/src/lua/init.h b/src/lua/init.h
index 7fc0b1a31..3b506a94b 100644
--- a/src/lua/init.h
+++ b/src/lua/init.h
@@ -73,7 +73,8 @@ tarantool_lua_free();
 int
 tarantool_lua_run_script(char *path, bool force_interactive,
 			 int optc, const char **optv,
-			 int argc, char **argv);
+			 int argc, char **argv,
+			 void (*cb)(void *), void *cb_data);
 
 extern char *history;
 
diff --git a/src/main.cc b/src/main.cc
index a424073ed..af4ca7247 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -618,8 +618,9 @@ print_help(const char *program)
 	puts("to see online documentation, submit bugs or contribute a patch.");
 }
 
+extern "C"
 int
-main(int argc, char **argv)
+tarantool_main(int argc, char **argv, void (*cb)(void *), void *cb_data)
 {
 	/* set locale to make iswXXXX function work */
 	if (setlocale(LC_CTYPE, "C.UTF-8") == NULL &&
@@ -787,7 +788,7 @@ main(int argc, char **argv)
 		 * initialized.
 		 */
 		if (tarantool_lua_run_script(script, interactive, optc, optv,
-					     main_argc, main_argv) != 0)
+					     main_argc, main_argv, cb, cb_data) != 0)
 			diag_raise();
 		/*
 		 * Start event loop after executing Lua script if signal_cb()
-- 
2.25.1