| OLD | NEW |
| (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 from dart_interface import suppress_getter, suppress_setter |
| 38 import dart_types |
| 39 from dart_utilities import DartUtilities |
| 40 from v8_globals import interfaces |
| 41 |
| 42 import v8_attributes |
| 43 |
| 44 |
| 45 def attribute_context(interface, attribute): |
| 46 # Call v8's implementation. |
| 47 context = v8_attributes.attribute_context(interface, attribute) |
| 48 |
| 49 extended_attributes = attribute.extended_attributes |
| 50 |
| 51 # Augment's Dart's information to context. |
| 52 idl_type = attribute.idl_type |
| 53 base_idl_type = idl_type.base_type |
| 54 # TODO(terry): Work around for DOMString[] base should be IDLTypeArray |
| 55 if base_idl_type == None: |
| 56 # Returns Array or Sequence. |
| 57 base_idl_type = idl_type.inner_name |
| 58 |
| 59 # [Custom] |
| 60 has_custom_getter = (('Custom' in extended_attributes and |
| 61 extended_attributes['Custom'] in [None, 'Getter']) or |
| 62 ('DartCustom' in extended_attributes and |
| 63 extended_attributes['DartCustom'] in [None, 'Getter',
'New'])) |
| 64 has_custom_setter = (not attribute.is_read_only and |
| 65 (('Custom' in extended_attributes and |
| 66 extended_attributes['Custom'] in [None, 'Setter']) or |
| 67 ('DartCustom' in extended_attributes and |
| 68 extended_attributes['DartCustom'] in [None, 'Setter',
'New']))) |
| 69 |
| 70 is_call_with_script_state = DartUtilities.has_extended_attribute_value(attri
bute, 'CallWith', 'ScriptState') |
| 71 |
| 72 is_auto_scope = not 'DartNoAutoScope' in extended_attributes |
| 73 |
| 74 context.update({ |
| 75 'activity_logging_world_list_for_getter': DartUtilities.activity_logging_w
orld_list(attribute, 'Getter'), # [ActivityLogging] |
| 76 'activity_logging_world_list_for_setter': DartUtilities.activity_logging_w
orld_list(attribute, 'Setter'), # [ActivityLogging] |
| 77 'deprecate_as': DartUtilities.deprecate_as(attribute), # [DeprecateAs] |
| 78 'has_custom_getter': has_custom_getter, |
| 79 'has_custom_setter': has_custom_setter, |
| 80 'is_auto_scope': is_auto_scope, # Used internally (outside of templates)
. |
| 81 'is_call_with_script_state': is_call_with_script_state, |
| 82 'auto_scope': DartUtilities.bool_to_cpp(is_auto_scope), |
| 83 'measure_as': DartUtilities.measure_as(attribute), # [MeasureAs] |
| 84 'v8_type': dart_types.v8_type(base_idl_type), |
| 85 }) |
| 86 |
| 87 if v8_attributes.is_constructor_attribute(attribute): |
| 88 v8_attributes.constructor_getter_context(interface, attribute, context) |
| 89 return context |
| 90 if not v8_attributes.has_custom_getter(attribute): |
| 91 getter_context(interface, attribute, context) |
| 92 if (not attribute.is_read_only): |
| 93 # FIXME: We did not previously support the PutForwards attribute, so I a
m |
| 94 # disabling it here for now to get things compiling. |
| 95 # We may wish to revisit this. |
| 96 # if (not has_custom_setter(attribute) and |
| 97 # (not attribute.is_read_only or 'PutForwards' in extended_attributes)): |
| 98 setter_context(interface, attribute, context) |
| 99 |
| 100 native_entry_getter = \ |
| 101 DartUtilities.generate_native_entry( |
| 102 interface.name, attribute.name, 'Getter', attribute.is_static, 0) |
| 103 native_entry_setter = \ |
| 104 DartUtilities.generate_native_entry( |
| 105 interface.name, attribute.name, 'Setter', attribute.is_static, 1) |
| 106 context.update({ |
| 107 'native_entry_getter': native_entry_getter, |
| 108 'native_entry_setter': native_entry_setter, |
| 109 }) |
| 110 |
| 111 return context |
| 112 |
| 113 |
| 114 ################################################################################ |
| 115 # Getter |
| 116 ################################################################################ |
| 117 |
| 118 def getter_context(interface, attribute, context): |
| 119 v8_attributes.getter_context(interface, attribute, context) |
| 120 |
| 121 idl_type = attribute.idl_type |
| 122 base_idl_type = idl_type.base_type |
| 123 extended_attributes = attribute.extended_attributes |
| 124 |
| 125 cpp_value = getter_expression(interface, attribute, context) |
| 126 # Normally we can inline the function call into the return statement to |
| 127 # avoid the overhead of using a Ref<> temporary, but for some cases |
| 128 # (nullable types, EventHandler, [CachedAttribute], or if there are |
| 129 # exceptions), we need to use a local variable. |
| 130 # FIXME: check if compilers are smart enough to inline this, and if so, |
| 131 # always use a local variable (for readability and CG simplicity). |
| 132 release = False |
| 133 if (idl_type.is_nullable or |
| 134 base_idl_type == 'EventHandler' or |
| 135 'CachedAttribute' in extended_attributes or |
| 136 'ReflectOnly' in extended_attributes or |
| 137 context['is_getter_raises_exception']): |
| 138 context['cpp_value_original'] = cpp_value |
| 139 cpp_value = 'result' |
| 140 # EventHandler has special handling |
| 141 if base_idl_type != 'EventHandler' and idl_type.is_interface_type: |
| 142 release = True |
| 143 |
| 144 dart_set_return_value = \ |
| 145 idl_type.dart_set_return_value(cpp_value, |
| 146 extended_attributes=extended_attributes, |
| 147 script_wrappable='impl', |
| 148 release=release, |
| 149 for_main_world=False, |
| 150 auto_scope=context['is_auto_scope']) |
| 151 |
| 152 # TODO(terry): Should be able to eliminate suppress_getter as we move from |
| 153 # IGNORE_MEMBERS to DartSuppress in the IDL. |
| 154 suppress = (suppress_getter(interface.name, attribute.name) or |
| 155 DartUtilities.has_extended_attribute_value(attribute, 'DartSuppr
ess', 'Getter')) |
| 156 |
| 157 context.update({ |
| 158 'cpp_value': cpp_value, |
| 159 'dart_set_return_value': dart_set_return_value, |
| 160 'is_getter_suppressed': suppress, |
| 161 }) |
| 162 |
| 163 |
| 164 def getter_expression(interface, attribute, context): |
| 165 v8_attributes.getter_expression(interface, attribute, context) |
| 166 |
| 167 arguments = [] |
| 168 this_getter_base_name = v8_attributes.getter_base_name(interface, attribute,
arguments) |
| 169 getter_name = DartUtilities.scoped_name(interface, attribute, this_getter_ba
se_name) |
| 170 |
| 171 arguments.extend(DartUtilities.call_with_arguments( |
| 172 attribute.extended_attributes.get('CallWith'))) |
| 173 if ('PartialInterfaceImplementedAs' in attribute.extended_attributes and |
| 174 not attribute.is_static): |
| 175 # Pass by reference. |
| 176 arguments.append('*receiver') |
| 177 |
| 178 if attribute.idl_type.is_explicit_nullable: |
| 179 arguments.append('isNull') |
| 180 if context['is_getter_raises_exception']: |
| 181 arguments.append('es') |
| 182 return '%s(%s)' % (getter_name, ', '.join(arguments)) |
| 183 |
| 184 |
| 185 ################################################################################ |
| 186 # Setter |
| 187 ################################################################################ |
| 188 |
| 189 def setter_context(interface, attribute, context): |
| 190 v8_attributes.setter_context(interface, attribute, context) |
| 191 |
| 192 def target_attribute(): |
| 193 target_interface_name = attribute.idl_type.base_type |
| 194 target_attribute_name = extended_attributes['PutForwards'] |
| 195 target_interface = interfaces[target_interface_name] |
| 196 try: |
| 197 return next(attribute |
| 198 for attribute in target_interface.attributes |
| 199 if attribute.name == target_attribute_name) |
| 200 except StopIteration: |
| 201 raise Exception('[PutForward] target not found:\n' |
| 202 'Attribute "%s" is not present in interface "%s"' % |
| 203 (target_attribute_name, target_interface_name)) |
| 204 |
| 205 extended_attributes = attribute.extended_attributes |
| 206 |
| 207 if 'PutForwards' in extended_attributes: |
| 208 # Use target attribute in place of original attribute |
| 209 attribute = target_attribute() |
| 210 this_cpp_type = 'DartStringAdapter' |
| 211 else: |
| 212 this_cpp_type = context['cpp_type'] |
| 213 |
| 214 idl_type = attribute.idl_type |
| 215 |
| 216 # TODO(terry): Should be able to eliminate suppress_setter as we move from |
| 217 # IGNORE_MEMBERS to DartSuppress in the IDL. |
| 218 suppress = (suppress_setter(interface.name, attribute.name) or |
| 219 DartUtilities.has_extended_attribute_value(attribute, 'DartSuppr
ess', 'Setter')) |
| 220 context.update({ |
| 221 'has_setter_exception_state': ( |
| 222 context['is_setter_raises_exception'] or context['has_type_checking_
interface'] or |
| 223 idl_type.is_integer_type), |
| 224 'is_setter_suppressed': suppress, |
| 225 'setter_lvalue': dart_types.check_reserved_name(attribute.name), |
| 226 'cpp_type': this_cpp_type, |
| 227 'local_cpp_type': idl_type.cpp_type_args(attribute.extended_attributes,
raw_type=True), |
| 228 'dart_value_to_local_cpp_value': |
| 229 attribute.idl_type.dart_value_to_local_cpp_value( |
| 230 extended_attributes, attribute.name, False, |
| 231 context['has_type_checking_interface'], 1, |
| 232 context['is_auto_scope']), |
| 233 }) |
| 234 |
| 235 # setter_expression() depends on context values we set above. |
| 236 context['cpp_setter'] = setter_expression(interface, attribute, context) |
| 237 |
| 238 |
| 239 def setter_expression(interface, attribute, context): |
| 240 extended_attributes = attribute.extended_attributes |
| 241 arguments = DartUtilities.call_with_arguments( |
| 242 extended_attributes.get('SetterCallWith') or |
| 243 extended_attributes.get('CallWith')) |
| 244 |
| 245 this_setter_base_name = v8_attributes.setter_base_name(interface, attribute,
arguments) |
| 246 setter_name = DartUtilities.scoped_name(interface, attribute, this_setter_ba
se_name) |
| 247 |
| 248 if ('PartialInterfaceImplementedAs' in extended_attributes and |
| 249 not attribute.is_static): |
| 250 arguments.append('*receiver') |
| 251 idl_type = attribute.idl_type |
| 252 if idl_type.base_type == 'EventHandler': |
| 253 getter_name = DartUtilities.scoped_name(interface, attribute, DartUtilit
ies.cpp_name(attribute)) |
| 254 context['event_handler_getter_expression'] = '%s(%s)' % ( |
| 255 getter_name, ', '.join(arguments)) |
| 256 # FIXME(vsm): Do we need to support this? If so, what's our analogue of |
| 257 # V8EventListenerList? |
| 258 arguments.append('nullptr') |
| 259 # if (interface.name in ['Window', 'WorkerGlobalScope'] and |
| 260 # attribute.name == 'onerror'): |
| 261 # includes.add('bindings/core/v8/V8ErrorHandler.h') |
| 262 # arguments.append('V8EventListenerList::findOrCreateWrapper<V8ErrorH
andler>(jsValue, true, info.GetIsolate())') |
| 263 # else: |
| 264 # arguments.append('V8EventListenerList::getEventListener(jsValue, tr
ue, ListenerFindOrCreate)') |
| 265 else: |
| 266 attribute_name = dart_types.check_reserved_name(attribute.name) |
| 267 arguments.append(attribute_name) |
| 268 if context['is_setter_raises_exception']: |
| 269 arguments.append('es') |
| 270 |
| 271 return '%s(%s)' % (setter_name, ', '.join(arguments)) |
| 272 |
| 273 |
| 274 ################################################################################ |
| 275 # Attribute configuration |
| 276 ################################################################################ |
| 277 |
| 278 # [Replaceable] |
| 279 def setter_callback_name(interface, attribute): |
| 280 cpp_class_name = DartUtilities.cpp_name(interface) |
| 281 extended_attributes = attribute.extended_attributes |
| 282 if (('Replaceable' in extended_attributes and |
| 283 'PutForwards' not in extended_attributes) or |
| 284 v8_attributes.is_constructor_attribute(attribute)): |
| 285 # FIXME: rename to ForceSetAttributeOnThisCallback, since also used for
Constructors |
| 286 return '{0}V8Internal::{0}ReplaceableAttributeSetterCallback'.format(cpp
_class_name) |
| 287 # FIXME:disabling PutForwards for now since we didn't support it before |
| 288 # if attribute.is_read_only and 'PutForwards' not in extended_attributes: |
| 289 if attribute.is_read_only: |
| 290 return '0' |
| 291 return '%sV8Internal::%sAttributeSetterCallback' % (cpp_class_name, attribut
e.name) |
| 292 |
| 293 |
| 294 ################################################################################ |
| 295 # Constructors |
| 296 ################################################################################ |
| 297 |
| 298 idl_types.IdlType.constructor_type_name = property( |
| 299 # FIXME: replace this with a [ConstructorAttribute] extended attribute |
| 300 lambda self: DartUtilities.strip_suffix(self.base_type, 'Constructor')) |
| OLD | NEW |