Commit 72f86a1d authored by Hitoshi Yoshida's avatar Hitoshi Yoshida Committed by Commit Bot

IDL compiler: Use inner type names for IDLUnion member accessors

Generally we use type names of each member as accessor names
in generated union implementations.
But for annotated types, the difference from its inner type is
the conversion from V8 values to native values. i.e. NativeValueTraits.
It means that their C++ accessors for each member work as same with
their inner types' ones.
And it is annoying to use annotated type's names for other objectives
like IsLongClamp() and SetStringTreatNullAsEmptyString("foo").

So this CL makes the generator to use inner type names in
IDLUnion member accessors.
It means we do not have to consider if a member has extended
attributes or not, when we access the members.


Bug: 894469
Change-Id: I80a6ea2366405a7ae344d740e1611cea20b88d9e
Reviewed-on: https://chromium-review.googlesource.com/c/1280382
Commit-Queue: Hitoshi Yoshida <peria@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#603047}
parent d833d2fe
...@@ -159,7 +159,7 @@ def member_context(member, info_provider): ...@@ -159,7 +159,7 @@ def member_context(member, info_provider):
'is_traceable': member.is_traceable, 'is_traceable': member.is_traceable,
'rvalue_cpp_type': member.cpp_type_args(used_as_rvalue_type=True), 'rvalue_cpp_type': member.cpp_type_args(used_as_rvalue_type=True),
'specific_type_enum': 'k' + member.name, 'specific_type_enum': 'k' + member.name,
'type_name': member.name, 'type_name': member.inner_type.name if member.is_annotated_type else member.name,
'v8_value_to_local_cpp_value': member.v8_value_to_local_cpp_value( 'v8_value_to_local_cpp_value': member.v8_value_to_local_cpp_value(
{}, 'v8Value', 'cppValue', isolate='isolate', {}, 'v8Value', 'cppValue', isolate='isolate',
use_exception_state=True) use_exception_state=True)
......
...@@ -46,6 +46,7 @@ dictionary TestDictionary { ...@@ -46,6 +46,7 @@ dictionary TestDictionary {
record<ByteString, (long or boolean)> unionInRecordMember; record<ByteString, (long or boolean)> unionInRecordMember;
record<DOMString, any> anyInRecordMember; record<DOMString, any> anyInRecordMember;
(Float or BooleanType) unionWithTypedefs; (Float or BooleanType) unionWithTypedefs;
([TreatNullAs=EmptyString] DOMString or long) unionWithAnnotatedTypeMember;
[Clamp] long applicableToTypeLongMember; [Clamp] long applicableToTypeLongMember;
[TreatNullAs=EmptyString] DOMString applicableToTypeStringMember; [TreatNullAs=EmptyString] DOMString applicableToTypeStringMember;
(double or sequence<double>) unionMemberWithSequenceDefault = []; (double or sequence<double>) unionMemberWithSequenceDefault = [];
......
// 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/core/string_treat_null_as_empty_string_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 {
StringTreatNullAsEmptyStringOrLong::StringTreatNullAsEmptyStringOrLong() : type_(SpecificType::kNone) {}
int32_t StringTreatNullAsEmptyStringOrLong::GetAsLong() const {
DCHECK(IsLong());
return long_;
}
void StringTreatNullAsEmptyStringOrLong::SetLong(int32_t value) {
DCHECK(IsNull());
long_ = value;
type_ = SpecificType::kLong;
}
StringTreatNullAsEmptyStringOrLong StringTreatNullAsEmptyStringOrLong::FromLong(int32_t value) {
StringTreatNullAsEmptyStringOrLong container;
container.SetLong(value);
return container;
}
const String& StringTreatNullAsEmptyStringOrLong::GetAsString() const {
DCHECK(IsString());
return string_treat_null_as_empty_string_;
}
void StringTreatNullAsEmptyStringOrLong::SetString(const String& value) {
DCHECK(IsNull());
string_treat_null_as_empty_string_ = value;
type_ = SpecificType::kStringTreatNullAsEmptyString;
}
StringTreatNullAsEmptyStringOrLong StringTreatNullAsEmptyStringOrLong::FromString(const String& value) {
StringTreatNullAsEmptyStringOrLong container;
container.SetString(value);
return container;
}
StringTreatNullAsEmptyStringOrLong::StringTreatNullAsEmptyStringOrLong(const StringTreatNullAsEmptyStringOrLong&) = default;
StringTreatNullAsEmptyStringOrLong::~StringTreatNullAsEmptyStringOrLong() = default;
StringTreatNullAsEmptyStringOrLong& StringTreatNullAsEmptyStringOrLong::operator=(const StringTreatNullAsEmptyStringOrLong&) = default;
void StringTreatNullAsEmptyStringOrLong::Trace(blink::Visitor* visitor) {
}
void V8StringTreatNullAsEmptyStringOrLong::ToImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value, StringTreatNullAsEmptyStringOrLong& impl, UnionTypeConversionMode conversionMode, ExceptionState& exceptionState) {
if (v8Value.IsEmpty())
return;
if (conversionMode == UnionTypeConversionMode::kNullable && IsUndefinedOrNull(v8Value))
return;
if (v8Value->IsNumber()) {
int32_t cppValue = NativeValueTraits<IDLLong>::NativeValue(isolate, v8Value, exceptionState);
if (exceptionState.HadException())
return;
impl.SetLong(cppValue);
return;
}
{
V8StringResource<kTreatNullAsEmptyString> cppValue = v8Value;
if (!cppValue.Prepare(exceptionState))
return;
impl.SetString(cppValue);
return;
}
}
v8::Local<v8::Value> ToV8(const StringTreatNullAsEmptyStringOrLong& impl, v8::Local<v8::Object> creationContext, v8::Isolate* isolate) {
switch (impl.type_) {
case StringTreatNullAsEmptyStringOrLong::SpecificType::kNone:
return v8::Null(isolate);
case StringTreatNullAsEmptyStringOrLong::SpecificType::kLong:
return v8::Integer::New(isolate, impl.GetAsLong());
case StringTreatNullAsEmptyStringOrLong::SpecificType::kStringTreatNullAsEmptyString:
return V8String(isolate, impl.GetAsStringTreatNullAsEmptyString());
default:
NOTREACHED();
}
return v8::Local<v8::Value>();
}
StringTreatNullAsEmptyStringOrLong NativeValueTraits<StringTreatNullAsEmptyStringOrLong>::NativeValue(v8::Isolate* isolate, v8::Local<v8::Value> value, ExceptionState& exceptionState) {
StringTreatNullAsEmptyStringOrLong impl;
V8StringTreatNullAsEmptyStringOrLong::ToImpl(isolate, value, impl, UnionTypeConversionMode::kNotNullable, exceptionState);
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_CORE_STRING_TREAT_NULL_AS_EMPTY_STRING_OR_LONG_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_TESTS_RESULTS_CORE_STRING_TREAT_NULL_AS_EMPTY_STRING_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/core/core_export.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
namespace blink {
class CORE_EXPORT StringTreatNullAsEmptyStringOrLong final {
DISALLOW_NEW();
public:
StringTreatNullAsEmptyStringOrLong();
bool IsNull() const { return type_ == SpecificType::kNone; }
bool IsLong() const { return type_ == SpecificType::kLong; }
int32_t GetAsLong() const;
void SetLong(int32_t);
static StringTreatNullAsEmptyStringOrLong FromLong(int32_t);
bool IsString() const { return type_ == SpecificType::kStringTreatNullAsEmptyString; }
const String& GetAsString() const;
void SetString(const String&);
static StringTreatNullAsEmptyStringOrLong FromString(const String&);
StringTreatNullAsEmptyStringOrLong(const StringTreatNullAsEmptyStringOrLong&);
~StringTreatNullAsEmptyStringOrLong();
StringTreatNullAsEmptyStringOrLong& operator=(const StringTreatNullAsEmptyStringOrLong&);
void Trace(blink::Visitor*);
private:
enum class SpecificType {
kNone,
kLong,
kStringTreatNullAsEmptyString,
};
SpecificType type_;
int32_t long_;
String string_treat_null_as_empty_string_;
friend CORE_EXPORT v8::Local<v8::Value> ToV8(const StringTreatNullAsEmptyStringOrLong&, v8::Local<v8::Object>, v8::Isolate*);
};
class V8StringTreatNullAsEmptyStringOrLong final {
public:
CORE_EXPORT static void ToImpl(v8::Isolate*, v8::Local<v8::Value>, StringTreatNullAsEmptyStringOrLong&, UnionTypeConversionMode, ExceptionState&);
};
CORE_EXPORT v8::Local<v8::Value> ToV8(const StringTreatNullAsEmptyStringOrLong&, v8::Local<v8::Object>, v8::Isolate*);
template <class CallbackInfo>
inline void V8SetReturnValue(const CallbackInfo& callbackInfo, StringTreatNullAsEmptyStringOrLong& impl) {
V8SetReturnValue(callbackInfo, ToV8(impl, callbackInfo.Holder(), callbackInfo.GetIsolate()));
}
template <class CallbackInfo>
inline void V8SetReturnValue(const CallbackInfo& callbackInfo, StringTreatNullAsEmptyStringOrLong& impl, v8::Local<v8::Object> creationContext) {
V8SetReturnValue(callbackInfo, ToV8(impl, creationContext, callbackInfo.GetIsolate()));
}
template <>
struct NativeValueTraits<StringTreatNullAsEmptyStringOrLong> : public NativeValueTraitsBase<StringTreatNullAsEmptyStringOrLong> {
CORE_EXPORT static StringTreatNullAsEmptyStringOrLong NativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&);
CORE_EXPORT static StringTreatNullAsEmptyStringOrLong NullValue() { return StringTreatNullAsEmptyStringOrLong(); }
};
template <>
struct V8TypeOf<StringTreatNullAsEmptyStringOrLong> {
typedef V8StringTreatNullAsEmptyStringOrLong 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::StringTreatNullAsEmptyStringOrLong);
#endif // THIRD_PARTY_BLINK_RENDERER_BINDINGS_TESTS_RESULTS_CORE_STRING_TREAT_NULL_AS_EMPTY_STRING_OR_LONG_H_
...@@ -194,6 +194,10 @@ void TestDictionary::setUnionOrNullSequenceMember(const HeapVector<DoubleOrStrin ...@@ -194,6 +194,10 @@ void TestDictionary::setUnionOrNullSequenceMember(const HeapVector<DoubleOrStrin
has_union_or_null_sequence_member_ = true; has_union_or_null_sequence_member_ = true;
} }
void TestDictionary::setUnionWithAnnotatedTypeMember(const StringTreatNullAsEmptyStringOrLong& value) {
union_with_annotated_type_member_ = value;
}
void TestDictionary::setUnionWithTypedefs(const FloatOrBoolean& value) { void TestDictionary::setUnionWithTypedefs(const FloatOrBoolean& value) {
union_with_typedefs_ = value; union_with_typedefs_ = value;
} }
...@@ -224,6 +228,7 @@ void TestDictionary::Trace(blink::Visitor* visitor) { ...@@ -224,6 +228,7 @@ void TestDictionary::Trace(blink::Visitor* visitor) {
visitor->Trace(union_member_with_sequence_default_); visitor->Trace(union_member_with_sequence_default_);
visitor->Trace(union_or_null_record_member_); visitor->Trace(union_or_null_record_member_);
visitor->Trace(union_or_null_sequence_member_); visitor->Trace(union_or_null_sequence_member_);
visitor->Trace(union_with_annotated_type_member_);
visitor->Trace(union_with_typedefs_); visitor->Trace(union_with_typedefs_);
IDLDictionaryBase::Trace(visitor); IDLDictionaryBase::Trace(visitor);
} }
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "third_party/blink/renderer/bindings/core/v8/idl_dictionary_base.h" #include "third_party/blink/renderer/bindings/core/v8/idl_dictionary_base.h"
#include "third_party/blink/renderer/bindings/core/v8/long_or_boolean.h" #include "third_party/blink/renderer/bindings/core/v8/long_or_boolean.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h" #include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/string_treat_null_as_empty_string_or_long.h"
#include "third_party/blink/renderer/bindings/core/v8/test_enum_or_test_enum_or_null_sequence.h" #include "third_party/blink/renderer/bindings/core/v8/test_enum_or_test_enum_or_null_sequence.h"
#include "third_party/blink/renderer/bindings/core/v8/test_enum_or_test_enum_sequence.h" #include "third_party/blink/renderer/bindings/core/v8/test_enum_or_test_enum_sequence.h"
#include "third_party/blink/renderer/bindings/core/v8/test_interface_2_or_uint8_array.h" #include "third_party/blink/renderer/bindings/core/v8/test_interface_2_or_uint8_array.h"
...@@ -421,6 +422,12 @@ class CORE_EXPORT TestDictionary : public IDLDictionaryBase { ...@@ -421,6 +422,12 @@ class CORE_EXPORT TestDictionary : public IDLDictionaryBase {
} }
void setUnionOrNullSequenceMember(const HeapVector<DoubleOrString>&); void setUnionOrNullSequenceMember(const HeapVector<DoubleOrString>&);
bool hasUnionWithAnnotatedTypeMember() const { return !union_with_annotated_type_member_.IsNull(); }
const StringTreatNullAsEmptyStringOrLong& unionWithAnnotatedTypeMember() const {
return union_with_annotated_type_member_;
}
void setUnionWithAnnotatedTypeMember(const StringTreatNullAsEmptyStringOrLong&);
bool hasUnionWithTypedefs() const { return !union_with_typedefs_.IsNull(); } bool hasUnionWithTypedefs() const { return !union_with_typedefs_.IsNull(); }
const FloatOrBoolean& unionWithTypedefs() const { const FloatOrBoolean& unionWithTypedefs() const {
return union_with_typedefs_; return union_with_typedefs_;
...@@ -536,6 +543,7 @@ class CORE_EXPORT TestDictionary : public IDLDictionaryBase { ...@@ -536,6 +543,7 @@ class CORE_EXPORT TestDictionary : public IDLDictionaryBase {
DoubleOrDoubleSequence union_member_with_sequence_default_; DoubleOrDoubleSequence union_member_with_sequence_default_;
HeapVector<std::pair<String, DoubleOrString>> union_or_null_record_member_; HeapVector<std::pair<String, DoubleOrString>> union_or_null_record_member_;
HeapVector<DoubleOrString> union_or_null_sequence_member_; HeapVector<DoubleOrString> union_or_null_sequence_member_;
StringTreatNullAsEmptyStringOrLong union_with_annotated_type_member_;
FloatOrBoolean union_with_typedefs_; FloatOrBoolean union_with_typedefs_;
double unrestricted_double_member_; double unrestricted_double_member_;
String usv_string_or_null_member_; String usv_string_or_null_member_;
......
...@@ -92,6 +92,7 @@ static const v8::Eternal<v8::Name>* eternalV8TestDictionaryKeys(v8::Isolate* iso ...@@ -92,6 +92,7 @@ static const v8::Eternal<v8::Name>* eternalV8TestDictionaryKeys(v8::Isolate* iso
"unionMemberWithSequenceDefault", "unionMemberWithSequenceDefault",
"unionOrNullRecordMember", "unionOrNullRecordMember",
"unionOrNullSequenceMember", "unionOrNullSequenceMember",
"unionWithAnnotatedTypeMember",
"unionWithTypedefs", "unionWithTypedefs",
"unrestrictedDoubleMember", "unrestrictedDoubleMember",
"usvStringOrNullMember", "usvStringOrNullMember",
...@@ -916,8 +917,23 @@ void V8TestDictionary::ToImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value ...@@ -916,8 +917,23 @@ void V8TestDictionary::ToImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value
impl.setUnionOrNullSequenceMember(union_or_null_sequence_member_cpp_value); impl.setUnionOrNullSequenceMember(union_or_null_sequence_member_cpp_value);
} }
v8::Local<v8::Value> union_with_annotated_type_member_value;
if (!v8Object->Get(context, keys[57].Get(isolate)).ToLocal(&union_with_annotated_type_member_value)) {
exceptionState.RethrowV8Exception(block.Exception());
return;
}
if (union_with_annotated_type_member_value.IsEmpty() || union_with_annotated_type_member_value->IsUndefined()) {
// Do nothing.
} else {
StringTreatNullAsEmptyStringOrLong union_with_annotated_type_member_cpp_value;
V8StringTreatNullAsEmptyStringOrLong::ToImpl(isolate, union_with_annotated_type_member_value, union_with_annotated_type_member_cpp_value, UnionTypeConversionMode::kNotNullable, exceptionState);
if (exceptionState.HadException())
return;
impl.setUnionWithAnnotatedTypeMember(union_with_annotated_type_member_cpp_value);
}
v8::Local<v8::Value> union_with_typedefs_value; v8::Local<v8::Value> union_with_typedefs_value;
if (!v8Object->Get(context, keys[57].Get(isolate)).ToLocal(&union_with_typedefs_value)) { if (!v8Object->Get(context, keys[58].Get(isolate)).ToLocal(&union_with_typedefs_value)) {
exceptionState.RethrowV8Exception(block.Exception()); exceptionState.RethrowV8Exception(block.Exception());
return; return;
} }
...@@ -932,7 +948,7 @@ void V8TestDictionary::ToImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value ...@@ -932,7 +948,7 @@ void V8TestDictionary::ToImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value
} }
v8::Local<v8::Value> unrestricted_double_member_value; v8::Local<v8::Value> unrestricted_double_member_value;
if (!v8Object->Get(context, keys[58].Get(isolate)).ToLocal(&unrestricted_double_member_value)) { if (!v8Object->Get(context, keys[59].Get(isolate)).ToLocal(&unrestricted_double_member_value)) {
exceptionState.RethrowV8Exception(block.Exception()); exceptionState.RethrowV8Exception(block.Exception());
return; return;
} }
...@@ -946,7 +962,7 @@ void V8TestDictionary::ToImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value ...@@ -946,7 +962,7 @@ void V8TestDictionary::ToImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value
} }
v8::Local<v8::Value> usv_string_or_null_member_value; v8::Local<v8::Value> usv_string_or_null_member_value;
if (!v8Object->Get(context, keys[59].Get(isolate)).ToLocal(&usv_string_or_null_member_value)) { if (!v8Object->Get(context, keys[60].Get(isolate)).ToLocal(&usv_string_or_null_member_value)) {
exceptionState.RethrowV8Exception(block.Exception()); exceptionState.RethrowV8Exception(block.Exception());
return; return;
} }
...@@ -1679,6 +1695,17 @@ bool toV8TestDictionary(const TestDictionary& impl, v8::Local<v8::Object> dictio ...@@ -1679,6 +1695,17 @@ bool toV8TestDictionary(const TestDictionary& impl, v8::Local<v8::Object> dictio
return false; return false;
} }
v8::Local<v8::Value> union_with_annotated_type_member_value;
bool union_with_annotated_type_member_has_value_or_default = false;
if (impl.hasUnionWithAnnotatedTypeMember()) {
union_with_annotated_type_member_value = ToV8(impl.unionWithAnnotatedTypeMember(), creationContext, isolate);
union_with_annotated_type_member_has_value_or_default = true;
}
if (union_with_annotated_type_member_has_value_or_default &&
!create_property(57, union_with_annotated_type_member_value)) {
return false;
}
v8::Local<v8::Value> union_with_typedefs_value; v8::Local<v8::Value> union_with_typedefs_value;
bool union_with_typedefs_has_value_or_default = false; bool union_with_typedefs_has_value_or_default = false;
if (impl.hasUnionWithTypedefs()) { if (impl.hasUnionWithTypedefs()) {
...@@ -1686,7 +1713,7 @@ bool toV8TestDictionary(const TestDictionary& impl, v8::Local<v8::Object> dictio ...@@ -1686,7 +1713,7 @@ bool toV8TestDictionary(const TestDictionary& impl, v8::Local<v8::Object> dictio
union_with_typedefs_has_value_or_default = true; union_with_typedefs_has_value_or_default = true;
} }
if (union_with_typedefs_has_value_or_default && if (union_with_typedefs_has_value_or_default &&
!create_property(57, union_with_typedefs_value)) { !create_property(58, union_with_typedefs_value)) {
return false; return false;
} }
...@@ -1700,7 +1727,7 @@ bool toV8TestDictionary(const TestDictionary& impl, v8::Local<v8::Object> dictio ...@@ -1700,7 +1727,7 @@ bool toV8TestDictionary(const TestDictionary& impl, v8::Local<v8::Object> dictio
unrestricted_double_member_has_value_or_default = true; unrestricted_double_member_has_value_or_default = true;
} }
if (unrestricted_double_member_has_value_or_default && if (unrestricted_double_member_has_value_or_default &&
!create_property(58, unrestricted_double_member_value)) { !create_property(59, unrestricted_double_member_value)) {
return false; return false;
} }
...@@ -1714,7 +1741,7 @@ bool toV8TestDictionary(const TestDictionary& impl, v8::Local<v8::Object> dictio ...@@ -1714,7 +1741,7 @@ bool toV8TestDictionary(const TestDictionary& impl, v8::Local<v8::Object> dictio
usv_string_or_null_member_has_value_or_default = true; usv_string_or_null_member_has_value_or_default = true;
} }
if (usv_string_or_null_member_has_value_or_default && if (usv_string_or_null_member_has_value_or_default &&
!create_property(59, usv_string_or_null_member_value)) { !create_property(60, usv_string_or_null_member_value)) {
return false; return false;
} }
......
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