Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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