Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / templates / union.cpp
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file has been auto-generated by {{code_generator}}. DO NOT MODIFY!
6
7 #include "config.h"
8 #include "{{header_filename}}"
9
10 {% macro assign_and_return_if_hasinstance(member) %}
11 if (V8{{member.type_name}}::hasInstance(v8Value, isolate)) {
12     {{member.cpp_local_type}} cppValue = V8{{member.type_name}}::toImpl(v8::Handle<v8::Object>::Cast(v8Value));
13     impl.set{{member.type_name}}(cppValue);
14     return;
15 }
16 {% endmacro %}
17 {% for filename in cpp_includes %}
18 #include "{{filename}}"
19 {% endfor %}
20
21 namespace blink {
22
23 {% for container in containers %}
24 {{container.cpp_class}}::{{container.cpp_class}}()
25     : m_type(SpecificTypeNone)
26 {
27 }
28
29 {% for member in container.members %}
30 {{member.rvalue_cpp_type}} {{container.cpp_class}}::getAs{{member.type_name}}() const
31 {
32     ASSERT(is{{member.type_name}}());
33     return m_{{member.cpp_name}};
34 }
35
36 void {{container.cpp_class}}::set{{member.type_name}}({{member.rvalue_cpp_type}} value)
37 {
38     ASSERT(isNull());
39     m_{{member.cpp_name}} = value;
40     m_type = {{member.specific_type_enum}};
41 }
42
43 {% endfor %}
44 {% if container.needs_trace %}
45 void {{container.cpp_class}}::trace(Visitor* visitor)
46 {
47     {% for member in container.members if member.is_traceable %}
48     visitor->trace(m_{{member.cpp_name}});
49     {% endfor %}
50 }
51
52 {% endif %}
53 void V8{{container.cpp_class}}::toImpl(v8::Isolate* isolate, v8::Handle<v8::Value> v8Value, {{container.cpp_class}}& impl, ExceptionState& exceptionState)
54 {
55     {# FIXME: We don't follow the spec on handling null and undefined at this
56        moment. Should be fixed once we implement all necessary conversion steps
57        below. #}
58     if (v8Value.IsEmpty())
59         return;
60
61     {# The numbers in the following comments refer to the steps described in
62        http://heycam.github.io/webidl/#es-union
63        FIXME: Implement all necessary steps #}
64     {# 3. Platform objects (interfaces) #}
65     {% for interface in container.interface_types %}
66     {{assign_and_return_if_hasinstance(interface) | indent}}
67
68     {% endfor %}
69     {# 8. ArrayBuffer #}
70     {% if container.array_buffer_type %}
71     {{assign_and_return_if_hasinstance(container.array_buffer_type) | indent}}
72
73     {% endif %}
74     {# 9., 10. ArrayBufferView #}
75     {# FIXME: Individual typed arrays (e.g. Uint8Array) aren't supported yet. #}
76     {% if container.array_buffer_view_type %}
77     {{assign_and_return_if_hasinstance(container.array_buffer_view_type) | indent}}
78
79     {% endif %}
80     {% if container.dictionary_type %}
81     {# 12. Dictionaries #}
82     {# FIXME: This should also check "object but not Date or RegExp". Add checks
83        when we implement conversions for Date and RegExp. #}
84     if (isUndefinedOrNull(v8Value) || v8Value->IsObject()) {
85         {{container.dictionary_type.cpp_local_type}} cppValue = V8{{container.dictionary_type.type_name}}::toImpl(isolate, v8Value, exceptionState);
86         if (!exceptionState.hadException())
87             impl.set{{container.dictionary_type.type_name}}(cppValue);
88         return;
89     }
90
91     {% endif %}
92     {# FIXME: In some cases, we can omit boolean and numeric type checks because
93        we have fallback conversions. (step 17 and 18) #}
94     {% if container.boolean_type %}
95     {# 14. Boolean #}
96     if (v8Value->IsBoolean()) {
97         impl.setBoolean(v8Value->ToBoolean()->Value());
98         return;
99     }
100
101     {% endif %}
102     {% if container.numeric_type %}
103     {# 15. Number #}
104     if (v8Value->IsNumber()) {
105         {{container.numeric_type.v8_value_to_local_cpp_value}};
106         impl.set{{container.numeric_type.type_name}}(cppValue);
107         return;
108     }
109
110     {% endif %}
111     {% if container.string_type %}
112     {# 16. String #}
113     {
114         {{container.string_type.v8_value_to_local_cpp_value}};
115         impl.set{{container.string_type.type_name}}(cppValue);
116         return;
117     }
118
119     {# 17. Number (fallback) #}
120     {% elif container.numeric_type %}
121     {
122         {{container.numeric_type.v8_value_to_local_cpp_value}};
123         impl.set{{container.numeric_type.type_name}}(cppValue);
124         return;
125     }
126
127     {# 18. Boolean (fallback) #}
128     {% elif container.boolean_type %}
129     {
130         impl.setBoolean(v8Value->ToBoolean()->Value());
131         return;
132     }
133
134     {% endif %}
135     {# 19. TypeError #}
136     exceptionState.throwTypeError("Not a valid union member.");
137 }
138
139 v8::Handle<v8::Value> toV8({{container.cpp_class}}& impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
140 {
141     switch (impl.m_type) {
142     case {{container.cpp_class}}::SpecificTypeNone:
143         {# FIXME: We might want to return undefined in some cases #}
144         return v8::Null(isolate);
145     {% for member in container.members %}
146     case {{container.cpp_class}}::{{member.specific_type_enum}}:
147         return {{member.cpp_value_to_v8_value}};
148     {% endfor %}
149     default:
150         ASSERT_NOT_REACHED();
151     }
152     return v8::Handle<v8::Value>();
153 }
154
155 {% endfor %}
156 } // namespace blink