mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
chore: fix v8 deprecation warnings
This commit is contained in:
@@ -10,17 +10,33 @@ using v8::Array;
|
||||
using v8::Boolean;
|
||||
using v8::External;
|
||||
using v8::Function;
|
||||
using v8::Int32;
|
||||
using v8::Integer;
|
||||
using v8::Isolate;
|
||||
using v8::Local;
|
||||
using v8::Maybe;
|
||||
using v8::MaybeLocal;
|
||||
using v8::Number;
|
||||
using v8::Object;
|
||||
using v8::Promise;
|
||||
using v8::String;
|
||||
using v8::Uint32;
|
||||
using v8::Value;
|
||||
|
||||
namespace mate {
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename T, typename U>
|
||||
bool FromMaybe(Maybe<T> maybe, U* out) {
|
||||
if (maybe.IsNothing())
|
||||
return false;
|
||||
*out = static_cast<U>(maybe.FromJust());
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Local<Value> Converter<bool>::ToV8(Isolate* isolate, bool val) {
|
||||
return v8::Boolean::New(isolate, val);
|
||||
}
|
||||
@@ -28,7 +44,7 @@ Local<Value> Converter<bool>::ToV8(Isolate* isolate, bool val) {
|
||||
bool Converter<bool>::FromV8(Isolate* isolate, Local<Value> val, bool* out) {
|
||||
if (!val->IsBoolean())
|
||||
return false;
|
||||
*out = val->BooleanValue();
|
||||
*out = val.As<Boolean>()->Value();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -43,8 +59,7 @@ bool Converter<unsigned long>::FromV8(Isolate* isolate,
|
||||
unsigned long* out) {
|
||||
if (!val->IsNumber())
|
||||
return false;
|
||||
*out = val->IntegerValue();
|
||||
return true;
|
||||
return FromMaybe(val->IntegerValue(isolate->GetCurrentContext()), out);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -57,7 +72,7 @@ bool Converter<int32_t>::FromV8(Isolate* isolate,
|
||||
int32_t* out) {
|
||||
if (!val->IsInt32())
|
||||
return false;
|
||||
*out = val->Int32Value();
|
||||
*out = val.As<Int32>()->Value();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -70,7 +85,7 @@ bool Converter<uint32_t>::FromV8(Isolate* isolate,
|
||||
uint32_t* out) {
|
||||
if (!val->IsUint32())
|
||||
return false;
|
||||
*out = val->Uint32Value();
|
||||
*out = val.As<Uint32>()->Value();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -85,8 +100,7 @@ bool Converter<int64_t>::FromV8(Isolate* isolate,
|
||||
return false;
|
||||
// Even though IntegerValue returns int64_t, JavaScript cannot represent
|
||||
// the full precision of int64_t, which means some rounding might occur.
|
||||
*out = val->IntegerValue();
|
||||
return true;
|
||||
return FromMaybe(val->IntegerValue(isolate->GetCurrentContext()), out);
|
||||
}
|
||||
|
||||
Local<Value> Converter<uint64_t>::ToV8(Isolate* isolate, uint64_t val) {
|
||||
@@ -98,8 +112,7 @@ bool Converter<uint64_t>::FromV8(Isolate* isolate,
|
||||
uint64_t* out) {
|
||||
if (!val->IsNumber())
|
||||
return false;
|
||||
*out = static_cast<uint64_t>(val->IntegerValue());
|
||||
return true;
|
||||
return FromMaybe(val->IntegerValue(isolate->GetCurrentContext()), out);
|
||||
}
|
||||
|
||||
Local<Value> Converter<float>::ToV8(Isolate* isolate, float val) {
|
||||
@@ -109,7 +122,7 @@ Local<Value> Converter<float>::ToV8(Isolate* isolate, float val) {
|
||||
bool Converter<float>::FromV8(Isolate* isolate, Local<Value> val, float* out) {
|
||||
if (!val->IsNumber())
|
||||
return false;
|
||||
*out = static_cast<float>(val->NumberValue());
|
||||
*out = static_cast<float>(val.As<Number>()->Value());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -122,7 +135,7 @@ bool Converter<double>::FromV8(Isolate* isolate,
|
||||
double* out) {
|
||||
if (!val->IsNumber())
|
||||
return false;
|
||||
*out = val->NumberValue();
|
||||
*out = val.As<Number>()->Value();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -147,9 +160,10 @@ bool Converter<std::string>::FromV8(Isolate* isolate,
|
||||
if (!val->IsString())
|
||||
return false;
|
||||
Local<String> str = Local<String>::Cast(val);
|
||||
int length = str->Utf8Length();
|
||||
int length = str->Utf8Length(isolate);
|
||||
out->resize(length);
|
||||
str->WriteUtf8(&(*out)[0], length, NULL, String::NO_NULL_TERMINATION);
|
||||
str->WriteUtf8(isolate, &(*out)[0], length, NULL,
|
||||
String::NO_NULL_TERMINATION);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -245,13 +259,4 @@ v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
|
||||
static_cast<uint32_t>(val.length()));
|
||||
}
|
||||
|
||||
std::string V8ToString(v8::Local<v8::Value> value) {
|
||||
if (value.IsEmpty())
|
||||
return std::string();
|
||||
std::string result;
|
||||
if (!ConvertFromV8(NULL, value, &result))
|
||||
return std::string();
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace mate
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "base/strings/string_piece.h"
|
||||
#include "gin/converter.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace mate {
|
||||
@@ -136,8 +137,6 @@ struct Converter<std::string> {
|
||||
v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
|
||||
const base::StringPiece& input);
|
||||
|
||||
std::string V8ToString(v8::Local<v8::Value> value);
|
||||
|
||||
template <>
|
||||
struct Converter<v8::Local<v8::Function>> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
@@ -276,13 +275,14 @@ struct Converter<std::map<std::string, T>> {
|
||||
if (!val->IsObject())
|
||||
return false;
|
||||
|
||||
v8::Local<v8::Object> dict = val->ToObject();
|
||||
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
||||
v8::Local<v8::Object> dict = val->ToObject(context).ToLocalChecked();
|
||||
v8::Local<v8::Array> keys = dict->GetOwnPropertyNames();
|
||||
for (uint32_t i = 0; i < keys->Length(); ++i) {
|
||||
v8::Local<v8::Value> key = keys->Get(i);
|
||||
T value;
|
||||
if (Converter<T>::FromV8(isolate, dict->Get(key), &value))
|
||||
(*out)[V8ToString(key)] = std::move(value);
|
||||
(*out)[gin::V8ToString(isolate, key)] = std::move(value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -13,15 +13,13 @@ namespace mate {
|
||||
// Currently we don't have a mechanism for retaining a mate::Wrappable object
|
||||
// in the C++ heap because strong references from C++ to V8 can cause memory
|
||||
// leaks.
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
class Handle {
|
||||
public:
|
||||
Handle() : object_(NULL) {}
|
||||
|
||||
Handle(v8::Local<v8::Object> wrapper, T* object)
|
||||
: wrapper_(wrapper),
|
||||
object_(object) {
|
||||
}
|
||||
: wrapper_(wrapper), object_(object) {}
|
||||
|
||||
bool IsEmpty() const { return !object_; }
|
||||
|
||||
@@ -39,13 +37,14 @@ class Handle {
|
||||
T* object_;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Converter<mate::Handle<T> > {
|
||||
template <typename T>
|
||||
struct Converter<mate::Handle<T>> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const mate::Handle<T>& val) {
|
||||
const mate::Handle<T>& val) {
|
||||
return val.ToV8();
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
mate::Handle<T>* out) {
|
||||
T* object = NULL;
|
||||
if (val->IsNull() || val->IsUndefined()) {
|
||||
@@ -55,14 +54,15 @@ struct Converter<mate::Handle<T> > {
|
||||
if (!Converter<T*>::FromV8(isolate, val, &object)) {
|
||||
return false;
|
||||
}
|
||||
*out = mate::Handle<T>(val->ToObject(), object);
|
||||
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
||||
*out = mate::Handle<T>(val->ToObject(context).ToLocalChecked(), object);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// This function is a convenient way to create a handle from a raw pointer
|
||||
// without having to write out the type of the object explicitly.
|
||||
template<typename T>
|
||||
template <typename T>
|
||||
mate::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) {
|
||||
return mate::Handle<T>(object->GetWrapper(), object);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user