OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import v8_utilities |
| 6 |
| 7 |
| 8 UNION_H_INCLUDES = frozenset([ |
| 9 'bindings/core/v8/Dictionary.h', |
| 10 'bindings/core/v8/ExceptionState.h', |
| 11 'bindings/core/v8/V8Binding.h', |
| 12 'platform/heap/Handle.h', |
| 13 ]) |
| 14 |
| 15 UNION_CPP_INCLUDES_BLACKLIST = frozenset([ |
| 16 # This header defines static functions needed to implement event handler |
| 17 # attributes in interfaces that implement GlobalEventHandlers. They are not |
| 18 # needed or used by UnionTypes*.cpp, so including the header causes |
| 19 # compilation errors. |
| 20 # FIXME: We should solve this problem in a way that doesn't involve special- |
| 21 # casing a header like this. |
| 22 'core/dom/GlobalEventHandlers.h', |
| 23 ]) |
| 24 |
| 25 |
| 26 cpp_includes = set() |
| 27 header_forward_decls = set() |
| 28 |
| 29 |
| 30 def union_context(union_types, interfaces_info): |
| 31 cpp_includes.clear() |
| 32 header_forward_decls.clear() |
| 33 |
| 34 # For container classes we strip nullable wrappers. For example, |
| 35 # both (A or B)? and (A? or B) will become AOrB. This should be OK |
| 36 # because container classes can handle null and it seems that |
| 37 # distinguishing (A or B)? and (A? or B) doesn't make sense. |
| 38 container_cpp_types = set() |
| 39 union_types_for_containers = set() |
| 40 nullable_cpp_types = set() |
| 41 for union_type in union_types: |
| 42 cpp_type = union_type.cpp_type |
| 43 if cpp_type not in container_cpp_types: |
| 44 union_types_for_containers.add(union_type) |
| 45 container_cpp_types.add(cpp_type) |
| 46 if union_type.includes_nullable_type: |
| 47 nullable_cpp_types.add(cpp_type) |
| 48 |
| 49 union_types_for_containers = sorted(union_types_for_containers, |
| 50 key=lambda union_type: union_type.cpp_ty
pe) |
| 51 nullable_cpp_types = sorted(nullable_cpp_types) |
| 52 |
| 53 return { |
| 54 'containers': [container_context(union_type, interfaces_info) |
| 55 for union_type in union_types_for_containers], |
| 56 'cpp_includes': sorted(cpp_includes - UNION_CPP_INCLUDES_BLACKLIST), |
| 57 'header_forward_decls': sorted(header_forward_decls), |
| 58 'header_includes': sorted(UNION_H_INCLUDES), |
| 59 'nullable_cpp_types': nullable_cpp_types, |
| 60 } |
| 61 |
| 62 |
| 63 def container_context(union_type, interfaces_info): |
| 64 members = [] |
| 65 |
| 66 # These variables refer to member contexts if the given union type has |
| 67 # corresponding types. They are used for V8 -> impl conversion. |
| 68 array_buffer_type = None |
| 69 array_buffer_view_type = None |
| 70 array_or_sequence_type = None |
| 71 boolean_type = None |
| 72 dictionary_type = None |
| 73 interface_types = [] |
| 74 numeric_type = None |
| 75 string_type = None |
| 76 for member in union_type.member_types: |
| 77 context = member_context(member, interfaces_info) |
| 78 members.append(context) |
| 79 if member.base_type == 'ArrayBuffer': |
| 80 if array_buffer_type: |
| 81 raise Exception('%s is ambiguous.' % union_type.name) |
| 82 array_buffer_type = context |
| 83 elif member.base_type == 'ArrayBufferView': |
| 84 if array_buffer_view_type: |
| 85 raise Exception('%s is ambiguous.' % union_type.name) |
| 86 array_buffer_view_type = context |
| 87 # FIXME: Remove generic Dictionary special casing. |
| 88 elif member.is_dictionary or member.base_type == 'Dictionary': |
| 89 if dictionary_type: |
| 90 raise Exception('%s is ambiguous.' % union_type.name) |
| 91 dictionary_type = context |
| 92 elif member.is_array_or_sequence_type: |
| 93 if array_or_sequence_type: |
| 94 raise Exception('%s is ambiguous.' % union_type.name) |
| 95 array_or_sequence_type = context |
| 96 elif member.is_interface_type: |
| 97 interface_types.append(context) |
| 98 elif member is union_type.boolean_member_type: |
| 99 boolean_type = context |
| 100 elif member is union_type.numeric_member_type: |
| 101 numeric_type = context |
| 102 elif member is union_type.string_member_type: |
| 103 string_type = context |
| 104 else: |
| 105 raise Exception('%s is not supported as an union member.' % member.n
ame) |
| 106 |
| 107 # Nullable restriction checks |
| 108 nullable_members = union_type.number_of_nullable_member_types |
| 109 if nullable_members > 1: |
| 110 raise Exception('%s contains more than one nullable members' % union_typ
e.name) |
| 111 if dictionary_type and nullable_members == 1: |
| 112 raise Exception('%s has a dictionary and a nullable member' % union_type
.name) |
| 113 |
| 114 return { |
| 115 'array_buffer_type': array_buffer_type, |
| 116 'array_buffer_view_type': array_buffer_view_type, |
| 117 'array_or_sequence_type': array_or_sequence_type, |
| 118 'boolean_type': boolean_type, |
| 119 'cpp_class': union_type.cpp_type, |
| 120 'dictionary_type': dictionary_type, |
| 121 'includes_nullable_type': union_type.includes_nullable_type, |
| 122 'interface_types': interface_types, |
| 123 'members': members, |
| 124 'numeric_type': numeric_type, |
| 125 'string_type': string_type, |
| 126 'type_string': str(union_type), |
| 127 } |
| 128 |
| 129 |
| 130 def member_context(member, interfaces_info): |
| 131 cpp_includes.update(member.includes_for_type()) |
| 132 interface_info = interfaces_info.get(member.name, None) |
| 133 if interface_info: |
| 134 cpp_includes.update(interface_info.get('dependencies_include_paths', [])
) |
| 135 header_forward_decls.add(member.implemented_as) |
| 136 if member.is_nullable: |
| 137 member = member.inner_type |
| 138 return { |
| 139 'cpp_name': v8_utilities.uncapitalize(member.name), |
| 140 'cpp_type': member.cpp_type_args(used_in_cpp_sequence=True), |
| 141 'cpp_local_type': member.cpp_type, |
| 142 'cpp_value_to_v8_value': member.cpp_value_to_v8_value( |
| 143 cpp_value='impl.getAs%s()' % member.name, isolate='isolate', |
| 144 creation_context='creationContext'), |
| 145 'enum_values': member.enum_values, |
| 146 'is_traceable': member.is_traceable, |
| 147 'rvalue_cpp_type': member.cpp_type_args(used_as_rvalue_type=True), |
| 148 'specific_type_enum': 'SpecificType' + member.name, |
| 149 'type_name': member.name, |
| 150 'v8_value_to_local_cpp_value': member.v8_value_to_local_cpp_value( |
| 151 {}, 'v8Value', 'cppValue', isolate='isolate', |
| 152 use_exception_state=True, restricted_float=True), |
| 153 } |
OLD | NEW |