Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: third_party/WebKit/Source/bindings/scripts/v8_callback_function.py

Issue 2312093003: Generated bindings for IDL callback functions (Closed)
Patch Set: Changed BUILD.gn Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 1 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 4 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 5 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 6 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 7 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 8 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 9 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 10 # (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. 11 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 12
29 """Generate template values for a callback interface. 13 """Generate template values for a callback function.
30 14
31 Extends IdlTypeBase with property |callback_cpp_type|. 15 Extends IdlTypeBase with property |callback_cpp_type|.
32 16
33 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler 17 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler
34 """ 18 """
35 19
36 from idl_types import IdlTypeBase 20 from idl_types import IdlTypeBase
37 from v8_globals import includes 21 from v8_globals import includes
38 import v8_types 22 import v8_types
39 import v8_utilities 23 import v8_utilities
40 24
41 CALLBACK_INTERFACE_H_INCLUDES = frozenset([ 25 CALLBACK_FUNCTION_H_INCLUDES = frozenset([
42 'bindings/core/v8/ActiveDOMCallback.h', 26 'bindings/core/v8/ActiveDOMCallback.h',
43 'bindings/core/v8/DOMWrapperWorld.h', 27 'bindings/core/v8/DOMWrapperWorld.h',
44 'bindings/core/v8/ScopedPersistent.h', 28 'bindings/core/v8/ScopedPersistent.h',
Yuki 2016/09/07 10:04:02 I think it's fine to start with copy&pasting, but
lkawai 2016/09/08 09:11:31 Done.
45 ]) 29 ])
46 CALLBACK_INTERFACE_CPP_INCLUDES = frozenset([ 30 CALLBACK_FUNCTION_CPP_INCLUDES = frozenset([
47 'bindings/core/v8/ScriptController.h', 31 'bindings/core/v8/ScriptController.h',
48 'bindings/core/v8/V8Binding.h', 32 'bindings/core/v8/V8Binding.h',
49 'core/dom/ExecutionContext.h', 33 'bindings/core/v8/V8PrivateProperty.h',
50 'wtf/Assertions.h', 34 'wtf/Assertions.h',
Yuki 2016/09/07 10:04:02 Ditto. I think we don't need ScriptController.h a
lkawai 2016/09/08 09:11:31 Done.
51 'wtf/GetPtr.h',
52 'wtf/RefPtr.h',
53 ]) 35 ])
54 36
55 37
56 def cpp_type(idl_type): 38 def cpp_type(idl_type):
57 # FIXME: remove this function by making callback types consistent 39 # FIXME: remove this function by making callback types consistent
58 # (always use usual v8_types.cpp_type) 40 # (always use usual v8_types.cpp_type)
59 idl_type_name = idl_type.name 41 idl_type_name = idl_type.name
60 if idl_type_name == 'String': 42 if idl_type_name == 'String':
61 return 'const String&' 43 return 'const String&'
62 if idl_type_name == 'void': 44 if idl_type_name == 'void':
63 return 'void' 45 return 'void'
64 # Callbacks use raw pointers, so raw_type=True 46 # Callbacks use raw pointers, so raw_type=True
65 raw_cpp_type = idl_type.cpp_type_args(raw_type=True) 47 raw_cpp_type = idl_type.cpp_type_args(raw_type=True)
66 # Pass containers and dictionaries to callback method by const reference rat her than by value 48 # Pass containers and dictionaries to callback method by const reference rat her than by value
67 if raw_cpp_type.startswith(('Vector', 'HeapVector')) or idl_type.is_dictiona ry: 49 if raw_cpp_type.startswith(('Vector', 'HeapVector')) or idl_type.is_dictiona ry:
68 return 'const %s&' % raw_cpp_type 50 return 'const %s&' % raw_cpp_type
69 return raw_cpp_type 51 return raw_cpp_type
70 52
71 IdlTypeBase.callback_cpp_type = property(cpp_type) 53 IdlTypeBase.callback_cpp_type = property(cpp_type)
Yuki 2016/09/07 10:04:02 v8_callback_interface.py already updated this prop
lkawai 2016/09/08 09:11:31 Done.
72 54
73 55
74 def callback_interface_context(callback_interface): 56 def callback_function_context(callback_function):
75 includes.clear() 57 includes.clear()
76 includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) 58 includes.update(CALLBACK_FUNCTION_CPP_INCLUDES)
77 return { 59 idl_type = callback_function.idl_type
78 'cpp_class': callback_interface.name, 60 idl_type_str = str(idl_type)
79 'v8_class': v8_utilities.v8_class_name(callback_interface), 61
80 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), 62 def member_cpp_type():
81 'methods': [method_context(operation) 63 member_cpp_type = idl_type.cpp_type_args(used_in_cpp_sequence=True)
82 for operation in callback_interface.operations], 64 if idl_type.impl_should_use_nullable_container:
65 return v8_types.cpp_template_type('Nullable', member_cpp_type)
66 return member_cpp_type
67 context = {
68 'cpp_class': callback_function.name,
69 'v8_class': v8_utilities.v8_class_name(callback_function),
70 'header_includes': set(CALLBACK_FUNCTION_H_INCLUDES),
71 'cpp_includes': set(CALLBACK_FUNCTION_CPP_INCLUDES),
72 'rvalue_cpp_type': idl_type.callback_cpp_type,
73 'idl_type': idl_type_str,
74 'member_cpp_type': member_cpp_type(),
75 'v8_value_to_local_cpp_value_dict': idl_type.v8_value_to_local_cpp_value (
76 callback_function.extended_attributes, 'currentValue', 'cppValue', b ailout_return_value="false"),
83 } 77 }
84 78 context.update(arguments_context(callback_function.arguments))
85
86 def add_includes_for_operation(operation):
87 operation.idl_type.add_includes_for_type()
88 for argument in operation.arguments:
89 argument.idl_type.add_includes_for_type()
90
91
92 def method_context(operation):
93 extended_attributes = operation.extended_attributes
94 idl_type = operation.idl_type
95 idl_type_str = str(idl_type)
96 if idl_type_str not in ['boolean', 'void']:
97 raise Exception('We only support callbacks that return boolean or void v alues.')
98 is_custom = 'Custom' in extended_attributes
99 if not is_custom:
100 add_includes_for_operation(operation)
101 call_with = extended_attributes.get('CallWith')
102 call_with_this_handle = v8_utilities.extended_attribute_value_contains(call_ with, 'ThisValue')
103 context = {
104 'call_with_this_handle': call_with_this_handle,
105 'cpp_type': idl_type.callback_cpp_type,
106 'idl_type': idl_type_str,
107 'is_custom': is_custom,
108 'name': operation.name,
109 }
110 context.update(arguments_context(operation.arguments,
111 call_with_this_handle))
112 return context 79 return context
113 80
114 81
115 def arguments_context(arguments, call_with_this_handle): 82 def arguments_context(arguments):
116 def argument_context(argument): 83 def argument_context(argument):
117 return { 84 return {
118 'handle': '%sHandle' % argument.name, 85 'handle': '%sHandle' % argument.name,
119 'cpp_value_to_v8_value': argument.idl_type.cpp_value_to_v8_value( 86 'cpp_value_to_v8_value': argument.idl_type.cpp_value_to_v8_value(
120 argument.name, isolate='m_scriptState->isolate()', 87 argument.name, isolate='m_scriptState->isolate()',
121 creation_context='m_scriptState->context()->Global()'), 88 creation_context='m_scriptState->context()->Global()'),
122 } 89 }
123 90
124 argument_declarations = ['ScriptValue thisValue'] if call_with_this_handle e lse [] 91 argument_declarations = ['ScriptValue thisValue']
125 argument_declarations.extend( 92 argument_declarations.extend(
126 '%s %s' % (argument.idl_type.callback_cpp_type, argument.name) 93 '%s %s' % (argument.idl_type.callback_cpp_type, argument.name)
127 for argument in arguments) 94 for argument in arguments)
128 return { 95 return {
129 'argument_declarations': argument_declarations, 96 'argument_declarations': argument_declarations,
130 'arguments': [argument_context(argument) for argument in arguments], 97 'arguments': [argument_context(argument) for argument in arguments],
131 } 98 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698