Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: sky/engine/bindings2/scripts/dart_attributes.py

Issue 914413004: Add a new bindings2/scripts directory for Dart bindings (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Remove idlrenderer.py it's not used Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright (C) 2013 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 """Generate template values for attributes.
30
31 Extends IdlType with property |constructor_type_name|.
32
33 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler
34 """
35
36 import idl_types
37 import dart_types
38 from dart_utilities import DartUtilities
39 from v8_globals import interfaces
40
41 import v8_attributes
42
43
44 def attribute_context(interface, attribute):
45 # Call v8's implementation.
46 context = v8_attributes.attribute_context(interface, attribute)
47
48 extended_attributes = attribute.extended_attributes
49
50 # Augment's Dart's information to context.
51 idl_type = attribute.idl_type
52 base_idl_type = idl_type.base_type
53 # TODO(terry): Work around for DOMString[] base should be IDLTypeArray
54 if base_idl_type == None:
55 # Returns Array or Sequence.
56 base_idl_type = idl_type.inner_name
57
58 # [Custom]
59 has_custom_getter = ('Custom' in extended_attributes and
60 extended_attributes['Custom'] in [None, 'Getter'])
61 has_custom_setter = (not attribute.is_read_only and
62 (('Custom' in extended_attributes and
63 extended_attributes['Custom'] in [None, 'Setter'])))
64
65 is_call_with_script_state = DartUtilities.has_extended_attribute_value(attri bute, 'CallWith', 'ScriptState')
66
67 is_auto_scope = not 'DartNoAutoScope' in extended_attributes
68
69 context.update({
70 'has_custom_getter': has_custom_getter,
71 'has_custom_setter': has_custom_setter,
72 'is_auto_scope': is_auto_scope, # Used internally (outside of templates) .
73 'is_call_with_script_state': is_call_with_script_state,
74 'auto_scope': DartUtilities.bool_to_cpp(is_auto_scope),
75 'dart_type': dart_types.idl_type_to_dart_type(idl_type),
76 })
77
78 if v8_attributes.is_constructor_attribute(attribute):
79 v8_attributes.constructor_getter_context(interface, attribute, context)
80 return context
81 if not v8_attributes.has_custom_getter(attribute):
82 getter_context(interface, attribute, context)
83 if (not attribute.is_read_only):
84 # FIXME: We did not previously support the PutForwards attribute, so I a m
85 # disabling it here for now to get things compiling.
86 # We may wish to revisit this.
87 # if (not has_custom_setter(attribute) and
88 # (not attribute.is_read_only or 'PutForwards' in extended_attributes)):
89 setter_context(interface, attribute, context)
90
91 native_entry_getter = \
92 DartUtilities.generate_native_entry(
93 interface.name, attribute.name, 'Getter', attribute.is_static, 0)
94 native_entry_setter = \
95 DartUtilities.generate_native_entry(
96 interface.name, attribute.name, 'Setter', attribute.is_static, 1)
97 context.update({
98 'native_entry_getter': native_entry_getter,
99 'native_entry_setter': native_entry_setter,
100 })
101
102 return context
103
104
105 ################################################################################
106 # Getter
107 ################################################################################
108
109 def getter_context(interface, attribute, context):
110 v8_attributes.getter_context(interface, attribute, context)
111
112 idl_type = attribute.idl_type
113 base_idl_type = idl_type.base_type
114 extended_attributes = attribute.extended_attributes
115
116 cpp_value = getter_expression(interface, attribute, context)
117 # Normally we can inline the function call into the return statement to
118 # avoid the overhead of using a Ref<> temporary, but for some cases
119 # (nullable types, EventHandler, [CachedAttribute], or if there are
120 # exceptions), we need to use a local variable.
121 # FIXME: check if compilers are smart enough to inline this, and if so,
122 # always use a local variable (for readability and CG simplicity).
123 release = False
124 if (idl_type.is_nullable or
125 base_idl_type == 'EventHandler' or
126 'CachedAttribute' in extended_attributes or
127 'ReflectOnly' in extended_attributes or
128 context['is_getter_raises_exception']):
129 context['cpp_value_original'] = cpp_value
130 cpp_value = 'result'
131 # EventHandler has special handling
132 if base_idl_type != 'EventHandler' and idl_type.is_interface_type:
133 release = True
134
135 dart_set_return_value = \
136 idl_type.dart_set_return_value(cpp_value,
137 extended_attributes=extended_attributes,
138 script_wrappable='impl',
139 release=release,
140 for_main_world=False,
141 auto_scope=context['is_auto_scope'])
142
143 context.update({
144 'cpp_value': cpp_value,
145 'dart_set_return_value': dart_set_return_value,
146 })
147
148
149 def getter_expression(interface, attribute, context):
150 v8_attributes.getter_expression(interface, attribute, context)
151
152 arguments = []
153 this_getter_base_name = v8_attributes.getter_base_name(interface, attribute, arguments)
154 getter_name = DartUtilities.scoped_name(interface, attribute, this_getter_ba se_name)
155
156 arguments.extend(DartUtilities.call_with_arguments(
157 attribute.extended_attributes.get('CallWith')))
158 if ('PartialInterfaceImplementedAs' in attribute.extended_attributes and
159 not attribute.is_static):
160 # Pass by reference.
161 arguments.append('*receiver')
162
163 if attribute.idl_type.is_explicit_nullable:
164 arguments.append('is_null')
165 if context['is_getter_raises_exception']:
166 arguments.append('es')
167 return '%s(%s)' % (getter_name, ', '.join(arguments))
168
169
170 ################################################################################
171 # Setter
172 ################################################################################
173
174 def setter_context(interface, attribute, context):
175 v8_attributes.setter_context(interface, attribute, context)
176
177 def target_attribute():
178 target_interface_name = attribute.idl_type.base_type
179 target_attribute_name = extended_attributes['PutForwards']
180 target_interface = interfaces[target_interface_name]
181 try:
182 return next(attribute
183 for attribute in target_interface.attributes
184 if attribute.name == target_attribute_name)
185 except StopIteration:
186 raise Exception('[PutForward] target not found:\n'
187 'Attribute "%s" is not present in interface "%s"' %
188 (target_attribute_name, target_interface_name))
189
190 extended_attributes = attribute.extended_attributes
191
192 if 'PutForwards' in extended_attributes:
193 # Use target attribute in place of original attribute
194 attribute = target_attribute()
195 this_cpp_type = 'DartStringAdapter'
196 else:
197 this_cpp_type = context['cpp_type']
198
199 idl_type = attribute.idl_type
200
201 context.update({
202 'has_setter_exception_state': (
203 context['is_setter_raises_exception'] or context['has_type_checking_ interface'] or
204 idl_type.is_integer_type),
205 'setter_lvalue': dart_types.check_reserved_name(attribute.name),
206 'cpp_type': this_cpp_type,
207 'local_cpp_type': idl_type.cpp_type_args(attribute.extended_attributes, raw_type=True),
208 'dart_value_to_local_cpp_value':
209 attribute.idl_type.dart_value_to_local_cpp_value(
210 extended_attributes, attribute.name, False,
211 context['has_type_checking_interface'], 1,
212 context['is_auto_scope']),
213 })
214
215 # setter_expression() depends on context values we set above.
216 context['cpp_setter'] = setter_expression(interface, attribute, context)
217
218
219 def setter_expression(interface, attribute, context):
220 extended_attributes = attribute.extended_attributes
221 arguments = DartUtilities.call_with_arguments(
222 extended_attributes.get('SetterCallWith') or
223 extended_attributes.get('CallWith'))
224
225 this_setter_base_name = v8_attributes.setter_base_name(interface, attribute, arguments)
226 setter_name = DartUtilities.scoped_name(interface, attribute, this_setter_ba se_name)
227
228 if ('PartialInterfaceImplementedAs' in extended_attributes and
229 not attribute.is_static):
230 arguments.append('*receiver')
231 idl_type = attribute.idl_type
232 if idl_type.base_type == 'EventHandler':
233 getter_name = DartUtilities.scoped_name(interface, attribute, DartUtilit ies.cpp_name(attribute))
234 context['event_handler_getter_expression'] = '%s(%s)' % (
235 getter_name, ', '.join(arguments))
236 # FIXME(vsm): Do we need to support this? If so, what's our analogue of
237 # V8EventListenerList?
238 arguments.append('nullptr')
239 else:
240 attribute_name = dart_types.check_reserved_name(attribute.name)
241 arguments.append(attribute_name)
242 if context['is_setter_raises_exception']:
243 arguments.append('es')
244
245 return '%s(%s)' % (setter_name, ', '.join(arguments))
246
247
248 ################################################################################
249 # Attribute configuration
250 ################################################################################
251
252 # [Replaceable]
253 def setter_callback_name(interface, attribute):
254 cpp_class_name = DartUtilities.cpp_name(interface)
255 extended_attributes = attribute.extended_attributes
256 if (('Replaceable' in extended_attributes and
257 'PutForwards' not in extended_attributes) or
258 v8_attributes.is_constructor_attribute(attribute)):
259 # FIXME: rename to ForceSetAttributeOnThisCallback, since also used for Constructors
260 return '{0}V8Internal::{0}ReplaceableAttributeSetterCallback'.format(cpp _class_name)
261 # FIXME:disabling PutForwards for now since we didn't support it before
262 # if attribute.is_read_only and 'PutForwards' not in extended_attributes:
263 if attribute.is_read_only:
264 return '0'
265 return '%sV8Internal::%sAttributeSetterCallback' % (cpp_class_name, attribut e.name)
266
267
268 ################################################################################
269 # Constructors
270 ################################################################################
271
272 idl_types.IdlType.constructor_type_name = property(
273 # FIXME: replace this with a [ConstructorAttribute] extended attribute
274 lambda self: DartUtilities.strip_suffix(self.base_type, 'Constructor'))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698