summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/templates/union_container.cc.tmpl
blob: 0599b20b16c016ab252e76332898674b5f6a82b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
{% from 'utilities.cc.tmpl' import declare_enum_validation_variable %}
{% from 'utilities.cc.tmpl' import v8_value_to_local_cpp_value %}
{#############################################################################}
{% macro assign_and_return_if_hasinstance(member) %}
{% if member.is_array_buffer_or_view_type %}
if (v8_value->Is{{member.type_name}}()) {
{% else %}
if (V8{{member.type_name}}::HasInstance(v8_value, isolate)) {
{% endif %}
{% if member.is_array_buffer_view_or_typed_array %}
  {{member.cpp_local_type}} cpp_value = ToNotShared<{{member.cpp_local_type}}>(isolate, v8_value, exception_state);
  if (exception_state.HadException())
    return;
{% else %}
  {{member.cpp_local_type}} cpp_value = V8{{member.type_name}}::ToImpl(v8::Local<v8::Object>::Cast(v8_value));
{% endif %}
  impl.Set{{member.type_name}}(cpp_value);
  return;
}
{% endmacro %}
{#############################################################################}
{% filter format_blink_cpp_source_code %}
{% include 'copyright_block.txt' %}
#include "{{this_include_header_path}}"

{% for filename in cpp_includes %}
#include "{{filename}}"
{% endfor %}

namespace blink {

{{cpp_class}}::{{cpp_class}}() : type_(SpecificType::kNone) {}

{% for member in members %}
{{member.rvalue_cpp_type}} {{cpp_class}}::GetAs{{member.type_name}}() const {
  DCHECK(Is{{member.type_name}}());
  return {{member.cpp_name}}_;
}

void {{cpp_class}}::Set{{member.type_name}}({{member.rvalue_cpp_type}} value) {
  DCHECK(IsNull());
  {% if member.enum_values %}
  NonThrowableExceptionState exception_state;
  {{declare_enum_validation_variable(member.enum_values) | trim | indent(2)}}
  if (!IsValidEnum(value, kValidValues, base::size(kValidValues), "{{member.enum_type}}", exception_state)) {
    NOTREACHED();
    return;
  }
  {% endif %}
  {{member.cpp_name}}_ = value;
  type_ = SpecificType::{{member.specific_type_enum}};
}

{{cpp_class}} {{cpp_class}}::From{{member.type_name}}({{member.rvalue_cpp_type}} value) {
  {{cpp_class}} container;
  container.Set{{member.type_name}}(value);
  return container;
}

{% endfor %}
{{cpp_class}}::{{cpp_class}}(const {{cpp_class}}&) = default;
{{cpp_class}}::~{{cpp_class}}() = default;
{{cpp_class}}& {{cpp_class}}::operator=(const {{cpp_class}}&) = default;

void {{cpp_class}}::Trace(Visitor* visitor) {
  {% for member in members if member.is_traceable %}
  visitor->Trace({{member.cpp_name}}_);
  {% endfor %}
}

void {{v8_class}}::ToImpl(
    v8::Isolate* isolate,
    v8::Local<v8::Value> v8_value,
    {{cpp_class}}& impl,
    UnionTypeConversionMode conversion_mode,
    ExceptionState& exception_state) {
  if (v8_value.IsEmpty())
    return;

  {# The numbers in the following comments refer to the steps described in
     http://heycam.github.io/webidl/#es-union #}
  {# 1. null or undefined #}
  if (conversion_mode == UnionTypeConversionMode::kNullable && IsUndefinedOrNull(v8_value))
    return;

  {% if dictionary_type %}
  {# 3. Dictionaries for null or undefined #}
  if (IsUndefinedOrNull(v8_value)) {
    {{v8_value_to_local_cpp_value(dictionary_type) | trim | indent}}
    impl.Set{{dictionary_type.type_name}}(cpp_value);
    return;
  }

  {% endif %}
  {# 4. Platform objects (interfaces) #}
  {% for interface in interface_types %}
  {{assign_and_return_if_hasinstance(interface) | trim | indent(2)}}

  {% endfor %}
  {# 8. ArrayBuffer #}
  {% if array_buffer_type %}
  {{assign_and_return_if_hasinstance(array_buffer_type) | trim | indent(2)}}

  {% endif %}
  {# 9., 10. ArrayBufferView #}
  {# FIXME: Individual typed arrays (e.g. Uint8Array) aren\'t supported yet. #}
  {% if array_buffer_view_type %}
  {{assign_and_return_if_hasinstance(array_buffer_view_type) | trim | indent(2)}}

  {% endif %}
  {% if array_or_sequence_type %}
  {# 11.1, 11.2. Sequences and frozen arrays #}
  if (v8_value->IsObject()) {
    ScriptIterator script_iterator = ScriptIterator::FromIterable(
        isolate, v8_value.As<v8::Object>(), exception_state);
    if (exception_state.HadException())
      return;
    if (!script_iterator.IsNull()) {
      {{v8_value_to_local_cpp_value(array_or_sequence_type) | trim | indent(6)}}
      {% if array_or_sequence_type.enum_values %}
      {{declare_enum_validation_variable(array_or_sequence_type.enum_values) | trim | indent(6)}}
      if (!IsValidEnum(cpp_value, kValidValues, base::size(kValidValues),
                       "{{array_or_sequence_type.enum_type}}", exception_state))
        return;
      {% endif %}
      impl.Set{{array_or_sequence_type.type_name}}(cpp_value);
      return;
    }
  }

  {% endif %}
  {% if dictionary_type %}
  {# 11.3. Dictionaries #}
  if (v8_value->IsObject()) {
    {{v8_value_to_local_cpp_value(dictionary_type) | trim | indent}}
    impl.Set{{dictionary_type.type_name}}(cpp_value);
    return;
  }

  {% endif %}
  {# 11.4. Records #}
  {% if record_type %}
  if (v8_value->IsObject()) {
    {{v8_value_to_local_cpp_value(record_type) | trim | indent}}
    impl.Set{{record_type.type_name}}(cpp_value);
    return;
  }

  {% endif %}
  {# TODO(bashi): Support 11.5 Callback interface when we need it #}
  {# 11.6. Objects #}
  {% if object_type %}
  if (IsUndefinedOrNull(v8_value) || v8_value->IsObject()) {
    {{v8_value_to_local_cpp_value(object_type) | trim | indent}}
    impl.Set{{object_type.type_name}}(cpp_value);
    return;
  }

  {% endif %}
  {# FIXME: In some cases, we can omit boolean and numeric type checks because
     we have fallback conversions. (step 16 and 17) #}
  {% if boolean_type %}
  {# 12. Boolean #}
  if (v8_value->IsBoolean()) {
    impl.SetBoolean(v8_value.As<v8::Boolean>()->Value());
    return;
  }

  {% endif %}
  {% if numeric_type %}
  {# 13. Number #}
  if (v8_value->IsNumber()) {
    {{v8_value_to_local_cpp_value(numeric_type) | trim | indent}}
    impl.Set{{numeric_type.type_name}}(cpp_value);
    return;
  }

  {% endif %}
  {% if string_type %}
  {# 14. String #}
  {
    {{v8_value_to_local_cpp_value(string_type) | trim | indent}}
    {% if string_type.enum_values %}
    {{declare_enum_validation_variable(string_type.enum_values) | trim | indent}}
    if (!IsValidEnum(cpp_value, kValidValues, base::size(kValidValues), "{{string_type.enum_type}}", exception_state))
      return;
    {% endif %}
    impl.Set{{string_type.type_name}}(cpp_value);
    return;
  }

  {# 15. Number (fallback) #}
  {% elif numeric_type %}
  {
    {{v8_value_to_local_cpp_value(numeric_type) | trim | indent}}
    impl.Set{{numeric_type.type_name}}(cpp_value);
    return;
  }

  {# 16. Boolean (fallback) #}
  {% elif boolean_type %}
  {
    impl.SetBoolean(v8_value->BooleanValue(isolate));
    return;
  }

  {% else %}
  {# 17. TypeError #}
  exception_state.ThrowTypeError("The provided value is not of type '{{type_string}}'");
  {% endif %}
}

v8::Local<v8::Value> ToV8(const {{cpp_class}}& impl, v8::Local<v8::Object> creationContext, v8::Isolate* isolate) {
  switch (impl.type_) {
    case {{cpp_class}}::SpecificType::kNone:
      {# FIXME: We might want to return undefined in some cases #}
      return v8::Null(isolate);
    {% for member in members %}
    case {{cpp_class}}::SpecificType::{{member.specific_type_enum}}:
      return {{member.cpp_value_to_v8_value}};
    {% endfor %}
    default:
      NOTREACHED();
  }
  return v8::Local<v8::Value>();
}

{{cpp_class}} NativeValueTraits<{{cpp_class}}>::NativeValue(
    v8::Isolate* isolate, v8::Local<v8::Value> value, ExceptionState& exception_state) {
  {{cpp_class}} impl;
  {{v8_class}}::ToImpl(isolate, value, impl, UnionTypeConversionMode::kNotNullable, exception_state);
  return impl;
}

}  // namespace blink

{% endfilter %}{# format_blink_cpp_source_code #}