From 92cfc4a62d68bd00232259509e127554a4482c48 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Mon, 21 Jan 2019 21:57:11 +0530 Subject: [PATCH] fix: update deprecated v8 api usage --- .../browser/net/url_request_async_asar_job.cc | 2 +- atom/browser/net/url_request_buffer_job.cc | 2 +- atom/browser/net/url_request_string_job.cc | 2 +- .../native_mate_converters/net_converter.cc | 8 +- .../v8_value_converter.cc | 127 ++++++++++-------- .../v8_value_converter.h | 31 +++-- atom/common/promise_util.cc | 5 +- native_mate/native_mate/converter.cc | 14 +- native_mate/native_mate/converter.h | 3 +- native_mate/native_mate/promise.cc | 17 ++- 10 files changed, 117 insertions(+), 94 deletions(-) diff --git a/atom/browser/net/url_request_async_asar_job.cc b/atom/browser/net/url_request_async_asar_job.cc index 1f3bebe8b7d7..1b94776f4973 100644 --- a/atom/browser/net/url_request_async_asar_job.cc +++ b/atom/browser/net/url_request_async_asar_job.cc @@ -29,7 +29,7 @@ void BeforeStartInUI(base::WeakPtr job, if (args->GetNext(&value)) { V8ValueConverter converter; v8::Local context = args->isolate()->GetCurrentContext(); - request_options.reset(converter.FromV8Value(value, context)); + request_options = converter.FromV8Value(value, context); } if (request_options) { diff --git a/atom/browser/net/url_request_buffer_job.cc b/atom/browser/net/url_request_buffer_job.cc index 852e8d80bb9d..caea8eb7fd1a 100644 --- a/atom/browser/net/url_request_buffer_job.cc +++ b/atom/browser/net/url_request_buffer_job.cc @@ -40,7 +40,7 @@ void BeforeStartInUI(base::WeakPtr job, if (args->GetNext(&value)) { V8ValueConverter converter; v8::Local context = args->isolate()->GetCurrentContext(); - request_options.reset(converter.FromV8Value(value, context)); + request_options = converter.FromV8Value(value, context); } if (request_options) { diff --git a/atom/browser/net/url_request_string_job.cc b/atom/browser/net/url_request_string_job.cc index 3ab4f5a2c44a..872fbaf36f9d 100644 --- a/atom/browser/net/url_request_string_job.cc +++ b/atom/browser/net/url_request_string_job.cc @@ -29,7 +29,7 @@ void BeforeStartInUI(base::WeakPtr job, if (args->GetNext(&value)) { V8ValueConverter converter; v8::Local context = args->isolate()->GetCurrentContext(); - request_options.reset(converter.FromV8Value(value, context)); + request_options = converter.FromV8Value(value, context); } if (request_options) { diff --git a/atom/common/native_mate_converters/net_converter.cc b/atom/common/native_mate_converters/net_converter.cc index 88061136c134..ebe68ad4a5ec 100644 --- a/atom/common/native_mate_converters/net_converter.cc +++ b/atom/common/native_mate_converters/net_converter.cc @@ -198,16 +198,16 @@ bool Converter::FromV8( auto context = isolate->GetCurrentContext(); auto headers = v8::Local::Cast(val); - auto keys = headers->GetOwnPropertyNames(); + auto keys = headers->GetOwnPropertyNames(context).ToLocalChecked(); for (uint32_t i = 0; i < keys->Length(); i++) { - v8::Local keyVal; - if (!keys->Get(i)->ToString(context).ToLocal(&keyVal)) { + v8::Local keyVal; + if (!keys->Get(context, i).ToLocal(&keyVal)) { return false; } std::string key; mate::ConvertFromV8(isolate, keyVal, &key); - auto localVal = headers->Get(keyVal); + auto localVal = headers->Get(context, keyVal).ToLocalChecked(); if (localVal->IsArray()) { auto values = v8::Local::Cast(localVal); for (uint32_t j = 0; j < values->Length(); j++) { diff --git a/atom/common/native_mate_converters/v8_value_converter.cc b/atom/common/native_mate_converters/v8_value_converter.cc index 73bfe5a96a8c..083deacf4c28 100644 --- a/atom/common/native_mate_converters/v8_value_converter.cc +++ b/atom/common/native_mate_converters/v8_value_converter.cc @@ -146,7 +146,7 @@ v8::Local V8ValueConverter::ToV8Value( return handle_scope.Escape(ToV8ValueImpl(context->GetIsolate(), value)); } -base::Value* V8ValueConverter::FromV8Value( +std::unique_ptr V8ValueConverter::FromV8Value( v8::Local val, v8::Local context) const { v8::Context::Scope context_scope(context); @@ -180,7 +180,8 @@ v8::Local V8ValueConverter::ToV8ValueImpl( case base::Value::Type::STRING: { std::string val = value->GetString(); return v8::String::NewFromUtf8(isolate, val.c_str(), - v8::String::kNormalString, val.length()); + v8::NewStringType::kNormal, val.length()) + .ToLocalChecked(); } case base::Value::Type::LIST: @@ -288,38 +289,38 @@ v8::Local V8ValueConverter::ToArrayBuffer( return v8::Uint8Array::New(array_buffer, 0, length); } -base::Value* V8ValueConverter::FromV8ValueImpl(FromV8ValueState* state, - v8::Local val, - v8::Isolate* isolate) const { +std::unique_ptr V8ValueConverter::FromV8ValueImpl( + FromV8ValueState* state, + v8::Local val, + v8::Isolate* isolate) const { FromV8ValueState::Level state_level(state); if (state->HasReachedMaxRecursionDepth()) return nullptr; if (val->IsExternal()) - return std::make_unique().release(); + return std::make_unique(); if (val->IsNull()) - return std::make_unique().release(); + return std::make_unique(); auto context = isolate->GetCurrentContext(); if (val->IsBoolean()) - return new base::Value(val->ToBoolean(context).ToLocalChecked()->Value()); + return std::make_unique(val->ToBoolean(isolate)->Value()); if (val->IsInt32()) - return new base::Value(val->ToInt32(context).ToLocalChecked()->Value()); + return std::make_unique(val.As()->Value()); if (val->IsNumber()) { - double val_as_double = val->ToNumber(context).ToLocalChecked()->Value(); + double val_as_double = val.As()->Value(); if (!std::isfinite(val_as_double)) return nullptr; - return new base::Value(val_as_double); + return std::make_unique(val_as_double); } if (val->IsString()) { - v8::String::Utf8Value utf8(isolate, - val->ToString(context).ToLocalChecked()); - return new base::Value(std::string(*utf8, utf8.length())); + v8::String::Utf8Value utf8(isolate, val); + return std::make_unique(std::string(*utf8, utf8.length())); } if (val->IsUndefined()) @@ -329,15 +330,16 @@ base::Value* V8ValueConverter::FromV8ValueImpl(FromV8ValueState* state, if (val->IsDate()) { v8::Date* date = v8::Date::Cast(*val); v8::Local toISOString = - date->Get(v8::String::NewFromUtf8(isolate, "toISOString")); + date->Get(v8::String::NewFromUtf8(isolate, "toISOString", + v8::NewStringType::kNormal) + .ToLocalChecked()); if (toISOString->IsFunction()) { v8::Local result = toISOString.As() ->Call(context, val, 0, nullptr) .ToLocalChecked(); if (!result.IsEmpty()) { - v8::String::Utf8Value utf8(isolate, - result->ToString(context).ToLocalChecked()); - return new base::Value(std::string(*utf8, utf8.length())); + v8::String::Utf8Value utf8(isolate, result); + return std::make_unique(std::string(*utf8, utf8.length())); } } } @@ -345,10 +347,8 @@ base::Value* V8ValueConverter::FromV8ValueImpl(FromV8ValueState* state, if (val->IsRegExp()) { if (!reg_exp_allowed_) // JSON.stringify converts to an object. - return FromV8Object(val->ToObject(context).ToLocalChecked(), state, - isolate); - return new base::Value(*v8::String::Utf8Value( - isolate, val->ToString(context).ToLocalChecked())); + return FromV8Object(val.As(), state, isolate); + return std::make_unique(*v8::String::Utf8Value(isolate, val)); } // v8::Value doesn't have a ToArray() method for some reason. @@ -359,8 +359,7 @@ base::Value* V8ValueConverter::FromV8ValueImpl(FromV8ValueState* state, if (!function_allowed_) // JSON.stringify refuses to convert function(){}. return nullptr; - return FromV8Object(val->ToObject(context).ToLocalChecked(), state, - isolate); + return FromV8Object(val.As(), state, isolate); } if (node::Buffer::HasInstance(val)) { @@ -368,20 +367,20 @@ base::Value* V8ValueConverter::FromV8ValueImpl(FromV8ValueState* state, } if (val->IsObject()) { - return FromV8Object(val->ToObject(context).ToLocalChecked(), state, - isolate); + return FromV8Object(val.As(), state, isolate); } LOG(ERROR) << "Unexpected v8 value type encountered."; return nullptr; } -base::Value* V8ValueConverter::FromV8Array(v8::Local val, - FromV8ValueState* state, - v8::Isolate* isolate) const { +std::unique_ptr V8ValueConverter::FromV8Array( + v8::Local val, + FromV8ValueState* state, + v8::Isolate* isolate) const { ScopedUniquenessGuard uniqueness_guard(state, val); if (!uniqueness_guard.is_valid()) - return std::make_unique().release(); + return std::make_unique(); std::unique_ptr scope; // If val was created in a different context than our current one, change to @@ -390,45 +389,54 @@ base::Value* V8ValueConverter::FromV8Array(v8::Local val, val->CreationContext() != isolate->GetCurrentContext()) scope.reset(new v8::Context::Scope(val->CreationContext())); - auto* result = new base::ListValue(); + std::unique_ptr result(new base::ListValue()); // Only fields with integer keys are carried over to the ListValue. for (uint32_t i = 0; i < val->Length(); ++i) { v8::TryCatch try_catch(isolate); - v8::Local child_v8 = val->Get(i); - if (try_catch.HasCaught()) { + v8::Local child_v8; + v8::MaybeLocal maybe_child = + val->Get(isolate->GetCurrentContext(), i); + if (try_catch.HasCaught() || !maybe_child.ToLocal(&child_v8)) { LOG(ERROR) << "Getter for index " << i << " threw an exception."; child_v8 = v8::Null(isolate); } - if (!val->HasRealIndexedProperty(i)) + if (!val->HasRealIndexedProperty(isolate->GetCurrentContext(), i) + .FromMaybe(false)) { + result->Append(std::make_unique()); continue; + } - base::Value* child = FromV8ValueImpl(state, child_v8, isolate); + std::unique_ptr child = + FromV8ValueImpl(state, child_v8, isolate); if (child) - result->Append(std::unique_ptr(child)); + result->Append(std::move(child)); else // JSON.stringify puts null in places where values don't serialize, for // example undefined and functions. Emulate that behavior. result->Append(std::make_unique()); } - return result; + return std::move(result); } -base::Value* V8ValueConverter::FromNodeBuffer(v8::Local value, - FromV8ValueState* state, - v8::Isolate* isolate) const { - return new base::Value(std::vector( +std::unique_ptr V8ValueConverter::FromNodeBuffer( + v8::Local value, + FromV8ValueState* state, + v8::Isolate* isolate) const { + std::vector buffer( node::Buffer::Data(value), - node::Buffer::Data(value) + node::Buffer::Length(value))); + node::Buffer::Data(value) + node::Buffer::Length(value)); + return std::make_unique(std::move(buffer)); } -base::Value* V8ValueConverter::FromV8Object(v8::Local val, - FromV8ValueState* state, - v8::Isolate* isolate) const { +std::unique_ptr V8ValueConverter::FromV8Object( + v8::Local val, + FromV8ValueState* state, + v8::Isolate* isolate) const { ScopedUniquenessGuard uniqueness_guard(state, val); if (!uniqueness_guard.is_valid()) - return std::make_unique().release(); + return std::make_unique(); std::unique_ptr scope; // If val was created in a different context than our current one, change to @@ -438,10 +446,15 @@ base::Value* V8ValueConverter::FromV8Object(v8::Local val, scope.reset(new v8::Context::Scope(val->CreationContext())); auto result = std::make_unique(); - v8::Local property_names(val->GetOwnPropertyNames()); + v8::Local property_names; + if (!val->GetOwnPropertyNames(isolate->GetCurrentContext()) + .ToLocal(&property_names)) { + return std::move(result); + } for (uint32_t i = 0; i < property_names->Length(); ++i) { - v8::Local key(property_names->Get(i)); + v8::Local key = + property_names->Get(isolate->GetCurrentContext(), i).ToLocalChecked(); // Extend this test to cover more types as necessary and if sensible. if (!key->IsString() && !key->IsNumber()) { @@ -451,21 +464,21 @@ base::Value* V8ValueConverter::FromV8Object(v8::Local val, continue; } - v8::String::Utf8Value name_utf8( - isolate, key->ToString(isolate->GetCurrentContext()).ToLocalChecked()); + v8::String::Utf8Value name_utf8(isolate, key); v8::TryCatch try_catch(isolate); - v8::Local child_v8 = val->Get(key); - - if (try_catch.HasCaught()) { + v8::Local child_v8; + v8::MaybeLocal maybe_child = + val->Get(isolate->GetCurrentContext(), key); + if (try_catch.HasCaught() || !maybe_child.ToLocal(&child_v8)) { LOG(ERROR) << "Getter for property " << *name_utf8 << " threw an exception."; child_v8 = v8::Null(isolate); } - std::unique_ptr child( - FromV8ValueImpl(state, child_v8, isolate)); - if (!child.get()) + std::unique_ptr child = + FromV8ValueImpl(state, child_v8, isolate); + if (!child) // JSON.stringify skips properties whose values don't serialize, for // example undefined and functions. Emulate that behavior. continue; @@ -497,7 +510,7 @@ base::Value* V8ValueConverter::FromV8Object(v8::Local val, std::move(child)); } - return result.release(); + return std::move(result); } } // namespace atom diff --git a/atom/common/native_mate_converters/v8_value_converter.h b/atom/common/native_mate_converters/v8_value_converter.h index 353f5d13597f..ea0909c98398 100644 --- a/atom/common/native_mate_converters/v8_value_converter.h +++ b/atom/common/native_mate_converters/v8_value_converter.h @@ -5,6 +5,8 @@ #ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_V8_VALUE_CONVERTER_H_ #define ATOM_COMMON_NATIVE_MATE_CONVERTERS_V8_VALUE_CONVERTER_H_ +#include + #include "base/compiler_specific.h" #include "base/macros.h" #include "v8/include/v8.h" @@ -26,8 +28,9 @@ class V8ValueConverter { void SetStripNullFromObjects(bool val); v8::Local ToV8Value(const base::Value* value, v8::Local context) const; - base::Value* FromV8Value(v8::Local value, - v8::Local context) const; + std::unique_ptr FromV8Value( + v8::Local value, + v8::Local context) const; private: class FromV8ValueState; @@ -43,18 +46,18 @@ class V8ValueConverter { v8::Local ToArrayBuffer(v8::Isolate* isolate, const base::Value* value) const; - base::Value* FromV8ValueImpl(FromV8ValueState* state, - v8::Local value, - v8::Isolate* isolate) const; - base::Value* FromV8Array(v8::Local array, - FromV8ValueState* state, - v8::Isolate* isolate) const; - base::Value* FromNodeBuffer(v8::Local value, - FromV8ValueState* state, - v8::Isolate* isolate) const; - base::Value* FromV8Object(v8::Local object, - FromV8ValueState* state, - v8::Isolate* isolate) const; + std::unique_ptr FromV8ValueImpl(FromV8ValueState* state, + v8::Local value, + v8::Isolate* isolate) const; + std::unique_ptr FromV8Array(v8::Local array, + FromV8ValueState* state, + v8::Isolate* isolate) const; + std::unique_ptr FromNodeBuffer(v8::Local value, + FromV8ValueState* state, + v8::Isolate* isolate) const; + std::unique_ptr FromV8Object(v8::Local object, + FromV8ValueState* state, + v8::Isolate* isolate) const; // If true, we will convert RegExp JavaScript objects to string. bool reg_exp_allowed_ = false; diff --git a/atom/common/promise_util.cc b/atom/common/promise_util.cc index 27eec4fe4f39..bf4f9ecca848 100644 --- a/atom/common/promise_util.cc +++ b/atom/common/promise_util.cc @@ -30,7 +30,10 @@ v8::Maybe Promise::RejectWithErrorMessage(const std::string& string) { v8::Local::New(isolate(), GetContext())); v8::Local error_message = - v8::String::NewFromUtf8(isolate(), string.c_str()); + v8::String::NewFromUtf8(isolate(), string.c_str(), + v8::NewStringType::kNormal, + static_cast(string.size())) + .ToLocalChecked(); v8::Local error = v8::Exception::Error(error_message); return Reject(error); } diff --git a/native_mate/native_mate/converter.cc b/native_mate/native_mate/converter.cc index c8260be162c2..b146395b8ffc 100644 --- a/native_mate/native_mate/converter.cc +++ b/native_mate/native_mate/converter.cc @@ -140,13 +140,16 @@ bool Converter::FromV8(Isolate* isolate, } Local Converter::ToV8(Isolate* isolate, const char* val) { - return v8::String::NewFromUtf8(isolate, val); + return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal) + .ToLocalChecked(); } Local Converter::ToV8(Isolate* isolate, const base::StringPiece& val) { - return v8::String::NewFromUtf8(isolate, val.data(), v8::String::kNormalString, - static_cast(val.length())); + return v8::String::NewFromUtf8(isolate, val.data(), + v8::NewStringType::kNormal, + static_cast(val.length())) + .ToLocalChecked(); } Local Converter::ToV8(Isolate* isolate, @@ -255,8 +258,9 @@ bool Converter>::FromV8(Isolate* isolate, v8::Local StringToSymbol(v8::Isolate* isolate, const base::StringPiece& val) { return v8::String::NewFromUtf8(isolate, val.data(), - v8::String::kInternalizedString, - static_cast(val.length())); + v8::NewStringType::kInternalized, + static_cast(val.length())) + .ToLocalChecked(); } } // namespace mate diff --git a/native_mate/native_mate/converter.h b/native_mate/native_mate/converter.h index dfe97edaf201..0e167eca9dfe 100644 --- a/native_mate/native_mate/converter.h +++ b/native_mate/native_mate/converter.h @@ -277,7 +277,8 @@ struct Converter> { v8::Local context = isolate->GetCurrentContext(); v8::Local dict = val->ToObject(context).ToLocalChecked(); - v8::Local keys = dict->GetOwnPropertyNames(); + v8::Local keys = + dict->GetOwnPropertyNames(context).ToLocalChecked(); for (uint32_t i = 0; i < keys->Length(); ++i) { v8::Local key = keys->Get(i); T value; diff --git a/native_mate/native_mate/promise.cc b/native_mate/native_mate/promise.cc index 81b79441e72e..e89b54ce3812 100644 --- a/native_mate/native_mate/promise.cc +++ b/native_mate/native_mate/promise.cc @@ -6,17 +6,13 @@ namespace mate { -Promise::Promise() - : isolate_(NULL) { -} +Promise::Promise() : isolate_(NULL) {} -Promise::Promise(v8::Isolate* isolate) - : isolate_(isolate) { +Promise::Promise(v8::Isolate* isolate) : isolate_(isolate) { resolver_ = v8::Promise::Resolver::New(isolate); } -Promise::~Promise() { -} +Promise::~Promise() {} Promise Promise::Create(v8::Isolate* isolate) { return Promise(isolate); @@ -28,7 +24,10 @@ Promise Promise::Create() { void Promise::RejectWithErrorMessage(const std::string& string) { v8::Local error_message = - v8::String::NewFromUtf8(isolate(), string.c_str()); + v8::String::NewFromUtf8(isolate(), string.c_str(), + v8::NewStringType::kNormal, + static_cast(string.size())) + .ToLocalChecked(); v8::Local error = v8::Exception::Error(error_message); resolver_->Reject(mate::ConvertToV8(isolate(), error)); } @@ -38,7 +37,7 @@ v8::Local Promise::GetHandle() const { } v8::Local Converter::ToV8(v8::Isolate* isolate, - Promise val) { + Promise val) { return val.GetHandle(); }