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

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

Issue 2452403002: [Bindings] Reformat template files (4/4) (Closed)
Patch Set: Rebase Created 4 years, 1 month 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 {% filter format_blink_cpp_source_code %}
2
3 {% from 'utilities.cpp.tmpl' import declare_enum_validation_variable %} 1 {% from 'utilities.cpp.tmpl' import declare_enum_validation_variable %}
4 {% include 'copyright_block.txt' %}
5 #include "{{this_include_header_name}}.h"
6
7 {% from 'utilities.cpp.tmpl' import v8_value_to_local_cpp_value %} 2 {% from 'utilities.cpp.tmpl' import v8_value_to_local_cpp_value %}
3 {#############################################################################}
8 {% macro assign_and_return_if_hasinstance(member) %} 4 {% macro assign_and_return_if_hasinstance(member) %}
9 {% if member.is_array_buffer_or_view_type %} 5 {% if member.is_array_buffer_or_view_type %}
10 if (v8Value->Is{{member.type_name}}()) { 6 if (v8Value->Is{{member.type_name}}()) {
11 {% else %} 7 {% else %}
12 if (V8{{member.type_name}}::hasInstance(v8Value, isolate)) { 8 if (V8{{member.type_name}}::hasInstance(v8Value, isolate)) {
13 {% endif %} 9 {% endif %}
14 {{member.cpp_local_type}} cppValue = V8{{member.type_name}}::toImpl(v8::Loca l<v8::Object>::Cast(v8Value)); 10 {{member.cpp_local_type}} cppValue = V8{{member.type_name}}::toImpl(v8::Local< v8::Object>::Cast(v8Value));
15 impl.set{{member.type_name}}(cppValue); 11 impl.set{{member.type_name}}(cppValue);
16 return; 12 return;
17 } 13 }
18 {% endmacro %} 14 {% endmacro %}
15 {#############################################################################}
16 {% filter format_blink_cpp_source_code %}
17 {% include 'copyright_block.txt' %}
18 #include "{{this_include_header_name}}.h"
19
19 {% for filename in cpp_includes %} 20 {% for filename in cpp_includes %}
20 #include "{{filename}}" 21 #include "{{filename}}"
21 {% endfor %} 22 {% endfor %}
22 23
23 namespace blink { 24 namespace blink {
24 25
25 {{cpp_class}}::{{cpp_class}}() 26 {{cpp_class}}::{{cpp_class}}() : m_type(SpecificTypeNone) {}
26 : m_type(SpecificTypeNone) 27
27 { 28 {% for member in members %}
29 {{member.rvalue_cpp_type}} {{cpp_class}}::getAs{{member.type_name}}() const {
30 DCHECK(is{{member.type_name}}());
31 return m_{{member.cpp_name}};
28 } 32 }
29 33
30 {% for member in members %} 34 void {{cpp_class}}::set{{member.type_name}}({{member.rvalue_cpp_type}} value) {
31 {{member.rvalue_cpp_type}} {{cpp_class}}::getAs{{member.type_name}}() const 35 DCHECK(isNull());
32 { 36 {% if member.enum_values %}
33 ASSERT(is{{member.type_name}}()); 37 NonThrowableExceptionState exceptionState;
34 return m_{{member.cpp_name}}; 38 {{declare_enum_validation_variable(member.enum_values) | indent(2)}}
39 if (!isValidEnum(value, validValues, WTF_ARRAY_LENGTH(validValues), "{{member. type_name}}", exceptionState)) {
40 NOTREACHED();
41 return;
42 }
43 {% endif %}
44 m_{{member.cpp_name}} = value;
45 m_type = {{member.specific_type_enum}};
35 } 46 }
36 47
37 void {{cpp_class}}::set{{member.type_name}}({{member.rvalue_cpp_type}} value) 48 {{cpp_class}} {{cpp_class}}::from{{member.type_name}}({{member.rvalue_cpp_type}} value) {
38 { 49 {{cpp_class}} container;
39 ASSERT(isNull()); 50 container.set{{member.type_name}}(value);
40 {% if member.enum_values %} 51 return container;
41 NonThrowableExceptionState exceptionState;
42 {{declare_enum_validation_variable(member.enum_values) | indent}}
43 if (!isValidEnum(value, validValues, WTF_ARRAY_LENGTH(validValues), "{{membe r.type_name}}", exceptionState)) {
44 ASSERT_NOT_REACHED();
45 return;
46 }
47 {% endif %}
48 m_{{member.cpp_name}} = value;
49 m_type = {{member.specific_type_enum}};
50 }
51
52 {{cpp_class}} {{cpp_class}}::from{{member.type_name}}({{member.rvalue_cpp_type}} value)
53 {
54 {{cpp_class}} container;
55 container.set{{member.type_name}}(value);
56 return container;
57 } 52 }
58 53
59 {% endfor %} 54 {% endfor %}
60 {{cpp_class}}::{{cpp_class}}(const {{cpp_class}}&) = default; 55 {{cpp_class}}::{{cpp_class}}(const {{cpp_class}}&) = default;
61 {{cpp_class}}::~{{cpp_class}}() = default; 56 {{cpp_class}}::~{{cpp_class}}() = default;
62 {{cpp_class}}& {{cpp_class}}::operator=(const {{cpp_class}}&) = default; 57 {{cpp_class}}& {{cpp_class}}::operator=(const {{cpp_class}}&) = default;
63 58
64 DEFINE_TRACE({{cpp_class}}) 59 DEFINE_TRACE({{cpp_class}}) {
65 { 60 {% for member in members if member.is_traceable %}
66 {% for member in members if member.is_traceable %} 61 visitor->trace(m_{{member.cpp_name}});
67 visitor->trace(m_{{member.cpp_name}}); 62 {% endfor %}
68 {% endfor %}
69 } 63 }
70 64
71 void {{v8_class}}::toImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value, {{ cpp_class}}& impl, UnionTypeConversionMode conversionMode, ExceptionState& excep tionState) 65 void {{v8_class}}::toImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value, {{ cpp_class}}& impl, UnionTypeConversionMode conversionMode, ExceptionState& excep tionState) {
72 { 66 if (v8Value.IsEmpty())
73 if (v8Value.IsEmpty()) 67 return;
74 return;
75 68
76 {# The numbers in the following comments refer to the steps described in 69 {# The numbers in the following comments refer to the steps described in
77 http://heycam.github.io/webidl/#es-union #} 70 http://heycam.github.io/webidl/#es-union #}
78 {# 1. null or undefined #} 71 {# 1. null or undefined #}
79 if (conversionMode == UnionTypeConversionMode::Nullable && isUndefinedOrNull (v8Value)) 72 if (conversionMode == UnionTypeConversionMode::Nullable && isUndefinedOrNull(v 8Value))
80 return; 73 return;
81 74
82 {% if dictionary_type %} 75 {% if dictionary_type %}
83 {# 3. Dictionaries for null or undefined #} 76 {# 3. Dictionaries for null or undefined #}
84 if (isUndefinedOrNull(v8Value)) { 77 if (isUndefinedOrNull(v8Value)) {
85 {{v8_value_to_local_cpp_value(dictionary_type) | indent(8)}} 78 {{v8_value_to_local_cpp_value(dictionary_type) | indent}}
86 impl.set{{dictionary_type.type_name}}(cppValue); 79 impl.set{{dictionary_type.type_name}}(cppValue);
87 return; 80 return;
88 } 81 }
89 82
83 {% endif %}
84 {# 4. Platform objects (interfaces) #}
85 {% for interface in interface_types %}
86 {{assign_and_return_if_hasinstance(interface) | indent(2)}}
87
88 {% endfor %}
89 {# 8. ArrayBuffer #}
90 {% if array_buffer_type %}
91 {{assign_and_return_if_hasinstance(array_buffer_type) | indent(2)}}
92
93 {% endif %}
94 {# 9., 10. ArrayBufferView #}
95 {# FIXME: Individual typed arrays (e.g. Uint8Array) aren\'t supported yet. #}
96 {% if array_buffer_view_type %}
97 {{assign_and_return_if_hasinstance(array_buffer_view_type) | indent(2)}}
98
99 {% endif %}
100 {% if array_or_sequence_type %}
101 {# 12.1, 12.2. Arrays and Sequences #}
102 {# FIXME: This should also check "object but not RegExp". Add checks
103 when we implement conversions for Date and RegExp. #}
104 {# TODO(bashi): Should check @@iterator symbol instead. #}
105 if (v8Value->IsArray()) {
106 {{v8_value_to_local_cpp_value(array_or_sequence_type) | indent}}
107 impl.set{{array_or_sequence_type.type_name}}(cppValue);
108 return;
109 }
110
111 {% endif %}
112 {% if dictionary_type %}
113 {# 12.3. Dictionaries #}
114 if (v8Value->IsObject()) {
115 {{v8_value_to_local_cpp_value(dictionary_type) | indent}}
116 impl.set{{dictionary_type.type_name}}(cppValue);
117 return;
118 }
119
120 {% endif %}
121 {# TODO(bashi): Support 12.4 Callback interface when we need it #}
122 {# 12.5. Objects #}
123 {% if object_type %}
124 if (isUndefinedOrNull(v8Value) || v8Value->IsObject()) {
125 {{v8_value_to_local_cpp_value(object_type) | indent}}
126 impl.set{{object_type.type_name}}(cppValue);
127 return;
128 }
129
130 {% endif %}
131 {# FIXME: In some cases, we can omit boolean and numeric type checks because
132 we have fallback conversions. (step 16 and 17) #}
133 {% if boolean_type %}
134 {# 13. Boolean #}
135 if (v8Value->IsBoolean()) {
136 impl.setBoolean(v8Value.As<v8::Boolean>()->Value());
137 return;
138 }
139
140 {% endif %}
141 {% if numeric_type %}
142 {# 14. Number #}
143 if (v8Value->IsNumber()) {
144 {{v8_value_to_local_cpp_value(numeric_type) | indent}}
145 impl.set{{numeric_type.type_name}}(cppValue);
146 return;
147 }
148
149 {% endif %}
150 {% if string_type %}
151 {# 15. String #}
152 {
153 {{v8_value_to_local_cpp_value(string_type) | indent}}
154 {% if string_type.enum_values %}
155 {{declare_enum_validation_variable(string_type.enum_values) | indent}}
156 if (!isValidEnum(cppValue, validValues, WTF_ARRAY_LENGTH(validValues), "{{st ring_type.type_name}}", exceptionState))
157 return;
90 {% endif %} 158 {% endif %}
91 {# 4. Platform objects (interfaces) #} 159 impl.set{{string_type.type_name}}(cppValue);
92 {% for interface in interface_types %} 160 return;
93 {{assign_and_return_if_hasinstance(interface) | indent}} 161 }
94 162
95 {% endfor %} 163 {# 16. Number (fallback) #}
96 {# 8. ArrayBuffer #} 164 {% elif numeric_type %}
97 {% if array_buffer_type %} 165 {
98 {{assign_and_return_if_hasinstance(array_buffer_type) | indent}} 166 {{v8_value_to_local_cpp_value(numeric_type) | indent}}
167 impl.set{{numeric_type.type_name}}(cppValue);
168 return;
169 }
99 170
100 {% endif %} 171 {# 17. Boolean (fallback) #}
101 {# 9., 10. ArrayBufferView #} 172 {% elif boolean_type %}
102 {# FIXME: Individual typed arrays (e.g. Uint8Array) aren\'t supported yet. # } 173 {
103 {% if array_buffer_view_type %} 174 impl.setBoolean(v8Value->BooleanValue());
104 {{assign_and_return_if_hasinstance(array_buffer_view_type) | indent}} 175 return;
176 }
105 177
106 {% endif %} 178 {% else %}
107 {% if array_or_sequence_type %} 179 {# 18. TypeError #}
108 {# 12.1, 12.2. Arrays and Sequences #} 180 exceptionState.throwTypeError("The provided value is not of type '{{type_strin g}}'");
109 {# FIXME: This should also check "object but not RegExp". Add checks 181 {% endif %}
110 when we implement conversions for Date and RegExp. #}
111 {# TODO(bashi): Should check @@iterator symbol instead. #}
112 if (v8Value->IsArray()) {
113 {{v8_value_to_local_cpp_value(array_or_sequence_type) | indent(8)}}
114 impl.set{{array_or_sequence_type.type_name}}(cppValue);
115 return;
116 }
117
118 {% endif %}
119 {% if dictionary_type %}
120 {# 12.3. Dictionaries #}
121 if (v8Value->IsObject()) {
122 {{v8_value_to_local_cpp_value(dictionary_type) | indent(8)}}
123 impl.set{{dictionary_type.type_name}}(cppValue);
124 return;
125 }
126
127 {% endif %}
128 {# TODO(bashi): Support 12.4 Callback interface when we need it #}
129 {# 12.5. Objects #}
130 {% if object_type %}
131 if (isUndefinedOrNull(v8Value) || v8Value->IsObject()) {
132 {{v8_value_to_local_cpp_value(object_type) | indent(8)}}
133 impl.set{{object_type.type_name}}(cppValue);
134 return;
135 }
136
137 {% endif %}
138 {# FIXME: In some cases, we can omit boolean and numeric type checks because
139 we have fallback conversions. (step 16 and 17) #}
140 {% if boolean_type %}
141 {# 13. Boolean #}
142 if (v8Value->IsBoolean()) {
143 impl.setBoolean(v8Value.As<v8::Boolean>()->Value());
144 return;
145 }
146
147 {% endif %}
148 {% if numeric_type %}
149 {# 14. Number #}
150 if (v8Value->IsNumber()) {
151 {{v8_value_to_local_cpp_value(numeric_type) | indent(8)}}
152 impl.set{{numeric_type.type_name}}(cppValue);
153 return;
154 }
155
156 {% endif %}
157 {% if string_type %}
158 {# 15. String #}
159 {
160 {{v8_value_to_local_cpp_value(string_type) | indent(8)}}
161 {% if string_type.enum_values %}
162 {{declare_enum_validation_variable(string_type.enum_values) | indent(8)} }
163 if (!isValidEnum(cppValue, validValues, WTF_ARRAY_LENGTH(validValues), " {{string_type.type_name}}", exceptionState))
164 return;
165 {% endif %}
166 impl.set{{string_type.type_name}}(cppValue);
167 return;
168 }
169
170 {# 16. Number (fallback) #}
171 {% elif numeric_type %}
172 {
173 {{v8_value_to_local_cpp_value(numeric_type) | indent(8)}}
174 impl.set{{numeric_type.type_name}}(cppValue);
175 return;
176 }
177
178 {# 17. Boolean (fallback) #}
179 {% elif boolean_type %}
180 {
181 impl.setBoolean(v8Value->BooleanValue());
182 return;
183 }
184
185 {% else %}
186 {# 18. TypeError #}
187 exceptionState.throwTypeError("The provided value is not of type '{{type_str ing}}'");
188 {% endif %}
189 } 182 }
190 183
191 v8::Local<v8::Value> toV8(const {{cpp_class}}& impl, v8::Local<v8::Object> creat ionContext, v8::Isolate* isolate) 184 v8::Local<v8::Value> toV8(const {{cpp_class}}& impl, v8::Local<v8::Object> creat ionContext, v8::Isolate* isolate) {
192 { 185 switch (impl.m_type) {
193 switch (impl.m_type) {
194 case {{cpp_class}}::SpecificTypeNone: 186 case {{cpp_class}}::SpecificTypeNone:
195 {# FIXME: We might want to return undefined in some cases #} 187 {# FIXME: We might want to return undefined in some cases #}
196 return v8::Null(isolate); 188 return v8::Null(isolate);
197 {% for member in members %} 189 {% for member in members %}
198 case {{cpp_class}}::{{member.specific_type_enum}}: 190 case {{cpp_class}}::{{member.specific_type_enum}}:
199 return {{member.cpp_value_to_v8_value}}; 191 return {{member.cpp_value_to_v8_value}};
200 {% endfor %} 192 {% endfor %}
201 default: 193 default:
202 ASSERT_NOT_REACHED(); 194 NOTREACHED();
203 } 195 }
204 return v8::Local<v8::Value>(); 196 return v8::Local<v8::Value>();
205 } 197 }
206 198
207 {{cpp_class}} NativeValueTraits<{{cpp_class}}>::nativeValue(v8::Isolate* isolate , v8::Local<v8::Value> value, ExceptionState& exceptionState) 199 {{cpp_class}} NativeValueTraits<{{cpp_class}}>::nativeValue(v8::Isolate* isolate , v8::Local<v8::Value> value, ExceptionState& exceptionState) {
208 { 200 {{cpp_class}} impl;
209 {{cpp_class}} impl; 201 {{v8_class}}::toImpl(isolate, value, impl, UnionTypeConversionMode::NotNullabl e, exceptionState);
210 {{v8_class}}::toImpl(isolate, value, impl, UnionTypeConversionMode::NotNulla ble, exceptionState); 202 return impl;
211 return impl;
212 } 203 }
213 204
214 } // namespace blink 205 } // namespace blink
215 206
216 {% endfilter %}{# format_blink_cpp_source_code #} 207 {% endfilter %}{# format_blink_cpp_source_code #}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698