2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-17 12:05:43 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
2013-04-17 12:05:43 +00:00
|
|
|
|
2014-12-17 00:46:23 +00:00
|
|
|
#include <map>
|
2013-04-17 12:05:43 +00:00
|
|
|
#include <string>
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <utility>
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
|
|
|
#include "base/values.h"
|
2015-08-27 07:23:23 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2015-08-12 07:39:33 +00:00
|
|
|
#include "vendor/node/src/node_buffer.h"
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2014-12-17 00:46:23 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-08-26 10:22:28 +00:00
|
|
|
const int kMaxRecursionDepth = 100;
|
2014-12-17 00:46:23 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// The state of a call to FromV8Value.
|
|
|
|
class V8ValueConverter::FromV8ValueState {
|
|
|
|
public:
|
|
|
|
// Level scope which updates the current depth of some FromV8ValueState.
|
|
|
|
class Level {
|
|
|
|
public:
|
|
|
|
explicit Level(FromV8ValueState* state) : state_(state) {
|
|
|
|
state_->max_recursion_depth_--;
|
|
|
|
}
|
|
|
|
~Level() {
|
|
|
|
state_->max_recursion_depth_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FromV8ValueState* state_;
|
|
|
|
};
|
|
|
|
|
|
|
|
FromV8ValueState() : max_recursion_depth_(kMaxRecursionDepth) {}
|
|
|
|
|
|
|
|
// If |handle| is not in |unique_map_|, then add it to |unique_map_| and
|
|
|
|
// return true.
|
|
|
|
//
|
|
|
|
// Otherwise do nothing and return false. Here "A is unique" means that no
|
|
|
|
// other handle B in the map points to the same object as A. Note that A can
|
|
|
|
// be unique even if there already is another handle with the same identity
|
|
|
|
// hash (key) in the map, because two objects can have the same hash.
|
2015-05-22 11:11:22 +00:00
|
|
|
bool UpdateAndCheckUniqueness(v8::Local<v8::Object> handle) {
|
2014-12-17 00:46:23 +00:00
|
|
|
typedef HashToHandleMap::const_iterator Iterator;
|
|
|
|
int hash = handle->GetIdentityHash();
|
|
|
|
// We only compare using == with handles to objects with the same identity
|
|
|
|
// hash. Different hash obviously means different objects, but two objects
|
|
|
|
// in a couple of thousands could have the same identity hash.
|
|
|
|
std::pair<Iterator, Iterator> range = unique_map_.equal_range(hash);
|
|
|
|
for (Iterator it = range.first; it != range.second; ++it) {
|
|
|
|
// Operator == for handles actually compares the underlying objects.
|
|
|
|
if (it->second == handle)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unique_map_.insert(std::make_pair(hash, handle));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasReachedMaxRecursionDepth() {
|
|
|
|
return max_recursion_depth_ < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-05-22 11:11:22 +00:00
|
|
|
typedef std::multimap<int, v8::Local<v8::Object> > HashToHandleMap;
|
2014-12-17 00:46:23 +00:00
|
|
|
HashToHandleMap unique_map_;
|
|
|
|
|
|
|
|
int max_recursion_depth_;
|
|
|
|
};
|
|
|
|
|
2013-12-23 14:42:21 +00:00
|
|
|
V8ValueConverter::V8ValueConverter()
|
2016-02-17 07:36:18 +00:00
|
|
|
: reg_exp_allowed_(false),
|
2013-04-17 12:05:43 +00:00
|
|
|
function_allowed_(false),
|
2014-12-17 00:46:23 +00:00
|
|
|
strip_null_from_objects_(false) {}
|
2013-04-17 12:05:43 +00:00
|
|
|
|
2013-12-23 14:42:21 +00:00
|
|
|
void V8ValueConverter::SetRegExpAllowed(bool val) {
|
2013-04-17 12:05:43 +00:00
|
|
|
reg_exp_allowed_ = val;
|
|
|
|
}
|
|
|
|
|
2013-12-23 14:42:21 +00:00
|
|
|
void V8ValueConverter::SetFunctionAllowed(bool val) {
|
2013-04-17 12:05:43 +00:00
|
|
|
function_allowed_ = val;
|
|
|
|
}
|
|
|
|
|
2013-12-23 14:42:21 +00:00
|
|
|
void V8ValueConverter::SetStripNullFromObjects(bool val) {
|
2013-04-17 12:05:43 +00:00
|
|
|
strip_null_from_objects_ = val;
|
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Value> V8ValueConverter::ToV8Value(
|
|
|
|
const base::Value* value, v8::Local<v8::Context> context) const {
|
2013-04-17 12:05:43 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::EscapableHandleScope handle_scope(context->GetIsolate());
|
|
|
|
return handle_scope.Escape(ToV8ValueImpl(context->GetIsolate(), value));
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
base::Value* V8ValueConverter::FromV8Value(
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
v8::Local<v8::Context> context) const {
|
2013-04-17 12:05:43 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::HandleScope handle_scope(context->GetIsolate());
|
2014-12-17 00:46:23 +00:00
|
|
|
FromV8ValueState state;
|
|
|
|
return FromV8ValueImpl(&state, val, context->GetIsolate());
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Value> V8ValueConverter::ToV8ValueImpl(
|
|
|
|
v8::Isolate* isolate, const base::Value* value) const {
|
2013-04-17 12:05:43 +00:00
|
|
|
CHECK(value);
|
|
|
|
switch (value->GetType()) {
|
|
|
|
case base::Value::TYPE_NULL:
|
2014-06-28 14:33:00 +00:00
|
|
|
return v8::Null(isolate);
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
case base::Value::TYPE_BOOLEAN: {
|
|
|
|
bool val = false;
|
|
|
|
CHECK(value->GetAsBoolean(&val));
|
2014-06-28 14:33:00 +00:00
|
|
|
return v8::Boolean::New(isolate, val);
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case base::Value::TYPE_INTEGER: {
|
|
|
|
int val = 0;
|
|
|
|
CHECK(value->GetAsInteger(&val));
|
2014-06-28 14:33:00 +00:00
|
|
|
return v8::Integer::New(isolate, val);
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case base::Value::TYPE_DOUBLE: {
|
|
|
|
double val = 0.0;
|
|
|
|
CHECK(value->GetAsDouble(&val));
|
2014-06-28 14:33:00 +00:00
|
|
|
return v8::Number::New(isolate, val);
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case base::Value::TYPE_STRING: {
|
|
|
|
std::string val;
|
|
|
|
CHECK(value->GetAsString(&val));
|
2014-06-28 14:33:00 +00:00
|
|
|
return v8::String::NewFromUtf8(
|
|
|
|
isolate, val.c_str(), v8::String::kNormalString, val.length());
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case base::Value::TYPE_LIST:
|
2014-06-28 14:33:00 +00:00
|
|
|
return ToV8Array(isolate, static_cast<const base::ListValue*>(value));
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
case base::Value::TYPE_DICTIONARY:
|
2014-06-28 14:33:00 +00:00
|
|
|
return ToV8Object(isolate,
|
|
|
|
static_cast<const base::DictionaryValue*>(value));
|
2013-04-17 12:05:43 +00:00
|
|
|
|
2015-10-12 19:43:59 +00:00
|
|
|
case base::Value::TYPE_BINARY:
|
|
|
|
return ToArrayBuffer(isolate,
|
|
|
|
static_cast<const base::BinaryValue*>(value));
|
|
|
|
|
2013-04-17 12:05:43 +00:00
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Unexpected value type: " << value->GetType();
|
2014-06-28 14:33:00 +00:00
|
|
|
return v8::Null(isolate);
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Value> V8ValueConverter::ToV8Array(
|
|
|
|
v8::Isolate* isolate, const base::ListValue* val) const {
|
|
|
|
v8::Local<v8::Array> result(v8::Array::New(isolate, val->GetSize()));
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < val->GetSize(); ++i) {
|
2016-07-10 09:52:28 +00:00
|
|
|
const base::Value* child = nullptr;
|
2013-04-17 12:05:43 +00:00
|
|
|
CHECK(val->Get(i, &child));
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Value> child_v8 = ToV8ValueImpl(isolate, child);
|
2013-04-17 12:05:43 +00:00
|
|
|
CHECK(!child_v8.IsEmpty());
|
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
2016-03-08 04:40:10 +00:00
|
|
|
result->Set(static_cast<uint32_t>(i), child_v8);
|
2013-04-17 12:05:43 +00:00
|
|
|
if (try_catch.HasCaught())
|
|
|
|
LOG(ERROR) << "Setter for index " << i << " threw an exception.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Value> V8ValueConverter::ToV8Object(
|
|
|
|
v8::Isolate* isolate, const base::DictionaryValue* val) const {
|
2015-08-27 07:23:23 +00:00
|
|
|
mate::Dictionary result = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
result.SetHidden("simple", true);
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
for (base::DictionaryValue::Iterator iter(*val);
|
|
|
|
!iter.IsAtEnd(); iter.Advance()) {
|
|
|
|
const std::string& key = iter.key();
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Value> child_v8 = ToV8ValueImpl(isolate, &iter.value());
|
2013-04-17 12:05:43 +00:00
|
|
|
CHECK(!child_v8.IsEmpty());
|
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
2015-08-27 07:23:23 +00:00
|
|
|
result.Set(key, child_v8);
|
2013-04-17 12:05:43 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
LOG(ERROR) << "Setter for property " << key.c_str() << " threw an "
|
|
|
|
<< "exception.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 07:23:23 +00:00
|
|
|
return result.GetHandle();
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 19:43:59 +00:00
|
|
|
v8::Local<v8::Value> V8ValueConverter::ToArrayBuffer(
|
|
|
|
v8::Isolate* isolate, const base::BinaryValue* value) const {
|
|
|
|
return node::Buffer::Copy(isolate,
|
|
|
|
value->GetBuffer(),
|
|
|
|
value->GetSize()).ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
2014-12-17 00:46:23 +00:00
|
|
|
base::Value* V8ValueConverter::FromV8ValueImpl(
|
|
|
|
FromV8ValueState* state,
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Value> val,
|
2014-12-17 00:46:23 +00:00
|
|
|
v8::Isolate* isolate) const {
|
2013-04-17 12:05:43 +00:00
|
|
|
CHECK(!val.IsEmpty());
|
|
|
|
|
2014-12-17 00:46:23 +00:00
|
|
|
FromV8ValueState::Level state_level(state);
|
|
|
|
if (state->HasReachedMaxRecursionDepth())
|
2016-07-10 09:52:28 +00:00
|
|
|
return nullptr;
|
2014-12-17 00:46:23 +00:00
|
|
|
|
2013-04-17 12:05:43 +00:00
|
|
|
if (val->IsNull())
|
2015-08-04 08:33:38 +00:00
|
|
|
return base::Value::CreateNullValue().release();
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
if (val->IsBoolean())
|
|
|
|
return new base::FundamentalValue(val->ToBoolean()->Value());
|
|
|
|
|
2016-03-08 14:28:53 +00:00
|
|
|
if (val->IsInt32())
|
|
|
|
return new base::FundamentalValue(val->ToInt32()->Value());
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
if (val->IsNumber())
|
|
|
|
return new base::FundamentalValue(val->ToNumber()->Value());
|
|
|
|
|
|
|
|
if (val->IsString()) {
|
|
|
|
v8::String::Utf8Value utf8(val->ToString());
|
|
|
|
return new base::StringValue(std::string(*utf8, utf8.length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val->IsUndefined())
|
|
|
|
// JSON.stringify ignores undefined.
|
2016-07-10 09:52:28 +00:00
|
|
|
return nullptr;
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
if (val->IsDate()) {
|
|
|
|
v8::Date* date = v8::Date::Cast(*val);
|
2016-02-24 10:55:41 +00:00
|
|
|
v8::Local<v8::Value> toISOString =
|
2016-02-25 05:51:21 +00:00
|
|
|
date->Get(v8::String::NewFromUtf8(isolate, "toISOString"));
|
2016-02-24 10:55:41 +00:00
|
|
|
if (toISOString->IsFunction()) {
|
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
toISOString.As<v8::Function>()->Call(val, 0, nullptr);
|
|
|
|
if (!result.IsEmpty()) {
|
|
|
|
v8::String::Utf8Value utf8(result->ToString());
|
|
|
|
return new base::StringValue(std::string(*utf8, utf8.length()));
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (val->IsRegExp()) {
|
|
|
|
if (!reg_exp_allowed_)
|
|
|
|
// JSON.stringify converts to an object.
|
2014-12-17 00:46:23 +00:00
|
|
|
return FromV8Object(val->ToObject(), state, isolate);
|
2013-04-17 12:05:43 +00:00
|
|
|
return new base::StringValue(*v8::String::Utf8Value(val->ToString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// v8::Value doesn't have a ToArray() method for some reason.
|
|
|
|
if (val->IsArray())
|
2014-12-17 00:46:23 +00:00
|
|
|
return FromV8Array(val.As<v8::Array>(), state, isolate);
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
if (val->IsFunction()) {
|
|
|
|
if (!function_allowed_)
|
|
|
|
// JSON.stringify refuses to convert function(){}.
|
2016-07-10 09:52:28 +00:00
|
|
|
return nullptr;
|
2014-12-17 00:46:23 +00:00
|
|
|
return FromV8Object(val->ToObject(), state, isolate);
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 07:39:33 +00:00
|
|
|
if (node::Buffer::HasInstance(val)) {
|
|
|
|
return FromNodeBuffer(val, state, isolate);
|
|
|
|
}
|
|
|
|
|
2013-04-17 12:05:43 +00:00
|
|
|
if (val->IsObject()) {
|
2014-12-17 00:46:23 +00:00
|
|
|
return FromV8Object(val->ToObject(), state, isolate);
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(ERROR) << "Unexpected v8 value type encountered.";
|
2016-07-10 09:52:28 +00:00
|
|
|
return nullptr;
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 00:46:23 +00:00
|
|
|
base::Value* V8ValueConverter::FromV8Array(
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Array> val,
|
2014-12-17 00:46:23 +00:00
|
|
|
FromV8ValueState* state,
|
|
|
|
v8::Isolate* isolate) const {
|
|
|
|
if (!state->UpdateAndCheckUniqueness(val))
|
2015-08-04 08:33:38 +00:00
|
|
|
return base::Value::CreateNullValue().release();
|
2013-04-17 12:05:43 +00:00
|
|
|
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<v8::Context::Scope> scope;
|
2013-04-17 12:05:43 +00:00
|
|
|
// If val was created in a different context than our current one, change to
|
|
|
|
// that context, but change back after val is converted.
|
|
|
|
if (!val->CreationContext().IsEmpty() &&
|
2014-06-28 14:33:00 +00:00
|
|
|
val->CreationContext() != isolate->GetCurrentContext())
|
2013-04-17 12:05:43 +00:00
|
|
|
scope.reset(new v8::Context::Scope(val->CreationContext()));
|
|
|
|
|
|
|
|
base::ListValue* result = new base::ListValue();
|
|
|
|
|
|
|
|
// Only fields with integer keys are carried over to the ListValue.
|
2016-03-08 04:40:10 +00:00
|
|
|
for (uint32_t i = 0; i < val->Length(); ++i) {
|
2013-04-17 12:05:43 +00:00
|
|
|
v8::TryCatch try_catch;
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Value> child_v8 = val->Get(i);
|
2013-04-17 12:05:43 +00:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
LOG(ERROR) << "Getter for index " << i << " threw an exception.";
|
2014-06-28 14:33:00 +00:00
|
|
|
child_v8 = v8::Null(isolate);
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!val->HasRealIndexedProperty(i))
|
|
|
|
continue;
|
|
|
|
|
2015-01-08 07:20:03 +00:00
|
|
|
base::Value* child = FromV8ValueImpl(state, child_v8, isolate);
|
2013-04-17 12:05:43 +00:00
|
|
|
if (child)
|
|
|
|
result->Append(child);
|
|
|
|
else
|
|
|
|
// JSON.stringify puts null in places where values don't serialize, for
|
|
|
|
// example undefined and functions. Emulate that behavior.
|
|
|
|
result->Append(base::Value::CreateNullValue());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-12 07:39:33 +00:00
|
|
|
base::Value* V8ValueConverter::FromNodeBuffer(
|
|
|
|
v8::Local<v8::Value> value,
|
|
|
|
FromV8ValueState* state,
|
|
|
|
v8::Isolate* isolate) const {
|
|
|
|
return base::BinaryValue::CreateWithCopiedBuffer(
|
|
|
|
node::Buffer::Data(value), node::Buffer::Length(value));
|
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
base::Value* V8ValueConverter::FromV8Object(
|
|
|
|
v8::Local<v8::Object> val,
|
2014-12-17 00:46:23 +00:00
|
|
|
FromV8ValueState* state,
|
|
|
|
v8::Isolate* isolate) const {
|
|
|
|
if (!state->UpdateAndCheckUniqueness(val))
|
2015-08-04 08:33:38 +00:00
|
|
|
return base::Value::CreateNullValue().release();
|
2014-12-17 00:46:23 +00:00
|
|
|
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<v8::Context::Scope> scope;
|
2013-04-17 12:05:43 +00:00
|
|
|
// If val was created in a different context than our current one, change to
|
|
|
|
// that context, but change back after val is converted.
|
|
|
|
if (!val->CreationContext().IsEmpty() &&
|
2014-06-28 14:33:00 +00:00
|
|
|
val->CreationContext() != isolate->GetCurrentContext())
|
2013-04-17 12:05:43 +00:00
|
|
|
scope.reset(new v8::Context::Scope(val->CreationContext()));
|
|
|
|
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Array> property_names(val->GetOwnPropertyNames());
|
2013-04-17 12:05:43 +00:00
|
|
|
|
2016-03-08 04:40:10 +00:00
|
|
|
for (uint32_t i = 0; i < property_names->Length(); ++i) {
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Value> key(property_names->Get(i));
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
// Extend this test to cover more types as necessary and if sensible.
|
|
|
|
if (!key->IsString() &&
|
|
|
|
!key->IsNumber()) {
|
2014-06-28 14:33:00 +00:00
|
|
|
NOTREACHED() << "Key \"" << *v8::String::Utf8Value(key) << "\" "
|
2013-04-17 12:05:43 +00:00
|
|
|
"is neither a string nor a number";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip all callbacks: crbug.com/139933
|
|
|
|
if (val->HasRealNamedCallbackProperty(key->ToString()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
v8::String::Utf8Value name_utf8(key->ToString());
|
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Value> child_v8 = val->Get(key);
|
2013-04-17 12:05:43 +00:00
|
|
|
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
LOG(ERROR) << "Getter for property " << *name_utf8
|
|
|
|
<< " threw an exception.";
|
2014-06-28 14:33:00 +00:00
|
|
|
child_v8 = v8::Null(isolate);
|
2013-04-17 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 04:03:43 +00:00
|
|
|
std::unique_ptr<base::Value> child(
|
|
|
|
FromV8ValueImpl(state, child_v8, isolate));
|
2013-04-17 12:05:43 +00:00
|
|
|
if (!child.get())
|
|
|
|
// JSON.stringify skips properties whose values don't serialize, for
|
|
|
|
// example undefined and functions. Emulate that behavior.
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Strip null if asked (and since undefined is turned into null, undefined
|
|
|
|
// too). The use case for supporting this is JSON-schema support,
|
|
|
|
// specifically for extensions, where "optional" JSON properties may be
|
|
|
|
// represented as null, yet due to buggy legacy code elsewhere isn't
|
|
|
|
// treated as such (potentially causing crashes). For example, the
|
|
|
|
// "tabs.create" function takes an object as its first argument with an
|
|
|
|
// optional "windowId" property.
|
|
|
|
//
|
|
|
|
// Given just
|
|
|
|
//
|
|
|
|
// tabs.create({})
|
|
|
|
//
|
|
|
|
// this will work as expected on code that only checks for the existence of
|
|
|
|
// a "windowId" property (such as that legacy code). However given
|
|
|
|
//
|
|
|
|
// tabs.create({windowId: null})
|
|
|
|
//
|
|
|
|
// there *is* a "windowId" property, but since it should be an int, code
|
|
|
|
// on the browser which doesn't additionally check for null will fail.
|
|
|
|
// We can avoid all bugs related to this by stripping null.
|
2014-06-28 14:33:00 +00:00
|
|
|
if (strip_null_from_objects_ && child->IsType(base::Value::TYPE_NULL))
|
2013-04-17 12:05:43 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()),
|
|
|
|
child.release());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|