Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: third_party/WebKit/Source/bindings/templates/union_container.cpp

Issue 2001493002: Update union conversion algorithm (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 {% from 'utilities.cpp' import declare_enum_validation_variable %} 1 {% from 'utilities.cpp' import declare_enum_validation_variable %}
2 {% include 'copyright_block.txt' %} 2 {% include 'copyright_block.txt' %}
3 #include "{{this_include_header_name}}.h" 3 #include "{{this_include_header_name}}.h"
4 4
5 {% from 'utilities.cpp' import v8_value_to_local_cpp_value %} 5 {% from 'utilities.cpp' import v8_value_to_local_cpp_value %}
6 {% macro assign_and_return_if_hasinstance(member) %} 6 {% macro assign_and_return_if_hasinstance(member) %}
7 if (V8{{member.type_name}}::hasInstance(v8Value, isolate)) { 7 if (V8{{member.type_name}}::hasInstance(v8Value, isolate)) {
8 {{member.cpp_local_type}} cppValue = V8{{member.type_name}}::toImpl(v8::Loca l<v8::Object>::Cast(v8Value)); 8 {{member.cpp_local_type}} cppValue = V8{{member.type_name}}::toImpl(v8::Loca l<v8::Object>::Cast(v8Value));
9 impl.set{{member.type_name}}(cppValue); 9 impl.set{{member.type_name}}(cppValue);
10 return; 10 return;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 { 66 {
67 if (v8Value.IsEmpty()) 67 if (v8Value.IsEmpty())
68 return; 68 return;
69 69
70 {# The numbers in the following comments refer to the steps described in 70 {# The numbers in the following comments refer to the steps described in
71 http://heycam.github.io/webidl/#es-union #} 71 http://heycam.github.io/webidl/#es-union #}
72 {# 1. null or undefined #} 72 {# 1. null or undefined #}
73 if (conversionMode == UnionTypeConversionMode::Nullable && isUndefinedOrNull (v8Value)) 73 if (conversionMode == UnionTypeConversionMode::Nullable && isUndefinedOrNull (v8Value))
74 return; 74 return;
75 75
76 {# 3. Platform objects (interfaces) #} 76 {% if dictionary_type %}
77 {# 3. Dictionaries for null or undefined #}
78 if (isUndefinedOrNull(v8Value)) {
79 {{v8_value_to_local_cpp_value(dictionary_type) | indent(8)}}
80 impl.set{{dictionary_type.type_name}}(cppValue);
81 return;
82 }
83
84 {% endif %}
85 {# 4. Platform objects (interfaces) #}
77 {% for interface in interface_types %} 86 {% for interface in interface_types %}
78 {{assign_and_return_if_hasinstance(interface) | indent}} 87 {{assign_and_return_if_hasinstance(interface) | indent}}
79 88
80 {% endfor %} 89 {% endfor %}
81 {# 8. ArrayBuffer #} 90 {# 8. ArrayBuffer #}
82 {% if array_buffer_type %} 91 {% if array_buffer_type %}
83 {{assign_and_return_if_hasinstance(array_buffer_type) | indent}} 92 {{assign_and_return_if_hasinstance(array_buffer_type) | indent}}
84 93
85 {% endif %} 94 {% endif %}
86 {# 9., 10. ArrayBufferView #} 95 {# 9., 10. ArrayBufferView #}
87 {# FIXME: Individual typed arrays (e.g. Uint8Array) aren't supported yet. #} 96 {# FIXME: Individual typed arrays (e.g. Uint8Array) aren't supported yet. #}
88 {% if array_buffer_view_type %} 97 {% if array_buffer_view_type %}
89 {{assign_and_return_if_hasinstance(array_buffer_view_type) | indent}} 98 {{assign_and_return_if_hasinstance(array_buffer_view_type) | indent}}
90 99
91 {% endif %} 100 {% endif %}
92 {% if dictionary_type %} 101 {% if array_or_sequence_type %}
93 {# 12. Dictionaries #} 102 {# 12.1, 12.2. Arrays and Sequences #}
94 {# FIXME: This should also check "object but not Date or RegExp". Add checks 103 {# FIXME: This should also check "object but not RegExp". Add checks
95 when we implement conversions for Date and RegExp. #} 104 when we implement conversions for Date and RegExp. #}
96 {# TODO(bashi): The spec doesn't say we should check !IsArray() but otherwis e 105 {# TODO(bashi): Should check @@iterator symbol instead. #}
97 we can't distinguish a sequence<T> and a dictionary.
98 https://github.com/heycam/webidl/issues/123 #}
99 if (isUndefinedOrNull(v8Value) || (v8Value->IsObject() && !v8Value->IsArray( ))) {
100 {{v8_value_to_local_cpp_value(dictionary_type) | indent(8)}}
101 impl.set{{dictionary_type.type_name}}(cppValue);
102 return;
103 }
104
105 {% endif %}
106 {% if array_or_sequence_type %}
107 {# 13.1, 13.2. Arrays and Sequences #}
108 {# FIXME: This should also check "object but not Date or RegExp". Add checks
109 when we implement conversions for Date and RegExp. #}
110 {# FIXME: Should check for sequences too, not just Array instances. #}
111 if (v8Value->IsArray()) { 106 if (v8Value->IsArray()) {
112 {{v8_value_to_local_cpp_value(array_or_sequence_type) | indent(8)}} 107 {{v8_value_to_local_cpp_value(array_or_sequence_type) | indent(8)}}
113 impl.set{{array_or_sequence_type.type_name}}(cppValue); 108 impl.set{{array_or_sequence_type.type_name}}(cppValue);
114 return; 109 return;
115 } 110 }
116 111
117 {% endif %} 112 {% endif %}
118 {# TODO(bashi): Support 13.3 Callback interface when we need it #} 113 {% if dictionary_type %}
119 {# 13.4. Objects #} 114 {# 12.3. Dictionaries #}
115 if (v8Value->IsObject()) {
116 {{v8_value_to_local_cpp_value(dictionary_type) | indent(8)}}
117 impl.set{{dictionary_type.type_name}}(cppValue);
118 return;
119 }
120
121 {% endif %}
122 {# TODO(bashi): Support 12.4 Callback interface when we need it #}
123 {# 12.5. Objects #}
120 {% if object_type %} 124 {% if object_type %}
121 if (isUndefinedOrNull(v8Value) || v8Value->IsObject()) { 125 if (isUndefinedOrNull(v8Value) || v8Value->IsObject()) {
122 {{v8_value_to_local_cpp_value(object_type) | indent(8)}} 126 {{v8_value_to_local_cpp_value(object_type) | indent(8)}}
123 impl.set{{object_type.type_name}}(cppValue); 127 impl.set{{object_type.type_name}}(cppValue);
124 return; 128 return;
125 } 129 }
126 130
127 {% endif %} 131 {% endif %}
128 {# FIXME: In some cases, we can omit boolean and numeric type checks because 132 {# FIXME: In some cases, we can omit boolean and numeric type checks because
129 we have fallback conversions. (step 17 and 18) #} 133 we have fallback conversions. (step 16 and 17) #}
130 {% if boolean_type %} 134 {% if boolean_type %}
131 {# 14. Boolean #} 135 {# 13. Boolean #}
132 if (v8Value->IsBoolean()) { 136 if (v8Value->IsBoolean()) {
133 impl.setBoolean(v8Value.As<v8::Boolean>()->Value()); 137 impl.setBoolean(v8Value.As<v8::Boolean>()->Value());
134 return; 138 return;
135 } 139 }
136 140
137 {% endif %} 141 {% endif %}
138 {% if numeric_type %} 142 {% if numeric_type %}
139 {# 15. Number #} 143 {# 14. Number #}
140 if (v8Value->IsNumber()) { 144 if (v8Value->IsNumber()) {
141 {{v8_value_to_local_cpp_value(numeric_type) | indent(8)}} 145 {{v8_value_to_local_cpp_value(numeric_type) | indent(8)}}
142 impl.set{{numeric_type.type_name}}(cppValue); 146 impl.set{{numeric_type.type_name}}(cppValue);
143 return; 147 return;
144 } 148 }
145 149
146 {% endif %} 150 {% endif %}
147 {% if string_type %} 151 {% if string_type %}
148 {# 16. String #} 152 {# 15. String #}
149 { 153 {
150 {{v8_value_to_local_cpp_value(string_type) | indent(8)}} 154 {{v8_value_to_local_cpp_value(string_type) | indent(8)}}
151 {% if string_type.enum_values %} 155 {% if string_type.enum_values %}
152 {{declare_enum_validation_variable(string_type.enum_values) | indent(8)} } 156 {{declare_enum_validation_variable(string_type.enum_values) | indent(8)} }
153 if (!isValidEnum(cppValue, validValues, WTF_ARRAY_LENGTH(validValues), " {{string_type.type_name}}", exceptionState)) 157 if (!isValidEnum(cppValue, validValues, WTF_ARRAY_LENGTH(validValues), " {{string_type.type_name}}", exceptionState))
154 return; 158 return;
155 {% endif %} 159 {% endif %}
156 impl.set{{string_type.type_name}}(cppValue); 160 impl.set{{string_type.type_name}}(cppValue);
157 return; 161 return;
158 } 162 }
159 163
160 {# 17. Number (fallback) #} 164 {# 16. Number (fallback) #}
161 {% elif numeric_type %} 165 {% elif numeric_type %}
162 { 166 {
163 {{v8_value_to_local_cpp_value(numeric_type) | indent(8)}} 167 {{v8_value_to_local_cpp_value(numeric_type) | indent(8)}}
164 impl.set{{numeric_type.type_name}}(cppValue); 168 impl.set{{numeric_type.type_name}}(cppValue);
165 return; 169 return;
166 } 170 }
167 171
168 {# 18. Boolean (fallback) #} 172 {# 17. Boolean (fallback) #}
169 {% elif boolean_type %} 173 {% elif boolean_type %}
170 { 174 {
171 impl.setBoolean(v8Value->BooleanValue()); 175 impl.setBoolean(v8Value->BooleanValue());
172 return; 176 return;
173 } 177 }
174 178
175 {% else %} 179 {% else %}
176 {# 19. TypeError #} 180 {# 18. TypeError #}
177 exceptionState.throwTypeError("The provided value is not of type '{{type_str ing}}'"); 181 exceptionState.throwTypeError("The provided value is not of type '{{type_str ing}}'");
178 {% endif %} 182 {% endif %}
179 } 183 }
180 184
181 v8::Local<v8::Value> toV8(const {{cpp_class}}& impl, v8::Local<v8::Object> creat ionContext, v8::Isolate* isolate) 185 v8::Local<v8::Value> toV8(const {{cpp_class}}& impl, v8::Local<v8::Object> creat ionContext, v8::Isolate* isolate)
182 { 186 {
183 switch (impl.m_type) { 187 switch (impl.m_type) {
184 case {{cpp_class}}::SpecificTypeNone: 188 case {{cpp_class}}::SpecificTypeNone:
185 {# FIXME: We might want to return undefined in some cases #} 189 {# FIXME: We might want to return undefined in some cases #}
186 return v8::Null(isolate); 190 return v8::Null(isolate);
187 {% for member in members %} 191 {% for member in members %}
188 case {{cpp_class}}::{{member.specific_type_enum}}: 192 case {{cpp_class}}::{{member.specific_type_enum}}:
189 return {{member.cpp_value_to_v8_value}}; 193 return {{member.cpp_value_to_v8_value}};
190 {% endfor %} 194 {% endfor %}
191 default: 195 default:
192 ASSERT_NOT_REACHED(); 196 ASSERT_NOT_REACHED();
193 } 197 }
194 return v8::Local<v8::Value>(); 198 return v8::Local<v8::Value>();
195 } 199 }
196 200
197 {{cpp_class}} NativeValueTraits<{{cpp_class}}>::nativeValue(v8::Isolate* isolate , v8::Local<v8::Value> value, ExceptionState& exceptionState) 201 {{cpp_class}} NativeValueTraits<{{cpp_class}}>::nativeValue(v8::Isolate* isolate , v8::Local<v8::Value> value, ExceptionState& exceptionState)
198 { 202 {
199 {{cpp_class}} impl; 203 {{cpp_class}} impl;
200 {{v8_class}}::toImpl(isolate, value, impl, UnionTypeConversionMode::NotNulla ble, exceptionState); 204 {{v8_class}}::toImpl(isolate, value, impl, UnionTypeConversionMode::NotNulla ble, exceptionState);
201 return impl; 205 return impl;
202 } 206 }
203 207
204 } // namespace blink 208 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698