Skip to content

Commit f87cf9f

Browse files
authored
Merge pull request #311 from stesie/fix-deprecated
Fix deprecated V8 API calls
2 parents 98c2a6b + 13e0b77 commit f87cf9f

10 files changed

+53
-37
lines changed

Vagrantfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
Vagrant.configure("2") do |config|
99
# Every Vagrant development environment requires a box. You can search for
1010
# boxes at https://atlas.hashicorp.com/search.
11-
config.vm.box = "ubuntu/trusty64"
11+
config.vm.box = "ubuntu/xenial64"
1212

1313
config.vm.provider "lxc" do |lxc, override|
14-
override.vm.box = "fgrehm/trusty64-lxc"
14+
override.vm.box = "zaikin/xenial64-lxc"
1515
end
1616

1717

1818
#
1919
# mass-define "generic" Ubuntu boxes
2020
#
21-
%w{5.1 5.2 5.4 5.7 5.8 5.9 6.0}.each { |version|
21+
%w{5.7 5.8 5.9 6.0}.each { |version|
2222
config.vm.define "v8-#{version}" do |i|
2323
i.vm.synced_folder ".", "/data/v8js"
2424

config.m4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ int main ()
188188
LDFLAGS="$old_LDFLAGS"
189189
CPPFLAGS=$old_CPPFLAGS
190190

191+
AC_DEFINE([V8_DEPRECATION_WARNINGS], [1], [Enable compiler warnings when using V8_DEPRECATED apis.])
192+
191193
PHP_ADD_INCLUDE($V8_DIR)
192194
PHP_NEW_EXTENSION(v8js, [ \
193195
v8js_array_access.cc \

v8js_class.cc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,7 @@ static PHP_METHOD(V8Js, __construct)
410410
v8::HandleScope handle_scope(isolate);
411411

412412
/* Redirect fatal errors to PHP error handler */
413-
// This needs to be done within the context isolate
414-
v8::V8::SetFatalErrorHandler(v8js_fatal_error_handler);
413+
isolate->SetFatalErrorHandler(v8js_fatal_error_handler);
415414

416415
/* Create global template for global object */
417416
// Now we are using multiple isolates this needs to be created for every context
@@ -486,7 +485,7 @@ static PHP_METHOD(V8Js, __construct)
486485
/* Add the PHP object into global object */
487486
php_obj_t->InstanceTemplate()->SetInternalFieldCount(2);
488487
v8::Local<v8::Object> php_obj = php_obj_t->InstanceTemplate()->NewInstance();
489-
V8JS_GLOBAL(isolate)->ForceSet(object_name_js, php_obj, v8::ReadOnly);
488+
V8JS_GLOBAL(isolate)->DefineOwnProperty(context, object_name_js, php_obj, v8::ReadOnly);
490489

491490
/* Export public property values */
492491
HashTable *properties = zend_std_get_properties(getThis());
@@ -504,12 +503,12 @@ static PHP_METHOD(V8Js, __construct)
504503
return;
505504
}
506505

507-
v8::Local<v8::Value> key = v8::String::NewFromUtf8(isolate, ZSTR_VAL(member),
506+
v8::Local<v8::Name> key = v8::String::NewFromUtf8(isolate, ZSTR_VAL(member),
508507
v8::String::kInternalizedString, static_cast<int>(ZSTR_LEN(member)));
509508

510509
/* Write value to PHP JS object */
511510
value = OBJ_PROP(Z_OBJ_P(getThis()), property_info->offset);
512-
php_obj->ForceSet(key, zval_to_v8js(value, isolate), v8::ReadOnly);
511+
php_obj->DefineOwnProperty(context, key, zval_to_v8js(value, isolate), v8::ReadOnly);
513512
}
514513
} ZEND_HASH_FOREACH_END();
515514

@@ -584,7 +583,7 @@ static PHP_METHOD(V8Js, __construct)
584583
persistent_ft->Reset(isolate, ft);
585584
}
586585

587-
php_obj->ForceSet(method_name, ft->GetFunction());
586+
php_obj->CreateDataProperty(context, method_name, ft->GetFunction());
588587
} ZEND_HASH_FOREACH_END();
589588
}
590589
/* }}} */
@@ -616,7 +615,7 @@ static void v8js_compile_script(zval *this_ptr, const zend_string *str, const ze
616615
V8JS_BEGIN_CTX(c, this_ptr)
617616

618617
/* Catch JS exceptions */
619-
v8::TryCatch try_catch;
618+
v8::TryCatch try_catch(isolate);
620619

621620
/* Set script identifier */
622621
if (identifier && ZSTR_LEN(identifier) > std::numeric_limits<int>::max()) {
@@ -1289,8 +1288,8 @@ static void v8js_write_property(zval *object, zval *member, zval *value, void **
12891288
}
12901289

12911290
/* Write value to PHP JS object */
1292-
v8::Local<v8::Value> key = V8JS_SYML(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member)));
1293-
jsobj->ForceSet(key, zval_to_v8js(value, isolate), v8::ReadOnly);
1291+
v8::Local<v8::Name> key = V8JS_SYML(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member)));
1292+
jsobj->DefineOwnProperty(v8_context, key, zval_to_v8js(value, isolate), v8::ReadOnly);
12941293
}
12951294

12961295
/* Write value to PHP object */

v8js_exceptions.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void v8js_create_script_exception(zval *return_value, v8::Isolate *isolate, v8::
4444
v8::Local<v8::Message> tc_message = try_catch->Message();
4545
const char *filename_string, *sourceline_string;
4646
char *message_string;
47-
int linenum, start_col, end_col;
47+
int linenum, start_col;
4848

4949
object_init_ex(return_value, php_ce_v8js_script_exception);
5050

@@ -70,8 +70,10 @@ void v8js_create_script_exception(zval *return_value, v8::Isolate *isolate, v8::
7070
start_col = tc_message->GetStartColumn();
7171
PHPV8_EXPROP(_long, JsStartColumn, start_col);
7272

73-
end_col = tc_message->GetEndColumn();
74-
PHPV8_EXPROP(_long, JsEndColumn, end_col);
73+
v8::Maybe<int> end_col = tc_message->GetEndColumn(isolate->GetEnteredContext());
74+
if (end_col.IsJust()) {
75+
PHPV8_EXPROP(_long, JsEndColumn, end_col.FromJust());
76+
}
7577

7678
spprintf(&message_string, 0, "%s:%d: %s", filename_string, linenum, exception_string);
7779

v8js_generator_export.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ v8::Local<v8::Value> v8js_wrap_generator(v8::Isolate *isolate, v8::Local<v8::Val
2424
assert(!wrapped_object.IsEmpty());
2525
assert(wrapped_object->IsObject());
2626

27-
v8::TryCatch try_catch;
27+
v8::TryCatch try_catch(isolate);
2828
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, "(\
2929
function(wrapped_object) { \
3030
return (function*() { \

v8js_methods.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ static void v8js_dumper(v8::Isolate *isolate, v8::Local<v8::Value> var, int leve
9292
return;
9393
}
9494

95-
v8::TryCatch try_catch; /* object.toString() can throw an exception */
95+
v8::TryCatch try_catch(isolate); /* object.toString() can throw an exception */
9696
v8::Local<v8::String> details;
9797

9898
if(var->IsRegExp()) {
9999
v8::RegExp *re = v8::RegExp::Cast(*var);
100100
details = re->GetSource();
101101
}
102102
else {
103-
details = var->ToDetailString();
103+
details = var->ToDetailString(isolate->GetEnteredContext()).FromMaybe(v8::Local<v8::String>());
104104

105105
if (try_catch.HasCaught()) {
106106
details = V8JS_SYM("<toString threw exception>");
@@ -401,26 +401,26 @@ V8JS_METHOD(require)
401401
}
402402

403403
// Create a template for the global object and set the built-in global functions
404-
v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New();
404+
v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(isolate);
405405
global_template->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(isolate, V8JS_MN(print)), v8::ReadOnly);
406406
global_template->Set(V8JS_SYM("var_dump"), v8::FunctionTemplate::New(isolate, V8JS_MN(var_dump)), v8::ReadOnly);
407407
global_template->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(isolate, V8JS_MN(sleep)), v8::ReadOnly);
408408
global_template->Set(V8JS_SYM("require"), v8::FunctionTemplate::New(isolate, V8JS_MN(require), v8::External::New(isolate, c)), v8::ReadOnly);
409409

410410
// Add the exports object in which the module can return its API
411-
v8::Local<v8::ObjectTemplate> exports_template = v8::ObjectTemplate::New();
411+
v8::Local<v8::ObjectTemplate> exports_template = v8::ObjectTemplate::New(isolate);
412412
global_template->Set(V8JS_SYM("exports"), exports_template);
413413

414414
// Add the module object in which the module can have more fine-grained control over what it can return
415-
v8::Local<v8::ObjectTemplate> module_template = v8::ObjectTemplate::New();
415+
v8::Local<v8::ObjectTemplate> module_template = v8::ObjectTemplate::New(isolate);
416416
module_template->Set(V8JS_SYM("id"), V8JS_STR(normalised_module_id));
417417
global_template->Set(V8JS_SYM("module"), module_template);
418418

419419
// Each module gets its own context so different modules do not affect each other
420420
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, v8::Context::New(isolate, NULL, global_template));
421421

422422
// Catch JS exceptions
423-
v8::TryCatch try_catch;
423+
v8::TryCatch try_catch(isolate);
424424

425425
v8::Locker locker(isolate);
426426
v8::Isolate::Scope isolate_scope(isolate);

v8js_object_export.cc

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,17 @@ static void v8js_invoke_callback(const v8::FunctionCallbackInfo<v8::Value>& info
438438
new_tpl = v8::Local<v8::FunctionTemplate>::New
439439
(isolate, ctx->template_cache.at(ce->name));
440440

441-
result = new_tpl->GetFunction()->NewInstance(argc, argv);
441+
v8::MaybeLocal<v8::Object> maybeResult = new_tpl->GetFunction()->NewInstance(isolate->GetEnteredContext(), argc, argv);
442+
443+
if (!maybeResult.IsEmpty()) {
444+
result = maybeResult.ToLocalChecked();
445+
} else {
446+
result = V8JS_UNDEFINED;
447+
}
442448
} else {
443449
result = cb->Call(self, argc, argv);
444450
}
451+
445452
info.GetReturnValue().Set(result);
446453
}
447454
/* }}} */
@@ -804,7 +811,7 @@ static void v8js_named_property_deleter(v8::Local<v8::String> property, const v8
804811

805812

806813

807-
static v8::Local<v8::Object> v8js_wrap_object(v8::Isolate *isolate, zend_class_entry *ce, zval *value) /* {{{ */
814+
static v8::MaybeLocal<v8::Object> v8js_wrap_object(v8::Isolate *isolate, zend_class_entry *ce, zval *value) /* {{{ */
808815
{
809816
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
810817
v8::Local<v8::FunctionTemplate> new_tpl;
@@ -903,11 +910,11 @@ static v8::Local<v8::Object> v8js_wrap_object(v8::Isolate *isolate, zend_class_e
903910

904911
// Create v8 wrapper object
905912
v8::Local<v8::Value> external = v8::External::New(isolate, Z_OBJ_P(value));
906-
v8::Local<v8::Object> newobj = new_tpl->GetFunction()->NewInstance(1, &external);
913+
v8::MaybeLocal<v8::Object> newobj = new_tpl->GetFunction()->NewInstance(isolate->GetEnteredContext(), 1, &external);
907914

908-
if (ce == zend_ce_closure) {
915+
if (ce == zend_ce_closure && !newobj.IsEmpty()) {
909916
// free uncached function template when object is freed
910-
ctx->weak_closures[persist_tpl_].Reset(isolate, newobj);
917+
ctx->weak_closures[persist_tpl_].Reset(isolate, newobj.ToLocalChecked());
911918
ctx->weak_closures[persist_tpl_].SetWeak(persist_tpl_, v8js_weak_closure_callback, v8::WeakCallbackType::kParameter);
912919
}
913920

@@ -1025,15 +1032,19 @@ v8::Local<v8::Value> v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate) /* {{
10251032

10261033
/* If it's a PHP object, wrap it */
10271034
if (ce) {
1028-
v8::Local<v8::Value> wrapped_object = v8js_wrap_object(isolate, ce, value);
1035+
v8::MaybeLocal<v8::Object> wrapped_object = v8js_wrap_object(isolate, ce, value);
1036+
1037+
if (wrapped_object.IsEmpty()) {
1038+
return V8JS_UNDEFINED;
1039+
}
10291040

10301041
if (ce == zend_ce_generator) {
10311042
/* Wrap PHP Generator object in a wrapper function that provides
10321043
* ES6 style behaviour. */
1033-
wrapped_object = v8js_wrap_generator(isolate, wrapped_object);
1044+
return v8js_wrap_generator(isolate, wrapped_object.ToLocalChecked());
10341045
}
10351046

1036-
return wrapped_object;
1047+
return wrapped_object.ToLocalChecked();
10371048
}
10381049

10391050
/* Associative PHP arrays cannot be wrapped to JS arrays, convert them to

v8js_timer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void v8js_timer_interrupt_handler(v8::Isolate *isolate, void *data) { /*
6767
if (timer_ctx->memory_limit > 0 && hs.used_heap_size() > timer_ctx->memory_limit) {
6868
if (has_sent_notification) {
6969
timer_ctx->killed = true;
70-
v8::V8::TerminateExecution(c->isolate);
70+
c->isolate->TerminateExecution();
7171
c->memory_limit_hit = true;
7272
} else {
7373
// force garbage collection, then check again
@@ -98,7 +98,7 @@ void v8js_timer_thread(zend_v8js_globals *globals) /* {{{ */
9898
}
9999
else if(timer_ctx->time_limit > 0 && now > timer_ctx->time_point) {
100100
timer_ctx->killed = true;
101-
v8::V8::TerminateExecution(c->isolate);
101+
c->isolate->TerminateExecution();
102102
c->time_limit_hit = true;
103103
}
104104
else if (timer_ctx->memory_limit > 0) {

v8js_v8.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value,
120120
V8JSG(timer_mutex).unlock();
121121

122122
/* Catch JS exceptions */
123-
v8::TryCatch try_catch;
123+
v8::TryCatch try_catch(isolate);
124124

125125
/* Set flags for runtime use */
126126
c->flags = flags;
@@ -246,7 +246,7 @@ void v8js_v8_call(v8js_ctx *c, zval **return_value,
246246

247247
void v8js_terminate_execution(v8::Isolate *isolate) /* {{{ */
248248
{
249-
if(v8::V8::IsExecutionTerminating(isolate)) {
249+
if(isolate->IsExecutionTerminating()) {
250250
/* Execution already terminating, needn't trigger it again and
251251
* especially must not execute the spinning loop (which would cause
252252
* crashes in V8 itself, at least with 4.2 and 4.3 version lines). */
@@ -264,7 +264,7 @@ void v8js_terminate_execution(v8::Isolate *isolate) /* {{{ */
264264

265265
v8::Local<v8::String> source = V8JS_STR("for(;;);");
266266
v8::Local<v8::Script> script = v8::Script::Compile(source);
267-
v8::V8::TerminateExecution(isolate);
267+
isolate->TerminateExecution();
268268
script->Run();
269269
}
270270
/* }}} */
@@ -282,7 +282,9 @@ int v8js_get_properties_hash(v8::Local<v8::Value> jsValue, HashTable *retval, in
282282
v8::Local<v8::String> jsKey = jsKeys->Get(i)->ToString();
283283

284284
/* Skip any prototype properties */
285-
if (!jsObj->HasOwnProperty(jsKey) && !jsObj->HasRealNamedProperty(jsKey) && !jsObj->HasRealNamedCallbackProperty(jsKey)) {
285+
if (!jsObj->HasOwnProperty(isolate->GetEnteredContext(), jsKey).FromMaybe(false)
286+
&& !jsObj->HasRealNamedProperty(jsKey)
287+
&& !jsObj->HasRealNamedCallbackProperty(jsKey)) {
286288
continue;
287289
}
288290

v8js_v8object_class.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static void v8js_v8object_write_property(zval *object, zval *member, zval *value
175175
}
176176

177177
if (v8obj->IsObject()) {
178-
v8obj->ToObject()->ForceSet(V8JS_SYML(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member))), zval_to_v8js(value, isolate));
178+
v8obj->ToObject()->CreateDataProperty(v8_context, V8JS_SYML(Z_STRVAL_P(member), static_cast<int>(Z_STRLEN_P(member))), zval_to_v8js(value, isolate));
179179
}
180180
}
181181
/* }}} */

0 commit comments

Comments
 (0)