def setter_base_name(interface, attribute, arguments): if "ImplementedInPrivateScript" in attribute.extended_attributes: return "%sAttributeSetter" % uncapitalize(cpp_name(attribute)) if "Reflect" not in attribute.extended_attributes: return "set%s" % capitalize(cpp_name(attribute)) arguments.append(scoped_content_attribute_name(interface, attribute)) base_idl_type = attribute.idl_type.base_type if base_idl_type in CONTENT_ATTRIBUTE_SETTER_NAMES: return CONTENT_ATTRIBUTE_SETTER_NAMES[base_idl_type] return "setAttribute"
def property_getter(getter, cpp_arguments): def is_null_expression(idl_type): if v8_types.is_union_type(idl_type): return ' && '.join('!result%sEnabled' % i for i, _ in enumerate(idl_type.union_member_types)) if idl_type == 'DOMString': return 'result.isNull()' if is_interface_type(idl_type): return '!result' return '' idl_type = getter.idl_type extended_attributes = getter.extended_attributes is_raises_exception = 'RaisesException' in extended_attributes if v8_types.is_union_type(idl_type): release = [v8_types.is_interface_type(union_member_type) for union_member_type in idl_type.union_member_types] else: release = v8_types.is_interface_type(idl_type) # FIXME: make more generic, so can use v8_methods.cpp_value cpp_method_name = 'imp->%s' % cpp_name(getter) if is_raises_exception: cpp_arguments.append('exceptionState') this_union_arguments = v8_methods.union_arguments(idl_type) if this_union_arguments: cpp_arguments.extend(this_union_arguments) cpp_value = '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments)) return { 'cpp_type': v8_types.cpp_type(idl_type), 'cpp_value': cpp_value, 'is_custom': 'Custom' in extended_attributes and (not extended_attributes['Custom'] or has_extended_attribute_value(getter, 'Custom', 'PropertyGetter')), 'is_custom_property_enumerator': has_extended_attribute_value( getter, 'Custom', 'PropertyEnumerator'), 'is_custom_property_query': has_extended_attribute_value( getter, 'Custom', 'PropertyQuery'), 'is_enumerable': 'NotEnumerable' not in extended_attributes, 'is_null_expression': is_null_expression(idl_type), 'is_raises_exception': is_raises_exception, 'name': cpp_name(getter), 'union_arguments': v8_methods.union_arguments(idl_type), 'v8_set_return_value': v8_types.v8_set_return_value(idl_type, 'result', extended_attributes=extended_attributes, script_wrappable='imp', release=release), }
def generate_interface(interface): includes.clear() includes.update(INTERFACE_CPP_INCLUDES) v8_class_name = v8_utilities.v8_class_name(interface) template_contents = { 'cpp_class_name': cpp_name(interface), 'header_includes': INTERFACE_H_INCLUDES, 'interface_name': interface.name, 'v8_class_name': v8_class_name, } template_contents.update({ 'constants': [generate_constant(constant) for constant in interface.constants], 'do_not_check_constants': 'DoNotCheckConstants' in interface.extended_attributes, }) attributes = [v8_attributes.generate_attribute(interface, attribute) for attribute in interface.attributes] template_contents.update({ 'attributes': attributes, 'has_constructor_attributes': any(attribute['is_constructor'] for attribute in attributes), 'has_per_context_enabled_attributes': any(attribute['per_context_enabled_function_name'] for attribute in attributes), 'has_replaceable_attributes': any(attribute['is_replaceable'] for attribute in attributes), 'has_runtime_enabled_attributes': any(attribute['runtime_enabled_function_name'] for attribute in attributes), }) template_contents['methods'] = [v8_methods.generate_method(method) for method in interface.operations] return template_contents
def setter_expression(interface, attribute, contents): extended_attributes = attribute.extended_attributes arguments = v8_utilities.call_with_arguments(attribute, extended_attributes.get('SetterCallWith')) this_setter_base_name = setter_base_name(attribute, arguments) setter_name = v8_utilities.scoped_name(interface, attribute, this_setter_base_name) if ('ImplementedBy' in extended_attributes and not attribute.is_static): arguments.append('imp') idl_type = attribute.idl_type if idl_type == 'EventHandler': # FIXME: pass the isolate instead of the isolated world isolated_world = 'isolatedWorldForIsolate(info.GetIsolate())' getter_name = v8_utilities.scoped_name(interface, attribute, cpp_name(attribute)) getter_arguments = arguments + [isolated_world] contents['event_handler_getter_expression'] = '%s(%s)' % ( getter_name, ', '.join(getter_arguments)) arguments.extend(['V8EventListenerList::getEventListener(jsValue, true, ListenerFindOrCreate)', isolated_world]) elif v8_types.is_interface_type(idl_type) and not v8_types.array_type(idl_type): # FIXME: should be able to eliminate WTF::getPtr in most or all cases arguments.append('WTF::getPtr(cppValue)') else: arguments.append('cppValue') if contents['is_setter_raises_exception']: arguments.append('exceptionState') return '%s(%s)' % (setter_name, ', '.join(arguments))
def setter_expression(interface, attribute, contents): extended_attributes = attribute.extended_attributes arguments = v8_utilities.call_with_arguments(attribute, extended_attributes.get('SetterCallWith')) this_setter_base_name = setter_base_name(interface, attribute, arguments) setter_name = scoped_name(interface, attribute, this_setter_base_name) # Members of IDL partial interface definitions are implemented in C++ as # static member functions, which for instance members (non-static members) # take *impl as their first argument if ('PartialInterfaceImplementedAs' in extended_attributes and not attribute.is_static): arguments.append('*impl') idl_type = attribute.idl_type if idl_type.base_type == 'EventHandler': getter_name = scoped_name(interface, attribute, cpp_name(attribute)) contents['event_handler_getter_expression'] = '%s(%s)' % ( getter_name, ', '.join(arguments)) if (interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/v8/V8ErrorHandler.h') arguments.append('V8EventListenerList::findOrCreateWrapper<V8ErrorHandler>(v8Value, true, info.GetIsolate())') else: arguments.append('V8EventListenerList::getEventListener(v8Value, true, ListenerFindOrCreate)') elif idl_type.is_interface_type and not idl_type.array_type: # FIXME: should be able to eliminate WTF::getPtr in most or all cases arguments.append('WTF::getPtr(cppValue)') else: arguments.append('cppValue') if contents['is_setter_raises_exception']: arguments.append('exceptionState') return '%s(%s)' % (setter_name, ', '.join(arguments))
def generate_code_internal(self, definitions, definition_name): if not definition_name in definitions.dictionaries: raise ValueError('%s is not an IDL dictionary') interfaces_info = self.info_provider.interfaces_info dictionary = definitions.dictionaries[definition_name] interface_info = interfaces_info[definition_name] header_template = self.jinja_env.get_template('dictionary_impl.h') cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') template_context = v8_dictionary.dictionary_impl_context( dictionary, interfaces_info) include_paths = interface_info.get('dependencies_include_paths') # Add union containers header file to header_includes rather than # cpp file so that union containers can be used in dictionary headers. union_container_headers = [header for header in include_paths if header.find('UnionTypes') > 0] include_paths = [header for header in include_paths if header not in union_container_headers] template_context['header_includes'].update(union_container_headers) if not is_testing_target(interface_info.get('full_path')): template_context['exported'] = self.info_provider.specifier_for_export template_context['header_includes'].add(self.info_provider.include_path_for_export) header_text, cpp_text = render_template( include_paths, header_template, cpp_template, template_context) header_path, cpp_path = self.output_paths( cpp_name(dictionary), interface_info) return ( (header_path, header_text), (cpp_path, cpp_text), )
def __init__(self, definitions, interface_name, output_directory, relative_dir_posix, idl_directories, verbose=False): self.idl_definitions = definitions self.interface_name = interface_name self.idl_directories = idl_directories self.output_directory = output_directory self.verbose = verbose # FIXME: remove definitions check when remove write_dummy_header_and_cpp if not definitions: return try: self.interface = definitions.interfaces[interface_name] except KeyError: raise Exception('%s not in IDL definitions' % interface_name) if self.interface.is_callback: header_template_filename = 'callback_interface.h' cpp_template_filename = 'callback_interface.cpp' self.generate_contents = v8_callback_interface.generate_callback_interface else: header_template_filename = 'interface.h' cpp_template_filename = 'interface.cpp' self.generate_contents = v8_interface.generate_interface jinja_env = jinja2.Environment( loader=jinja2.FileSystemLoader(templates_dir), keep_trailing_newline=True, # newline-terminate generated files lstrip_blocks=True, # so can indent control flow tags trim_blocks=True) jinja_env.filters['conditional'] = conditional_if_endif self.header_template = jinja_env.get_template(header_template_filename) self.cpp_template = jinja_env.get_template(cpp_template_filename) class_name = cpp_name(self.interface) self.include_for_cpp_class = posixpath.join(relative_dir_posix, class_name + '.h') enumerations = definitions.enumerations if enumerations: v8_types.set_enum_types(enumerations)
def dictionary_context(dictionary, interfaces_info): includes.clear() includes.update(DICTIONARY_CPP_INCLUDES) members = [member_context(dictionary, member) for member in sorted(dictionary.members, key=operator.attrgetter('name'))] for member in members: if member['runtime_enabled_function']: includes.add('platform/RuntimeEnabledFeatures.h') break cpp_class = v8_utilities.cpp_name(dictionary) context = { 'cpp_class': cpp_class, 'header_includes': set(DICTIONARY_H_INCLUDES), 'members': members, 'required_member_names': sorted([member.name for member in dictionary.members if member.is_required]), 'use_permissive_dictionary_conversion': 'PermissiveDictionaryConversion' in dictionary.extended_attributes, 'v8_class': v8_types.v8_type(cpp_class), 'v8_original_class': v8_types.v8_type(dictionary.name), } if dictionary.parent: IdlType(dictionary.parent).add_includes_for_type() parent_cpp_class = v8_utilities.cpp_name_from_interfaces_info( dictionary.parent, interfaces_info) context.update({ 'parent_cpp_class': parent_cpp_class, 'parent_v8_class': v8_types.v8_type(parent_cpp_class), }) return context
def dictionary_impl_context(dictionary, interfaces_info): def remove_duplicate_members(members): # When [ImplementedAs] is used, cpp_name can conflict. For example, # dictionary D { long foo; [ImplementedAs=foo, DeprecateAs=Foo] long oldFoo; }; # This function removes such duplications, checking they have the same type. members_dict = {} for member in members: cpp_name = member['cpp_name'] duplicated_member = members_dict.get(cpp_name) if duplicated_member and duplicated_member != member: raise Exception('Member name conflict: %s' % cpp_name) members_dict[cpp_name] = member return sorted(members_dict.values(), key=lambda member: member['cpp_name']) includes.clear() header_includes = set(['platform/heap/Handle.h']) members = [member_impl_context(member, interfaces_info, header_includes) for member in dictionary.members] members = remove_duplicate_members(members) context = { 'header_includes': header_includes, 'cpp_class': v8_utilities.cpp_name(dictionary), 'members': members, } if dictionary.parent: context['parent_cpp_class'] = v8_utilities.cpp_name_from_interfaces_info( dictionary.parent, interfaces_info) parent_interface_info = interfaces_info.get(dictionary.parent) if parent_interface_info: context['header_includes'].add( parent_interface_info['include_path']) return context
def setter_expression(interface, attribute, contents): extended_attributes = attribute.extended_attributes arguments = v8_utilities.call_with_arguments(attribute, extended_attributes.get('SetterCallWith')) this_setter_base_name = setter_base_name(attribute, arguments) setter_name = v8_utilities.scoped_name(interface, attribute, this_setter_base_name) if ('ImplementedBy' in extended_attributes and not attribute.is_static): arguments.append('imp') idl_type = attribute.idl_type if idl_type == 'EventHandler': getter_name = v8_utilities.scoped_name(interface, attribute, cpp_name(attribute)) contents['event_handler_getter_expression'] = '%s(%s)' % ( getter_name, ', '.join(arguments)) if (interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/v8/V8ErrorHandler.h') arguments.append('V8EventListenerList::findOrCreateWrapper<V8ErrorHandler>(jsValue, true, info.GetIsolate())') else: arguments.append('V8EventListenerList::getEventListener(jsValue, true, ListenerFindOrCreate)') elif v8_types.is_interface_type(idl_type) and not v8_types.array_type(idl_type): # FIXME: should be able to eliminate WTF::getPtr in most or all cases arguments.append('WTF::getPtr(cppValue)') else: arguments.append('cppValue') if contents['is_setter_raises_exception']: arguments.append('exceptionState') return '%s(%s)' % (setter_name, ', '.join(arguments))
def constructor_context(interface, constructor): # [RaisesException=Constructor] is_constructor_raises_exception = \ interface.extended_attributes.get('RaisesException') == 'Constructor' return { 'arguments': [v8_methods.argument_context(interface, constructor, argument, index) for index, argument in enumerate(constructor.arguments)], 'cpp_type': cpp_template_type( cpp_ptr_type('RefPtr', 'RawPtr', gc_type(interface)), cpp_name(interface)), 'cpp_value': v8_methods.cpp_value( interface, constructor, len(constructor.arguments)), 'has_exception_state': is_constructor_raises_exception or any(argument for argument in constructor.arguments if argument.idl_type.name == 'SerializedScriptValue' or argument.idl_type.v8_conversion_needs_exception_state), 'is_call_with_document': # [ConstructorCallWith=Document] has_extended_attribute_value(interface, 'ConstructorCallWith', 'Document'), 'is_call_with_execution_context': # [ConstructorCallWith=ExecutionContext] has_extended_attribute_value(interface, 'ConstructorCallWith', 'ExecutionContext'), 'is_constructor': True, 'is_named_constructor': False, 'is_raises_exception': is_constructor_raises_exception, 'number_of_required_arguments': number_of_required_arguments(constructor), }
def cpp_value(interface, method, number_of_arguments): def cpp_argument(argument): idl_type = argument.idl_type if (v8_types.is_callback_interface(idl_type) or idl_type in ['NodeFilter', 'XPathNSResolver']): # FIXME: remove this special case return '%s.release()' % argument.name return argument.name # Truncate omitted optional arguments arguments = method.arguments[:number_of_arguments] cpp_arguments = v8_utilities.call_with_arguments(method) if ('ImplementedBy' in method.extended_attributes and not method.is_static): cpp_arguments.append('imp') cpp_arguments.extend(cpp_argument(argument) for argument in arguments) this_union_arguments = union_arguments(method.idl_type) if this_union_arguments: cpp_arguments.extend(this_union_arguments) if 'RaisesException' in method.extended_attributes: cpp_arguments.append('exceptionState') cpp_method_name = v8_utilities.scoped_name(interface, method, v8_utilities.cpp_name(method)) return '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments))
def member_context(member): idl_type = member.idl_type idl_type.add_includes_for_type() idl_type = unwrap_nullable_if_needed(idl_type) def default_values(): if not member.default_value: return None, None if member.default_value.is_null: return None, 'v8::Null(isolate)' cpp_default_value = str(member.default_value) v8_default_value = idl_type.cpp_value_to_v8_value( cpp_value=cpp_default_value, isolate='isolate', creation_context='creationContext') return cpp_default_value, v8_default_value cpp_default_value, v8_default_value = default_values() cpp_name = v8_utilities.cpp_name(member) return { 'cpp_default_value': cpp_default_value, 'cpp_name': cpp_name, 'cpp_type': idl_type.cpp_type, 'cpp_value_to_v8_value': idl_type.cpp_value_to_v8_value( cpp_value='impl.%s()' % cpp_name, isolate='isolate', creation_context='creationContext', extended_attributes=member.extended_attributes), 'enum_validation_expression': idl_type.enum_validation_expression, 'has_method_name': has_method_name_for_dictionary_member(member), 'is_object': idl_type.name == 'Object', 'name': member.name, 'setter_name': setter_name_for_dictionary_member(member), 'v8_default_value': v8_default_value, }
def property_getter(getter, cpp_arguments): def is_null_expression(idl_type): if idl_type.is_union_type: notnull = ' || '.join([ member_argument['null_check_value'] for member_argument in idl_type.union_arguments]) return '!(%s)' % notnull if idl_type.name == 'String': return 'result.isNull()' if idl_type.is_interface_type: return '!result' return '' idl_type = getter.idl_type extended_attributes = getter.extended_attributes is_raises_exception = 'RaisesException' in extended_attributes # FIXME: make more generic, so can use v8_methods.cpp_value cpp_method_name = 'impl->%s' % cpp_name(getter) if is_raises_exception: cpp_arguments.append('exceptionState') union_arguments = idl_type.union_arguments if union_arguments: cpp_arguments.extend([member_argument['cpp_value'] for member_argument in union_arguments]) cpp_value = '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments)) return { 'cpp_type': idl_type.cpp_type, 'cpp_value': cpp_value, 'is_custom': 'Custom' in extended_attributes and (not extended_attributes['Custom'] or has_extended_attribute_value(getter, 'Custom', 'PropertyGetter')), 'is_custom_property_enumerator': has_extended_attribute_value( getter, 'Custom', 'PropertyEnumerator'), 'is_custom_property_query': has_extended_attribute_value( getter, 'Custom', 'PropertyQuery'), 'is_enumerable': 'NotEnumerable' not in extended_attributes, 'is_null_expression': is_null_expression(idl_type), 'is_raises_exception': is_raises_exception, 'name': cpp_name(getter), 'union_arguments': union_arguments, 'v8_set_return_value': idl_type.v8_set_return_value('result', extended_attributes=extended_attributes, script_wrappable='impl', release=idl_type.release), }
def cpp_value(interface, method, number_of_arguments): def cpp_argument(argument): idl_type = argument.idl_type if idl_type.name == "EventListener": return argument.name if idl_type.is_dictionary: return "*%s" % argument.name if idl_type.name in ["NodeFilter", "NodeFilterOrNull", "XPathNSResolver", "XPathNSResolverOrNull"]: # FIXME: remove this special case return "%s.release()" % argument.name return argument.name # Truncate omitted optional arguments arguments = method.arguments[:number_of_arguments] cpp_arguments = [] if "ImplementedInPrivateScript" in method.extended_attributes: cpp_arguments.append("toFrameIfNotDetached(info.GetIsolate()->GetCurrentContext())") cpp_arguments.append("impl") if method.is_constructor: call_with_values = interface.extended_attributes.get("ConstructorCallWith") else: call_with_values = method.extended_attributes.get("CallWith") cpp_arguments.extend(v8_utilities.call_with_arguments(call_with_values)) # Members of IDL partial interface definitions are implemented in C++ as # static member functions, which for instance members (non-static members) # take *impl as their first argument if ( "PartialInterfaceImplementedAs" in method.extended_attributes and not "ImplementedInPrivateScript" in method.extended_attributes and not method.is_static ): cpp_arguments.append("*impl") cpp_arguments.extend(cpp_argument(argument) for argument in arguments) this_union_arguments = method.idl_type and method.idl_type.union_arguments if this_union_arguments: cpp_arguments.extend([member_argument["cpp_value"] for member_argument in this_union_arguments]) if "ImplementedInPrivateScript" in method.extended_attributes: if method.idl_type.name != "void": cpp_arguments.append("&result") elif "RaisesException" in method.extended_attributes or ( method.is_constructor and has_extended_attribute_value(interface, "RaisesException", "Constructor") ): cpp_arguments.append("exceptionState") if method.name == "Constructor": base_name = "create" elif method.name == "NamedConstructor": base_name = "createForJSConstructor" elif "ImplementedInPrivateScript" in method.extended_attributes: base_name = "%sMethod" % method.name else: base_name = v8_utilities.cpp_name(method) cpp_method_name = v8_utilities.scoped_name(interface, method, base_name) return "%s(%s)" % (cpp_method_name, ", ".join(cpp_arguments))
def dictionary_impl_context(dictionary, interfaces_info): includes.clear() header_includes = set(["platform/heap/Handle.h"]) return { "header_includes": header_includes, "cpp_class": v8_utilities.cpp_name(dictionary), "members": [member_impl_context(member, interfaces_info, header_includes) for member in dictionary.members], }
def attribute_context(interface, attribute): idl_type = attribute.idl_type base_idl_type = idl_type.base_type extended_attributes = attribute.extended_attributes idl_type.add_includes_for_type() # [TypeChecking] has_type_checking_unrestricted = ( (has_extended_attribute_value(interface, 'TypeChecking', 'Unrestricted') or has_extended_attribute_value(attribute, 'TypeChecking', 'Unrestricted')) and idl_type.name in ('Float', 'Double')) context = { 'argument_cpp_type': idl_type.cpp_type_args(used_as_rvalue_type=True), 'cached_attribute_validation_method': extended_attributes.get('CachedAttribute'), 'constructor_type': idl_type.constructor_type_name if is_constructor_attribute(attribute) else None, 'cpp_name': cpp_name(attribute), 'cpp_type': idl_type.cpp_type, 'cpp_type_initializer': idl_type.cpp_type_initializer, 'enum_validation_expression': idl_type.enum_validation_expression, 'exposed_test': v8_utilities.exposed(attribute, interface), # [Exposed] 'has_custom_getter': has_custom_getter(attribute), 'has_custom_setter': has_custom_setter(attribute), 'has_type_checking_unrestricted': has_type_checking_unrestricted, 'idl_type': str(idl_type), # need trailing [] on array for Dictionary::ConversionContext::setConversionType 'is_call_with_execution_context': v8_utilities.has_extended_attribute_value(attribute, 'CallWith', 'ExecutionContext'), 'is_call_with_script_state': v8_utilities.has_extended_attribute_value(attribute, 'CallWith', 'ScriptState'), 'is_getter_raises_exception': # [RaisesException] 'RaisesException' in extended_attributes and extended_attributes['RaisesException'] in (None, 'Getter'), 'is_initialized_by_event_constructor': 'InitializedByEventConstructor' in extended_attributes, 'is_keep_alive_for_gc': is_keep_alive_for_gc(interface, attribute), 'is_nullable': idl_type.is_nullable, 'is_explicit_nullable': idl_type.is_explicit_nullable, 'is_partial_interface_member': 'PartialInterfaceImplementedAs' in extended_attributes, 'is_read_only': attribute.is_read_only, 'is_replaceable': 'Replaceable' in attribute.extended_attributes, 'is_static': attribute.is_static, 'is_url': 'URL' in extended_attributes, 'name': attribute.name, 'put_forwards': 'PutForwards' in extended_attributes, 'setter_callback': setter_callback_name(interface, attribute), } if is_constructor_attribute(attribute): constructor_getter_context(interface, attribute, context) return context if not has_custom_getter(attribute): getter_context(interface, attribute, context) if (not has_custom_setter(attribute) and (not attribute.is_read_only or 'PutForwards' in extended_attributes)): setter_context(interface, attribute, context) return context
def cpp_value(interface, method, number_of_arguments): def cpp_argument(argument): idl_type = argument.idl_type if idl_type.name == 'EventListener': return argument.name if (idl_type.name in ['NodeFilter', 'NodeFilterOrNull', 'XPathNSResolver', 'XPathNSResolverOrNull']): # FIXME: remove this special case return '%s.release()' % argument.name return argument.name # Truncate omitted optional arguments arguments = method.arguments[:number_of_arguments] cpp_arguments = [] if 'ImplementedInPrivateScript' in method.extended_attributes: cpp_arguments.append('toLocalFrame(toFrameIfNotDetached(info.GetIsolate()->GetCurrentContext()))') cpp_arguments.append('impl') if method.is_constructor: call_with_values = interface.extended_attributes.get('ConstructorCallWith') else: call_with_values = method.extended_attributes.get('CallWith') cpp_arguments.extend(v8_utilities.call_with_arguments(call_with_values)) # Members of IDL partial interface definitions are implemented in C++ as # static member functions, which for instance members (non-static members) # take *impl as their first argument if ('PartialInterfaceImplementedAs' in method.extended_attributes and not 'ImplementedInPrivateScript' in method.extended_attributes and not method.is_static): cpp_arguments.append('*impl') cpp_arguments.extend(cpp_argument(argument) for argument in arguments) if 'ImplementedInPrivateScript' in method.extended_attributes: if method.idl_type.name != 'void': cpp_arguments.append('&result') elif ('RaisesException' in method.extended_attributes or (method.is_constructor and has_extended_attribute_value(interface, 'RaisesException', 'Constructor'))): cpp_arguments.append('exceptionState') # If a method returns an IDL dictionary or union type, the return value is # passed as an argument to impl classes. idl_type = method.idl_type if idl_type and idl_type.use_output_parameter_for_result: cpp_arguments.append('result') if method.name == 'Constructor': base_name = 'create' elif method.name == 'NamedConstructor': base_name = 'createForJSConstructor' elif 'ImplementedInPrivateScript' in method.extended_attributes: base_name = '%sMethod' % method.name else: base_name = v8_utilities.cpp_name(method) cpp_method_name = v8_utilities.scoped_name(interface, method, base_name) return '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments))
def runtime_call_stats_context(interface, method): includes.add('platform/bindings/RuntimeCallStats.h') generic_counter_name = 'Blink_' + v8_utilities.cpp_name(interface) + '_' + method.name (method_counter, extended_attribute_defined) = v8_utilities.rcs_counter_name(method, generic_counter_name) return { 'extended_attribute_defined': extended_attribute_defined, 'method_counter': method_counter, 'origin_safe_method_getter_counter': generic_counter_name + '_OriginSafeMethodGetter' }
def dictionary_context(dictionary): includes.clear() includes.update(DICTIONARY_CPP_INCLUDES) return { "cpp_class": v8_utilities.cpp_name(dictionary), "header_includes": set(DICTIONARY_H_INCLUDES), "members": [member_context(member) for member in sorted(dictionary.members, key=operator.attrgetter("name"))], "v8_class": v8_utilities.v8_class_name(dictionary), }
def setter_base_name(attribute, arguments): if 'Reflect' not in attribute.extended_attributes: return 'set%s' % capitalize(cpp_name(attribute)) arguments.append(scoped_content_attribute_name(attribute)) idl_type = attribute.idl_type if idl_type in CONTENT_ATTRIBUTE_SETTER_NAMES: return CONTENT_ATTRIBUTE_SETTER_NAMES[idl_type] return 'setAttribute'
def setter_callback_name(interface, attribute): cpp_class_name = cpp_name(interface) extended_attributes = attribute.extended_attributes if ("Replaceable" in extended_attributes and "PutForwards" not in extended_attributes) or is_constructor_attribute( attribute ): return "{0}V8Internal::{0}ForceSetAttributeOnThisCallback".format(cpp_class_name) if attribute.is_read_only and "PutForwards" not in extended_attributes: return "0" return "%sV8Internal::%sAttributeSetterCallback" % (cpp_class_name, attribute.name)
def dictionary_impl_context(dictionary, interfaces_info): includes.clear() header_includes = set(['platform/heap/Handle.h']) return { 'header_includes': header_includes, 'cpp_class': v8_utilities.cpp_name(dictionary), 'members': [member_impl_context(member, interfaces_info, header_includes) for member in dictionary.members], }
def property_deleter(deleter): idl_type = deleter.idl_type if str(idl_type) != "boolean": raise Exception('Only deleters with boolean type are allowed, but type is "%s"' % idl_type) extended_attributes = deleter.extended_attributes return { "is_custom": "Custom" in extended_attributes, "is_raises_exception": "RaisesException" in extended_attributes, "name": cpp_name(deleter), }
def setter_callback_name(interface, attribute): cpp_class_name = cpp_name(interface) if ('Replaceable' in attribute.extended_attributes or is_constructor_attribute(attribute)): # FIXME: rename to ForceSetAttributeOnThisCallback, since also used for Constructors return '{0}V8Internal::{0}ReplaceableAttributeSetterCallback'.format(cpp_class_name) # FIXME: support [PutForwards] if attribute.is_read_only: return '0' return '%sV8Internal::%sAttributeSetterCallback' % (cpp_class_name, attribute.name)
def _scoped_name(interface, definition, base_name): # partial interfaces are implemented as separate classes, with their members # implemented as static member functions partial_interface_implemented_as = definition.extended_attributes.get('PartialInterfaceImplementedAs') if partial_interface_implemented_as: return '%s::%s' % (partial_interface_implemented_as, base_name) if (definition.is_static or definition.name in ('Constructor', 'NamedConstructor')): return '%s::%s' % (v8_utilities.cpp_name(interface), base_name) return 'receiver->%s' % base_name
def setter_callback_name(interface, attribute): cpp_class_name = cpp_name(interface) extended_attributes = attribute.extended_attributes if (('Replaceable' in extended_attributes and 'PutForwards' not in extended_attributes) or is_constructor_attribute(attribute)): return '{0}V8Internal::{0}ForceSetAttributeOnThisCallback'.format(cpp_class_name) if attribute.is_read_only and 'PutForwards' not in extended_attributes: return '0' return '%sV8Internal::%sAttributeSetterCallback' % (cpp_class_name, attribute.name)
def member_impl_context(member, interfaces_info, header_includes, header_forward_decls): idl_type = unwrap_nullable_if_needed(member.idl_type) cpp_name = v8_utilities.cpp_name(member) nullable_indicator_name = None if not idl_type.cpp_type_has_null_value: nullable_indicator_name = 'm_has' + cpp_name[0].upper() + cpp_name[1:] def has_method_expression(): if nullable_indicator_name: return nullable_indicator_name elif idl_type.is_union_type: return '!m_%s.isNull()' % cpp_name elif idl_type.is_enum or idl_type.is_string_type: return '!m_%s.IsNull()' % cpp_name elif idl_type.name in ['Any', 'Object']: return '!(m_{0}.IsEmpty() || m_{0}.IsNull() || m_{0}.IsUndefined())'.format(cpp_name) elif idl_type.name == 'Dictionary': return '!m_%s.IsUndefinedOrNull()' % cpp_name else: return 'm_%s' % cpp_name cpp_default_value = None if member.default_value and not member.default_value.is_null: cpp_default_value = idl_type.literal_cpp_value(member.default_value) forward_decl_name = idl_type.impl_forward_declaration_name if forward_decl_name: includes.update(idl_type.impl_includes_for_type(interfaces_info)) header_forward_decls.add(forward_decl_name) else: header_includes.update(idl_type.impl_includes_for_type(interfaces_info)) setter_value = 'value' if idl_type.is_array_buffer_view_or_typed_array: setter_value += '.View()' return { 'cpp_default_value': cpp_default_value, 'cpp_name': cpp_name, 'getter_expression': 'm_' + cpp_name, 'getter_name': getter_name_for_dictionary_member(member), 'has_method_expression': has_method_expression(), 'has_method_name': has_method_name_for_dictionary_member(member), 'is_nullable': idl_type.is_nullable, 'is_traceable': idl_type.is_traceable, 'member_cpp_type': idl_type.cpp_type_args(used_in_cpp_sequence=True), 'null_setter_name': null_setter_name_for_dictionary_member(member), 'nullable_indicator_name': nullable_indicator_name, 'rvalue_cpp_type': idl_type.cpp_type_args(used_as_rvalue_type=True), 'setter_name': setter_name_for_dictionary_member(member), 'setter_value': setter_value, }
def dictionary_context(dictionary): includes.clear() includes.update(DICTIONARY_CPP_INCLUDES) return { 'cpp_class': v8_utilities.cpp_name(dictionary), 'header_includes': set(DICTIONARY_H_INCLUDES), 'members': [member_context(member) for member in sorted(dictionary.members, key=operator.attrgetter('name'))], 'v8_class': v8_utilities.v8_class_name(dictionary), }
def member_context(dictionary, member): extended_attributes = member.extended_attributes idl_type = member.idl_type idl_type.add_includes_for_type(extended_attributes) unwrapped_idl_type = unwrap_nullable_if_needed(idl_type) if member.is_required and member.default_value: raise Exception( 'Required member %s must not have a default value.' % member.name) def default_values(): if not member.default_value: return None, None if member.default_value.is_null: return None, 'v8::Null(isolate)' cpp_default_value = unwrapped_idl_type.literal_cpp_value( member.default_value) v8_default_value = unwrapped_idl_type.cpp_value_to_v8_value( cpp_value=cpp_default_value, isolate='isolate', creation_context='creationContext') return cpp_default_value, v8_default_value cpp_default_value, v8_default_value = default_values() cpp_name = v8_utilities.cpp_name(member) getter_name = getter_name_for_dictionary_member(member) is_deprecated_dictionary = unwrapped_idl_type.name == 'Dictionary' return { 'cpp_default_value': cpp_default_value, 'cpp_name': cpp_name, 'cpp_type': unwrapped_idl_type.cpp_type, 'cpp_value_to_v8_value': unwrapped_idl_type.cpp_value_to_v8_value( cpp_value='impl.%s()' % getter_name, isolate='isolate', creation_context='creationContext', extended_attributes=extended_attributes), 'deprecate_as': v8_utilities.deprecate_as(member), 'enum_type': idl_type.enum_type, 'enum_values': unwrapped_idl_type.enum_values, 'getter_name': getter_name, 'has_method_name': has_method_name_for_dictionary_member(member), 'idl_type': idl_type.base_type, 'is_interface_type': idl_type.is_interface_type and not is_deprecated_dictionary, 'is_nullable': idl_type.is_nullable, 'is_object': unwrapped_idl_type.name == 'Object' or is_deprecated_dictionary, 'is_required': member.is_required, 'name': member.name, 'runtime_enabled_feature_name': v8_utilities.runtime_enabled_feature_name(member), # [RuntimeEnabled] 'setter_name': setter_name_for_dictionary_member(member), 'null_setter_name': null_setter_name_for_dictionary_member(member), 'v8_default_value': v8_default_value, 'v8_value_to_local_cpp_value': unwrapped_idl_type.v8_value_to_local_cpp_value( extended_attributes, member.name + 'Value', member.name + 'CppValue', isolate='isolate', use_exception_state=True), }
def dictionary_context(dictionary, interfaces_info): includes.clear() includes.update(DICTIONARY_CPP_INCLUDES) if 'RuntimeEnabled' in dictionary.extended_attributes: raise Exception( 'Dictionary cannot be RuntimeEnabled: %s' % dictionary.name) members = [member_context(dictionary, member) for member in sorted(dictionary.members, key=operator.attrgetter('name'))] for member in members: if member['runtime_enabled_feature_name']: includes.add('platform/RuntimeEnabledFeatures.h') break cpp_class = v8_utilities.cpp_name(dictionary) context = { 'cpp_class': cpp_class, 'header_includes': set(DICTIONARY_H_INCLUDES), 'members': members, 'required_member_names': sorted([member.name for member in dictionary.members if member.is_required]), 'use_permissive_dictionary_conversion': 'PermissiveDictionaryConversion' in dictionary.extended_attributes, 'v8_class': v8_types.v8_type(cpp_class), 'v8_original_class': v8_types.v8_type(dictionary.name), } if dictionary.parent: IdlType(dictionary.parent).add_includes_for_type() parent_cpp_class = v8_utilities.cpp_name_from_interfaces_info( dictionary.parent, interfaces_info) context.update({ 'parent_cpp_class': parent_cpp_class, 'parent_v8_class': v8_types.v8_type(parent_cpp_class), }) return context
def member_context(member, info_provider): _update_includes_and_forward_decls(member, info_provider) if member.is_nullable: member = member.inner_type return { 'cpp_name': to_snake_case(v8_utilities.cpp_name(member)), 'cpp_type': member.cpp_type_args(used_in_cpp_sequence=True), 'cpp_local_type': member.cpp_type, 'cpp_value_to_v8_value': member.cpp_value_to_v8_value(cpp_value='impl.GetAs%s()' % member.name, isolate='isolate', creation_context='creationContext'), 'enum_type': member.enum_type, 'enum_values': member.enum_values, 'is_array_buffer_or_view_type': member.is_array_buffer_or_view, 'is_array_buffer_view_or_typed_array': member.is_array_buffer_view_or_typed_array, 'is_traceable': member.is_traceable, 'rvalue_cpp_type': member.cpp_type_args(used_as_rvalue_type=True), 'specific_type_enum': 'k' + member.name, 'type_name': member.name, 'v8_value_to_local_cpp_value': member.v8_value_to_local_cpp_value({}, 'v8Value', 'cppValue', isolate='isolate', use_exception_state=True) }
def setter_expression(interface, attribute, contents): extended_attributes = attribute.extended_attributes arguments = v8_utilities.call_with_arguments( attribute, extended_attributes.get('SetterCallWith')) this_setter_base_name = setter_base_name(interface, attribute, arguments) setter_name = scoped_name(interface, attribute, this_setter_base_name) # Members of IDL partial interface definitions are implemented in C++ as # static member functions, which for instance members (non-static members) # take *impl as their first argument if ('PartialInterfaceImplementedAs' in extended_attributes and not attribute.is_static): arguments.append('*impl') idl_type = attribute.idl_type if idl_type.base_type == 'EventHandler': getter_name = scoped_name(interface, attribute, cpp_name(attribute)) contents['event_handler_getter_expression'] = '%s(%s)' % ( getter_name, ', '.join(arguments)) if (interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/v8/V8ErrorHandler.h') arguments.append( 'V8EventListenerList::findOrCreateWrapper<V8ErrorHandler>(v8Value, true, info.GetIsolate())' ) else: arguments.append( 'V8EventListenerList::getEventListener(ScriptState::current(info.GetIsolate()), v8Value, true, ListenerFindOrCreate)' ) elif idl_type.is_interface_type and not idl_type.array_type: # FIXME: should be able to eliminate WTF::getPtr in most or all cases arguments.append('WTF::getPtr(cppValue)') else: arguments.append('cppValue') if contents['is_setter_raises_exception']: arguments.append('exceptionState') return '%s(%s)' % (setter_name, ', '.join(arguments))
def setter_expression(interface, attribute, context): extended_attributes = attribute.extended_attributes arguments = v8_utilities.call_with_arguments( extended_attributes.get('SetterCallWith') or extended_attributes.get('CallWith')) this_setter_base_name = setter_base_name(interface, attribute, arguments) setter_name = scoped_name(interface, attribute, this_setter_base_name) # Members of IDL partial interface definitions are implemented in C++ as # static member functions, which for instance members (non-static members) # take *impl as their first argument if ('PartialInterfaceImplementedAs' in extended_attributes and not attribute.is_static): arguments.append('*impl') idl_type = attribute.idl_type if idl_type.base_type == 'EventHandler': getter_name = scoped_name(interface, attribute, cpp_name(attribute)) context['event_handler_getter_expression'] = '%s(%s)' % ( getter_name, ', '.join(arguments)) if (interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/core/v8/V8ErrorHandler.h') arguments.append('V8EventListenerHelper::EnsureErrorHandler(' + 'ScriptState::ForRelevantRealm(info), v8Value)') else: arguments.append( 'V8EventListenerHelper::GetEventListener(' + 'ScriptState::ForRelevantRealm(info), v8Value, true, ' + 'kListenerFindOrCreate)') else: arguments.append('cppValue') if idl_type.is_explicit_nullable: arguments.append('isNull') if context['is_setter_raises_exception']: arguments.append('exceptionState') return '%s(%s)' % (setter_name, ', '.join(arguments))
def dictionary_context(dictionary, interfaces_info): includes.clear() includes.update(DICTIONARY_CPP_INCLUDES) cpp_class = v8_utilities.cpp_name(dictionary) context = { 'cpp_class': cpp_class, 'header_includes': set(DICTIONARY_H_INCLUDES), 'members': [member_context(member) for member in sorted(dictionary.members, key=operator.attrgetter('name'))], 'use_permissive_dictionary_conversion': 'PermissiveDictionaryConversion' in dictionary.extended_attributes, 'v8_class': v8_types.v8_type(cpp_class), 'v8_original_class': v8_types.v8_type(dictionary.name), } if dictionary.parent: IdlType(dictionary.parent).add_includes_for_type() parent_cpp_class = v8_utilities.cpp_name_from_interfaces_info( dictionary.parent, interfaces_info) context.update({ 'parent_cpp_class': parent_cpp_class, 'parent_v8_class': v8_types.v8_type(parent_cpp_class), }) return context
def setter_expression(interface, attribute, contents): extended_attributes = attribute.extended_attributes arguments = v8_utilities.call_with_arguments( attribute, extended_attributes.get('SetterCallWith')) this_setter_base_name = setter_base_name(attribute, arguments) setter_name = v8_utilities.scoped_name(interface, attribute, this_setter_base_name) if ('ImplementedBy' in extended_attributes and not attribute.is_static): arguments.append('imp') idl_type = attribute.idl_type if idl_type == 'EventHandler': getter_name = v8_utilities.scoped_name(interface, attribute, cpp_name(attribute)) contents['event_handler_getter_expression'] = '%s(%s)' % ( getter_name, ', '.join(arguments)) if (interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/v8/V8ErrorHandler.h') arguments.append( 'V8EventListenerList::findOrCreateWrapper<V8ErrorHandler>(jsValue, true, info.GetIsolate())' ) else: arguments.append( 'V8EventListenerList::getEventListener(jsValue, true, ListenerFindOrCreate)' ) elif v8_types.is_interface_type( idl_type) and not v8_types.array_type(idl_type): # FIXME: should be able to eliminate WTF::getPtr in most or all cases arguments.append('WTF::getPtr(cppValue)') else: arguments.append('cppValue') if contents['is_setter_raises_exception']: arguments.append('exceptionState') return '%s(%s)' % (setter_name, ', '.join(arguments))
def getter_base_name(interface, attribute, arguments): extended_attributes = attribute.extended_attributes if 'Reflect' not in extended_attributes: name = cpp_name(attribute) return name if 'ImplementedAs' in extended_attributes \ else uncapitalize(name) content_attribute_name = extended_attributes['Reflect'] or attribute.name.lower() if content_attribute_name in ['class', 'id', 'name']: # Special-case for performance optimization. return 'Get%sAttribute' % content_attribute_name.capitalize() arguments.append(scoped_content_attribute_name(interface, attribute)) base_idl_type = attribute.idl_type.base_type if base_idl_type in CONTENT_ATTRIBUTE_GETTER_NAMES: return CONTENT_ATTRIBUTE_GETTER_NAMES[base_idl_type] if 'URL' in attribute.extended_attributes: return 'GetURLAttribute' idl_type = attribute.idl_type if idl_type.is_frozen_array: return 'Get%sArrayAttribute' % idl_type.element_type return 'FastGetAttribute'
def generate_code_internal(self, definitions, definition_name): if not definition_name in definitions.dictionaries: raise ValueError('%s is not an IDL dictionary' % definition_name) interfaces_info = self.info_provider.interfaces_info dictionary = definitions.dictionaries[definition_name] interface_info = interfaces_info[definition_name] header_template = self.jinja_env.get_template('dictionary_impl.h.tmpl') cpp_template = self.jinja_env.get_template('dictionary_impl.cpp.tmpl') template_context = v8_dictionary.dictionary_impl_context( dictionary, interfaces_info) include_paths = interface_info.get('dependencies_include_paths') if not is_testing_target(interface_info.get('full_path')): template_context['exported'] = self.info_provider.specifier_for_export template_context['header_includes'].add(self.info_provider.include_path_for_export) template_context['header_includes'].update( interface_info.get('additional_header_includes', [])) header_text, cpp_text = self.render_template( include_paths, header_template, cpp_template, template_context) header_path, cpp_path = self.output_paths( cpp_name(dictionary), interface_info) return ( (header_path, header_text), (cpp_path, cpp_text), )
def dictionary_impl_context(dictionary, interfaces_info): def remove_duplicate_members(members): # When [ImplementedAs] is used, cpp_name can conflict. For example, # dictionary D { long foo; [ImplementedAs=foo, DeprecateAs=Foo] long oldFoo; }; # This function removes such duplications, checking they have the same type. members_dict = {} for member in members: cpp_name = member['cpp_name'] duplicated_member = members_dict.get(cpp_name) if duplicated_member and duplicated_member != member: raise Exception('Member name conflict: %s' % cpp_name) members_dict[cpp_name] = member return sorted(members_dict.values(), key=lambda member: member['cpp_name']) includes.clear() header_includes = set(['platform/heap/Handle.h']) members = [ member_impl_context(member, interfaces_info, header_includes) for member in dictionary.members ] members = remove_duplicate_members(members) context = { 'header_includes': header_includes, 'cpp_class': v8_utilities.cpp_name(dictionary), 'members': members, } if dictionary.parent: context[ 'parent_cpp_class'] = v8_utilities.cpp_name_from_interfaces_info( dictionary.parent, interfaces_info) parent_interface_info = interfaces_info.get(dictionary.parent) if parent_interface_info: context['header_includes'].add( parent_interface_info['include_path']) return context
def constructor_context(interface, constructor): arguments_need_try_catch = any(v8_methods.argument_needs_try_catch(constructor, argument) for argument in constructor.arguments) # [RaisesException=Constructor] is_constructor_raises_exception = \ interface.extended_attributes.get('RaisesException') == 'Constructor' return { 'arguments': [v8_methods.argument_context(interface, constructor, argument, index) for index, argument in enumerate(constructor.arguments)], 'arguments_need_try_catch': arguments_need_try_catch, 'cpp_type': cpp_template_type( 'RefPtr', cpp_name(interface)), 'cpp_value': v8_methods.cpp_value( interface, constructor, len(constructor.arguments)), 'has_exception_state': is_constructor_raises_exception or any(argument for argument in constructor.arguments if argument.idl_type.name == 'SerializedScriptValue' or argument.idl_type.may_raise_exception_on_conversion), 'is_call_with_document': # [ConstructorCallWith=Document] has_extended_attribute_value(interface, 'ConstructorCallWith', 'Document'), 'is_call_with_execution_context': # [ConstructorCallWith=ExecutionContext] has_extended_attribute_value(interface, 'ConstructorCallWith', 'ExecutionContext'), 'is_constructor': True, 'is_named_constructor': False, 'is_raises_exception': is_constructor_raises_exception, 'number_of_required_arguments': number_of_required_arguments(constructor), }
def setter_name_for_dictionary_member(member): name = 'set_{}'.format(v8_utilities.cpp_name(member)) return NameStyleConverter(name).to_lower_camel_case()
def attribute_context(interface, attribute, interfaces): """Creates a Jinja template context for an attribute of an interface. Args: interface: An interface which |attribute| belongs to attribute: An attribute to create the context for interfaces: A dict which maps an interface name to the definition which can be referred if needed Returns: A Jinja template context for |attribute| """ idl_type = attribute.idl_type base_idl_type = idl_type.base_type extended_attributes = attribute.extended_attributes idl_type.add_includes_for_type(extended_attributes) if idl_type.enum_values: includes.add('core/inspector/ConsoleMessage.h') # [CheckSecurity] is_cross_origin = 'CrossOrigin' in extended_attributes is_check_security_for_receiver = (has_extended_attribute_value( interface, 'CheckSecurity', 'Receiver') and is_cross_origin) is_check_security_for_return_value = (has_extended_attribute_value( attribute, 'CheckSecurity', 'ReturnValue')) if is_check_security_for_receiver or is_check_security_for_return_value: includes.add('bindings/core/v8/BindingSecurity.h') # [CrossOrigin] if has_extended_attribute_value(attribute, 'CrossOrigin', 'Setter'): includes.add('bindings/core/v8/V8CrossOriginSetterInfo.h') # [Constructor] # TODO(yukishiino): Constructors are much like methods although constructors # are not methods. Constructors must be data-type properties, and we can # support them as a kind of methods. constructor_type = idl_type.constructor_type_name if is_constructor_attribute( attribute) else None # [CEReactions] is_ce_reactions = 'CEReactions' in extended_attributes if is_ce_reactions: includes.add('core/dom/custom/CEReactionsScope.h') # [CustomElementCallbacks], [Reflect] is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attributes is_reflect = 'Reflect' in extended_attributes if is_custom_element_callbacks or is_reflect: includes.add('core/dom/custom/V0CustomElementProcessingStack.h') # [PerWorldBindings] if 'PerWorldBindings' in extended_attributes: assert idl_type.is_wrapper_type or 'LogActivity' in extended_attributes, '[PerWorldBindings] should only be used with wrapper types: %s.%s' % ( interface.name, attribute.name) # [SaveSameObject] is_save_same_object = ('SameObject' in attribute.extended_attributes and 'SaveSameObject' in attribute.extended_attributes) if is_save_same_object: includes.add('bindings/core/v8/V8PrivateProperty.h') if (base_idl_type == 'EventHandler' and interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/core/v8/V8ErrorHandler.h') cached_attribute_validation_method = extended_attributes.get( 'CachedAttribute') keep_alive_for_gc = is_keep_alive_for_gc(interface, attribute) if cached_attribute_validation_method or keep_alive_for_gc: includes.add('bindings/core/v8/V8HiddenValue.h') # [CachedAccessor] is_cached_accessor = 'CachedAccessor' in extended_attributes if is_cached_accessor: includes.add('bindings/core/v8/V8PrivateProperty.h') context = { 'access_control_list': access_control_list(interface, attribute), 'activity_logging_world_list_for_getter': v8_utilities.activity_logging_world_list(attribute, 'Getter'), # [ActivityLogging] 'activity_logging_world_list_for_setter': v8_utilities.activity_logging_world_list(attribute, 'Setter'), # [ActivityLogging] 'activity_logging_world_check': v8_utilities.activity_logging_world_check(attribute), # [ActivityLogging] 'cached_attribute_validation_method': cached_attribute_validation_method, 'constructor_type': constructor_type, 'cpp_name': cpp_name(attribute), 'cpp_type': idl_type.cpp_type, 'cpp_type_initializer': idl_type.cpp_type_initializer, 'deprecate_as': v8_utilities.deprecate_as(attribute), # [DeprecateAs] 'enum_type': idl_type.enum_type, 'enum_values': idl_type.enum_values, 'exposed_test': v8_utilities.exposed(attribute, interface), # [Exposed] 'has_cross_origin_getter': has_extended_attribute_value(attribute, 'CrossOrigin', None) or has_extended_attribute_value(attribute, 'CrossOrigin', 'Getter'), 'has_cross_origin_setter': has_extended_attribute_value(attribute, 'CrossOrigin', 'Setter'), 'has_custom_getter': has_custom_getter(attribute), 'has_custom_setter': has_custom_setter(attribute), 'has_setter': has_setter(interface, attribute), 'idl_type': str(idl_type), # need trailing [] on array for Dictionary::ConversionContext::setConversionType 'is_cached_accessor': is_cached_accessor, 'is_call_with_execution_context': has_extended_attribute_value(attribute, 'CallWith', 'ExecutionContext'), 'is_call_with_script_state': has_extended_attribute_value(attribute, 'CallWith', 'ScriptState'), 'is_ce_reactions': is_ce_reactions, 'is_check_security_for_receiver': is_check_security_for_receiver, 'is_check_security_for_return_value': is_check_security_for_return_value, 'is_custom_element_callbacks': is_custom_element_callbacks, # TODO(yukishiino): Make all DOM attributes accessor-type properties. 'is_data_type_property': is_data_type_property(interface, attribute), 'is_getter_raises_exception': # [RaisesException] 'RaisesException' in extended_attributes and extended_attributes['RaisesException'] in (None, 'Getter'), 'is_keep_alive_for_gc': keep_alive_for_gc, 'is_lenient_this': 'LenientThis' in extended_attributes, 'is_nullable': idl_type.is_nullable, 'is_explicit_nullable': idl_type.is_explicit_nullable, 'is_partial_interface_member': 'PartialInterfaceImplementedAs' in extended_attributes, 'is_per_world_bindings': 'PerWorldBindings' in extended_attributes, 'is_put_forwards': 'PutForwards' in extended_attributes, 'is_read_only': attribute.is_read_only, 'is_reflect': is_reflect, 'is_replaceable': 'Replaceable' in attribute.extended_attributes, 'is_save_same_object': is_save_same_object, 'is_static': attribute.is_static, 'is_url': 'URL' in extended_attributes, 'is_unforgeable': is_unforgeable(interface, attribute), 'on_instance': v8_utilities.on_instance(interface, attribute), 'on_interface': v8_utilities.on_interface(interface, attribute), 'on_prototype': v8_utilities.on_prototype(interface, attribute), 'origin_trial_enabled_function': v8_utilities.origin_trial_enabled_function_name(attribute), # [OriginTrialEnabled] 'origin_trial_feature_name': v8_utilities.origin_trial_feature_name(attribute), # [OriginTrialEnabled] 'use_output_parameter_for_result': idl_type.use_output_parameter_for_result, 'measure_as': v8_utilities.measure_as(attribute, interface), # [MeasureAs] 'name': attribute.name, 'property_attributes': property_attributes(interface, attribute), 'reflect_empty': extended_attributes.get('ReflectEmpty'), 'reflect_invalid': extended_attributes.get('ReflectInvalid', ''), 'reflect_missing': extended_attributes.get('ReflectMissing'), 'reflect_only': extended_attribute_value_as_list(attribute, 'ReflectOnly'), 'runtime_enabled_feature_name': v8_utilities.runtime_enabled_feature_name(attribute), # [RuntimeEnabled] 'secure_context_test': v8_utilities.secure_context(attribute, interface), # [SecureContext] 'cached_accessor_name': '%s%sCachedAccessor' % (interface.name, attribute.name.capitalize()), 'world_suffixes': ( ['', 'ForMainWorld'] if 'PerWorldBindings' in extended_attributes else ['']), # [PerWorldBindings] } if is_constructor_attribute(attribute): update_constructor_attribute_context(interface, attribute, context) if not has_custom_getter(attribute): getter_context(interface, attribute, context) if not has_custom_setter(attribute) and has_setter(interface, attribute): setter_context(interface, attribute, interfaces, context) # [CrossOrigin] is incompatible with a number of other attributes, so check # for them here. if is_cross_origin: if context['has_cross_origin_getter'] and context['has_custom_getter']: raise Exception( '[CrossOrigin] and [Custom] are incompatible on the same getter: %s.%s', interface.name, attribute.name) if context['has_cross_origin_setter'] and context['has_custom_setter']: raise Exception( '[CrossOrigin] and [Custom] are incompatible on the same setter: %s.%s', interface.name, attribute.name) if context['is_per_world_bindings']: raise Exception( '[CrossOrigin] and [PerWorldBindings] are incompatible: %s.%s', interface.name, attribute.name) if context['constructor_type']: raise Exception( '[CrossOrigin] cannot be used for constructors: %s.%s', interface.name, attribute.name) return context
def has_method_name_for_dictionary_member(member): name = v8_utilities.cpp_name(member) return 'has%s' % v8_utilities.capitalize(name)
def null_setter_name_for_dictionary_member(member): if member.idl_type.is_nullable: name = v8_utilities.cpp_name(member) return 'set%sToNull' % v8_utilities.capitalize(name) return None
def setter_name_for_dictionary_member(member): name = v8_utilities.cpp_name(member) return 'set%s' % v8_utilities.capitalize(name)
def null_setter_name_for_dictionary_member(member): if member.idl_type.is_nullable: name = 'set_{}_to_null'.format(v8_utilities.cpp_name(member)) return NameStyleConverter(name).to_lower_camel_case() return None
def cpp_value(interface, method, number_of_arguments): def cpp_argument(argument): idl_type = argument.idl_type if idl_type.name == 'EventListener': return argument.name if (idl_type.name in [ 'NodeFilter', 'NodeFilterOrNull', 'XPathNSResolver', 'XPathNSResolverOrNull' ]): # FIXME: remove this special case return '%s.release()' % argument.name return argument.name # Truncate omitted optional arguments arguments = method.arguments[:number_of_arguments] cpp_arguments = [] if 'ImplementedInPrivateScript' in method.extended_attributes: cpp_arguments.append( 'toFrameIfNotDetached(info.GetIsolate()->GetCurrentContext())') cpp_arguments.append('impl') if method.is_constructor: call_with_values = interface.extended_attributes.get( 'ConstructorCallWith') else: call_with_values = method.extended_attributes.get('CallWith') cpp_arguments.extend(v8_utilities.call_with_arguments(call_with_values)) # Members of IDL partial interface definitions are implemented in C++ as # static member functions, which for instance members (non-static members) # take *impl as their first argument if ('PartialInterfaceImplementedAs' in method.extended_attributes and not 'ImplementedInPrivateScript' in method.extended_attributes and not method.is_static): cpp_arguments.append('*impl') cpp_arguments.extend(cpp_argument(argument) for argument in arguments) if 'ImplementedInPrivateScript' in method.extended_attributes: if method.idl_type.name != 'void': cpp_arguments.append('&result') elif ('RaisesException' in method.extended_attributes or (method.is_constructor and has_extended_attribute_value( interface, 'RaisesException', 'Constructor'))): cpp_arguments.append('exceptionState') # If a method returns an IDL dictionary or union type, the return value is # passed as an argument to impl classes. idl_type = method.idl_type if idl_type and idl_type.use_output_parameter_for_result: cpp_arguments.append('result') if method.name == 'Constructor': base_name = 'create' elif method.name == 'NamedConstructor': base_name = 'createForJSConstructor' elif 'ImplementedInPrivateScript' in method.extended_attributes: base_name = '%sMethod' % method.name else: base_name = v8_utilities.cpp_name(method) cpp_method_name = v8_utilities.scoped_name(interface, method, base_name) return '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments))
def getter_name_for_dictionary_member(member): name = v8_utilities.cpp_name(member) if 'PrefixGet' in member.extended_attributes: return 'get%s' % v8_utilities.capitalize(name) return name
def attribute_context(interface, attribute): idl_type = attribute.idl_type base_idl_type = idl_type.base_type extended_attributes = attribute.extended_attributes idl_type.add_includes_for_type(extended_attributes) if idl_type.enum_values: includes.add('core/inspector/ConsoleMessage.h') # [CheckSecurity] is_do_not_check_security = 'DoNotCheckSecurity' in extended_attributes is_check_security_for_receiver = (has_extended_attribute_value( interface, 'CheckSecurity', 'Receiver') and not is_do_not_check_security) is_check_security_for_return_value = (has_extended_attribute_value( attribute, 'CheckSecurity', 'ReturnValue')) if is_check_security_for_receiver or is_check_security_for_return_value: includes.add('bindings/core/v8/BindingSecurity.h') # [Constructor] # TODO(yukishiino): Constructors are much like methods although constructors # are not methods. Constructors must be data-type properties, and we can # support them as a kind of methods. constructor_type = idl_type.constructor_type_name if is_constructor_attribute( attribute) else None # [CustomElementCallbacks], [Reflect] is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attributes is_reflect = 'Reflect' in extended_attributes if is_custom_element_callbacks or is_reflect: includes.add('core/dom/custom/CustomElementProcessingStack.h') # [ImplementedInPrivateScript] is_implemented_in_private_script = 'ImplementedInPrivateScript' in extended_attributes if is_implemented_in_private_script: includes.add('bindings/core/v8/PrivateScriptRunner.h') includes.add('core/frame/LocalFrame.h') includes.add('platform/ScriptForbiddenScope.h') # [OnlyExposedToPrivateScript] is_only_exposed_to_private_script = 'OnlyExposedToPrivateScript' in extended_attributes # [PerWorldBindings] if 'PerWorldBindings' in extended_attributes: assert idl_type.is_wrapper_type or 'LogActivity' in extended_attributes, '[PerWorldBindings] should only be used with wrapper types: %s.%s' % ( interface.name, attribute.name) if (base_idl_type == 'EventHandler' and interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/core/v8/V8ErrorHandler.h') cached_attribute_validation_method = extended_attributes.get( 'CachedAttribute') keep_alive_for_gc = is_keep_alive_for_gc(interface, attribute) if cached_attribute_validation_method or keep_alive_for_gc: includes.add('bindings/core/v8/V8HiddenValue.h') if 'RuntimeEnabled' in extended_attributes: includes.add('platform/RuntimeEnabledFeatures.h') if 'OriginTrialEnabled' in extended_attributes: includes.add('core/inspector/ConsoleMessage.h') includes.add('core/origin_trials/OriginTrials.h') context = { 'access_control_list': access_control_list(interface, attribute), 'activity_logging_world_list_for_getter': v8_utilities.activity_logging_world_list(attribute, 'Getter'), # [ActivityLogging] 'activity_logging_world_list_for_setter': v8_utilities.activity_logging_world_list(attribute, 'Setter'), # [ActivityLogging] 'activity_logging_world_check': v8_utilities.activity_logging_world_check(attribute), # [ActivityLogging] 'argument_cpp_type': idl_type.cpp_type_args(used_as_rvalue_type=True), 'cached_attribute_validation_method': cached_attribute_validation_method, 'constructor_type': constructor_type, 'cpp_name': cpp_name(attribute), 'cpp_type': idl_type.cpp_type, 'cpp_type_initializer': idl_type.cpp_type_initializer, 'deprecate_as': v8_utilities.deprecate_as(attribute), # [DeprecateAs] 'enum_type': idl_type.enum_type, 'enum_values': idl_type.enum_values, 'exposed_test': v8_utilities.exposed(attribute, interface), # [Exposed] 'has_custom_getter': has_custom_getter(attribute), 'has_custom_setter': has_custom_setter(attribute), 'has_setter': has_setter(attribute), 'idl_type': str(idl_type), # need trailing [] on array for Dictionary::ConversionContext::setConversionType 'is_origin_trial_enabled': v8_utilities.origin_trial_enabled_function(attribute) or v8_utilities.origin_trial_enabled_function(interface), # [OriginTrialEnabled] 'is_call_with_execution_context': has_extended_attribute_value(attribute, 'CallWith', 'ExecutionContext'), 'is_call_with_script_state': has_extended_attribute_value(attribute, 'CallWith', 'ScriptState'), 'is_check_security_for_receiver': is_check_security_for_receiver, 'is_check_security_for_return_value': is_check_security_for_return_value, 'is_custom_element_callbacks': is_custom_element_callbacks, # TODO(yukishiino): Make all DOM attributes accessor-type properties. 'is_data_type_property': constructor_type or interface.name == 'Window' or interface.name == 'Location', 'is_getter_raises_exception': # [RaisesException] 'RaisesException' in extended_attributes and extended_attributes['RaisesException'] in (None, 'Getter'), 'is_implemented_in_private_script': is_implemented_in_private_script, 'is_keep_alive_for_gc': keep_alive_for_gc, 'is_lenient_this': 'LenientThis' in extended_attributes, 'is_nullable': idl_type.is_nullable, 'is_explicit_nullable': idl_type.is_explicit_nullable, 'is_partial_interface_member': 'PartialInterfaceImplementedAs' in extended_attributes, 'is_per_world_bindings': 'PerWorldBindings' in extended_attributes, 'is_put_forwards': 'PutForwards' in extended_attributes, 'is_read_only': attribute.is_read_only, 'is_reflect': is_reflect, 'is_replaceable': 'Replaceable' in attribute.extended_attributes, 'is_static': attribute.is_static, 'is_url': 'URL' in extended_attributes, 'is_unforgeable': is_unforgeable(interface, attribute), 'on_instance': v8_utilities.on_instance(interface, attribute), 'on_interface': v8_utilities.on_interface(interface, attribute), 'on_prototype': v8_utilities.on_prototype(interface, attribute), 'origin_trial_enabled': v8_utilities.origin_trial_enabled_function(attribute), # [OriginTrialEnabled] 'origin_trial_enabled_per_interface': v8_utilities.origin_trial_enabled_function(interface), # [OriginTrialEnabled] 'origin_trial_name': extended_attributes.get('OriginTrialEnabled'), # [OriginTrialEnabled] 'use_output_parameter_for_result': idl_type.use_output_parameter_for_result, 'measure_as': v8_utilities.measure_as(attribute, interface), # [MeasureAs] 'name': attribute.name, 'only_exposed_to_private_script': is_only_exposed_to_private_script, 'private_script_v8_value_to_local_cpp_value': idl_type.v8_value_to_local_cpp_value( extended_attributes, 'v8Value', 'cppValue', bailout_return_value='false', isolate='scriptState->isolate()'), 'property_attributes': property_attributes(interface, attribute), 'reflect_empty': extended_attributes.get('ReflectEmpty'), 'reflect_invalid': extended_attributes.get('ReflectInvalid', ''), 'reflect_missing': extended_attributes.get('ReflectMissing'), 'reflect_only': extended_attribute_value_as_list(attribute, 'ReflectOnly'), 'runtime_enabled_function': v8_utilities.runtime_enabled_function_name(attribute), # [RuntimeEnabled] 'should_be_exposed_to_script': not (is_implemented_in_private_script and is_only_exposed_to_private_script), 'world_suffixes': ['', 'ForMainWorld'] if 'PerWorldBindings' in extended_attributes else [''], # [PerWorldBindings] } if is_constructor_attribute(attribute): update_constructor_attribute_context(interface, attribute, context) if not has_custom_getter(attribute): getter_context(interface, attribute, context) if not has_custom_setter(attribute) and has_setter(attribute): setter_context(interface, attribute, context) return context
def getter_name_for_dictionary_member(member): name = v8_utilities.cpp_name(member) return NameStyleConverter(name).to_lower_camel_case()
def setter_callback_name_for_main_world(interface, attribute): if ('PerWorldBindings' not in attribute.extended_attributes or attribute.is_read_only): return '0' return '%sV8Internal::%sAttributeSetterCallbackForMainWorld' % ( cpp_name(interface), attribute.name)
def generate_attribute(interface, attribute): idl_type = attribute.idl_type extended_attributes = attribute.extended_attributes v8_types.add_includes_for_type(idl_type) # [CheckSecurity] is_check_security_for_node = 'CheckSecurity' in extended_attributes if is_check_security_for_node: includes.add('bindings/v8/BindingSecurity.h') # [Custom] has_custom_getter = ('Custom' in extended_attributes and extended_attributes['Custom'] in [None, 'Getter']) has_custom_setter = (not attribute.is_read_only and 'Custom' in extended_attributes and extended_attributes['Custom'] in [None, 'Setter']) # [Reflect] is_reflect = 'Reflect' in extended_attributes if is_reflect: includes.add('core/dom/custom/CustomElementCallbackDispatcher.h') if (idl_type == 'EventHandler' and interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/v8/V8ErrorHandler.h') contents = { 'access_control_list': access_control_list(attribute), 'activity_logging_world_list_for_getter': v8_utilities.activity_logging_world_list( attribute, 'Getter'), # [ActivityLogging] 'activity_logging_world_list_for_setter': v8_utilities.activity_logging_world_list( attribute, 'Setter'), # [ActivityLogging] 'cached_attribute_validation_method': extended_attributes.get('CachedAttribute'), 'conditional_string': v8_utilities.conditional_string(attribute), 'constructor_type': v8_types.constructor_type(idl_type) if is_constructor_attribute(attribute) else None, 'cpp_name': cpp_name(attribute), 'cpp_type': v8_types.cpp_type(idl_type), 'deprecate_as': v8_utilities.deprecate_as(attribute), # [DeprecateAs] 'enum_validation_expression': v8_utilities.enum_validation_expression(idl_type), 'has_custom_getter': has_custom_getter, 'has_custom_setter': has_custom_setter, 'has_strict_type_checking': ('StrictTypeChecking' in extended_attributes and v8_types.is_interface_type(idl_type)), 'idl_type': idl_type, 'is_call_with_execution_context': v8_utilities.has_extended_attribute_value(attribute, 'CallWith', 'ExecutionContext'), 'is_check_security_for_node': is_check_security_for_node, 'is_expose_js_accessors': 'ExposeJSAccessors' in extended_attributes, 'is_getter_raises_exception': ( # [RaisesException] 'RaisesException' in extended_attributes and extended_attributes['RaisesException'] in [None, 'Getter']), 'is_initialized_by_event_constructor': 'InitializedByEventConstructor' in extended_attributes, 'is_keep_alive_for_gc': is_keep_alive_for_gc(attribute), 'is_nullable': attribute.is_nullable, 'is_per_world_bindings': 'PerWorldBindings' in extended_attributes, 'is_read_only': attribute.is_read_only, 'is_reflect': is_reflect, 'is_replaceable': 'Replaceable' in attribute.extended_attributes, 'is_setter_raises_exception': ('RaisesException' in extended_attributes and extended_attributes['RaisesException'] in [None, 'Setter']), 'is_static': attribute.is_static, 'is_unforgeable': 'Unforgeable' in extended_attributes, 'measure_as': v8_utilities.measure_as(attribute), # [MeasureAs] 'name': attribute.name, 'per_context_enabled_function': v8_utilities.per_context_enabled_function_name( attribute), # [PerContextEnabled] 'property_attributes': property_attributes(attribute), 'setter_callback': setter_callback_name(interface, attribute), 'v8_type': v8_types.v8_type(idl_type), 'runtime_enabled_function': v8_utilities.runtime_enabled_function_name( attribute), # [RuntimeEnabled] 'world_suffixes': ['', 'ForMainWorld'] if 'PerWorldBindings' in extended_attributes else [''], # [PerWorldBindings] } if is_constructor_attribute(attribute): return contents if not has_custom_getter: generate_getter(interface, attribute, contents) if not (attribute.is_read_only or has_custom_setter): contents.update({ 'cpp_setter': setter_expression(interface, attribute, contents), 'v8_value_to_local_cpp_value': v8_types.v8_value_to_local_cpp_value(idl_type, extended_attributes, 'jsValue', 'cppValue'), }) return contents
def member_impl_context(member, interfaces_info, header_includes, header_forward_decls): idl_type = unwrap_nullable_if_needed(member.idl_type) cpp_name = to_snake_case(v8_utilities.cpp_name(member)) # In most cases, we don't have to distinguish `null` and `not present`, # and use null-states (e.g. nullptr, foo.IsUndefinedOrNull()) to show such # states for some types for memory usage and performance. # For types whose |has_explicit_presence| is True, we provide explicit # states of presence. has_explicit_presence = (member.idl_type.is_nullable and member.idl_type.inner_type.is_interface_type) nullable_indicator_name = None if not idl_type.cpp_type_has_null_value or has_explicit_presence: nullable_indicator_name = 'has_' + cpp_name + '_' def has_method_expression(): if nullable_indicator_name: return nullable_indicator_name if idl_type.is_union_type or idl_type.is_enum or idl_type.is_string_type: return '!%s_.IsNull()' % cpp_name if idl_type.name == 'Any': return '!({0}_.IsEmpty() || {0}_.IsUndefined())'.format(cpp_name) if idl_type.name == 'Object': return '!({0}_.IsEmpty() || {0}_.IsNull() || {0}_.IsUndefined())'.format( cpp_name) if idl_type.name == 'Dictionary': return '!%s_.IsUndefinedOrNull()' % cpp_name return '%s_' % cpp_name cpp_default_value = None if member.default_value: if not member.default_value.is_null or has_explicit_presence: cpp_default_value = idl_type.literal_cpp_value( member.default_value) forward_decl_name = idl_type.impl_forward_declaration_name if forward_decl_name: includes.update(idl_type.impl_includes_for_type(interfaces_info)) header_forward_decls.add(forward_decl_name) else: header_includes.update( idl_type.impl_includes_for_type(interfaces_info)) setter_value = 'value' if idl_type.is_array_buffer_view_or_typed_array: setter_value += '.View()' non_null_type = idl_type.inner_type if idl_type.is_nullable else idl_type setter_inline = 'inline ' if (non_null_type.is_basic_type or non_null_type.is_enum or non_null_type.is_wrapper_type) else '' return { 'cpp_default_value': cpp_default_value, 'cpp_name': cpp_name, 'has_explicit_presence': has_explicit_presence, 'getter_expression': cpp_name + '_', 'getter_name': getter_name_for_dictionary_member(member), 'has_method_expression': has_method_expression(), 'has_method_name': has_method_name_for_dictionary_member(member), 'is_nullable': idl_type.is_nullable, 'is_traceable': idl_type.is_traceable, 'member_cpp_type': idl_type.cpp_type_args(used_in_cpp_sequence=True), 'null_setter_name': null_setter_name_for_dictionary_member(member), 'nullable_indicator_name': nullable_indicator_name, 'rvalue_cpp_type': idl_type.cpp_type_args(used_as_rvalue_type=True), 'setter_inline': setter_inline, 'setter_name': setter_name_for_dictionary_member(member), 'setter_value': setter_value, }
def getter_name_for_dictionary_member(member): name = v8_utilities.cpp_name(member) return name
def generate_interface(interface): includes.clear() includes.update(INTERFACE_CPP_INCLUDES) header_includes = set(INTERFACE_H_INCLUDES) parent_interface = interface.parent if parent_interface: header_includes.update( v8_types.includes_for_interface(parent_interface)) extended_attributes = interface.extended_attributes is_audio_buffer = inherits_interface(interface.name, 'AudioBuffer') if is_audio_buffer: includes.add('modules/webaudio/AudioBuffer.h') is_document = inherits_interface(interface.name, 'Document') if is_document: includes.update([ 'bindings/v8/ScriptController.h', 'bindings/v8/V8WindowShell.h', 'core/frame/LocalFrame.h' ]) # [ActiveDOMObject] is_active_dom_object = 'ActiveDOMObject' in extended_attributes # [CheckSecurity] is_check_security = 'CheckSecurity' in extended_attributes if is_check_security: includes.add('bindings/v8/BindingSecurity.h') # [DependentLifetime] is_dependent_lifetime = 'DependentLifetime' in extended_attributes # [MeasureAs] is_measure_as = 'MeasureAs' in extended_attributes if is_measure_as: includes.add('core/frame/UseCounter.h') # [SetWrapperReferenceFrom] reachable_node_function = extended_attributes.get( 'SetWrapperReferenceFrom') if reachable_node_function: includes.update(['bindings/v8/V8GCController.h', 'core/dom/Element.h']) # [SetWrapperReferenceTo] set_wrapper_reference_to_list = [ { 'name': argument.name, # FIXME: properly should be: # 'cpp_type': argument.idl_type.cpp_type_args(used_as_argument=True), # (if type is non-wrapper type like NodeFilter, normally RefPtr) # Raw pointers faster though, and NodeFilter hacky anyway. 'cpp_type': argument.idl_type.implemented_as + '*', 'idl_type': argument.idl_type, 'v8_type': v8_types.v8_type(argument.idl_type.name), } for argument in extended_attributes.get('SetWrapperReferenceTo', []) ] for set_wrapper_reference_to in set_wrapper_reference_to_list: set_wrapper_reference_to['idl_type'].add_includes_for_type() # [SpecialWrapFor] if 'SpecialWrapFor' in extended_attributes: special_wrap_for = extended_attributes['SpecialWrapFor'].split('|') else: special_wrap_for = [] for special_wrap_interface in special_wrap_for: v8_types.add_includes_for_interface(special_wrap_interface) # [Custom=Wrap], [SetWrapperReferenceFrom] has_visit_dom_wrapper = (has_extended_attribute_value( interface, 'Custom', 'VisitDOMWrapper') or reachable_node_function or set_wrapper_reference_to_list) this_gc_type = gc_type(interface) template_contents = { 'conditional_string': conditional_string(interface), # [Conditional] 'cpp_class': cpp_name(interface), 'gc_type': this_gc_type, 'has_custom_legacy_call_as_function': has_extended_attribute_value( interface, 'Custom', 'LegacyCallAsFunction'), # [Custom=LegacyCallAsFunction] 'has_custom_to_v8': has_extended_attribute_value(interface, 'Custom', 'ToV8'), # [Custom=ToV8] 'has_custom_wrap': has_extended_attribute_value(interface, 'Custom', 'Wrap'), # [Custom=Wrap] 'has_visit_dom_wrapper': has_visit_dom_wrapper, 'header_includes': header_includes, 'interface_name': interface.name, 'is_active_dom_object': is_active_dom_object, 'is_audio_buffer': is_audio_buffer, 'is_check_security': is_check_security, 'is_dependent_lifetime': is_dependent_lifetime, 'is_document': is_document, 'is_event_target': inherits_interface(interface.name, 'EventTarget'), 'is_exception': interface.is_exception, 'is_node': inherits_interface(interface.name, 'Node'), 'measure_as': v8_utilities.measure_as(interface), # [MeasureAs] 'parent_interface': parent_interface, 'pass_cpp_type': cpp_template_type(cpp_ptr_type('PassRefPtr', 'RawPtr', this_gc_type), cpp_name(interface)), 'reachable_node_function': reachable_node_function, 'runtime_enabled_function': runtime_enabled_function_name(interface), # [RuntimeEnabled] 'set_wrapper_reference_to_list': set_wrapper_reference_to_list, 'special_wrap_for': special_wrap_for, 'v8_class': v8_utilities.v8_class_name(interface), 'wrapper_configuration': 'WrapperConfiguration::Dependent' if (has_visit_dom_wrapper or is_active_dom_object or is_dependent_lifetime) else 'WrapperConfiguration::Independent', } # Constructors constructors = [ generate_constructor(interface, constructor) for constructor in interface.constructors # FIXME: shouldn't put named constructors with constructors # (currently needed for Perl compatibility) # Handle named constructors separately if constructor.name == 'Constructor' ] if len(constructors) > 1: template_contents['constructor_overloads'] = generate_overloads( constructors) # [CustomConstructor] custom_constructors = [{ # Only needed for computing interface length 'number_of_required_arguments': number_of_required_arguments(constructor), } for constructor in interface.custom_constructors] # [EventConstructor] has_event_constructor = 'EventConstructor' in extended_attributes any_type_attributes = [ attribute for attribute in interface.attributes if attribute.idl_type.name == 'Any' ] if has_event_constructor: includes.add('bindings/v8/Dictionary.h') if any_type_attributes: includes.add('bindings/v8/SerializedScriptValue.h') # [NamedConstructor] named_constructor = generate_named_constructor(interface) if (constructors or custom_constructors or has_event_constructor or named_constructor): includes.add('bindings/v8/V8ObjectConstructor.h') includes.add('core/frame/DOMWindow.h') template_contents.update({ 'any_type_attributes': any_type_attributes, 'constructors': constructors, 'has_custom_constructor': bool(custom_constructors), 'has_event_constructor': has_event_constructor, 'interface_length': interface_length(interface, constructors + custom_constructors), 'is_constructor_call_with_document': has_extended_attribute_value( interface, 'ConstructorCallWith', 'Document'), # [ConstructorCallWith=Document] 'is_constructor_call_with_execution_context': has_extended_attribute_value( interface, 'ConstructorCallWith', 'ExecutionContext'), # [ConstructorCallWith=ExeuctionContext] 'is_constructor_raises_exception': extended_attributes.get('RaisesException') == 'Constructor', # [RaisesException=Constructor] 'named_constructor': named_constructor, }) # Constants template_contents.update({ 'constants': [generate_constant(constant) for constant in interface.constants], 'do_not_check_constants': 'DoNotCheckConstants' in extended_attributes, }) # Attributes attributes = [ v8_attributes.generate_attribute(interface, attribute) for attribute in interface.attributes ] template_contents.update({ 'attributes': attributes, 'has_accessors': any(attribute['is_expose_js_accessors'] for attribute in attributes), 'has_attribute_configuration': any(not (attribute['is_expose_js_accessors'] or attribute['is_static'] or attribute['runtime_enabled_function'] or attribute['per_context_enabled_function']) for attribute in attributes), 'has_constructor_attributes': any(attribute['constructor_type'] for attribute in attributes), 'has_per_context_enabled_attributes': any(attribute['per_context_enabled_function'] for attribute in attributes), 'has_replaceable_attributes': any(attribute['is_replaceable'] for attribute in attributes), }) # Methods methods = [ v8_methods.generate_method(interface, method) for method in interface.operations if method.name ] # Skip anonymous special operations (methods) generate_method_overloads(methods) for method in methods: method['do_generate_method_configuration'] = ( method['do_not_check_signature'] and not method['per_context_enabled_function'] and # For overloaded methods, only generate one accessor ('overload_index' not in method or method['overload_index'] == 1)) template_contents.update({ 'has_origin_safe_method_setter': any(method['is_check_security_for_frame'] and not method['is_read_only'] for method in methods), 'has_method_configuration': any(method['do_generate_method_configuration'] for method in methods), 'has_per_context_enabled_methods': any(method['per_context_enabled_function'] for method in methods), 'methods': methods, }) template_contents.update({ 'indexed_property_getter': indexed_property_getter(interface), 'indexed_property_setter': indexed_property_setter(interface), 'indexed_property_deleter': indexed_property_deleter(interface), 'is_override_builtins': 'OverrideBuiltins' in extended_attributes, 'named_property_getter': named_property_getter(interface), 'named_property_setter': named_property_setter(interface), 'named_property_deleter': named_property_deleter(interface), }) return template_contents
def getter_callback_name(interface, attribute): if attribute.idl_type.endswith('Constructor'): return '{0}V8Internal::{0}ConstructorGetter'.format( cpp_name(interface)) return '%sV8Internal::%sAttributeGetterCallback' % (cpp_name(interface), attribute.name)
def attribute_context(interface, attribute, interfaces, component_info): """Creates a Jinja template context for an attribute of an interface. Args: interface: An interface which |attribute| belongs to attribute: An attribute to create the context for interfaces: A dict which maps an interface name to the definition which can be referred if needed component_info: A dict containing component wide information Returns: A Jinja template context for |attribute| """ idl_type = attribute.idl_type base_idl_type = idl_type.base_type extended_attributes = attribute.extended_attributes idl_type.add_includes_for_type(extended_attributes) if idl_type.enum_values: includes.add('core/inspector/console_message.h') includes.add('platform/heap/heap.h') # [CheckSecurity] is_cross_origin = 'CrossOrigin' in extended_attributes is_check_security_for_receiver = (has_extended_attribute_value( interface, 'CheckSecurity', 'Receiver') and is_cross_origin) is_check_security_for_return_value = (has_extended_attribute_value( attribute, 'CheckSecurity', 'ReturnValue')) if is_check_security_for_receiver or is_check_security_for_return_value: includes.add('bindings/core/v8/binding_security.h') if is_check_security_for_return_value: includes.add('core/frame/web_feature.h') includes.add('platform/instrumentation/use_counter.h') # [CrossOrigin] if has_extended_attribute_value(attribute, 'CrossOrigin', 'Setter'): includes.add('platform/bindings/v8_cross_origin_callback_info.h') # [Constructor] # TODO(yukishiino): Constructors are much like methods although constructors # are not methods. Constructors must be data-type properties, and we can # support them as a kind of methods. constructor_type = idl_type.constructor_type_name if is_constructor_attribute( attribute) else None # [CEReactions] is_ce_reactions = 'CEReactions' in extended_attributes if is_ce_reactions: includes.add('core/html/custom/ce_reactions_scope.h') # [CustomElementCallbacks], [Reflect] is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attributes is_reflect = 'Reflect' in extended_attributes # [ReflectOnly] reflect_only = extended_attribute_value_as_list(attribute, 'ReflectOnly') if reflect_only: reflect_only = map( lambda v: cpp_content_attribute_value_name(interface, v), reflect_only) if is_custom_element_callbacks or is_reflect: includes.add('core/html/custom/v0_custom_element_processing_stack.h') # [PerWorldBindings] if 'PerWorldBindings' in extended_attributes: assert idl_type.is_wrapper_type or 'LogActivity' in \ extended_attributes, \ '[PerWorldBindings] should only be used with wrapper types: %s.%s' % \ (interface.name, attribute.name) # [SaveSameObject] is_save_same_object = ('SameObject' in attribute.extended_attributes and 'SaveSameObject' in attribute.extended_attributes) # [StringContext] if idl_type.has_string_context: includes.add('bindings/core/v8/generated_code_helper.h') # [CachedAccessor] is_cached_accessor = 'CachedAccessor' in extended_attributes # [LenientSetter] is_lenient_setter = 'LenientSetter' in extended_attributes # [CachedAttribute] cached_attribute_validation_method = extended_attributes.get( 'CachedAttribute') keep_alive_for_gc = is_keep_alive_for_gc(interface, attribute) does_generate_getter = (not has_custom_getter(attribute) and not constructor_type) does_generate_setter = ( has_setter(interface, attribute) and not (has_custom_setter(attribute) or is_lenient_setter)) use_private_property_in_getter = (does_generate_getter and (cached_attribute_validation_method or is_save_same_object or keep_alive_for_gc)) use_private_property_in_setter = (does_generate_setter and cached_attribute_validation_method) private_property_is_shared_between_getter_and_setter = ( use_private_property_in_getter and use_private_property_in_setter) does_use_private_property = (use_private_property_in_getter or use_private_property_in_setter or is_cached_accessor) if does_use_private_property: includes.add('platform/bindings/v8_private_property.h') # [LogActivity] if 'LogActivity' in extended_attributes: includes.add('platform/bindings/v8_per_context_data.h') # [DeprecateAs], [MeasureAs] deprecate_as = v8_utilities.deprecate_as(attribute) measure_as = v8_utilities.measure_as(attribute, interface) # [HighEntropy] high_entropy = v8_utilities.high_entropy(attribute) is_lazy_data_attribute = \ (constructor_type and not (measure_as or deprecate_as)) or \ (str(idl_type) == 'Window' and attribute.name in ('frames', 'self', 'window')) runtime_features = component_info['runtime_enabled_features'] internal_name = cpp_encoded_property_name(attribute) cpp_type = idl_type.cpp_type if idl_type.is_explicit_nullable: cpp_type = v8_types.cpp_template_type('base::Optional', cpp_type) context = { # [ActivityLogging] 'activity_logging_world_list_for_getter': v8_utilities.activity_logging_world_list(attribute, 'Getter'), # [ActivityLogging] 'activity_logging_world_list_for_setter': v8_utilities.activity_logging_world_list(attribute, 'Setter'), # [ActivityLogging] 'activity_logging_world_check': v8_utilities.activity_logging_world_check(attribute), 'cached_accessor_name': 'k%s%s' % (interface.name, attribute.name.capitalize()), 'cached_attribute_validation_method': cached_attribute_validation_method, 'camel_case_name': NameStyleConverter(internal_name).to_upper_camel_case(), 'constructor_type': constructor_type, 'context_enabled_feature_name': v8_utilities.context_enabled_feature_name(attribute), 'cpp_name': cpp_name(attribute), 'cpp_type': cpp_type, 'cpp_type_initializer': idl_type.cpp_type_initializer, 'deprecate_as': deprecate_as, 'does_generate_getter': does_generate_getter, 'does_generate_setter': does_generate_setter, 'enum_type': idl_type.enum_type, 'enum_values': idl_type.enum_values, # [Exposed] 'exposed_test': v8_utilities.exposed(attribute, interface), 'getter_has_no_side_effect': has_extended_attribute_value(attribute, 'Affects', 'Nothing'), 'has_cross_origin_getter': has_extended_attribute_value(attribute, 'CrossOrigin', None) or has_extended_attribute_value(attribute, 'CrossOrigin', 'Getter'), 'has_cross_origin_setter': has_extended_attribute_value(attribute, 'CrossOrigin', 'Setter'), 'has_custom_getter': has_custom_getter(attribute), 'has_custom_setter': has_custom_setter(attribute), 'has_promise_type': idl_type.name == 'Promise', 'has_setter': has_setter(interface, attribute), 'high_entropy': high_entropy, 'idl_type': str(idl_type), 'is_cached_accessor': is_cached_accessor, 'is_call_with_execution_context': has_extended_attribute_value(attribute, 'CallWith', 'ExecutionContext'), 'is_call_with_script_state': has_extended_attribute_value(attribute, 'CallWith', 'ScriptState'), 'is_ce_reactions': is_ce_reactions, 'is_check_security_for_receiver': is_check_security_for_receiver, 'is_check_security_for_return_value': is_check_security_for_return_value, 'is_custom_element_callbacks': is_custom_element_callbacks, # TODO(yukishiino): Make all DOM attributes accessor-type properties. 'is_data_type_property': is_data_type_property(interface, attribute), 'is_getter_raises_exception': # [RaisesException] 'RaisesException' in extended_attributes and extended_attributes['RaisesException'] in (None, 'Getter'), 'is_keep_alive_for_gc': keep_alive_for_gc, 'is_lazy_data_attribute': is_lazy_data_attribute, 'is_lenient_setter': is_lenient_setter, 'is_lenient_this': 'LegacyLenientThis' in extended_attributes, 'is_nullable': idl_type.is_nullable, 'is_explicit_nullable': idl_type.is_explicit_nullable, 'is_named_constructor': is_named_constructor_attribute(attribute), 'is_partial_interface_member': 'PartialInterfaceImplementedAs' in extended_attributes, 'is_per_world_bindings': 'PerWorldBindings' in extended_attributes, 'is_put_forwards': 'PutForwards' in extended_attributes, 'is_read_only': attribute.is_read_only, 'is_reflect': is_reflect, 'is_replaceable': 'Replaceable' in attribute.extended_attributes, 'is_save_same_object': is_save_same_object, 'is_static': attribute.is_static, 'is_url': 'URL' in extended_attributes, 'is_unforgeable': is_unforgeable(attribute), 'measure_as': measure_as, 'name': attribute.name, 'on_instance': v8_utilities.on_instance(interface, attribute), 'on_interface': v8_utilities.on_interface(interface, attribute), 'on_prototype': v8_utilities.on_prototype(interface, attribute), # [RuntimeEnabled] for origin trial 'origin_trial_feature_name': v8_utilities.origin_trial_feature_name(attribute, runtime_features), 'private_property_is_shared_between_getter_and_setter': private_property_is_shared_between_getter_and_setter, 'property_attributes': property_attributes(interface, attribute), 'reflect_empty': cpp_content_attribute_value_name( interface, extended_attributes.get('ReflectEmpty')), 'reflect_invalid': cpp_content_attribute_value_name( interface, extended_attributes.get('ReflectInvalid', '')), 'reflect_missing': cpp_content_attribute_value_name( interface, extended_attributes.get('ReflectMissing')), 'reflect_only': reflect_only, # [RuntimeEnabled] if not in origin trial 'runtime_enabled_feature_name': v8_utilities.runtime_enabled_feature_name(attribute, runtime_features), # [SecureContext] 'secure_context_test': v8_utilities.secure_context(attribute, interface), 'use_output_parameter_for_result': idl_type.use_output_parameter_for_result, 'world_suffixes': ( ['', 'ForMainWorld'] if 'PerWorldBindings' in extended_attributes else ['']), # [PerWorldBindings] } if not has_custom_getter(attribute): getter_context(interface, attribute, context) if not has_custom_setter(attribute) and has_setter(interface, attribute): setter_context(interface, attribute, interfaces, context) # [RuntimeCallStatsCounter] runtime_call_stats_context(interface, attribute, context) # [CrossOrigin] is incompatible with a number of other attributes, so check # for them here. if is_cross_origin: if context['has_cross_origin_setter'] and context['has_custom_setter']: raise Exception( '[CrossOrigin] and [Custom] are incompatible on the same setter: %s.%s', interface.name, attribute.name) if context['is_per_world_bindings']: raise Exception( '[CrossOrigin] and [PerWorldBindings] are incompatible: %s.%s', interface.name, attribute.name) if context['constructor_type']: raise Exception( '[CrossOrigin] cannot be used for constructors: %s.%s', interface.name, attribute.name) return context
def member_context(dictionary, member): extended_attributes = member.extended_attributes idl_type = member.idl_type idl_type.add_includes_for_type(extended_attributes) unwrapped_idl_type = unwrap_nullable_if_needed(idl_type) if member.is_required and member.default_value: raise Exception('Required member %s must not have a default value.' % member.name) def default_values(): if not member.default_value: return None, None if member.default_value.is_null: return None, 'v8::Null(isolate)' cpp_default_value = unwrapped_idl_type.literal_cpp_value( member.default_value) v8_default_value = unwrapped_idl_type.cpp_value_to_v8_value( cpp_value=cpp_default_value, isolate='isolate', creation_context='creationContext') return cpp_default_value, v8_default_value cpp_default_value, v8_default_value = default_values() cpp_name = to_snake_case(v8_utilities.cpp_name(member)) getter_name = getter_name_for_dictionary_member(member) is_deprecated_dictionary = unwrapped_idl_type.name == 'Dictionary' return { 'cpp_default_value': cpp_default_value, 'cpp_name': cpp_name, 'cpp_type': unwrapped_idl_type.cpp_type, 'cpp_value_to_v8_value': unwrapped_idl_type.cpp_value_to_v8_value( cpp_value='impl.%s()' % getter_name, isolate='isolate', creation_context='creationContext', extended_attributes=extended_attributes), 'deprecate_as': v8_utilities.deprecate_as(member), 'enum_type': idl_type.enum_type, 'enum_values': unwrapped_idl_type.enum_values, 'getter_name': getter_name, 'has_method_name': has_method_name_for_dictionary_member(member), 'idl_type': idl_type.base_type, 'is_interface_type': idl_type.is_interface_type and not is_deprecated_dictionary, 'is_nullable': idl_type.is_nullable, 'is_object': unwrapped_idl_type.name == 'Object' or is_deprecated_dictionary, 'is_required': member.is_required, 'name': member.name, 'origin_trial_feature_name': v8_utilities.origin_trial_feature_name(member), # [OriginTrialEnabled] 'runtime_enabled_feature_name': v8_utilities.runtime_enabled_feature_name(member), # [RuntimeEnabled] 'setter_name': setter_name_for_dictionary_member(member), 'null_setter_name': null_setter_name_for_dictionary_member(member), 'v8_default_value': v8_default_value, 'v8_value_to_local_cpp_value': unwrapped_idl_type.v8_value_to_local_cpp_value( extended_attributes, member.name + 'Value', member.name + 'CppValue', isolate='isolate', use_exception_state=True), }
def has_method_name_for_dictionary_member(member): name = NameStyleConverter('has_' + v8_utilities.cpp_name(member)) return name.to_lower_camel_case()
def getter_callback_name_for_main_world(interface, attribute): if 'PerWorldBindings' not in attribute.extended_attributes: return '0' return '%sV8Internal::%sAttributeGetterCallbackForMainWorld' % ( cpp_name(interface), attribute.name)