Commit 3fe3b9e4 authored by Hitoshi Yoshida's avatar Hitoshi Yoshida Committed by Commit Bot

IDL Compiler: Make default dictionary value work for union

Before this CL, a default dictionary value {} was not usable
for union types that contains a dictionary type, though it is
allowed in spec.

This CL makes it possible.


Bug: 839389, 984949
Change-Id: I4c053a482d5b9623dea665e63ae9ad267555730e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838800Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Commit-Queue: Hitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702733}
parent 87cd452e
......@@ -364,6 +364,11 @@ class IdlUnionType(IdlTypeBase):
return self.single_matching_member_type(
lambda member_type: member_type.is_sequence_type)
@property
def dictionary_member_type(self):
return self.single_matching_member_type(
lambda member_type: member_type.is_dictionary)
@property
def as_union_type(self):
# Note: Use this to "look through" a possible IdlNullableType wrapper.
......
......@@ -1071,6 +1071,8 @@ def union_literal_cpp_value(idl_type, idl_literal):
member_type = idl_type.boolean_member_type
elif idl_literal.idl_type == 'sequence':
member_type = idl_type.sequence_member_type
elif idl_literal.idl_type == 'dictionary':
member_type = idl_type.dictionary_member_type
else:
raise ValueError('Unsupported literal type: ' + idl_literal.idl_type)
......
......@@ -4,4 +4,5 @@
dictionary TestDictionary2 {
TestDictionary defaultEmptyDictionary = {};
(TestDictionary or long) defaultEmptyDictionaryForUnion = {};
};
......@@ -14,6 +14,7 @@ namespace blink {
TestDictionary2::TestDictionary2() {
setDefaultEmptyDictionary(MakeGarbageCollected<TestDictionary>());
setDefaultEmptyDictionaryForUnion(TestDictionaryOrLong::FromTestDictionary(MakeGarbageCollected<TestDictionary>()));
}
TestDictionary2::~TestDictionary2() = default;
......@@ -22,8 +23,13 @@ void TestDictionary2::setDefaultEmptyDictionary(TestDictionary* value) {
default_empty_dictionary_ = value;
}
void TestDictionary2::setDefaultEmptyDictionaryForUnion(const TestDictionaryOrLong& value) {
default_empty_dictionary_for_union_ = value;
}
void TestDictionary2::Trace(blink::Visitor* visitor) {
visitor->Trace(default_empty_dictionary_);
visitor->Trace(default_empty_dictionary_for_union_);
IDLDictionaryBase::Trace(visitor);
}
......
......@@ -12,6 +12,7 @@
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_TESTS_RESULTS_MODULES_TEST_DICTIONARY_2_H_
#include "third_party/blink/renderer/bindings/core/v8/idl_dictionary_base.h"
#include "third_party/blink/renderer/bindings/modules/v8/test_dictionary_or_long.h"
#include "third_party/blink/renderer/bindings/tests/idls/core/test_dictionary.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
......@@ -31,12 +32,19 @@ class MODULES_EXPORT TestDictionary2 : public IDLDictionaryBase {
}
void setDefaultEmptyDictionary(TestDictionary*);
bool hasDefaultEmptyDictionaryForUnion() const { return !default_empty_dictionary_for_union_.IsNull(); }
const TestDictionaryOrLong& defaultEmptyDictionaryForUnion() const {
return default_empty_dictionary_for_union_;
}
void setDefaultEmptyDictionaryForUnion(const TestDictionaryOrLong&);
v8::Local<v8::Value> ToV8Impl(v8::Local<v8::Object>, v8::Isolate*) const override;
void Trace(blink::Visitor*) override;
private:
Member<TestDictionary> default_empty_dictionary_;
TestDictionaryOrLong default_empty_dictionary_for_union_;
friend class V8TestDictionary2;
};
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file has been auto-generated from the Jinja2 template
// third_party/blink/renderer/bindings/templates/union_container.cc.tmpl
// by the script code_generator_v8.py.
// DO NOT MODIFY!
// clang-format off
#include "third_party/blink/renderer/bindings/tests/results/modules/test_dictionary_or_long.h"
#include "base/stl_util.h"
#include "third_party/blink/renderer/bindings/core/v8/idl_types.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
#include "third_party/blink/renderer/bindings/core/v8/to_v8_for_core.h"
namespace blink {
TestDictionaryOrLong::TestDictionaryOrLong() : type_(SpecificType::kNone) {}
int32_t TestDictionaryOrLong::GetAsLong() const {
DCHECK(IsLong());
return long_;
}
void TestDictionaryOrLong::SetLong(int32_t value) {
DCHECK(IsNull());
long_ = value;
type_ = SpecificType::kLong;
}
TestDictionaryOrLong TestDictionaryOrLong::FromLong(int32_t value) {
TestDictionaryOrLong container;
container.SetLong(value);
return container;
}
TestDictionary* TestDictionaryOrLong::GetAsTestDictionary() const {
DCHECK(IsTestDictionary());
return test_dictionary_;
}
void TestDictionaryOrLong::SetTestDictionary(TestDictionary* value) {
DCHECK(IsNull());
test_dictionary_ = value;
type_ = SpecificType::kTestDictionary;
}
TestDictionaryOrLong TestDictionaryOrLong::FromTestDictionary(TestDictionary* value) {
TestDictionaryOrLong container;
container.SetTestDictionary(value);
return container;
}
TestDictionaryOrLong::TestDictionaryOrLong(const TestDictionaryOrLong&) = default;
TestDictionaryOrLong::~TestDictionaryOrLong() = default;
TestDictionaryOrLong& TestDictionaryOrLong::operator=(const TestDictionaryOrLong&) = default;
void TestDictionaryOrLong::Trace(blink::Visitor* visitor) {
visitor->Trace(test_dictionary_);
}
void V8TestDictionaryOrLong::ToImpl(
v8::Isolate* isolate,
v8::Local<v8::Value> v8_value,
TestDictionaryOrLong& impl,
UnionTypeConversionMode conversion_mode,
ExceptionState& exception_state) {
if (v8_value.IsEmpty())
return;
if (conversion_mode == UnionTypeConversionMode::kNullable && IsUndefinedOrNull(v8_value))
return;
if (IsUndefinedOrNull(v8_value)) {
TestDictionary* cpp_value = NativeValueTraits<TestDictionary>::NativeValue(isolate, v8_value, exception_state);
if (exception_state.HadException())
return;
impl.SetTestDictionary(cpp_value);
return;
}
if (v8_value->IsObject()) {
TestDictionary* cpp_value = NativeValueTraits<TestDictionary>::NativeValue(isolate, v8_value, exception_state);
if (exception_state.HadException())
return;
impl.SetTestDictionary(cpp_value);
return;
}
if (v8_value->IsNumber()) {
int32_t cpp_value = NativeValueTraits<IDLLong>::NativeValue(isolate, v8_value, exception_state);
if (exception_state.HadException())
return;
impl.SetLong(cpp_value);
return;
}
{
int32_t cpp_value = NativeValueTraits<IDLLong>::NativeValue(isolate, v8_value, exception_state);
if (exception_state.HadException())
return;
impl.SetLong(cpp_value);
return;
}
}
v8::Local<v8::Value> ToV8(const TestDictionaryOrLong& impl, v8::Local<v8::Object> creationContext, v8::Isolate* isolate) {
switch (impl.type_) {
case TestDictionaryOrLong::SpecificType::kNone:
return v8::Null(isolate);
case TestDictionaryOrLong::SpecificType::kLong:
return v8::Integer::New(isolate, impl.GetAsLong());
case TestDictionaryOrLong::SpecificType::kTestDictionary:
return ToV8(impl.GetAsTestDictionary(), creationContext, isolate);
default:
NOTREACHED();
}
return v8::Local<v8::Value>();
}
TestDictionaryOrLong NativeValueTraits<TestDictionaryOrLong>::NativeValue(
v8::Isolate* isolate, v8::Local<v8::Value> value, ExceptionState& exception_state) {
TestDictionaryOrLong impl;
V8TestDictionaryOrLong::ToImpl(isolate, value, impl, UnionTypeConversionMode::kNotNullable, exception_state);
return impl;
}
} // namespace blink
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file has been auto-generated from the Jinja2 template
// third_party/blink/renderer/bindings/templates/union_container.h.tmpl
// by the script code_generator_v8.py.
// DO NOT MODIFY!
// clang-format off
#ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_TESTS_RESULTS_MODULES_TEST_DICTIONARY_OR_LONG_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_TESTS_RESULTS_MODULES_TEST_DICTIONARY_OR_LONG_H_
#include "base/optional.h"
#include "third_party/blink/renderer/bindings/core/v8/dictionary.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_test_dictionary.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
namespace blink {
class MODULES_EXPORT TestDictionaryOrLong final {
DISALLOW_NEW();
public:
TestDictionaryOrLong();
bool IsNull() const { return type_ == SpecificType::kNone; }
bool IsLong() const { return type_ == SpecificType::kLong; }
int32_t GetAsLong() const;
void SetLong(int32_t);
static TestDictionaryOrLong FromLong(int32_t);
bool IsTestDictionary() const { return type_ == SpecificType::kTestDictionary; }
TestDictionary* GetAsTestDictionary() const;
void SetTestDictionary(TestDictionary*);
static TestDictionaryOrLong FromTestDictionary(TestDictionary*);
TestDictionaryOrLong(const TestDictionaryOrLong&);
~TestDictionaryOrLong();
TestDictionaryOrLong& operator=(const TestDictionaryOrLong&);
void Trace(blink::Visitor*);
private:
enum class SpecificType {
kNone,
kLong,
kTestDictionary,
};
SpecificType type_;
int32_t long_;
Member<TestDictionary> test_dictionary_;
friend MODULES_EXPORT v8::Local<v8::Value> ToV8(const TestDictionaryOrLong&, v8::Local<v8::Object>, v8::Isolate*);
};
class V8TestDictionaryOrLong final {
public:
MODULES_EXPORT static void ToImpl(v8::Isolate*, v8::Local<v8::Value>, TestDictionaryOrLong&, UnionTypeConversionMode, ExceptionState&);
};
MODULES_EXPORT v8::Local<v8::Value> ToV8(const TestDictionaryOrLong&, v8::Local<v8::Object>, v8::Isolate*);
template <class CallbackInfo>
inline void V8SetReturnValue(const CallbackInfo& callbackInfo, TestDictionaryOrLong& impl) {
V8SetReturnValue(callbackInfo, ToV8(impl, callbackInfo.Holder(), callbackInfo.GetIsolate()));
}
template <class CallbackInfo>
inline void V8SetReturnValue(const CallbackInfo& callbackInfo, TestDictionaryOrLong& impl, v8::Local<v8::Object> creationContext) {
V8SetReturnValue(callbackInfo, ToV8(impl, creationContext, callbackInfo.GetIsolate()));
}
template <>
struct NativeValueTraits<TestDictionaryOrLong> : public NativeValueTraitsBase<TestDictionaryOrLong> {
MODULES_EXPORT static TestDictionaryOrLong NativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&);
MODULES_EXPORT static TestDictionaryOrLong NullValue() { return TestDictionaryOrLong(); }
};
template <>
struct V8TypeOf<TestDictionaryOrLong> {
typedef V8TestDictionaryOrLong Type;
};
} // namespace blink
// We need to set canInitializeWithMemset=true because HeapVector supports
// items that can initialize with memset or have a vtable. It is safe to
// set canInitializeWithMemset=true for a union type object in practice.
// See https://codereview.chromium.org/1118993002/#msg5 for more details.
WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(blink::TestDictionaryOrLong)
#endif // THIRD_PARTY_BLINK_RENDERER_BINDINGS_TESTS_RESULTS_MODULES_TEST_DICTIONARY_OR_LONG_H_
......@@ -11,6 +11,8 @@
#include "third_party/blink/renderer/bindings/tests/results/modules/v8_test_dictionary_2.h"
#include "base/stl_util.h"
#include "third_party/blink/renderer/bindings/core/v8/idl_types.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_test_dictionary.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
......@@ -19,6 +21,7 @@ namespace blink {
static const v8::Eternal<v8::Name>* eternalV8TestDictionary2Keys(v8::Isolate* isolate) {
static const char* const kKeys[] = {
"defaultEmptyDictionary",
"defaultEmptyDictionaryForUnion",
};
return V8PerIsolateData::From(isolate)->FindOrCreateEternalNameCache(
kKeys, kKeys, base::size(kKeys));
......@@ -51,6 +54,21 @@ void V8TestDictionary2::ToImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8_val
return;
impl->setDefaultEmptyDictionary(default_empty_dictionary_cpp_value);
}
v8::Local<v8::Value> default_empty_dictionary_for_union_value;
if (!v8Object->Get(context, keys[1].Get(isolate)).ToLocal(&default_empty_dictionary_for_union_value)) {
exception_state.RethrowV8Exception(block.Exception());
return;
}
if (default_empty_dictionary_for_union_value.IsEmpty() || default_empty_dictionary_for_union_value->IsUndefined()) {
// Do nothing.
} else {
TestDictionaryOrLong default_empty_dictionary_for_union_cpp_value;
V8TestDictionaryOrLong::ToImpl(isolate, default_empty_dictionary_for_union_value, default_empty_dictionary_for_union_cpp_value, UnionTypeConversionMode::kNotNullable, exception_state);
if (exception_state.HadException())
return;
impl->setDefaultEmptyDictionaryForUnion(default_empty_dictionary_for_union_cpp_value);
}
}
v8::Local<v8::Value> TestDictionary2::ToV8Impl(v8::Local<v8::Object> creationContext, v8::Isolate* isolate) const {
......@@ -89,6 +107,20 @@ bool toV8TestDictionary2(const TestDictionary2* impl, v8::Local<v8::Object> dict
return false;
}
v8::Local<v8::Value> default_empty_dictionary_for_union_value;
bool default_empty_dictionary_for_union_has_value_or_default = false;
if (impl->hasDefaultEmptyDictionaryForUnion()) {
default_empty_dictionary_for_union_value = ToV8(impl->defaultEmptyDictionaryForUnion(), creationContext, isolate);
default_empty_dictionary_for_union_has_value_or_default = true;
} else {
default_empty_dictionary_for_union_value = ToV8(TestDictionaryOrLong::FromTestDictionary(MakeGarbageCollected<TestDictionary>()), creationContext, isolate);
default_empty_dictionary_for_union_has_value_or_default = true;
}
if (default_empty_dictionary_for_union_has_value_or_default &&
!create_property(1, default_empty_dictionary_for_union_value)) {
return false;
}
return true;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment