def callback_function_context(callback_function): includes.clear() includes.update(CALLBACK_FUNCTION_CPP_INCLUDES) idl_type = callback_function.idl_type idl_type_str = str(idl_type) for argument in callback_function.arguments: argument.idl_type.add_includes_for_type( callback_function.extended_attributes) context = { # While both |callback_function_name| and |cpp_class| are identical at # the moment, the two are being defined because their values may change # in the future (e.g. if we support [ImplementedAs=] in callback # functions). 'callback_function_name': callback_function.name, 'cpp_class': 'V8%s' % callback_function.name, 'cpp_includes': sorted(includes), 'forward_declarations': sorted(forward_declarations(callback_function)), 'header_includes': sorted(CALLBACK_FUNCTION_H_INCLUDES), 'idl_type': idl_type_str, 'is_treat_non_object_as_null': 'TreatNonObjectAsNull' in callback_function.extended_attributes, 'native_value_traits_tag': v8_types.idl_type_to_native_value_traits_tag(idl_type), 'return_cpp_type': idl_type.cpp_type, } context.update(arguments_context(callback_function.arguments)) return context
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(dictionary, 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 callback_function_context(callback_function): includes.clear() includes.update(CALLBACK_FUNCTION_CPP_INCLUDES) idl_type = callback_function.idl_type idl_type_str = str(idl_type) for argument in callback_function.arguments: argument.idl_type.add_includes_for_type( callback_function.extended_attributes) context = { # While both |callback_function_name| and |cpp_class| are identical at # the moment, the two are being defined because their values may change # in the future (e.g. if we support [ImplementedAs=] in callback # functions). 'callback_function_name': callback_function.name, 'cpp_class': 'V8%s' % callback_function.name, 'cpp_includes': sorted(includes), 'forward_declarations': sorted(forward_declarations(callback_function)), 'header_includes': sorted(CALLBACK_FUNCTION_H_INCLUDES), 'idl_type': idl_type_str, 'return_cpp_type': idl_type.cpp_type, 'this_include_header_name': to_snake_case('V8%s' % callback_function.name), } if idl_type_str != 'void': context.update({ 'return_value_conversion': idl_type.v8_value_to_local_cpp_value( callback_function.extended_attributes, 'call_result', 'native_result', isolate='GetIsolate()', bailout_return_value='v8::Nothing<%s>()' % context['return_cpp_type']), }) context.update(arguments_context(callback_function.arguments)) return context
def callback_interface_context(callback_interface, _): includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) # https://heycam.github.io/webidl/#dfn-single-operation-callback-interface is_single_operation = True if (callback_interface.parent or len(callback_interface.attributes) > 0 or len(callback_interface.operations) == 0): is_single_operation = False else: operations = callback_interface.operations basis = operations[0] for op in operations[1:]: if op.name != basis.name: is_single_operation = False break return { 'cpp_class': callback_interface.name, 'forward_declarations': sorted(forward_declarations(callback_interface)), 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), 'is_single_operation_callback_interface': is_single_operation, 'methods': [method_context(operation) for operation in callback_interface.operations], 'v8_class': v8_utilities.v8_class_name(callback_interface), }
def callback_function_context(callback_function): includes.clear() includes.update(CALLBACK_FUNCTION_CPP_INCLUDES) idl_type = callback_function.idl_type idl_type_str = str(idl_type) for argument in callback_function.arguments: argument.idl_type.add_includes_for_type( callback_function.extended_attributes) context = { # While both |callback_function_name| and |cpp_class| are identical at # the moment, the two are being defined because their values may change # in the future (e.g. if we support [ImplementedAs=] in callback # functions). 'callback_function_name': callback_function.name, 'cpp_class': callback_function.name, 'cpp_includes': sorted(includes), 'forward_declarations': sorted(forward_declarations(callback_function)), 'header_includes': sorted(CALLBACK_FUNCTION_H_INCLUDES), 'idl_type': idl_type_str, } if idl_type_str != 'void': context.update({ 'return_cpp_type': idl_type.cpp_type + '&', 'return_value': idl_type.v8_value_to_local_cpp_value( callback_function.extended_attributes, 'v8ReturnValue', 'cppValue', isolate='script_state_->GetIsolate()', bailout_return_value='false'), }) context.update(arguments_context(callback_function.arguments, context.get('return_cpp_type'))) return context
def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index): this_array_or_sequence_type = array_or_sequence_type(idl_type) if this_array_or_sequence_type: return v8_value_to_cpp_value_array_or_sequence( this_array_or_sequence_type, v8_value, index) idl_type = preprocess_idl_type(idl_type) if 'EnforceRange' in extended_attributes: arguments = ', '.join([v8_value, 'EnforceRange', 'ok']) else: # NormalConversion arguments = v8_value if idl_type in V8_VALUE_TO_CPP_VALUE_BASIC: cpp_expression_format = V8_VALUE_TO_CPP_VALUE_BASIC[idl_type] elif idl_type in V8_VALUE_TO_CPP_VALUE_AND_INCLUDES: cpp_expression_format, new_includes = V8_VALUE_TO_CPP_VALUE_AND_INCLUDES[ idl_type] includes.update(new_includes) elif is_typed_array_type(idl_type): cpp_expression_format = ( '{v8_value}->Is{idl_type}() ? ' 'V8{idl_type}::toNative(v8::Handle<v8::{idl_type}>::Cast({v8_value})) : 0' ) add_includes_for_type(idl_type) else: cpp_expression_format = ( 'V8{idl_type}::HasInstance({v8_value}, info.GetIsolate(), worldType(info.GetIsolate())) ? ' 'V8{idl_type}::toNative(v8::Handle<v8::Object>::Cast({v8_value})) : 0' ) add_includes_for_type(idl_type) return cpp_expression_format.format(arguments=arguments, idl_type=idl_type, v8_value=v8_value)
def callback_function_context(callback_function): includes.clear() includes.update(CALLBACK_FUNCTION_CPP_INCLUDES) idl_type = callback_function.idl_type idl_type_str = str(idl_type) forward_declarations = [] for argument in callback_function.arguments: if argument.idl_type.is_interface_type: forward_declarations.append(argument.idl_type) argument.idl_type.add_includes_for_type(callback_function.extended_attributes) context = { 'cpp_class': callback_function.name, 'cpp_includes': sorted(includes), 'forward_declarations': sorted(forward_declarations), 'header_includes': sorted(CALLBACK_FUNCTION_H_INCLUDES), 'idl_type': idl_type_str, } if idl_type_str != 'void': context.update({ 'return_cpp_type': idl_type.cpp_type + '&', 'return_value': idl_type.v8_value_to_local_cpp_value( callback_function.extended_attributes, 'v8ReturnValue', 'cppValue', isolate='m_scriptState->isolate()', bailout_return_value='false'), }) context.update(arguments_context(callback_function.arguments, context.get('return_cpp_type'))) return context
def callback_function_context(callback_function): includes.clear() includes.update(CALLBACK_FUNCTION_CPP_INCLUDES) idl_type = callback_function.idl_type idl_type_str = str(idl_type) forward_declarations = [] for argument in callback_function.arguments: if argument.idl_type.is_interface_type: forward_declarations.append(argument.idl_type) argument.idl_type.add_includes_for_type(callback_function.extended_attributes) context = { "cpp_class": callback_function.name, "cpp_includes": sorted(includes), "forward_declarations": sorted(forward_declarations), "header_includes": sorted(CALLBACK_FUNCTION_H_INCLUDES), "idl_type": idl_type_str, } if idl_type_str != "void": context.update( { "return_cpp_type": idl_type.cpp_type + "&", "return_value": idl_type.v8_value_to_local_cpp_value( callback_function.extended_attributes, "v8ReturnValue", "cppValue", isolate="scriptState->isolate()", bailout_return_value="false", ), } ) context.update(arguments_context(callback_function.arguments, context.get("return_cpp_type"))) return context
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 generate_interface_code(self, definitions, interface_name, interface): interface_info = self.info_provider.interfaces_info[interface_name] full_path = interface_info.get('full_path') component = idl_filename_to_component(full_path) include_paths = interface_info.get('dependencies_include_paths') # Select appropriate Jinja template and contents function if interface.is_callback: header_template_filename = 'callback_interface.h.tmpl' cpp_template_filename = 'callback_interface.cc.tmpl' interface_context = v8_callback_interface.callback_interface_context elif interface.is_partial: interface_context = v8_interface.interface_context header_template_filename = 'partial_interface.h.tmpl' cpp_template_filename = 'partial_interface.cc.tmpl' interface_name += 'Partial' assert component == 'core' component = 'modules' include_paths = interface_info.get( 'dependencies_other_component_include_paths') else: header_template_filename = 'interface.h.tmpl' cpp_template_filename = 'interface.cc.tmpl' interface_context = v8_interface.interface_context template_context = interface_context(interface, definitions.interfaces) includes.update( interface_info.get('cpp_includes', {}).get(component, set())) if not interface.is_partial and not is_testing_target(full_path): template_context['header_includes'].add( self.info_provider.include_path_for_export) template_context[ 'exported'] = self.info_provider.specifier_for_export # Add the include for interface itself if IdlType(interface_name).is_typed_array: template_context['header_includes'].add( 'core/typed_arrays/dom_typed_array.h') elif interface.is_callback: pass else: template_context['header_includes'].add( interface_info['include_path']) template_context['header_includes'].update( interface_info.get('additional_header_includes', [])) header_path, cpp_path = self.output_paths(interface_name) this_include_header_path = self.normalize_this_header_path(header_path) template_context['this_include_header_path'] = this_include_header_path template_context['header_guard'] = to_header_guard( this_include_header_path) header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) header_text, cpp_text = self.render_templates(include_paths, header_template, cpp_template, template_context, component) return ( (header_path, header_text), (cpp_path, cpp_text), )
def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index): this_array_or_sequence_type = array_or_sequence_type(idl_type) if this_array_or_sequence_type: return v8_value_to_cpp_value_array_or_sequence(this_array_or_sequence_type, v8_value, index) idl_type = preprocess_idl_type(idl_type) if 'EnforceRange' in extended_attributes: arguments = ', '.join([v8_value, 'EnforceRange', 'ok']) else: # NormalConversion arguments = v8_value if idl_type in V8_VALUE_TO_CPP_VALUE_BASIC: cpp_expression_format = V8_VALUE_TO_CPP_VALUE_BASIC[idl_type] elif idl_type in V8_VALUE_TO_CPP_VALUE_AND_INCLUDES: cpp_expression_format, new_includes = V8_VALUE_TO_CPP_VALUE_AND_INCLUDES[idl_type] includes.update(new_includes) elif is_typed_array_type(idl_type): cpp_expression_format = ( '{v8_value}->Is{idl_type}() ? ' 'V8{idl_type}::toNative(v8::Handle<v8::{idl_type}>::Cast({v8_value})) : 0') add_includes_for_type(idl_type) else: cpp_expression_format = ( 'V8{idl_type}::HasInstance({v8_value}, info.GetIsolate(), worldType(info.GetIsolate())) ? ' 'V8{idl_type}::toNative(v8::Handle<v8::Object>::Cast({v8_value})) : 0') add_includes_for_type(idl_type) return cpp_expression_format.format(arguments=arguments, idl_type=idl_type, v8_value=v8_value)
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)) nullable_indicator_name = None if not idl_type.cpp_type_has_null_value: 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 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()' 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, '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 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 callback_interface_context(callback_interface): includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) return { "cpp_class": callback_interface.name, "v8_class": v8_utilities.v8_class_name(callback_interface), "header_includes": set(CALLBACK_INTERFACE_H_INCLUDES), "methods": [method_context(operation) for operation in callback_interface.operations], }
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 generate_interface_code(self, definitions, interface_name, interface): interface_info = self.info_provider.interfaces_info[interface_name] full_path = interface_info.get('full_path') component = idl_filename_to_component(full_path) include_paths = interface_info.get('dependencies_include_paths') # Select appropriate Jinja template and contents function # # A callback interface with constants needs a special handling. # https://heycam.github.io/webidl/#legacy-callback-interface-object if interface.is_callback and len(interface.constants) > 0: header_template_filename = 'legacy_callback_interface.h.tmpl' cpp_template_filename = 'legacy_callback_interface.cpp.tmpl' interface_context = v8_callback_interface.legacy_callback_interface_context elif interface.is_callback: header_template_filename = 'callback_interface.h.tmpl' cpp_template_filename = 'callback_interface.cpp.tmpl' interface_context = v8_callback_interface.callback_interface_context elif interface.is_partial: interface_context = v8_interface.interface_context header_template_filename = 'partial_interface.h.tmpl' cpp_template_filename = 'partial_interface.cpp.tmpl' interface_name += 'Partial' assert component == 'core' component = 'modules' include_paths = interface_info.get('dependencies_other_component_include_paths') else: header_template_filename = 'interface.h.tmpl' cpp_template_filename = 'interface.cpp.tmpl' interface_context = v8_interface.interface_context template_context = interface_context(interface, definitions.interfaces) includes.update(interface_info.get('cpp_includes', {}).get(component, set())) if not interface.is_partial and not is_testing_target(full_path): template_context['header_includes'].add(self.info_provider.include_path_for_export) template_context['exported'] = self.info_provider.specifier_for_export # Add the include for interface itself if IdlType(interface_name).is_typed_array: template_context['header_includes'].add('core/typed_arrays/dom_typed_array.h') elif interface.is_callback: if len(interface.constants) > 0: # legacy callback interface includes.add(interface_info['include_path']) else: template_context['header_includes'].add(interface_info['include_path']) template_context['header_includes'].update( interface_info.get('additional_header_includes', [])) header_path, cpp_path = self.output_paths(interface_name) template_context['this_include_header_name'] = posixpath.basename(header_path) header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) header_text, cpp_text = self.render_template( include_paths, header_template, cpp_template, template_context, component) return ( (header_path, header_text), (cpp_path, cpp_text), )
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/runtime_enabled_features.h') break has_origin_trial_members = False for member in members: if member['origin_trial_feature_name']: has_origin_trial_members = True includes.add('core/origin_trials/origin_trials.h') break cpp_class = v8_utilities.cpp_name(dictionary) context = { 'cpp_class': cpp_class, 'has_origin_trial_members': has_origin_trial_members, '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 callback_interface_context(callback_interface): includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) return { 'cpp_class': callback_interface.name, 'v8_class': v8_utilities.v8_class_name(callback_interface), 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), 'methods': [method_context(operation) for operation in callback_interface.operations], }
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_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 private_script_interface_context(private_script_interface): includes.clear() includes.update(BLINK_IN_JS_INTERFACE_CPP_INCLUDES) return { 'cpp_class': private_script_interface.name, 'forward_declarations': forward_declarations(private_script_interface), 'header_includes': set(BLINK_IN_JS_INTERFACE_H_INCLUDES), 'methods': [method_context(operation) for operation in private_script_interface.operations], 'v8_class': v8_utilities.v8_class_name(private_script_interface), }
def generate_callback_interface(callback_interface): includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) return { 'conditional_string': v8_utilities.conditional_string(callback_interface), 'cpp_class': callback_interface.name, 'v8_class': v8_utilities.v8_class_name(callback_interface), 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), 'methods': [generate_method(operation) for operation in callback_interface.operations], }
def legacy_callback_interface_context(callback_interface, _): includes.clear() includes.update(LEGACY_CALLBACK_INTERFACE_CPP_INCLUDES) return { # TODO(bashi): Fix crbug.com/630986, and add 'methods'. 'constants': [constant_context(constant, callback_interface) for constant in callback_interface.constants], 'cpp_class': callback_interface.name, 'header_includes': set(LEGACY_CALLBACK_INTERFACE_H_INCLUDES), 'interface_name': callback_interface.name, 'v8_class': v8_utilities.v8_class_name(callback_interface), }
def legacy_callback_interface_context(callback_interface, _): includes.clear() includes.update(LEGACY_CALLBACK_INTERFACE_CPP_INCLUDES) return { # TODO(bashi): Fix crbug.com/630986, and add 'methods'. 'constants': [constant_context(constant, callback_interface) for constant in callback_interface.constants], 'cpp_class': callback_interface.name, 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), 'interface_name': callback_interface.name, 'v8_class': v8_utilities.v8_class_name(callback_interface), }
def generate_interface_code(self, definitions, interface_name, interface): interface_info = self.info_provider.interfaces_info[interface_name] full_path = interface_info.get('full_path') component = idl_filename_to_component(full_path) include_paths = interface_info.get('dependencies_include_paths') # Select appropriate Jinja template and contents function # # A callback interface with constants needs a special handling. # https://heycam.github.io/webidl/#legacy-callback-interface-object if interface.is_callback and len(interface.constants) > 0: header_template_filename = 'legacy_callback_interface.h.tmpl' cpp_template_filename = 'legacy_callback_interface.cpp.tmpl' interface_context = v8_callback_interface.legacy_callback_interface_context elif interface.is_callback: header_template_filename = 'callback_interface.h.tmpl' cpp_template_filename = 'callback_interface.cpp.tmpl' interface_context = v8_callback_interface.callback_interface_context elif interface.is_partial: interface_context = v8_interface.interface_context header_template_filename = 'partial_interface.h.tmpl' cpp_template_filename = 'partial_interface.cpp.tmpl' interface_name += 'Partial' assert component == 'core' component = 'modules' include_paths = interface_info.get('dependencies_other_component_include_paths') else: header_template_filename = 'interface.h.tmpl' cpp_template_filename = 'interface.cpp.tmpl' interface_context = v8_interface.interface_context template_context = interface_context(interface, definitions.interfaces) includes.update(interface_info.get('cpp_includes', {}).get(component, set())) if not interface.is_partial and not is_testing_target(full_path): template_context['header_includes'].add(self.info_provider.include_path_for_export) template_context['exported'] = self.info_provider.specifier_for_export # Add the include for interface itself if IdlType(interface_name).is_typed_array: template_context['header_includes'].add('core/typed_arrays/DOMTypedArray.h') else: template_context['header_includes'].add(interface_info['include_path']) template_context['header_includes'].update( interface_info.get('additional_header_includes', [])) header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) header_text, cpp_text = self.render_template( include_paths, header_template, cpp_template, template_context, component) header_path, cpp_path = self.output_paths(interface_name) return ( (header_path, header_text), (cpp_path, cpp_text), )
def generate_callback_interface(callback_interface): includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) methods = [generate_method(operation) for operation in callback_interface.operations] template_contents = { 'cpp_class_name': callback_interface.name, 'v8_class_name': v8_class_name(callback_interface), 'header_includes': CALLBACK_INTERFACE_H_INCLUDES, 'methods': methods, } return template_contents
def generate_getter(interface, attribute, contents): idl_type = attribute.idl_type v8_types.add_includes_for_type(idl_type) extended_attributes = attribute.extended_attributes cpp_value = getter_expression(interface, attribute, contents) # Normally we can inline the function call into the return statement to # avoid the overhead of using a Ref<> temporary, but for some cases # (nullable types, EventHandler, [CachedAttribute], or if there are # exceptions), we need to use a local variable. # FIXME: check if compilers are smart enough to inline this, and if so, # always use a local variable (for readability and CG simplicity). if (attribute.is_nullable or idl_type == 'EventHandler' or 'CachedAttribute' in extended_attributes or contents['is_getter_raises_exception']): contents['cpp_value_original'] = cpp_value cpp_value = 'jsValue' contents['cpp_value'] = cpp_value if contents['is_keep_alive_for_gc']: v8_set_return_value_statement = 'v8SetReturnValue(info, wrapper)' includes.add('bindings/v8/V8HiddenPropertyName.h') else: v8_set_return_value_statement = v8_types.v8_set_return_value( idl_type, cpp_value, extended_attributes=extended_attributes, script_wrappable='imp') contents['v8_set_return_value'] = v8_set_return_value_statement if (idl_type == 'EventHandler' and interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/v8/V8ErrorHandler.h') # [CheckSecurityForNode] is_check_security_for_node = 'CheckSecurityForNode' in extended_attributes if is_check_security_for_node: includes.add('bindings/v8/BindingSecurity.h') if is_check_security_for_node or contents['is_getter_raises_exception']: includes.update( set([ 'bindings/v8/ExceptionMessages.h', 'bindings/v8/ExceptionState.h' ])) v8_utilities.generate_deprecate_as(attribute, contents) # [DeprecateAs] v8_utilities.generate_measure_as(attribute, contents) # [MeasureAs] contents.update({ 'is_check_security_for_node': is_check_security_for_node, 'is_unforgeable': 'Unforgeable' in extended_attributes, })
def render_template(interface_info, header_template, cpp_template, template_context): template_context['code_generator'] = module_pyname # Add includes for any dependencies template_context['header_includes'] = sorted( template_context['header_includes']) includes.update(interface_info.get('dependencies_include_paths', [])) template_context['cpp_includes'] = sorted(includes) header_text = header_template.render(template_context) cpp_text = cpp_template.render(template_context) return header_text, cpp_text
def callback_interface_context(callback_interface, _): is_legacy_callback_interface = len(callback_interface.constants) > 0 includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) if is_legacy_callback_interface: includes.update(LEGACY_CALLBACK_INTERFACE_CPP_INCLUDES) header_includes = set(CALLBACK_INTERFACE_H_INCLUDES) if is_legacy_callback_interface: header_includes.update(LEGACY_CALLBACK_INTERFACE_H_INCLUDES) # https://heycam.github.io/webidl/#dfn-single-operation-callback-interface is_single_operation = True if (callback_interface.parent or len(callback_interface.attributes) > 0 or len(callback_interface.operations) == 0): is_single_operation = False else: operations = callback_interface.operations basis = operations[0] for op in operations[1:]: if op.name != basis.name: is_single_operation = False break return { 'constants': [ constant_context(constant, callback_interface) for constant in callback_interface.constants ], 'cpp_class': callback_interface.name, 'do_not_check_constants': 'DoNotCheckConstants' in callback_interface.extended_attributes, 'forward_declarations': sorted(forward_declarations(callback_interface)), 'header_includes': header_includes, 'interface_name': callback_interface.name, 'is_legacy_callback_interface': is_legacy_callback_interface, 'is_single_operation_callback_interface': is_single_operation, 'methods': [ method_context(operation) for operation in callback_interface.operations ], 'v8_class': v8_utilities.v8_class_name(callback_interface), }
def generate_callback_interface(callback_interface): includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) name = callback_interface.name methods = [generate_method(operation) for operation in callback_interface.operations] template_contents = { 'cpp_class': name, 'dart_class': dart_types.dart_type(callback_interface.name), 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), 'methods': methods, } return template_contents
def generate_attribute(interface, attribute): idl_type = attribute.idl_type extended_attributes = attribute.extended_attributes 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']) 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), 'cpp_type': v8_types.cpp_type(idl_type), 'getter_callback_name': getter_callback_name(interface, attribute), 'getter_callback_name_for_main_world': getter_callback_name_for_main_world(interface, attribute), 'has_custom_getter': has_custom_getter, 'has_custom_setter': has_custom_setter, 'idl_type': idl_type, 'is_call_with_execution_context': v8_utilities.has_extended_attribute_value(attribute, 'CallWith', 'ExecutionContext'), 'is_constructor': is_constructor_attribute(attribute), 'is_getter_raises_exception': has_extended_attribute(attribute, ('GetterRaisesException', 'RaisesException')), 'is_keep_alive_for_gc': is_keep_alive_for_gc(attribute), 'is_nullable': attribute.is_nullable, 'is_read_only': attribute.is_read_only, 'is_replaceable': 'Replaceable' in attribute.extended_attributes, 'is_setter_raises_exception': has_extended_attribute(attribute, ('RaisesException', 'SetterRaisesException')), 'is_static': attribute.is_static, 'name': attribute.name, 'per_context_enabled_function_name': v8_utilities.per_context_enabled_function_name(attribute), # [PerContextEnabled] 'property_attributes': property_attributes(attribute), 'setter_callback_name': setter_callback_name(interface, attribute), 'setter_callback_name_for_main_world': setter_callback_name_for_main_world(interface, attribute), 'v8_type': v8_types.v8_type(idl_type), 'runtime_enabled_function_name': v8_utilities.runtime_enabled_function_name(attribute), # [RuntimeEnabled] 'world_suffixes': ['', 'ForMainWorld'] if 'PerWorldBindings' in extended_attributes else [''], # [PerWorldBindings] 'wrapper_type_info': wrapper_type_info(attribute), } if is_constructor_attribute(attribute): includes.update(v8_types.includes_for_type(idl_type)) return contents if not has_custom_getter: generate_getter(interface, attribute, contents) if not attribute.is_read_only and not has_custom_setter: generate_setter(interface, attribute, contents) return contents
def generate_code(self, definitions, interface_name): """Returns .h/.cpp code as (header_text, cpp_text).""" try: interface = definitions.interfaces[interface_name] except KeyError: raise Exception('%s not in IDL definitions' % interface_name) # Store other interfaces for introspection interfaces.update(definitions.interfaces) # Set local type info IdlType.set_callback_functions(definitions.callback_functions.keys()) IdlType.set_enums((enum.name, enum.values) for enum in definitions.enumerations.values()) # Select appropriate Jinja template and contents function if interface.is_callback: header_template_filename = 'callback_interface.h' cpp_template_filename = 'callback_interface.cpp' interface_context = v8_callback_interface.callback_interface_context elif 'PrivateScriptInterface' in interface.extended_attributes: # Currently private scripts don't have dependencies. Once private scripts have dependencies, # we should add them to interface_info. header_template_filename = 'private_script_interface.h' cpp_template_filename = 'private_script_interface.cpp' interface_context = v8_private_script_interface.private_script_interface_context else: header_template_filename = 'interface.h' cpp_template_filename = 'interface.cpp' interface_context = v8_interface.interface_context header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) # Compute context (input values for Jinja) template_context = interface_context(interface) template_context['code_generator'] = module_pyname # Add includes for interface itself and any dependencies interface_info = self.interfaces_info[interface_name] if 'PrivateScriptInterface' not in interface.extended_attributes: template_context['header_includes'].add(interface_info['include_path']) template_context['header_includes'] = sorted(template_context['header_includes']) includes.update(interface_info.get('dependencies_include_paths', [])) template_context['cpp_includes'] = sorted(includes) # Render Jinja templates header_text = header_template.render(template_context) cpp_text = cpp_template.render(template_context) return header_text, cpp_text
def generate_interface_code(self, definitions, interface_name, interface): # Store other interfaces for introspection interfaces.update(definitions.interfaces) interface_info = self.info_provider.interfaces_info[interface_name] full_path = interface_info.get('full_path') component = idl_filename_to_component(full_path) include_paths = interface_info.get('dependencies_include_paths') # Select appropriate Jinja template and contents function if interface.is_callback: header_template_filename = 'callback_interface.h' cpp_template_filename = 'callback_interface.cpp' interface_context = v8_callback_interface.callback_interface_context elif interface.is_partial: interface_context = v8_interface.interface_context header_template_filename = 'partial_interface.h' cpp_template_filename = 'partial_interface.cpp' interface_name += 'Partial' assert component == 'core' component = 'modules' include_paths = interface_info.get('dependencies_other_component_include_paths') else: header_template_filename = 'interface.h' cpp_template_filename = 'interface.cpp' interface_context = v8_interface.interface_context template_context = interface_context(interface) includes.update(interface_info.get('cpp_includes', {}).get(component, set())) if not interface.is_partial and not is_testing_target(full_path): template_context['header_includes'].add(self.info_provider.include_path_for_export) template_context['exported'] = self.info_provider.specifier_for_export # Add the include for interface itself if IdlType(interface_name).is_typed_array: template_context['header_includes'].add('core/dom/DOMTypedArray.h') elif interface_info['include_path']: template_context['header_includes'].add(interface_info['include_path']) template_context['header_includes'].update( interface_info.get('additional_header_includes', [])) header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) header_text, cpp_text = render_template( include_paths, header_template, cpp_template, template_context, component) header_path, cpp_path = self.output_paths(interface_name) return ( (header_path, header_text), (cpp_path, cpp_text), )
def generate_callback_interface(callback_interface): includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) methods = [ generate_method(operation) for operation in callback_interface.operations ] template_contents = { 'cpp_class_name': callback_interface.name, 'v8_class_name': v8_class_name(callback_interface), 'header_includes': CALLBACK_INTERFACE_H_INCLUDES, 'methods': methods, } return template_contents
def generate_callback_interface(callback_interface): includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) name = callback_interface.name methods = [ generate_method(operation) for operation in callback_interface.operations ] template_contents = { 'cpp_class': name, 'dart_class': dart_types.dart_type(callback_interface.name), 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), 'methods': methods, } return template_contents
def generate_callback_interface(callback_interface): includes.clear() includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) return { 'conditional_string': v8_utilities.conditional_string(callback_interface), 'cpp_class': callback_interface.name, 'v8_class': v8_utilities.v8_class_name(callback_interface), 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), 'methods': [ generate_method(operation) for operation in callback_interface.operations ], }
def generate_getter(interface, attribute, contents): idl_type = attribute.idl_type v8_types.add_includes_for_type(idl_type) extended_attributes = attribute.extended_attributes cpp_value = getter_expression(interface, attribute, contents) # Normally we can inline the function call into the return statement to # avoid the overhead of using a Ref<> temporary, but for some cases # (nullable types, EventHandler, [CachedAttribute], or if there are # exceptions), we need to use a local variable. # FIXME: check if compilers are smart enough to inline this, and if so, # always use a local variable (for readability and CG simplicity). if (attribute.is_nullable or idl_type == 'EventHandler' or 'CachedAttribute' in extended_attributes or contents['is_getter_raises_exception']): contents['cpp_value_original'] = cpp_value cpp_value = 'jsValue' contents['cpp_value'] = cpp_value if contents['is_keep_alive_for_gc']: v8_set_return_value_statement = 'v8SetReturnValue(info, wrapper)' includes.add('bindings/v8/V8HiddenPropertyName.h') else: v8_set_return_value_statement = v8_types.v8_set_return_value(idl_type, cpp_value, extended_attributes=extended_attributes, script_wrappable='imp') contents['v8_set_return_value'] = v8_set_return_value_statement if (idl_type == 'EventHandler' and interface.name in ['Window', 'WorkerGlobalScope'] and attribute.name == 'onerror'): includes.add('bindings/v8/V8ErrorHandler.h') # [CheckSecurityForNode] is_check_security_for_node = 'CheckSecurityForNode' in extended_attributes if is_check_security_for_node: includes.add('bindings/v8/BindingSecurity.h') if is_check_security_for_node or contents['is_getter_raises_exception']: includes.update(set(['bindings/v8/ExceptionMessages.h', 'bindings/v8/ExceptionState.h'])) contents.update({ 'deprecate_as': v8_utilities.deprecate_as(attribute), # [DeprecateAs] 'is_check_security_for_node': is_check_security_for_node, 'is_unforgeable': 'Unforgeable' in extended_attributes, 'measure_as': v8_utilities.measure_as(attribute), # [MeasureAs] })
def generate_code(self, definitions, interface_name): """Returns .h/.cpp code as (header_text, cpp_text).""" try: interface = definitions.interfaces[interface_name] except KeyError: raise Exception('%s not in IDL definitions' % interface_name) # Store other interfaces for introspection interfaces.update(definitions.interfaces) # Set local type info IdlType.set_callback_functions(definitions.callback_functions.keys()) IdlType.set_enums((enum.name, enum.values) for enum in definitions.enumerations.values()) # Select appropriate Jinja template and contents function if interface.is_callback: header_template_filename = 'callback_interface.h' cpp_template_filename = 'callback_interface.cpp' generate_contents = v8_callback_interface.generate_callback_interface else: header_template_filename = 'interface.h' cpp_template_filename = 'interface.cpp' generate_contents = v8_interface.generate_interface header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) # Generate contents (input parameters for Jinja) template_contents = generate_contents(interface) template_contents['code_generator'] = module_pyname # Add includes for interface itself and any dependencies interface_info = self.interfaces_info[interface_name] template_contents['header_includes'].add( interface_info['include_path']) template_contents['header_includes'] = sorted( template_contents['header_includes']) includes.update(interface_info.get('dependencies_include_paths', [])) template_contents['cpp_includes'] = sorted(includes) # Render Jinja templates header_text = header_template.render(template_contents) cpp_text = cpp_template.render(template_contents) return header_text, cpp_text
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 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(dictionary, 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 generate_interface_code(self, definitions, interface_name, interface): interface_info = self.info_provider.interfaces_info[interface_name] full_path = interface_info.get("full_path") component = idl_filename_to_component(full_path) include_paths = interface_info.get("dependencies_include_paths") # Select appropriate Jinja template and contents function if interface.is_callback: header_template_filename = "callback_interface.h.tmpl" cpp_template_filename = "callback_interface.cpp.tmpl" interface_context = v8_callback_interface.callback_interface_context elif interface.is_partial: interface_context = v8_interface.interface_context header_template_filename = "partial_interface.h.tmpl" cpp_template_filename = "partial_interface.cpp.tmpl" interface_name += "Partial" assert component == "core" component = "modules" include_paths = interface_info.get("dependencies_other_component_include_paths") else: header_template_filename = "interface.h.tmpl" cpp_template_filename = "interface.cpp.tmpl" interface_context = v8_interface.interface_context template_context = interface_context(interface, definitions.interfaces) includes.update(interface_info.get("cpp_includes", {}).get(component, set())) if not interface.is_partial and not is_testing_target(full_path): template_context["header_includes"].add(self.info_provider.include_path_for_export) template_context["exported"] = self.info_provider.specifier_for_export # Add the include for interface itself if IdlType(interface_name).is_typed_array: template_context["header_includes"].add("core/dom/DOMTypedArray.h") elif interface_info["include_path"]: template_context["header_includes"].add(interface_info["include_path"]) template_context["header_includes"].update(interface_info.get("additional_header_includes", [])) header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) header_text, cpp_text = self.render_template( include_paths, header_template, cpp_template, template_context, component ) header_path, cpp_path = self.output_paths(interface_name) return ((header_path, header_text), (cpp_path, cpp_text))
def generate_code(self, definitions, interface_name): """Returns .h/.cpp code as (header_text, cpp_text).""" try: interface = definitions.interfaces[interface_name] except KeyError: raise Exception('%s not in IDL definitions' % interface_name) # Store other interfaces for introspection interfaces.update(definitions.interfaces) # Set local type info IdlType.set_callback_functions(definitions.callback_functions.keys()) IdlType.set_enums((enum.name, enum.values) for enum in definitions.enumerations.values()) # Select appropriate Jinja template and contents function if interface.is_callback: header_template_filename = 'callback_interface.h' cpp_template_filename = 'callback_interface.cpp' generate_contents = v8_callback_interface.generate_callback_interface else: header_template_filename = 'interface.h' cpp_template_filename = 'interface.cpp' generate_contents = v8_interface.generate_interface header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) # Generate contents (input parameters for Jinja) template_contents = generate_contents(interface) template_contents['code_generator'] = module_pyname # Add includes for interface itself and any dependencies interface_info = self.interfaces_info[interface_name] template_contents['header_includes'].add(interface_info['include_path']) template_contents['header_includes'] = sorted(template_contents['header_includes']) includes.update(interface_info.get('dependencies_include_paths', [])) template_contents['cpp_includes'] = sorted(includes) # Render Jinja templates header_text = header_template.render(template_contents) cpp_text = cpp_template.render(template_contents) return header_text, cpp_text
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(dictionary, 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 add_includes_for_interface(interface_name): includes.update(includes_for_interface(interface_name))
def add_includes_for_type(idl_type): includes.update(idl_type.includes_for_type)
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 method_context(interface, method, is_visible=True): arguments = method.arguments extended_attributes = method.extended_attributes idl_type = method.idl_type is_static = method.is_static name = method.name if is_visible: idl_type.add_includes_for_type(extended_attributes) this_cpp_value = cpp_value(interface, method, len(arguments)) 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 is_call_with_script_arguments = has_extended_attribute_value(method, 'CallWith', 'ScriptArguments') if is_call_with_script_arguments: includes.update(['bindings/core/v8/ScriptCallStackFactory.h', 'core/inspector/ScriptArguments.h']) is_call_with_script_state = has_extended_attribute_value(method, 'CallWith', 'ScriptState') is_call_with_this_value = has_extended_attribute_value(method, 'CallWith', 'ThisValue') if is_call_with_script_state or is_call_with_this_value: includes.add('bindings/core/v8/ScriptState.h') is_check_security_for_node = 'CheckSecurity' in extended_attributes if is_check_security_for_node: includes.add('bindings/core/v8/BindingSecurity.h') is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attributes if is_custom_element_callbacks: includes.add('core/dom/custom/CustomElementProcessingStack.h') is_do_not_check_security = 'DoNotCheckSecurity' in extended_attributes is_check_security_for_frame = ( has_extended_attribute_value(interface, 'CheckSecurity', 'Frame') and not is_do_not_check_security) is_check_security_for_window = ( has_extended_attribute_value(interface, 'CheckSecurity', 'Window') and not is_do_not_check_security) is_raises_exception = 'RaisesException' in extended_attributes is_custom_call_prologue = has_extended_attribute_value(method, 'Custom', 'CallPrologue') is_custom_call_epilogue = has_extended_attribute_value(method, 'Custom', 'CallEpilogue') is_post_message = 'PostMessage' in extended_attributes if is_post_message: includes.add('bindings/core/v8/SerializedScriptValueFactory.h') includes.add('core/dom/DOMArrayBuffer.h') includes.add('core/dom/MessagePort.h') if 'LenientThis' in extended_attributes: raise Exception('[LenientThis] is not supported for operations.') return { 'activity_logging_world_list': v8_utilities.activity_logging_world_list(method), # [ActivityLogging] 'arguments': [argument_context(interface, method, argument, index, is_visible=is_visible) for index, argument in enumerate(arguments)], 'argument_declarations_for_private_script': argument_declarations_for_private_script(interface, method), 'conditional_string': v8_utilities.conditional_string(method), 'cpp_type': (v8_types.cpp_template_type('Nullable', idl_type.cpp_type) if idl_type.is_explicit_nullable else idl_type.cpp_type), 'cpp_value': this_cpp_value, 'cpp_type_initializer': idl_type.cpp_type_initializer, 'custom_registration_extended_attributes': CUSTOM_REGISTRATION_EXTENDED_ATTRIBUTES.intersection( extended_attributes.iterkeys()), 'deprecate_as': v8_utilities.deprecate_as(method), # [DeprecateAs] 'exposed_test': v8_utilities.exposed(method, interface), # [Exposed] # TODO(yukishiino): Retire has_custom_registration flag. Should be # replaced with V8DOMConfiguration::PropertyLocationConfiguration. 'has_custom_registration': is_static or is_unforgeable(interface, method) or v8_utilities.has_extended_attribute( method, CUSTOM_REGISTRATION_EXTENDED_ATTRIBUTES), 'has_exception_state': is_raises_exception or is_check_security_for_frame or is_check_security_for_window or any(argument for argument in arguments if (argument.idl_type.name == 'SerializedScriptValue' or argument_conversion_needs_exception_state(method, argument))), 'idl_type': idl_type.base_type, 'is_call_with_execution_context': has_extended_attribute_value(method, 'CallWith', 'ExecutionContext'), 'is_call_with_script_arguments': is_call_with_script_arguments, 'is_call_with_script_state': is_call_with_script_state, 'is_call_with_this_value': is_call_with_this_value, 'is_check_security_for_frame': is_check_security_for_frame, 'is_check_security_for_node': is_check_security_for_node, 'is_check_security_for_window': is_check_security_for_window, 'is_custom': 'Custom' in extended_attributes and not (is_custom_call_prologue or is_custom_call_epilogue), 'is_custom_call_prologue': is_custom_call_prologue, 'is_custom_call_epilogue': is_custom_call_epilogue, 'is_custom_element_callbacks': is_custom_element_callbacks, 'is_do_not_check_security': is_do_not_check_security, 'is_do_not_check_signature': 'DoNotCheckSignature' in extended_attributes, 'is_explicit_nullable': idl_type.is_explicit_nullable, 'is_implemented_in_private_script': is_implemented_in_private_script, 'is_partial_interface_member': 'PartialInterfaceImplementedAs' in extended_attributes, 'is_per_world_bindings': 'PerWorldBindings' in extended_attributes, 'is_post_message': is_post_message, 'is_raises_exception': is_raises_exception, 'is_read_only': is_unforgeable(interface, method), 'is_static': is_static, 'is_variadic': arguments and arguments[-1].is_variadic, 'measure_as': v8_utilities.measure_as(method, interface), # [MeasureAs] 'name': name, 'number_of_arguments': len(arguments), 'number_of_required_arguments': len([ argument for argument in arguments if not (argument.is_optional or argument.is_variadic)]), 'number_of_required_or_variadic_arguments': len([ argument for argument in arguments if not argument.is_optional]), 'on_instance': v8_utilities.on_instance(interface, method), 'on_interface': v8_utilities.on_interface(interface, method), 'on_prototype': v8_utilities.on_prototype(interface, method), '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', isolate='scriptState->isolate()', bailout_return_value='false'), 'property_attributes': property_attributes(interface, method), 'returns_promise': method.returns_promise, 'runtime_enabled_function': v8_utilities.runtime_enabled_function_name(method), # [RuntimeEnabled] 'should_be_exposed_to_script': not (is_implemented_in_private_script and is_only_exposed_to_private_script), 'use_output_parameter_for_result': idl_type.use_output_parameter_for_result, 'use_local_result': use_local_result(method), 'v8_set_return_value': v8_set_return_value(interface.name, method, this_cpp_value), 'v8_set_return_value_for_main_world': v8_set_return_value(interface.name, method, this_cpp_value, for_main_world=True), 'visible': is_visible, 'world_suffixes': ['', 'ForMainWorld'] if 'PerWorldBindings' in extended_attributes else [''], # [PerWorldBindings], }
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 add_includes_for_type(idl_type, extended_attributes=None): includes.update(idl_type.includes_for_type(extended_attributes))
def interface_context(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 # [ActiveDOMObject] is_active_dom_object = 'ActiveDOMObject' in extended_attributes # [CheckSecurity] is_check_security = 'CheckSecurity' in extended_attributes if is_check_security: includes.add('bindings/core/v8/BindingSecurity.h') # [DependentLifetime] is_dependent_lifetime = 'DependentLifetime' in extended_attributes # [Iterable] iterator_method = None if 'Iterable' in extended_attributes: iterator_operation = IdlOperation(interface.idl_name) iterator_operation.name = 'iterator' iterator_operation.idl_type = IdlType('Iterator') iterator_operation.extended_attributes['RaisesException'] = None iterator_operation.extended_attributes['CallWith'] = 'ScriptState' iterator_method = v8_methods.method_context(interface, iterator_operation) # [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/core/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(raw_type=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() # [NotScriptWrappable] is_script_wrappable = 'NotScriptWrappable' not in extended_attributes # [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) wrapper_class_id = ('NodeClassId' if inherits_interface(interface.name, 'Node') else 'ObjectClassId') context = { 'conditional_string': conditional_string(interface), # [Conditional] 'cpp_class': cpp_name(interface), 'gc_type': this_gc_type, # FIXME: Remove 'EventTarget' special handling, http://crbug.com/383699 'has_access_check_callbacks': (is_check_security and interface.name != 'Window' and interface.name != 'EventTarget'), '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_check_security': is_check_security, 'is_dependent_lifetime': is_dependent_lifetime, 'is_event_target': inherits_interface(interface.name, 'EventTarget'), 'is_exception': interface.is_exception, 'is_node': inherits_interface(interface.name, 'Node'), 'is_script_wrappable': is_script_wrappable, 'iterator_method': iterator_method, 'lifetime': 'Dependent' if (has_visit_dom_wrapper or is_active_dom_object or is_dependent_lifetime) else 'Independent', '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, 'v8_class': v8_utilities.v8_class_name(interface), 'wrapper_class_id': wrapper_class_id, } # Constructors constructors = [constructor_context(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: context['constructor_overloads'] = overloads_context(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/core/v8/Dictionary.h') if any_type_attributes: includes.add('bindings/core/v8/SerializedScriptValue.h') # [NamedConstructor] named_constructor = named_constructor_context(interface) if (constructors or custom_constructors or has_event_constructor or named_constructor): includes.add('bindings/core/v8/V8ObjectConstructor.h') includes.add('core/frame/LocalDOMWindow.h') context.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_raises_exception': extended_attributes.get('RaisesException') == 'Constructor', # [RaisesException=Constructor] 'named_constructor': named_constructor, }) constants = [constant_context(constant) for constant in interface.constants] special_getter_constants = [] runtime_enabled_constants = [] constant_configuration_constants = [] for constant in constants: if constant['measure_as'] or constant['deprecate_as']: special_getter_constants.append(constant) continue if constant['runtime_enabled_function']: runtime_enabled_constants.append(constant) continue constant_configuration_constants.append(constant) # Constants context.update({ 'constant_configuration_constants': constant_configuration_constants, 'constants': constants, 'do_not_check_constants': 'DoNotCheckConstants' in extended_attributes, 'has_constant_configuration': any( not constant['runtime_enabled_function'] for constant in constants), 'runtime_enabled_constants': runtime_enabled_constants, 'special_getter_constants': special_getter_constants, }) # Attributes attributes = [v8_attributes.attribute_context(interface, attribute) for attribute in interface.attributes] context.update({ 'attributes': attributes, 'has_accessors': any(attribute['is_expose_js_accessors'] and attribute['should_be_exposed_to_script'] 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']) and attribute['should_be_exposed_to_script'] for attribute in attributes), 'has_conditional_attributes': any(attribute['per_context_enabled_function'] or attribute['exposed_test'] for attribute in attributes), 'has_constructor_attributes': any(attribute['constructor_type'] for attribute in attributes), 'has_replaceable_attributes': any(attribute['is_replaceable'] for attribute in attributes), }) # Methods methods = [v8_methods.method_context(interface, method) for method in interface.operations if method.name] # Skip anonymous special operations (methods) compute_method_overloads_context(methods) # Stringifier if interface.stringifier: stringifier = interface.stringifier method = IdlOperation(interface.idl_name) method.name = 'toString' method.idl_type = IdlType('DOMString') method.extended_attributes.update(stringifier.extended_attributes) if stringifier.attribute: method.extended_attributes['ImplementedAs'] = stringifier.attribute.name elif stringifier.operation: method.extended_attributes['ImplementedAs'] = stringifier.operation.name methods.append(v8_methods.method_context(interface, method)) conditionally_enabled_methods = [] custom_registration_methods = [] method_configuration_methods = [] for method in methods: # Skip all but one method in each set of overloaded methods. if 'overload_index' in method and 'overloads' not in method: continue if 'overloads' in method: overloads = method['overloads'] per_context_enabled_function = overloads['per_context_enabled_function_all'] conditionally_exposed_function = overloads['exposed_test_all'] runtime_enabled_function = overloads['runtime_enabled_function_all'] has_custom_registration = overloads['has_custom_registration_all'] else: per_context_enabled_function = method['per_context_enabled_function'] conditionally_exposed_function = method['exposed_test'] runtime_enabled_function = method['runtime_enabled_function'] has_custom_registration = method['has_custom_registration'] if per_context_enabled_function or conditionally_exposed_function: conditionally_enabled_methods.append(method) continue if runtime_enabled_function or has_custom_registration: custom_registration_methods.append(method) continue if method['should_be_exposed_to_script']: method_configuration_methods.append(method) for method in methods: # The value of the Function object’s “length” property is a Number # determined as follows: # 1. Let S be the effective overload set for regular operations (if the # operation is a regular operation) or for static operations (if the # operation is a static operation) with identifier id on interface I and # with argument count 0. # 2. Return the length of the shortest argument list of the entries in S. # FIXME: This calculation doesn't take into account whether runtime # enabled overloads are actually enabled, so length may be incorrect. # E.g., [RuntimeEnabled=Foo] void f(); void f(long x); # should have length 1 if Foo is not enabled, but length 0 if it is. method['length'] = (method['overloads']['minarg'] if 'overloads' in method else method['number_of_required_arguments']) context.update({ 'conditionally_enabled_methods': conditionally_enabled_methods, 'custom_registration_methods': custom_registration_methods, 'has_origin_safe_method_setter': any( method['is_check_security_for_frame'] and not method['is_read_only'] for method in methods), 'has_private_script': any(attribute['is_implemented_in_private_script'] for attribute in attributes) or any(method['is_implemented_in_private_script'] for method in methods), 'method_configuration_methods': method_configuration_methods, 'methods': methods, }) context.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 context
def method_context(interface, method, is_visible=True): arguments = method.arguments extended_attributes = method.extended_attributes idl_type = method.idl_type is_static = method.is_static name = method.name if is_visible: idl_type.add_includes_for_type(extended_attributes) this_cpp_value = cpp_value(interface, method, len(arguments)) is_call_with_script_arguments = has_extended_attribute_value(method, 'CallWith', 'ScriptArguments') if is_call_with_script_arguments: includes.update(['bindings/core/v8/ScriptCallStack.h', 'core/inspector/ScriptArguments.h']) is_call_with_script_state = has_extended_attribute_value(method, 'CallWith', 'ScriptState') is_call_with_this_value = has_extended_attribute_value(method, 'CallWith', 'ThisValue') if is_call_with_script_state or is_call_with_this_value: includes.add('platform/bindings/ScriptState.h') # [CheckSecurity] is_cross_origin = 'CrossOrigin' in extended_attributes is_check_security_for_receiver = ( has_extended_attribute_value(interface, 'CheckSecurity', 'Receiver') and not is_cross_origin) is_check_security_for_return_value = ( has_extended_attribute_value(method, 'CheckSecurity', 'ReturnValue')) if is_check_security_for_receiver or is_check_security_for_return_value: includes.add('bindings/core/v8/BindingSecurity.h') is_ce_reactions = 'CEReactions' in extended_attributes if is_ce_reactions: includes.add('core/html/custom/CEReactionsScope.h') is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attributes if is_custom_element_callbacks: includes.add('core/html/custom/V0CustomElementProcessingStack.h') is_raises_exception = 'RaisesException' in extended_attributes is_custom_call_prologue = has_extended_attribute_value(method, 'Custom', 'CallPrologue') is_custom_call_epilogue = has_extended_attribute_value(method, 'Custom', 'CallEpilogue') is_post_message = 'PostMessage' in extended_attributes if is_post_message: includes.add('bindings/core/v8/serialization/SerializedScriptValueFactory.h') includes.add('bindings/core/v8/serialization/Transferables.h') includes.add('core/typed_arrays/DOMArrayBufferBase.h') includes.add('core/imagebitmap/ImageBitmap.h') if 'LenientThis' in extended_attributes: raise Exception('[LenientThis] is not supported for operations.') argument_contexts = [ argument_context(interface, method, argument, index, is_visible=is_visible) for index, argument in enumerate(arguments)] return { 'activity_logging_world_list': v8_utilities.activity_logging_world_list(method), # [ActivityLogging] 'arguments': argument_contexts, 'cpp_type': (v8_types.cpp_template_type('Nullable', idl_type.cpp_type) if idl_type.is_explicit_nullable else idl_type.cpp_type), 'cpp_value': this_cpp_value, 'cpp_type_initializer': idl_type.cpp_type_initializer, 'deprecate_as': v8_utilities.deprecate_as(method), # [DeprecateAs] 'do_not_test_new_object': 'DoNotTestNewObject' in extended_attributes, 'exposed_test': v8_utilities.exposed(method, interface), # [Exposed] 'has_exception_state': is_raises_exception or is_check_security_for_receiver or any(argument for argument in arguments if (argument.idl_type.name == 'SerializedScriptValue' or argument_conversion_needs_exception_state(method, argument))), 'has_optional_argument_without_default_value': any(True for argument_context in argument_contexts if argument_context['is_optional_without_default_value']), 'idl_type': idl_type.base_type, 'is_call_with_execution_context': has_extended_attribute_value(method, 'CallWith', 'ExecutionContext'), 'is_call_with_script_arguments': is_call_with_script_arguments, 'is_call_with_script_state': is_call_with_script_state, 'is_call_with_this_value': is_call_with_this_value, '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_cross_origin': 'CrossOrigin' in extended_attributes, 'is_custom': 'Custom' in extended_attributes and not (is_custom_call_prologue or is_custom_call_epilogue), 'is_custom_call_prologue': is_custom_call_prologue, 'is_custom_call_epilogue': is_custom_call_epilogue, 'is_custom_element_callbacks': is_custom_element_callbacks, 'is_explicit_nullable': idl_type.is_explicit_nullable, 'is_new_object': 'NewObject' in extended_attributes, 'is_partial_interface_member': 'PartialInterfaceImplementedAs' in extended_attributes, 'is_per_world_bindings': 'PerWorldBindings' in extended_attributes, 'is_post_message': is_post_message, 'is_raises_exception': is_raises_exception, 'is_static': is_static, 'is_unforgeable': is_unforgeable(interface, method), 'is_variadic': arguments and arguments[-1].is_variadic, 'measure_as': v8_utilities.measure_as(method, interface), # [MeasureAs] 'name': name, 'number_of_arguments': len(arguments), 'number_of_required_arguments': len([ argument for argument in arguments if not (argument.is_optional or argument.is_variadic)]), 'number_of_required_or_variadic_arguments': len([ argument for argument in arguments if not argument.is_optional]), 'on_instance': v8_utilities.on_instance(interface, method), 'on_interface': v8_utilities.on_interface(interface, method), 'on_prototype': v8_utilities.on_prototype(interface, method), 'origin_trial_enabled_function': v8_utilities.origin_trial_enabled_function_name(method), # [OriginTrialEnabled] 'origin_trial_feature_name': v8_utilities.origin_trial_feature_name(method), # [OriginTrialEnabled] 'property_attributes': property_attributes(interface, method), 'returns_promise': method.returns_promise, 'runtime_call_stats': runtime_call_stats_context(interface, method), 'runtime_enabled_feature_name': v8_utilities.runtime_enabled_feature_name(method), # [RuntimeEnabled] 'secure_context_test': v8_utilities.secure_context(method, interface), # [SecureContext] 'use_output_parameter_for_result': idl_type.use_output_parameter_for_result, 'use_local_result': use_local_result(method), 'v8_set_return_value': v8_set_return_value(interface.name, method, this_cpp_value), 'v8_set_return_value_for_main_world': v8_set_return_value(interface.name, method, this_cpp_value, for_main_world=True), 'visible': is_visible, 'world_suffixes': ['', 'ForMainWorld'] if 'PerWorldBindings' in extended_attributes else [''], # [PerWorldBindings], }
def generate_method(interface, method): arguments = method.arguments extended_attributes = method.extended_attributes idl_type = method.idl_type is_static = method.is_static name = method.name this_cpp_value = cpp_value(interface, method, len(arguments)) this_custom_signature = custom_signature(method, arguments) def function_template(): if is_static: return 'functionTemplate' if 'Unforgeable' in extended_attributes: return 'instanceTemplate' return 'prototypeTemplate' def signature(): if this_custom_signature: return name + 'Signature' if is_static or 'DoNotCheckSignature' in extended_attributes: return 'v8::Local<v8::Signature>()' return 'defaultSignature' is_call_with_script_arguments = has_extended_attribute_value( method, 'CallWith', 'ScriptArguments') if is_call_with_script_arguments: includes.update([ 'bindings/v8/ScriptCallStackFactory.h', 'core/inspector/ScriptArguments.h' ]) is_call_with_script_state = has_extended_attribute_value( method, 'CallWith', 'ScriptState') if is_call_with_script_state: includes.add('bindings/v8/ScriptState.h') is_check_security_for_node = 'CheckSecurity' in extended_attributes if is_check_security_for_node: includes.add('bindings/v8/BindingSecurity.h') is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attributes if is_custom_element_callbacks: includes.add('core/dom/custom/CustomElementCallbackDispatcher.h') contents = { 'activity_logging_world_list': v8_utilities.activity_logging_world_list(method), # [ActivityLogging] 'arguments': [ generate_argument(interface, method, argument, index) for index, argument in enumerate(arguments) ], 'conditional_string': v8_utilities.conditional_string(method), 'cpp_type': v8_types.cpp_type(idl_type), 'cpp_value': this_cpp_value, 'custom_signature': this_custom_signature, 'deprecate_as': v8_utilities.deprecate_as(method), # [DeprecateAs] 'do_not_check_signature': not (this_custom_signature or is_static or v8_utilities.has_extended_attribute(method, [ 'DoNotCheckSecurity', 'DoNotCheckSignature', 'NotEnumerable', 'ReadOnly', 'RuntimeEnabled', 'Unforgeable' ])), 'function_template': function_template(), 'idl_type': idl_type, 'is_call_with_execution_context': has_extended_attribute_value(method, 'CallWith', 'ExecutionContext'), 'is_call_with_script_arguments': is_call_with_script_arguments, 'is_call_with_script_state': is_call_with_script_state, 'is_check_security_for_frame': ('CheckSecurity' in interface.extended_attributes and 'DoNotCheckSecurity' not in extended_attributes), 'is_check_security_for_node': is_check_security_for_node, 'is_custom': 'Custom' in extended_attributes, 'is_custom_element_callbacks': is_custom_element_callbacks, 'is_do_not_check_security': 'DoNotCheckSecurity' in extended_attributes, 'is_per_world_bindings': 'PerWorldBindings' in extended_attributes, 'is_raises_exception': 'RaisesException' in extended_attributes, 'is_read_only': 'ReadOnly' in extended_attributes, 'is_static': is_static, 'is_strict_type_checking': 'StrictTypeChecking' in extended_attributes, 'is_variadic': arguments and arguments[-1].is_variadic, 'measure_as': v8_utilities.measure_as(method), # [MeasureAs] 'name': name, 'number_of_arguments': len(arguments), 'number_of_required_arguments': len([ argument for argument in arguments if not (argument.is_optional or argument.is_variadic) ]), 'number_of_required_or_variadic_arguments': len([argument for argument in arguments if not argument.is_optional]), 'per_context_enabled_function': v8_utilities.per_context_enabled_function_name( method), # [PerContextEnabled] 'property_attributes': property_attributes(method), 'runtime_enabled_function': v8_utilities.runtime_enabled_function_name(method), # [RuntimeEnabled] 'signature': signature(), 'v8_set_return_value': v8_set_return_value(method, this_cpp_value), 'world_suffixes': ['', 'ForMainWorld'] if 'PerWorldBindings' in extended_attributes else [''], # [PerWorldBindings] } 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, }