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