OLD | NEW |
(Empty) | |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # |
| 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are |
| 5 # met: |
| 6 # |
| 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer |
| 11 # in the documentation and/or other materials provided with the |
| 12 # distribution. |
| 13 # * Neither the name of Google Inc. nor the names of its |
| 14 # contributors may be used to endorse or promote products derived from |
| 15 # this software without specific prior written permission. |
| 16 # |
| 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 # 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
| 29 """Generate template values for constructors. |
| 30 |
| 31 FIXME: rename: "template parameters" is easily confused with "function parameter
s" |
| 32 Also, "parameter*s*", not "parameter". |
| 33 """ |
| 34 |
| 35 from v8_functions import get_function_mandatory_parameters, get_parameter_check_
parameters |
| 36 from v8_includes import * |
| 37 from v8_types import * |
| 38 from v8_utilities import implemented_as_cpp_name |
| 39 |
| 40 |
| 41 NAMED_PARAMETER_INCLUDES = [ |
| 42 'V8Document.h', |
| 43 'bindings/v8/V8ObjectConstructor.h', |
| 44 ] |
| 45 |
| 46 |
| 47 def generate_constructor_contents(interface): |
| 48 includes = [] |
| 49 |
| 50 constructor_raises_exception = 'ConstructorRaisesException' in interface.ext
ended_attributes |
| 51 if constructor_raises_exception: |
| 52 includes.append('bindings/v8/ExceptionState.h') |
| 53 |
| 54 has_named_constructor = 'NamedConstructor' in interface.extended_attributes |
| 55 if has_named_constructor: |
| 56 named_constructor_parameter, named_constructor_includes = generate_named
_constructor(interface) |
| 57 includes += named_constructor_includes |
| 58 else: |
| 59 named_constructor_parameter = {} |
| 60 |
| 61 constructor_parameters, constructor_includes = generate_constructors(interfa
ce) |
| 62 includes += constructor_includes |
| 63 |
| 64 is_constructor_template_of_event = is_constructor_template(interface, 'Event
') |
| 65 if is_constructor_template_of_event: |
| 66 includes.append('bindings/v8/Dictionary.h') |
| 67 |
| 68 is_constructor_template_of_typed_array = is_constructor_template(interface,
'TypedArray') |
| 69 |
| 70 constructable = is_constructable(interface) |
| 71 if constructable: |
| 72 includes.append('bindings/v8/V8ObjectConstructor.h') |
| 73 |
| 74 template_parameters = { |
| 75 'is_constructable': constructable, |
| 76 'named_constructor': named_constructor_parameter, |
| 77 'constructor_raises_exception': constructor_raises_exception, |
| 78 'constructor_call_with': interface.extended_attributes.get('ConstructorC
allWith'), |
| 79 'constructors': constructor_parameters, |
| 80 'has_custom_constructor': has_custom_constructor(interface), |
| 81 'has_named_constructor': has_named_constructor, |
| 82 'has_constructor': 'Constructor' in interface.extended_attributes, |
| 83 'is_constructor_template_of_event': is_constructor_template_of_event, |
| 84 'is_constructor_template_of_typed_array': is_constructor_template_of_typ
ed_array, |
| 85 } |
| 86 return template_parameters, includes |
| 87 |
| 88 |
| 89 def generate_constructors(interface): |
| 90 # FIXME: clearer with a list comprehension + nested function to handle inclu
des |
| 91 constructors = [] |
| 92 includes = [] |
| 93 for constructor in interface.constructors: |
| 94 if len(interface.constructors) == 1: |
| 95 constructor_contents, constructor_includes = generate_constructor(in
terface, constructor) |
| 96 constructors.append(constructor_contents) |
| 97 includes += constructor_includes |
| 98 else: |
| 99 # TODO |
| 100 pass |
| 101 return constructors, includes |
| 102 |
| 103 |
| 104 def generate_named_constructor(interface): |
| 105 named_constructor = interface.constructors[0] |
| 106 parameter_check_parameters, parameter_check_includes, replacements = get_par
ameter_check_parameters(interface, named_constructor) |
| 107 includes = NAMED_PARAMETER_INCLUDES + parameter_check_includes |
| 108 |
| 109 argument_list = ['document'] |
| 110 for argument in named_constructor.arguments: |
| 111 argument_name = argument.name |
| 112 if argument_name in replacements: |
| 113 argument_name = replacements[argument_name] |
| 114 argument_list.append(argument_name) |
| 115 |
| 116 constructor_raises_exception = 'ConstructorRaisesException' in interface.ext
ended_attributes |
| 117 if constructor_raises_exception: |
| 118 argument_list.append('ec') |
| 119 |
| 120 argument_string = ', '.join(argument_list) |
| 121 named_constructor_parameter = { |
| 122 'argument_string': argument_string, |
| 123 'parameters': parameter_check_parameters, |
| 124 } |
| 125 return named_constructor_parameter, includes |
| 126 |
| 127 |
| 128 def generate_constructor(interface, constructor): |
| 129 includes = [] |
| 130 cpp_class_name = implemented_as_cpp_name(interface) |
| 131 v8_class_name = get_v8_class_name(interface) |
| 132 overloaded_index_string = '' |
| 133 if constructor.overloaded_index > 0: |
| 134 overloaded_index_string = constructor.overloaded_index |
| 135 # print '####### [ctor]', len(constructor.arguments) |
| 136 |
| 137 # FIXME: Currently [Constructor(...)] does not yet support optional argument
s without [Default=...] |
| 138 # parameter_check_string, _, replacements) = GenerateParametersCheck($fu
nction, $interface, '') |
| 139 parameter_check_parameters, parameter_check_includes, replacements = get_par
ameter_check_parameters(interface, constructor) |
| 140 includes += parameter_check_includes |
| 141 |
| 142 argument_list = [] |
| 143 if interface.extended_attributes.get('ConstructorCallWith') == 'ScriptExecut
ionContext': |
| 144 argument_list.append('context') |
| 145 for parameter in constructor.arguments: |
| 146 parameter_name = parameter.name |
| 147 if parameter_name in replacements: |
| 148 parameter_name = replacements[parameter_name] |
| 149 argument_list.append(parameter_name) |
| 150 |
| 151 if 'ConstructorRaisesException' in interface.extended_attributes: |
| 152 argument_list.append('es') |
| 153 argument_string = ', '.join(argument_list) |
| 154 |
| 155 parameter = { |
| 156 'argument_string': argument_string, |
| 157 'mandatory_parameters': get_function_mandatory_parameters(constructor), |
| 158 'parameters': parameter_check_parameters, |
| 159 'overloaded_index': constructor.overloaded_index, |
| 160 'overloaded_index_string': overloaded_index_string, |
| 161 } |
| 162 return parameter, includes |
OLD | NEW |