| OLD | NEW |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | 28 |
| 29 """Generate template values for a callback interface. | 29 """Generate template values for a callback interface. |
| 30 | 30 |
| 31 Extends IdlTypeBase with property |callback_cpp_type|. | 31 Extends IdlType with property |callback_cpp_type|. |
| 32 | 32 |
| 33 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | 33 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler |
| 34 """ | 34 """ |
| 35 | 35 |
| 36 from idl_types import IdlTypeBase | 36 from idl_types import IdlType, IdlTypeBase |
| 37 import dart_types |
| 38 from dart_utilities import DartUtilities |
| 37 from v8_globals import includes | 39 from v8_globals import includes |
| 38 import v8_types | |
| 39 import v8_utilities | |
| 40 | 40 |
| 41 CALLBACK_INTERFACE_H_INCLUDES = frozenset([ | 41 CALLBACK_INTERFACE_H_INCLUDES = frozenset([ |
| 42 'bindings/core/v8/ActiveDOMCallback.h', | 42 'bindings2/dart_callback.h', |
| 43 'bindings/core/v8/DOMWrapperWorld.h', | |
| 44 'bindings/core/v8/ScopedPersistent.h', | |
| 45 ]) | 43 ]) |
| 44 |
| 46 CALLBACK_INTERFACE_CPP_INCLUDES = frozenset([ | 45 CALLBACK_INTERFACE_CPP_INCLUDES = frozenset([ |
| 47 'bindings/core/v8/ScriptController.h', | |
| 48 'bindings/core/v8/V8Binding.h', | |
| 49 'core/dom/ExecutionContext.h', | |
| 50 'wtf/Assertions.h', | |
| 51 'wtf/GetPtr.h', | 46 'wtf/GetPtr.h', |
| 52 'wtf/RefPtr.h', | 47 'wtf/RefPtr.h', |
| 53 ]) | 48 ]) |
| 54 | 49 |
| 55 | |
| 56 def cpp_type(idl_type): | 50 def cpp_type(idl_type): |
| 57 # FIXME: remove this function by making callback types consistent | 51 # FIXME: remove this function by making callback types consistent |
| 58 # (always use usual v8_types.cpp_type) | 52 # (always use usual v8_types.cpp_type) |
| 59 idl_type_name = idl_type.name | 53 idl_type_name = idl_type.name |
| 60 if idl_type_name == 'String': | 54 if idl_type_name == 'String': |
| 61 return 'const String&' | 55 return 'const String&' |
| 62 if idl_type_name == 'void': | 56 if idl_type_name == 'void': |
| 63 return 'void' | 57 return 'void' |
| 64 # Callbacks use raw pointers, so raw_type=True | 58 # Callbacks use raw pointers, so raw_type=True |
| 65 raw_cpp_type = idl_type.cpp_type_args(raw_type=True) | 59 usual_cpp_type = idl_type.cpp_type_args(raw_type=True) |
| 66 if raw_cpp_type.startswith(('Vector', 'Vector')): | 60 if usual_cpp_type.startswith(('Vector', 'HeapVector', 'WillBeHeapVector')): |
| 67 return 'const %s&' % raw_cpp_type | 61 return 'const %s&' % usual_cpp_type |
| 68 return raw_cpp_type | 62 return usual_cpp_type |
| 69 | 63 |
| 70 IdlTypeBase.callback_cpp_type = property(cpp_type) | 64 IdlTypeBase.callback_cpp_type = property(cpp_type) |
| 71 | 65 |
| 72 | 66 |
| 73 def callback_interface_context(callback_interface): | 67 def generate_callback_interface(callback_interface): |
| 74 includes.clear() | 68 includes.clear() |
| 75 includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) | 69 includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) |
| 76 return { | 70 name = callback_interface.name |
| 77 'conditional_string': v8_utilities.conditional_string(callback_interface
), | 71 |
| 78 'cpp_class': callback_interface.name, | 72 methods = [generate_method(operation) |
| 79 'v8_class': v8_utilities.v8_class_name(callback_interface), | 73 for operation in callback_interface.operations] |
| 74 template_contents = { |
| 75 'cpp_class': name, |
| 76 'dart_class': dart_types.dart_type(callback_interface.name), |
| 80 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), | 77 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), |
| 81 'methods': [method_context(operation) | 78 'methods': methods, |
| 82 for operation in callback_interface.operations], | |
| 83 } | 79 } |
| 80 return template_contents |
| 84 | 81 |
| 85 | 82 |
| 86 def add_includes_for_operation(operation): | 83 def add_includes_for_operation(operation): |
| 87 operation.idl_type.add_includes_for_type() | 84 operation.idl_type.add_includes_for_type() |
| 88 for argument in operation.arguments: | 85 for argument in operation.arguments: |
| 89 argument.idl_type.add_includes_for_type() | 86 argument.idl_type.add_includes_for_type() |
| 90 | 87 |
| 91 | 88 |
| 92 def method_context(operation): | 89 def generate_method(operation): |
| 93 extended_attributes = operation.extended_attributes | 90 extended_attributes = operation.extended_attributes |
| 94 idl_type = operation.idl_type | 91 idl_type = operation.idl_type |
| 95 idl_type_str = str(idl_type) | 92 idl_type_str = str(idl_type) |
| 96 if idl_type_str not in ['boolean', 'void']: | 93 if idl_type_str not in ['boolean', 'void']: |
| 97 raise Exception('We only support callbacks that return boolean or void v
alues.') | 94 raise Exception('We only support callbacks that return boolean or void v
alues.') |
| 98 is_custom = 'Custom' in extended_attributes | 95 is_custom = 'Custom' in extended_attributes |
| 99 if not is_custom: | 96 if not is_custom: |
| 100 add_includes_for_operation(operation) | 97 add_includes_for_operation(operation) |
| 101 call_with = extended_attributes.get('CallWith') | 98 call_with = extended_attributes.get('CallWith') |
| 102 call_with_this_handle = v8_utilities.extended_attribute_value_contains(call_
with, 'ThisValue') | 99 call_with_this_handle = DartUtilities.extended_attribute_value_contains(call
_with, 'ThisValue') |
| 103 context = { | 100 contents = { |
| 104 'call_with_this_handle': call_with_this_handle, | 101 'call_with_this_handle': call_with_this_handle, |
| 105 'cpp_type': idl_type.callback_cpp_type, | 102 'cpp_type': idl_type.callback_cpp_type, |
| 103 'custom': is_custom, |
| 106 'idl_type': idl_type_str, | 104 'idl_type': idl_type_str, |
| 107 'is_custom': is_custom, | |
| 108 'name': operation.name, | 105 'name': operation.name, |
| 109 } | 106 } |
| 110 context.update(arguments_context(operation.arguments, | 107 contents.update(generate_arguments_contents(operation.arguments, call_with_t
his_handle)) |
| 111 call_with_this_handle)) | 108 return contents |
| 112 return context | |
| 113 | 109 |
| 114 | 110 |
| 115 def arguments_context(arguments, call_with_this_handle): | 111 def generate_arguments_contents(arguments, call_with_this_handle): |
| 116 def argument_context(argument): | 112 def generate_argument(argument): |
| 113 creation_context = '' |
| 114 if argument.idl_type.native_array_element_type is not None: |
| 115 creation_context = '<Dart%s>' % argument.idl_type.native_array_eleme
nt_type |
| 117 return { | 116 return { |
| 118 'handle': '%sHandle' % argument.name, | 117 'handle': '%sHandle' % argument.name, |
| 119 'cpp_value_to_v8_value': argument.idl_type.cpp_value_to_v8_value( | 118 'cpp_value_to_dart_value': argument.idl_type.cpp_value_to_dart_value
(argument.name, |
| 120 argument.name, isolate='m_scriptState->isolate()', | 119
creation_context=creation_context), |
| 121 creation_context='m_scriptState->context()->Global()'), | |
| 122 } | 120 } |
| 123 | 121 |
| 124 argument_declarations = ['ScriptValue thisValue'] if call_with_this_handle e
lse [] | 122 argument_declarations = [ |
| 125 argument_declarations.extend( | 123 '%s %s' % (argument.idl_type.callback_cpp_type, argument.name) |
| 126 '%s %s' % (argument.idl_type.callback_cpp_type, argument.name) | 124 for argument in arguments] |
| 127 for argument in arguments) | 125 if call_with_this_handle: |
| 126 argument_declarations.insert(0, 'ScriptValue thisValue') |
| 128 return { | 127 return { |
| 129 'argument_declarations': argument_declarations, | 128 'argument_declarations': argument_declarations, |
| 130 'arguments': [argument_context(argument) for argument in arguments], | 129 'arguments': [generate_argument(argument) for argument in arguments], |
| 131 } | 130 } |
| OLD | NEW |