OLD | NEW |
(Empty) | |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # |
| 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are |
| 5 # met: |
| 6 # |
| 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer |
| 11 # in the documentation and/or other materials provided with the |
| 12 # distribution. |
| 13 # * Neither the name of Google Inc. nor the names of its |
| 14 # contributors may be used to endorse or promote products derived from |
| 15 # this software without specific prior written permission. |
| 16 # |
| 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 # 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
| 29 """Generate Blink V8 bindings (.h and .cpp files). |
| 30 |
| 31 Input: An object of class IdlDefinitions, containing an IDL interface X |
| 32 Output: V8X.h and V8X.cpp |
| 33 """ |
| 34 |
| 35 |
| 36 from code_generator_idl_reader import inherits_interface, interface_inherits_ext
ended_attribute |
| 37 from v8_includes import header_files_for_interface |
| 38 import v8_special_accessors |
| 39 # from v8_types import get_native_type, get_v8_class_name |
| 40 from v8_types import * |
| 41 from v8_utilities import generate_conditional_string, implemented_as_cpp_name |
| 42 |
| 43 |
| 44 def generate_header(interface): |
| 45 includes = base_header_includes(interface) |
| 46 cpp_class_name = implemented_as_cpp_name(interface) |
| 47 cpp_class_name_as_parameter = get_native_type(interface.name, used_as_parame
ter=True) |
| 48 |
| 49 # Ensure the IsDOMNodeType function is in sync. |
| 50 if is_dom_node_type(interface.name) != inherits_interface(interface, 'Node')
: |
| 51 # inh = 'INHERIT' if inherits_interface(interface, 'Node') else '' |
| 52 # dom = 'DOM' if is_dom_node_type(interface.name) else '' |
| 53 # print '[IsDOMNodeType]', dom, inh |
| 54 raise Exception('IsDOMNodeType is out of date with respect to %s' % inte
rface.name) |
| 55 |
| 56 # SVG |
| 57 svg_property_type, svg_list_property_type, svg_native_type, svg_header_inclu
des, svg_cpp_includes = get_svg_property_types(interface.name) |
| 58 includes |= set(svg_header_includes) |
| 59 # print '[[]]', svg_native_type |
| 60 svg_native_type_contains_svg_static_list_property_tear_off = 'SVGStaticListP
ropertyTearOff' in svg_native_type |
| 61 svg_type_needing_tear_off = get_svg_type_needing_tear_off(interface.name) |
| 62 if svg_type_needing_tear_off: |
| 63 cpp_class_name = svg_type_needing_tear_off |
| 64 # cpp_class_name_as_parameter = svg_type_needing_tear_off |
| 65 cpp_class_name_as_parameter = get_native_type(svg_type_needing_tear_off,
used_as_parameter=True) |
| 66 |
| 67 # Wrap |
| 68 no_to_v8 = 'DoNotGenerateToV8' in interface.extended_attributes |
| 69 no_wrap = 'DoNotGenerateWrap' in interface.extended_attributes or no_to_v8 |
| 70 generate_to_v8 = False |
| 71 custom_wrap = 'CustomToV8' in interface.extended_attributes |
| 72 if no_to_v8: |
| 73 if interface.parent: |
| 74 raise Exception("Can't suppress toV8 for subclass") |
| 75 elif no_wrap: |
| 76 if not custom_wrap: |
| 77 raise Exception('Must have custom toV8') |
| 78 generate_to_v8 = True |
| 79 |
| 80 if interface.parent: |
| 81 v8_parent_class_name = 'V8' + interface.parent |
| 82 to_wrapped_type = '%s::toInternalPointer(impl)' % v8_parent_class_name |
| 83 from_wrapped_type = 'static_cast<%s*>(%s::fromInternalPointer(object))'
% (cpp_class_name, v8_parent_class_name) |
| 84 else: |
| 85 to_wrapped_type = 'impl' |
| 86 from_wrapped_type = 'static_cast<%s*>(object)' % cpp_class_name |
| 87 |
| 88 # Enabled per context |
| 89 enabled_per_context_functions = [operation for operation in interface.operat
ions if operation.name and operation.extended_attributes.get('EnabledPerContext'
)] |
| 90 enabled_per_context_attributes = [attribute for attribute in interface.attri
butes if attribute.extended_attributes.get('EnabledPerContext')] |
| 91 |
| 92 template_parameters = { |
| 93 'conditional_string': generate_conditional_string(interface), |
| 94 'cpp_class_name': cpp_class_name, |
| 95 # used in function parameter -> can be replaced |
| 96 'cpp_class_name_as_parameter': cpp_class_name_as_parameter, |
| 97 'inherits_event_target': inherits_interface(interface, 'EventTarget'), |
| 98 'internal_fields': get_internal_fields(interface), |
| 99 'generate_to_v8': generate_to_v8, |
| 100 'header_includes': sorted(includes), |
| 101 'operation_definitions': operation_definitions(interface), |
| 102 'attribute_definitions': attribute_definitions(interface), |
| 103 'has_custom_named_enumerator': has_custom_named_enumerator(interface), |
| 104 'install_per_context_properties_body': semicolon_or_braces(enabled_per_c
ontext_attributes), |
| 105 'install_per_context_prototype_properties_body': semicolon_or_braces(ena
bled_per_context_functions), |
| 106 'wrap': not no_wrap, |
| 107 'custom_wrap': custom_wrap, |
| 108 'to_wrapped_type': to_wrapped_type, |
| 109 'from_wrapped_type': from_wrapped_type, |
| 110 'svg_property_type': svg_property_type, |
| 111 'svg_list_property_type': svg_list_property_type, |
| 112 'svg_native_type': svg_native_type, |
| 113 'svg_native_type_contains_svg_static_list_property_tear_off': svg_native
_type_contains_svg_static_list_property_tear_off, |
| 114 } |
| 115 template_parameters.update(v8_special_accessors.has_custom_accessors(interfa
ce)) |
| 116 return template_parameters |
| 117 |
| 118 |
| 119 def base_header_includes(interface): |
| 120 includes = set([ |
| 121 'bindings/v8/WrapperTypeInfo.h', |
| 122 'bindings/v8/V8Binding.h', |
| 123 'bindings/v8/V8DOMWrapper.h', |
| 124 ]) |
| 125 if interface.parent: |
| 126 includes.add('V8%s.h' % interface.parent) |
| 127 includes |= set(header_files_for_interface(interface.name, implemented_as_cp
p_name(interface))) |
| 128 return includes |
| 129 |
| 130 |
| 131 def get_internal_fields(interface): |
| 132 # Event listeners on DOM nodes are explicitly supported in the GC controller
. |
| 133 if (not inherits_interface(interface, 'Node') and |
| 134 (inherits_interface(interface, 'EventTarget') or |
| 135 any([attribute.data_type == 'EventListener' |
| 136 for attribute in interface.attributes]))): |
| 137 return ['eventListenerCacheIndex'] |
| 138 return [] |
| 139 |
| 140 |
| 141 # FIXME: list comprehension |
| 142 # FIXME: in fact, template instead |
| 143 def operation_definitions(interface): |
| 144 definitions = [] |
| 145 for operation in interface.operations: |
| 146 if not operation.name: |
| 147 continue |
| 148 if has_custom_implementation(operation) and operation.overload_index ==
1: |
| 149 code = 'static void %sMethodCustom(const v8::FunctionCallbackInfo<v8
::Value>&);' % operation.name |
| 150 definitions.append(apply_conditional(operation, code)) |
| 151 return definitions |
| 152 |
| 153 |
| 154 # FIXME: list comprehension |
| 155 # FIXME: in fact, template instead |
| 156 def attribute_definitions(interface): |
| 157 definitions = [] |
| 158 for attribute in interface.attributes: |
| 159 if has_custom_getter(attribute): |
| 160 code = 'static void %sAttrGetterCustom(v8::Local<v8::String> name, c
onst v8::PropertyCallbackInfo<v8::Value>&);' % attribute.name |
| 161 definitions.append(apply_conditional(attribute, code)) |
| 162 if has_custom_setter(attribute): |
| 163 code = 'static void %sAttrSetterCustom(v8::Local<v8::String> name, v
8::Local<v8::Value>, const v8::PropertyCallbackInfo<void>&);' % attribute.name |
| 164 definitions.append(apply_conditional(attribute, code)) |
| 165 return definitions |
| 166 |
| 167 |
| 168 def has_custom_named_enumerator(interface): |
| 169 named_getter_operation = v8_special_accessors.get_named_getter_operation(int
erface) |
| 170 return named_getter_operation and 'CustomEnumerateProperty' in named_getter_
operation.extended_attributes |
| 171 |
| 172 |
| 173 def semicolon_or_braces(l): |
| 174 if l: |
| 175 return ';' |
| 176 return ' { }' |
| 177 |
| 178 |
| 179 # FIXME: template instead? |
| 180 def apply_conditional(operation_or_attribute, code): |
| 181 conditional_string = generate_conditional_string(operation_or_attribute) |
| 182 if conditional_string: |
| 183 return ('#if %s\n' % conditional_string + |
| 184 code + |
| 185 '#endif // %s\n' % conditional_string) |
| 186 return code |
OLD | NEW |