From 8af11c7a0f0cc679af90e6fa8d8917b5b3f99958 Mon Sep 17 00:00:00 2001 From: Ross McIlroy Date: Fri, 1 Feb 2019 15:26:07 +0000 Subject: [PATCH] Update code to the API changes in V8 7.3 Replaces calls to APIs that are deprecated or removed in V8 7.3, namely: - GetPropertyNames requires a Context and returns a MaybeLocal - Call requires a Context and returns a MaybeLocal - String::Utf8Value requires an isolate - Run requires a Context and returns a MaybeLocal - GetFunction requires a Context and retunrs a MaybeLocal --- gjstest/internal/cpp/run_tests.cc | 11 +++++++---- gjstest/internal/cpp/test_case.cc | 11 +++++++---- gjstest/internal/cpp/v8_utils.cc | 28 ++++++++++++++++++---------- gjstest/internal/cpp/v8_utils.h | 3 ++- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/gjstest/internal/cpp/run_tests.cc b/gjstest/internal/cpp/run_tests.cc index 4856312..a341b30 100644 --- a/gjstest/internal/cpp/run_tests.cc +++ b/gjstest/internal/cpp/run_tests.cc @@ -182,14 +182,16 @@ static void ProcessTestSuite( std::unordered_map* test_durations) { StringAppendF(output, "[----------]\n"); - const Local test_names = test_functions->GetPropertyNames(); + const Local test_names = + test_functions->GetPropertyNames(isolate->GetCurrentContext()) + .ToLocalChecked(); for (uint32 i = 0; i < test_names->Length(); ++i) { const Local name = test_names->Get(i); const Local test_function = test_functions->Get(name); CHECK(test_function->IsFunction()); // Skip this test if it doesn't match our filter. - const string string_name = ConvertToString(name); + const string string_name = ConvertToString(isolate, name); if (!RE2::FullMatch(string_name, test_filter)) continue; tests_run->push_back(string_name); @@ -279,9 +281,10 @@ bool RunTests( Handle args[] = { test_suite }; const Local test_functions_value = get_test_functions->Call( + isolate.get()->GetCurrentContext(), context->Global(), arraysize(args), - args); + args).ToLocalChecked(); CHECK(test_functions_value->IsObject()); const Local test_functions = Local::Cast(test_functions_value); @@ -329,7 +332,7 @@ bool RunTests( CHECK(coverage_result->IsString()); - *coverage_info += ConvertToString(coverage_result); + *coverage_info += ConvertToString(isolate.get(), coverage_result); } return success; diff --git a/gjstest/internal/cpp/test_case.cc b/gjstest/internal/cpp/test_case.cc index 7e5e827..ebdb282 100644 --- a/gjstest/internal/cpp/test_case.cc +++ b/gjstest/internal/cpp/test_case.cc @@ -25,6 +25,7 @@ using v8::Function; using v8::Handle; using v8::Isolate; using v8::Local; +using v8::MaybeLocal; using v8::Object; using v8::TryCatch; using v8::Value; @@ -42,7 +43,7 @@ Local TestCase::GetFunctionNamed(const string& name) const { v8::Handle TestCase::LogString( const v8::FunctionCallbackInfo& cb_info) { CHECK_EQ(1, cb_info.Length()); - const string message = ConvertToString(cb_info[0]); + const string message = ConvertToString(isolate_, cb_info[0]); StringAppendF(&this->output, "%s\n", message.c_str()); return v8::Undefined(isolate_); @@ -53,7 +54,7 @@ v8::Handle TestCase::LogString( v8::Handle TestCase::RecordFailure( const v8::FunctionCallbackInfo& cb_info) { CHECK_EQ(1, cb_info.Length()); - const string message = ConvertToString(cb_info[0]); + const string message = ConvertToString(isolate_, cb_info[0]); this->succeeded = false; StringAppendF(&this->output, "%s\n\n", message.c_str()); @@ -123,14 +124,16 @@ void TestCase::Run() { // Run the test. TryCatch try_catch(isolate_); Handle args[] = { test_function_, test_env }; - const Local result = + const MaybeLocal maybe_result = run_test->Call( + isolate_->GetCurrentContext(), isolate_->GetCurrentContext()->Global(), arraysize(args), args); // Was there an exception while running the test? - if (result.IsEmpty()) { + Local result; + if (!maybe_result.ToLocal(&result)) { succeeded = false; const string description = DescribeError(try_catch); diff --git a/gjstest/internal/cpp/v8_utils.cc b/gjstest/internal/cpp/v8_utils.cc index d2d8941..ac8b45e 100644 --- a/gjstest/internal/cpp/v8_utils.cc +++ b/gjstest/internal/cpp/v8_utils.cc @@ -86,12 +86,13 @@ static Local ConvertString( return String::NewFromUtf8( isolate, s.data(), - String::kNormalString, - s.size()); + v8::NewStringType::kNormal, + s.size()).ToLocalChecked(); } -std::string ConvertToString(const Handle& value) { - const String::Utf8Value utf8_value(value); +std::string ConvertToString(Isolate* const isolate, + const Handle& value) { + const String::Utf8Value utf8_value(isolate, value); return std::string(*utf8_value, utf8_value.length()); } @@ -106,7 +107,7 @@ void ConvertToStringVector( const uint32 length = array->Length(); for (uint32 i = 0; i < length; ++i) { - result->push_back(ConvertToString(array->Get(i))); + result->push_back(ConvertToString(isolate, array->Get(i))); } } @@ -139,7 +140,11 @@ Local ExecuteJs( } // Run the script. - auto result = script->BindToCurrentContext()->Run(); + Local result; + if (!script->BindToCurrentContext()->Run(isolate->GetCurrentContext()) + .ToLocal(&result)) { + return Local(); + } // Give v8 a chance to process any foreground tasks that are pending. while (v8::platform::PumpMessageLoop(platform_, isolate)) {} @@ -148,8 +153,10 @@ Local ExecuteJs( } std::string DescribeError(const TryCatch& try_catch) { - const std::string exception = ConvertToString(try_catch.Exception()); const Local message = try_catch.Message(); + Isolate* const isolate = message->GetIsolate(); + const std::string exception = + ConvertToString(isolate, try_catch.Exception()); // If there's no message, just return the exception. if (message.IsEmpty()) return exception; @@ -159,8 +166,9 @@ std::string DescribeError(const TryCatch& try_catch) { // foo.js:7: ReferenceError: blah is not defined. // const std::string filename = - ConvertToString(message->GetScriptResourceName()); - const int line = message->GetLineNumber(); + ConvertToString(message->GetIsolate(), message->GetScriptResourceName()); + const int line = message->GetLineNumber(isolate->GetCurrentContext()) + .ToChecked(); // Sometimes for multi-line errors there is no line number. if (!line) { @@ -219,7 +227,7 @@ Local MakeFunction( isolate, RunAssociatedCallback, data) - ->GetFunction(); + ->GetFunction(isolate->GetCurrentContext()).ToLocalChecked(); result->SetName(ConvertString(isolate, name)); diff --git a/gjstest/internal/cpp/v8_utils.h b/gjstest/internal/cpp/v8_utils.h index 176b814..5cb9854 100644 --- a/gjstest/internal/cpp/v8_utils.h +++ b/gjstest/internal/cpp/v8_utils.h @@ -36,7 +36,8 @@ typedef std::shared_ptr IsolateHandle; IsolateHandle CreateIsolate(); // Convert the supplied value to a UTF-8 string. -std::string ConvertToString(const v8::Handle& value); +std::string ConvertToString(v8::Isolate* const isolate, + const v8::Handle& value); // Convert the supplied value, which must be an array, into a vector of strings. void ConvertToStringVector(