From f9bd8c4328f52b77ac1f7831e5355d77cf141655 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Wed, 17 Apr 2019 18:22:40 +0300 Subject: [PATCH 1/9] Mechanical removal of use of non-maybe String::ToObject() Due to the non-maybe version of String::ToObject() being deprecated and altogether removed from V8 [1] it is necessary to migrate to using the maybe version. This commit is a mechanical change that uses the context at hand when calling String::ToObject() to pass it to it. The resulting MaybeLocal is then unwrapped with MaybeLocal::ToLocalChecked() as I consider the verifications performed on the String instances to be sufficient to ensure no crashes. [1] https://chromium-review.googlesource.com/c/v8/v8/+/1172350/ https://chromium.googlesource.com/v8/v8/+/c8376b0069ebe16c67acf90c3cda3457ddccba4f --- ext/mini_racer_extension/mini_racer_extension.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ext/mini_racer_extension/mini_racer_extension.cc b/ext/mini_racer_extension/mini_racer_extension.cc index e2c11212..277d83bc 100644 --- a/ext/mini_racer_extension/mini_racer_extension.cc +++ b/ext/mini_racer_extension/mini_racer_extension.cc @@ -230,13 +230,13 @@ static void prepare_result(MaybeLocal v8res, Local local_value = v8res.ToLocalChecked(); if ((local_value->IsObject() || local_value->IsArray()) && !local_value->IsDate() && !local_value->IsFunction()) { - Local JSON = context->Global()->Get( - String::NewFromUtf8(isolate, "JSON"))->ToObject(); + Local JSON = context->Global()->Get(String::NewFromUtf8(isolate, "JSON")) + ->ToObject(context).ToLocalChecked(); Local stringify = JSON->Get(v8::String::NewFromUtf8(isolate, "stringify")) .As(); - Local object = local_value->ToObject(); + Local object = local_value->ToObject(context).ToLocalChecked(); const unsigned argc = 1; Local argv[argc] = { object }; MaybeLocal json = stringify->Call(JSON, argc, argv); @@ -418,7 +418,7 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local context, VALUE rb_hash = rb_hash_new(); TryCatch trycatch(isolate); - Local object = value->ToObject(); + Local object = value->ToObject(context).ToLocalChecked(); auto maybe_props = object->GetOwnPropertyNames(context); if (!maybe_props.IsEmpty()) { Local props = maybe_props.ToLocalChecked(); From f68aa1acfbf13b2bd0746a5cf60ef1ce66ac286a Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Wed, 17 Apr 2019 18:36:52 +0300 Subject: [PATCH 2/9] Mechanical removal of use of non-maybe Local::ToString() Due to the non-maybe version of Local::ToString() being deprecated and altogether removed from V8 [1] it is necessary to migrate to using the maybe version. This commit is a mechanical change that uses the context at hand when calling Local::ToString() to pass it to it. The resulting MaybeLocal is then unwrapped with MaybeLocal::ToLocalChecked() as I consider the context of the uses to be sufficiently safe. [1] https://chromium-review.googlesource.com/c/v8/v8/+/1172350/ https://chromium.googlesource.com/v8/v8/+/c8376b0069ebe16c67acf90c3cda3457ddccba4f --- ext/mini_racer_extension/mini_racer_extension.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ext/mini_racer_extension/mini_racer_extension.cc b/ext/mini_racer_extension/mini_racer_extension.cc index 277d83bc..ffc05024 100644 --- a/ext/mini_racer_extension/mini_racer_extension.cc +++ b/ext/mini_racer_extension/mini_racer_extension.cc @@ -274,7 +274,7 @@ static void prepare_result(MaybeLocal v8res, } len = snprintf(buf, sizeof(buf), "%s at %s:%i:%i", *String::Utf8Value(isolate, message->Get()), - *String::Utf8Value(isolate, message->GetScriptResourceName()->ToString()), + *String::Utf8Value(isolate, message->GetScriptResourceName()->ToString(context).ToLocalChecked()), line, column); @@ -293,7 +293,8 @@ static void prepare_result(MaybeLocal v8res, } if (!trycatch.StackTrace(context).IsEmpty()) { evalRes.backtrace = new Persistent(); - evalRes.backtrace->Reset(isolate, trycatch.StackTrace(context).ToLocalChecked()->ToString()); + evalRes.backtrace->Reset(isolate, + trycatch.StackTrace(context).ToLocalChecked()->ToString(context).ToLocalChecked()); } } } @@ -441,7 +442,7 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local context, return rb_hash; } - Local rstr = value->ToString(); + Local rstr = value->ToString(context).ToLocalChecked(); return rb_enc_str_new(*String::Utf8Value(isolate, rstr), rstr->Utf8Length(), rb_enc_find("utf-8")); } From bf129b8f15ed36ab9b344fbd3f77aef7cc8c988b Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Wed, 17 Apr 2019 19:00:07 +0300 Subject: [PATCH 3/9] Mechanical removal of the use of non-maybe Value::Int32Value() and NumberValue() Due to the non-maybe version of Value::Int32Value() and Value::NumberValue() being deprecated and altogether removed from V8 [1] it is necessary to migrate to using the maybe versions. This commit is a mechanical change that uses the context at hand when calling Value::Int32Value() or Value::NumberValue() to pass it to the respective function. The resulting Maybe is then unwrapped with Maybe::ToChecked() as I consider the verifications performed on the Value instances to be sufficient to ensure no crashes. [1] https://chromium-review.googlesource.com/c/v8/v8/+/1172350/ https://chromium.googlesource.com/v8/v8/+/c8376b0069ebe16c67acf90c3cda3457ddccba4f --- ext/mini_racer_extension/mini_racer_extension.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/mini_racer_extension/mini_racer_extension.cc b/ext/mini_racer_extension/mini_racer_extension.cc index ffc05024..82616f94 100644 --- a/ext/mini_racer_extension/mini_racer_extension.cc +++ b/ext/mini_racer_extension/mini_racer_extension.cc @@ -374,11 +374,11 @@ static VALUE convert_v8_to_ruby(Isolate* isolate, Local context, } if (value->IsInt32()) { - return INT2FIX(value->Int32Value()); + return INT2FIX(value->Int32Value(context).ToChecked()); } if (value->IsNumber()) { - return rb_float_new(value->NumberValue()); + return rb_float_new(value->NumberValue(context).ToChecked()); } if (value->IsTrue()) { From 161048b52e1eb626f96f65ef43bbdd1fe1e6d9e2 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Thu, 18 Apr 2019 09:16:12 +0300 Subject: [PATCH 4/9] Remove the uses of deprecated snapshot-related functions Due to V8::CreateSnapshotDataBlob() and V8::WarmUpSnapshotDataBlob() being deprecated and altogether removed from V8 [1] it is necessary to migrate to using local implementations of them. This commit introduces create_snapshot_data_blob(), warm_up_snapshot_data_blob() and the helper function run_extra_code(). Their implementations have been copied over from [2]. [1] https://github.com/v8/v8/commit/b3738e658345adabaa958b9f9a94ca01fc87d5e4 https://chromium-review.googlesource.com/c/v8/v8/+/1019442/ [2] https://github.com/v8/v8/blob/7.3.492.27/test/cctest/test-serialize.cc https://chromium.googlesource.com/v8/v8.git/+/30602560a8fdb0bbfb50d70be867f32b72758a2f/test/cctest/test-serialize.cc --- .../mini_racer_extension.cc | 89 ++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/ext/mini_racer_extension/mini_racer_extension.cc b/ext/mini_racer_extension/mini_racer_extension.cc index 82616f94..7002677a 100644 --- a/ext/mini_racer_extension/mini_racer_extension.cc +++ b/ext/mini_racer_extension/mini_racer_extension.cc @@ -547,6 +547,91 @@ static void unblock_eval(void *ptr) { eval->context_info->isolate_info->interrupted = true; } +/* + * The implementations of the run_extra_code(), create_snapshot_data_blob() and + * warm_up_snapshot_data_blob() functions have been derived from V8's test suite. + */ +bool run_extra_code(Isolate *isolate, Local context, + const char *utf8_source, const char *name) { + Context::Scope context_scope(context); + TryCatch try_catch(isolate); + Local source_string; + if (!String::NewFromUtf8(isolate, utf8_source, + NewStringType::kNormal) + .ToLocal(&source_string)) { + return false; + } + Local resource_name = + String::NewFromUtf8(isolate, name, NewStringType::kNormal) + .ToLocalChecked(); + ScriptOrigin origin(resource_name); + ScriptCompiler::Source source(source_string, origin); + Local