Skip to content

Commit 74440ed

Browse files
committed
Move V8JSG extensions and v8_flags to process globals
1 parent 7d97c97 commit 74440ed

File tree

4 files changed

+81
-32
lines changed

4 files changed

+81
-32
lines changed

php_v8js_macros.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,8 @@ void v8js_register_accessors(std::vector<v8js_accessor_ctx*> *accessor_list, v8:
113113
ZEND_BEGIN_MODULE_GLOBALS(v8js)
114114
// Thread-local cache whether V8 has been initialized so far
115115
int v8_initialized;
116-
HashTable *extensions;
117116

118117
/* Ini globals */
119-
char *v8_flags; /* V8 command line flags */
120118
bool use_date; /* Generate JS Date objects instead of PHP DateTime */
121119
bool use_array_access; /* Convert ArrayAccess, Countable objects to array-like objects */
122120
bool compat_php_exceptions; /* Don't stop JS execution on PHP exception */
@@ -149,6 +147,8 @@ ZEND_EXTERN_MODULE_GLOBALS(v8js)
149147
*
150148
* - whether V8 has been initialized at all
151149
* - the V8 backend platform
150+
* - loaded extensions
151+
* - V8 "command line" flags
152152
*
153153
* In a ZTS-enabled environment access to all of these variables must happen
154154
* while holding a mutex lock.
@@ -159,6 +159,11 @@ struct _v8js_process_globals {
159159
std::mutex lock;
160160
#endif
161161

162+
HashTable *extensions;
163+
164+
/* V8 command line flags */
165+
char *v8_flags;
166+
162167
#if !defined(_WIN32) && PHP_V8_API_VERSION >= 3029036
163168
v8::Platform *v8_platform;
164169
#endif

v8js.cc

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,35 @@ struct _v8js_process_globals v8js_process_globals;
3535

3636
static ZEND_INI_MH(v8js_OnUpdateV8Flags) /* {{{ */
3737
{
38+
bool immutable = false;
39+
40+
#ifdef ZTS
41+
v8js_process_globals.lock.lock();
42+
43+
if(v8js_process_globals.v8_initialized) {
44+
v8js_process_globals.lock.unlock();
45+
immutable = true;
46+
}
47+
48+
v8js_process_globals.lock.unlock();
49+
#else
50+
immutable = V8JSG(v8_initialized);
51+
#endif
52+
53+
if(immutable) {
54+
/* V8 already has been initialized -> cannot be changed anymore */
55+
return FAILURE;
56+
}
57+
3858
if (new_value) {
39-
if (V8JSG(v8_flags)) {
40-
free(V8JSG(v8_flags));
41-
V8JSG(v8_flags) = NULL;
59+
if (v8js_process_globals.v8_flags) {
60+
free(v8js_process_globals.v8_flags);
61+
v8js_process_globals.v8_flags = NULL;
4262
}
4363
if (!new_value[0]) {
4464
return FAILURE;
4565
}
46-
V8JSG(v8_flags) = zend_strndup(new_value, new_value_length);
66+
v8js_process_globals.v8_flags = zend_strndup(new_value, new_value_length);
4767
}
4868

4969
return SUCCESS;
@@ -129,6 +149,17 @@ static PHP_MSHUTDOWN_FUNCTION(v8js)
129149
#endif
130150
}
131151

152+
if (v8js_process_globals.v8_flags) {
153+
free(v8js_process_globals.v8_flags);
154+
v8js_process_globals.v8_flags = NULL;
155+
}
156+
157+
if (v8js_process_globals.extensions) {
158+
zend_hash_destroy(v8js_process_globals.extensions);
159+
free(v8js_process_globals.extensions);
160+
v8js_process_globals.extensions = NULL;
161+
}
162+
132163
return SUCCESS;
133164
}
134165
/* }}} */
@@ -180,9 +211,7 @@ static PHP_GINIT_FUNCTION(v8js)
180211
run the destructors manually.
181212
*/
182213
#ifdef ZTS
183-
v8js_globals->extensions = NULL;
184214
v8js_globals->v8_initialized = 0;
185-
v8js_globals->v8_flags = NULL;
186215

187216
v8js_globals->timer_thread = NULL;
188217
v8js_globals->timer_stop = false;
@@ -198,17 +227,6 @@ static PHP_GINIT_FUNCTION(v8js)
198227
*/
199228
static PHP_GSHUTDOWN_FUNCTION(v8js)
200229
{
201-
if (v8js_globals->extensions) {
202-
zend_hash_destroy(v8js_globals->extensions);
203-
free(v8js_globals->extensions);
204-
v8js_globals->extensions = NULL;
205-
}
206-
207-
if (v8js_globals->v8_flags) {
208-
free(v8js_globals->v8_flags);
209-
v8js_globals->v8_flags = NULL;
210-
}
211-
212230
#ifdef ZTS
213231
v8js_globals->timer_stack.~deque();
214232
v8js_globals->timer_mutex.~mutex();

v8js_class.cc

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,17 @@ static int v8js_register_extension(char *name, uint name_len, char *source, uint
827827
{
828828
v8js_jsext *jsext = NULL;
829829

830-
if (!V8JSG(extensions)) {
831-
V8JSG(extensions) = (HashTable *) malloc(sizeof(HashTable));
832-
zend_hash_init(V8JSG(extensions), 1, NULL, (dtor_func_t) v8js_jsext_dtor, 1);
833-
} else if (zend_hash_exists(V8JSG(extensions), name, name_len + 1)) {
830+
#ifdef ZTS
831+
v8js_process_globals.lock.lock();
832+
#endif
833+
834+
if (!v8js_process_globals.extensions) {
835+
v8js_process_globals.extensions = (HashTable *) malloc(sizeof(HashTable));
836+
zend_hash_init(v8js_process_globals.extensions, 1, NULL, (dtor_func_t) v8js_jsext_dtor, 1);
837+
} else if (zend_hash_exists(v8js_process_globals.extensions, name, name_len + 1)) {
838+
#ifdef ZTS
839+
v8js_process_globals.lock.unlock();
840+
#endif
834841
return FAILURE;
835842
}
836843

@@ -843,6 +850,9 @@ static int v8js_register_extension(char *name, uint name_len, char *source, uint
843850
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid dependency array passed");
844851
v8js_jsext_dtor(jsext);
845852
free(jsext);
853+
#ifdef ZTS
854+
v8js_process_globals.lock.unlock();
855+
#endif
846856
return FAILURE;
847857
}
848858
}
@@ -859,17 +869,23 @@ static int v8js_register_extension(char *name, uint name_len, char *source, uint
859869

860870
jsext->extension = new v8::Extension(jsext->name, jsext->source, jsext->deps_count, jsext->deps);
861871

862-
if (zend_hash_add(V8JSG(extensions), name, name_len + 1, jsext, sizeof(v8js_jsext), NULL) == FAILURE) {
872+
if (zend_hash_add(v8js_process_globals.extensions, name, name_len + 1, jsext, sizeof(v8js_jsext), NULL) == FAILURE) {
863873
v8js_jsext_dtor(jsext);
864874
free(jsext);
875+
#ifdef ZTS
876+
v8js_process_globals.lock.unlock();
877+
#endif
865878
return FAILURE;
866879
}
867880

881+
#ifdef ZTS
882+
v8js_process_globals.lock.unlock();
883+
#endif
884+
868885
jsext->extension->set_auto_enable(auto_enable ? true : false);
869886
v8::RegisterExtension(jsext->extension);
870887

871888
free(jsext);
872-
873889
return SUCCESS;
874890
}
875891
/* }}} */
@@ -916,10 +932,15 @@ static PHP_METHOD(V8Js, getExtensions)
916932
}
917933

918934
array_init(return_value);
919-
if (V8JSG(extensions)) {
920-
zend_hash_internal_pointer_reset_ex(V8JSG(extensions), &pos);
921-
while (zend_hash_get_current_data_ex(V8JSG(extensions), (void **) &jsext, &pos) == SUCCESS) {
922-
if (zend_hash_get_current_key_ex(V8JSG(extensions), &key, &key_len, &index, 0, &pos) == HASH_KEY_IS_STRING) {
935+
936+
#ifdef ZTS
937+
v8js_process_globals.lock.lock();
938+
#endif
939+
940+
if (v8js_process_globals.extensions) {
941+
zend_hash_internal_pointer_reset_ex(v8js_process_globals.extensions, &pos);
942+
while (zend_hash_get_current_data_ex(v8js_process_globals.extensions, (void **) &jsext, &pos) == SUCCESS) {
943+
if (zend_hash_get_current_key_ex(v8js_process_globals.extensions, &key, &key_len, &index, 0, &pos) == HASH_KEY_IS_STRING) {
923944
MAKE_STD_ZVAL(ext)
924945
array_init(ext);
925946
add_assoc_bool_ex(ext, ZEND_STRS("auto_enable"), jsext->auto_enable);
@@ -931,9 +952,13 @@ static PHP_METHOD(V8Js, getExtensions)
931952
}
932953
add_assoc_zval_ex(return_value, key, key_len, ext);
933954
}
934-
zend_hash_move_forward_ex(V8JSG(extensions), &pos);
955+
zend_hash_move_forward_ex(v8js_process_globals.extensions, &pos);
935956
}
936957
}
958+
959+
#ifdef ZTS
960+
v8js_process_globals.lock.unlock();
961+
#endif
937962
}
938963
/* }}} */
939964

v8js_v8.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ void v8js_v8_init(TSRMLS_D) /* {{{ */
6161
#endif
6262

6363
/* Set V8 command line flags (must be done before V8::Initialize()!) */
64-
if (V8JSG(v8_flags)) {
65-
v8::V8::SetFlagsFromString(V8JSG(v8_flags), strlen(V8JSG(v8_flags)));
64+
if (v8js_process_globals.v8_flags) {
65+
v8::V8::SetFlagsFromString(v8js_process_globals.v8_flags,
66+
strlen(v8js_process_globals.v8_flags));
6667
}
6768

6869
/* Initialize V8 */

0 commit comments

Comments
 (0)