-
Notifications
You must be signed in to change notification settings - Fork 200
/
v8js_main.cc
295 lines (246 loc) · 6.62 KB
/
v8js_main.cc
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Jani Taskinen <[email protected]> |
| Author: Patrick Reilly <[email protected]> |
| Author: Stefan Siegl <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_v8js_macros.h"
extern "C" {
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
}
#include "v8js_class.h"
#include "v8js_exceptions.h"
#include "v8js_v8object_class.h"
ZEND_DECLARE_MODULE_GLOBALS(v8js)
struct _v8js_process_globals v8js_process_globals;
/* {{{ INI Settings */
static bool v8js_ini_string(char **field, const zend_string *new_value)/* {{{ */
{
bool immutable = false;
#ifdef ZTS
v8js_process_globals.lock.lock();
if(v8js_process_globals.v8_initialized) {
v8js_process_globals.lock.unlock();
immutable = true;
}
v8js_process_globals.lock.unlock();
#else
immutable = V8JSG(v8_initialized);
#endif
if(immutable) {
/* V8 already has been initialized -> cannot be changed anymore */
return FAILURE;
}
if (new_value) {
if (*field) {
free(*field);
*field = NULL;
}
if (!ZSTR_VAL(new_value)[0]) {
return SUCCESS;
}
*field = zend_strndup(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
}
return SUCCESS;
}
/* }}} */
static ZEND_INI_MH(v8js_OnUpdateV8Flags) /* {{{ */
{
return v8js_ini_string(&v8js_process_globals.v8_flags, new_value);
}
/* }}} */
static ZEND_INI_MH(v8js_OnUpdateIcudatPath) /* {{{ */
{
return v8js_ini_string(&v8js_process_globals.icudtl_dat_path, new_value);
}
/* }}} */
static bool v8js_ini_to_bool(const zend_string *new_value) /* {{{ */
{
if (ZSTR_LEN(new_value) == 2 && strcasecmp("on", ZSTR_VAL(new_value)) == 0) {
return true;
} else if (ZSTR_LEN(new_value) == 3 && strcasecmp("yes", ZSTR_VAL(new_value)) == 0) {
return true;
} else if (ZSTR_LEN(new_value) == 4 && strcasecmp("true", ZSTR_VAL(new_value)) == 0) {
return true;
} else {
return 0 != atoi(ZSTR_VAL(new_value));
}
}
/* }}} */
static ZEND_INI_MH(v8js_OnUpdateUseDate) /* {{{ */
{
V8JSG(use_date) = v8js_ini_to_bool(new_value);
return SUCCESS;
}
/* }}} */
static ZEND_INI_MH(v8js_OnUpdateUseArrayAccess) /* {{{ */
{
V8JSG(use_array_access) = v8js_ini_to_bool(new_value);
return SUCCESS;
}
/* }}} */
ZEND_INI_BEGIN() /* {{{ */
ZEND_INI_ENTRY("v8js.flags", NULL, ZEND_INI_ALL, v8js_OnUpdateV8Flags)
ZEND_INI_ENTRY("v8js.icudtl_dat_path", NULL, ZEND_INI_ALL, v8js_OnUpdateIcudatPath)
ZEND_INI_ENTRY("v8js.use_date", "0", ZEND_INI_ALL, v8js_OnUpdateUseDate)
ZEND_INI_ENTRY("v8js.use_array_access", "0", ZEND_INI_ALL, v8js_OnUpdateUseArrayAccess)
ZEND_INI_END()
/* }}} */
/* }}} INI */
#ifdef COMPILE_DL_V8JS
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
ZEND_GET_MODULE(v8js)
#endif
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(v8js)
{
PHP_MINIT(v8js_class)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(v8js_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(v8js_v8object_class)(INIT_FUNC_ARGS_PASSTHRU);
REGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
static PHP_MSHUTDOWN_FUNCTION(v8js)
{
UNREGISTER_INI_ENTRIES();
bool v8_initialized;
#ifdef ZTS
v8_initialized = v8js_process_globals.v8_initialized;
#else
v8_initialized = V8JSG(v8_initialized);
#endif
if(v8_initialized) {
v8::V8::Dispose();
#if PHP_V8_API_VERSION >= 10000000
v8::V8::DisposePlatform();
#else
v8::V8::ShutdownPlatform();
#endif
// @fixme call virtual destructor somehow
//delete v8js_process_globals.v8_platform;
}
if (v8js_process_globals.v8_flags) {
free(v8js_process_globals.v8_flags);
v8js_process_globals.v8_flags = NULL;
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
static PHP_RSHUTDOWN_FUNCTION(v8js)
{
// If the timer thread is running then stop it
if (V8JSG(timer_thread)) {
V8JSG(timer_stop) = true;
V8JSG(timer_thread)->join();
V8JSG(timer_stop) = false;
delete V8JSG(timer_thread);
V8JSG(timer_thread) = NULL;
}
V8JSG(fatal_error_abort) = 0;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
static PHP_MINFO_FUNCTION(v8js)
{
php_info_print_table_start();
php_info_print_table_header(2, "V8 Javascript Engine", "enabled");
php_info_print_table_header(2, "V8 Engine Compiled Version", PHP_V8_VERSION);
php_info_print_table_header(2, "V8 Engine Linked Version", v8::V8::GetVersion());
php_info_print_table_header(2, "Version", PHP_V8JS_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ PHP_GINIT_FUNCTION
*/
static PHP_GINIT_FUNCTION(v8js)
{
#if defined(COMPILE_DL_MYSQLI) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
/*
If ZTS is disabled, the v8js_globals instance is declared right
in the BSS and hence automatically initialized by C++ compiler.
Most of the variables are just zeroed.
If ZTS is enabled however, v8js_globals just points to a freshly
allocated, uninitialized piece of memory, hence we need to
initialize all fields on our own. Likewise on shutdown we have to
run the destructors manually.
*/
#ifdef ZTS
v8js_globals->v8_initialized = 0;
v8js_globals->timer_thread = NULL;
v8js_globals->timer_stop = false;
new(&v8js_globals->timer_mutex) std::mutex;
new(&v8js_globals->timer_stack) std::deque<v8js_timer_ctx *>;
v8js_globals->fatal_error_abort = 0;
#endif
}
/* }}} */
/* {{{ PHP_GSHUTDOWN_FUNCTION
*/
static PHP_GSHUTDOWN_FUNCTION(v8js)
{
#ifdef ZTS
v8js_globals->timer_stack.~deque();
v8js_globals->timer_mutex.~mutex();
#endif
}
/* }}} */
/* {{{ v8js_functions[] */
static const zend_function_entry v8js_functions[] = {
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ v8js_module_entry
*/
zend_module_entry v8js_module_entry = {
STANDARD_MODULE_HEADER_EX,
NULL,
NULL,
"v8js",
v8js_functions,
PHP_MINIT(v8js),
PHP_MSHUTDOWN(v8js),
NULL,
PHP_RSHUTDOWN(v8js),
PHP_MINFO(v8js),
PHP_V8JS_VERSION,
PHP_MODULE_GLOBALS(v8js),
PHP_GINIT(v8js),
PHP_GSHUTDOWN(v8js),
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/