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.
5 """Generate template contexts of dictionaries for both v8 bindings and
6 implementation classes that are used by blink's core/modules.
10 from v8_globals import includes
15 DICTIONARY_H_INCLUDES = frozenset([
16 'bindings/core/v8/V8Binding.h',
17 'platform/heap/Handle.h',
20 DICTIONARY_CPP_INCLUDES = frozenset([
21 'bindings/core/v8/ExceptionState.h',
22 # FIXME: Remove this, http://crbug.com/321462
23 'bindings/core/v8/Dictionary.h',
27 def setter_name_for_dictionary_member(member):
28 return 'set%s' % v8_utilities.capitalize(member.name)
31 def has_method_name_for_dictionary_member(member):
32 return 'has%s' % v8_utilities.capitalize(member.name)
35 def unwrap_nullable_if_needed(idl_type):
36 if idl_type.is_nullable:
37 return idl_type.inner_type
41 # Context for V8 bindings
43 def dictionary_context(dictionary):
45 includes.update(DICTIONARY_CPP_INCLUDES)
47 'cpp_class': v8_utilities.cpp_name(dictionary),
48 'header_includes': set(DICTIONARY_H_INCLUDES),
49 'members': [member_context(member)
50 for member in sorted(dictionary.members,
51 key=operator.attrgetter('name'))],
52 'v8_class': v8_utilities.v8_class_name(dictionary),
56 def member_context(member):
57 idl_type = member.idl_type
58 idl_type.add_includes_for_type()
59 idl_type = unwrap_nullable_if_needed(idl_type)
62 if not member.default_value:
64 if member.default_value.is_null:
65 return None, 'v8::Null(isolate)'
66 cpp_default_value = str(member.default_value)
67 v8_default_value = idl_type.cpp_value_to_v8_value(
68 cpp_value=cpp_default_value, isolate='isolate',
69 creation_context='creationContext')
70 return cpp_default_value, v8_default_value
72 cpp_default_value, v8_default_value = default_values()
75 'cpp_default_value': cpp_default_value,
76 'cpp_type': idl_type.cpp_type,
77 'cpp_value_to_v8_value': idl_type.cpp_value_to_v8_value(
78 cpp_value='impl->%s()' % member.name, isolate='isolate',
79 creation_context='creationContext',
80 extended_attributes=member.extended_attributes),
81 'enum_validation_expression': idl_type.enum_validation_expression,
82 'has_method_name': has_method_name_for_dictionary_member(member),
83 'is_object': idl_type.name == 'Object',
85 'setter_name': setter_name_for_dictionary_member(member),
86 'v8_default_value': v8_default_value,
90 # Context for implementation classes
92 def dictionary_impl_context(dictionary, interfaces_info):
94 header_includes = set(['platform/heap/Handle.h'])
96 'header_includes': header_includes,
97 'cpp_class': v8_utilities.cpp_name(dictionary),
98 'members': [member_impl_context(member, interfaces_info,
100 for member in dictionary.members],
104 def member_impl_context(member, interfaces_info, header_includes):
105 idl_type = unwrap_nullable_if_needed(member.idl_type)
106 is_object = idl_type.name == 'Object'
108 def getter_expression():
109 if idl_type.impl_should_use_nullable_container:
110 return 'm_%s.get()' % member.name
111 return 'm_%s' % member.name
113 def has_method_expression():
114 if idl_type.impl_should_use_nullable_container or idl_type.is_enum or idl_type.is_string_type:
115 return '!m_%s.isNull()' % member.name
117 return '!(m_{0}.isEmpty() || m_{0}.isNull() || m_{0}.isUndefined())'.format(member.name)
119 return 'm_%s' % member.name
121 def member_cpp_type():
122 member_cpp_type = idl_type.cpp_type_args(used_in_cpp_sequence=True)
123 if idl_type.impl_should_use_nullable_container:
124 return v8_types.cpp_template_type('Nullable', member_cpp_type)
125 return member_cpp_type
127 cpp_default_value = None
128 if member.default_value and not member.default_value.is_null:
129 cpp_default_value = str(member.default_value)
131 header_includes.update(idl_type.impl_includes_for_type(interfaces_info))
133 'cpp_default_value': cpp_default_value,
134 'getter_expression': getter_expression(),
135 'has_method_expression': has_method_expression(),
136 'has_method_name': has_method_name_for_dictionary_member(member),
137 'is_object': is_object,
138 'is_traceable': (idl_type.is_garbage_collected or
139 idl_type.is_will_be_garbage_collected),
140 'member_cpp_type': member_cpp_type(),
142 'rvalue_cpp_type': idl_type.cpp_type_args(used_as_rvalue_type=True),
143 'setter_name': setter_name_for_dictionary_member(member),