Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / bindings / templates / dictionary_v8.cpp
blob823124424dd837482101e8ebbf17e64d6cf2d756
1 {% from 'conversions.cpp' import declare_enum_validation_variable %}
2 {% include 'copyright_block.txt' %}
3 #include "config.h"
4 #include "{{v8_original_class}}.h"
6 {% for filename in cpp_includes if filename != '%s.h' % v8_class %}
7 #include "{{filename}}"
8 {% endfor %}
10 namespace blink {
12 {% from 'conversions.cpp' import v8_value_to_local_cpp_value %}
13 void {{v8_class}}::toImpl(v8::Isolate* isolate, v8::Local<v8::Value> v8Value, {{cpp_class}}& impl, ExceptionState& exceptionState)
15 if (isUndefinedOrNull(v8Value))
16 return;
17 if (!v8Value->IsObject()) {
18 {% if use_permissive_dictionary_conversion %}
19 // Do nothing.
20 return;
21 {% else %}
22 exceptionState.throwTypeError("cannot convert to dictionary.");
23 return;
24 {% endif %}
27 {% if parent_v8_class %}
28 {{parent_v8_class}}::toImpl(isolate, v8Value, impl, exceptionState);
29 if (exceptionState.hadException())
30 return;
32 {% endif %}
33 {# Declare local variables only when the dictionary has members to avoid unused variable warnings. #}
34 {% if members %}
35 v8::TryCatch block;
36 v8::Local<v8::Object> v8Object;
37 if (!v8Call(v8Value->ToObject(isolate->GetCurrentContext()), v8Object, block)) {
38 exceptionState.rethrowV8Exception(block.Exception());
39 return;
41 {% endif %}
42 {% for member in members %}
44 v8::Local<v8::Value> {{member.name}}Value;
45 if (!v8Object->Get(isolate->GetCurrentContext(), v8String(isolate, "{{member.name}}")).ToLocal(&{{member.name}}Value)) {
46 exceptionState.rethrowV8Exception(block.Exception());
47 return;
49 if ({{member.name}}Value.IsEmpty() || {{member.name}}Value->IsUndefined()) {
50 {% if member.is_required %}
51 exceptionState.throwTypeError("required member {{member.name}} is undefined.");
52 return;
53 {% else %}
54 // Do nothing.
55 {% endif %}
56 {% if member.is_nullable %}
57 } else if ({{member.name}}Value->IsNull()) {
58 impl.{{member.null_setter_name}}();
59 {% endif %}
60 } else {
61 {% if member.deprecate_as %}
62 UseCounter::countDeprecationIfNotPrivateScript(isolate, callingExecutionContext(isolate), UseCounter::{{member.deprecate_as}});
63 {% endif %}
64 {{v8_value_to_local_cpp_value(member) | indent(12)}}
65 {% if member.is_interface_type %}
66 if (!{{member.name}} && !{{member.name}}Value->IsNull()) {
67 exceptionState.throwTypeError("member {{member.name}} is not of type {{member.idl_type}}.");
68 return;
70 {% endif %}
71 {% if member.enum_values %}
72 {{declare_enum_validation_variable(member.enum_values) | indent(12)}}
73 if (!isValidEnum({{member.name}}, validValues, WTF_ARRAY_LENGTH(validValues), "{{member.enum_type}}", exceptionState))
74 return;
75 {% elif member.is_object %}
76 if (!{{member.name}}.isObject()) {
77 exceptionState.throwTypeError("member {{member.name}} is not an object.");
78 return;
80 {% endif %}
81 impl.{{member.setter_name}}({{member.name}});
85 {% endfor %}
88 v8::Local<v8::Value> toV8(const {{cpp_class}}& impl, v8::Local<v8::Object> creationContext, v8::Isolate* isolate)
90 v8::Local<v8::Object> v8Object = v8::Object::New(isolate);
91 {% if parent_v8_class %}
92 if (!toV8{{parent_cpp_class}}(impl, v8Object, creationContext, isolate))
93 return v8::Local<v8::Value>();
94 {% endif %}
95 if (!toV8{{cpp_class}}(impl, v8Object, creationContext, isolate))
96 return v8::Local<v8::Value>();
97 return v8Object;
100 bool toV8{{cpp_class}}(const {{cpp_class}}& impl, v8::Local<v8::Object> dictionary, v8::Local<v8::Object> creationContext, v8::Isolate* isolate)
102 {% for member in members %}
103 if (impl.{{member.has_method_name}}()) {
104 {% if member.is_object %}
105 ASSERT(impl.{{member.cpp_name}}().isObject());
106 {% endif %}
107 if (!v8CallBoolean(dictionary->CreateDataProperty(isolate->GetCurrentContext(), v8String(isolate, "{{member.name}}"), {{member.cpp_value_to_v8_value}})))
108 return false;
109 {% if member.v8_default_value %}
110 } else {
111 if (!v8CallBoolean(dictionary->CreateDataProperty(isolate->GetCurrentContext(), v8String(isolate, "{{member.name}}"), {{member.v8_default_value}})))
112 return false;
113 {% elif member.is_required %}
114 } else {
115 ASSERT_NOT_REACHED();
116 {% endif %}
119 {% endfor %}
120 return true;
123 {{cpp_class}} NativeValueTraits<{{cpp_class}}>::nativeValue(v8::Isolate* isolate, v8::Local<v8::Value> value, ExceptionState& exceptionState)
125 {{cpp_class}} impl;
126 {{v8_class}}::toImpl(isolate, value, impl, exceptionState);
127 return impl;
130 } // namespace blink