Skip to content
Snippets Groups Projects
0001-picodata-make-tarantool-callable-over-ffi-with-a-c-c.patch 3.3 KiB
Newer Older
From 4ddd3f313bc7f95aeb5109786a7b4a62ed6dae46 Mon Sep 17 00:00:00 2001
From: Georgy Moshkin <gmoshkin@picodata.io>
Date: Thu, 17 Feb 2022 13:56:53 +0300
Subject: [PATCH 1/3] 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 0a0699d6e..f8a813553 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -273,7 +273,7 @@ if (ENABLE_LTO)
     set_source_files_properties(exports.c PROPERTIES COMPILE_FLAGS -fno-lto)
 endif()
 
-add_executable(
+add_library(
     tarantool main.cc exports.c
     ${LIBUTIL_FREEBSD_SRC}/flopen.c
     ${LIBUTIL_FREEBSD_SRC}/pidfile.c)
diff --git a/src/lua/init.c b/src/lua/init.c
index 21582dd5e..931521f32 100644
--- a/src/lua/init.c
+++ b/src/lua/init.c
@@ -584,9 +584,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
@@ -701,7 +708,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";
 	/*
@@ -725,7 +733,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 de082c17f..a6add06c5 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -587,8 +587,9 @@ print_help(const char *program)
 extern "C" void **
 export_syms(void);
 
+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 &&
@@ -758,7 +759,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