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 a callback interface. | |
30 | |
31 FIXME: Not currently used in build. | |
32 This is a rewrite of the Perl IDL compiler in Python, but is not complete. | |
33 Once it is complete, we will switch all IDL files over to Python at once. | |
34 Until then, please work on the Perl IDL compiler. | |
35 For details, see bug http://crbug.com/239771 | |
36 """ | |
37 | |
38 from v8_globals import includes | |
39 import v8_types | |
40 import v8_utilities | |
41 | |
42 CALLBACK_INTERFACE_H_INCLUDES = frozenset([ | |
43 'bindings/v8/ActiveDOMCallback.h', | |
44 'bindings/v8/DOMWrapperWorld.h', | |
45 'bindings/v8/ScopedPersistent.h', | |
46 ]) | |
47 CALLBACK_INTERFACE_CPP_INCLUDES = frozenset([ | |
48 'bindings/v8/V8Binding.h', | |
49 'bindings/v8/V8Callback.h', | |
50 'core/dom/ExecutionContext.h', | |
51 'wtf/Assertions.h', | |
52 'wtf/GetPtr.h', # FIXME: remove if can eliminate WTF::getPtr | |
53 'wtf/RefPtr.h', | |
54 ]) | |
55 | |
56 | |
57 def cpp_to_v8_conversion(idl_type, name): | |
58 # FIXME: setting creation_context=v8::Handle<v8::Object>() is wrong, | |
59 # as toV8 then implicitly uses the current context, which causes leaks | |
60 # between isolate worlds if a different context should be used. | |
61 cpp_value_to_v8_value = v8_types.cpp_value_to_v8_value(idl_type, name, | |
62 isolate='m_isolate', creation_context='v8::Handle<v8::Object>()') | |
63 return 'v8::Handle<v8::Value> {name}Handle = {cpp_to_v8};'.format( | |
64 name=name, cpp_to_v8=cpp_value_to_v8_value) | |
65 | |
66 | |
67 def cpp_type(idl_type): | |
68 # FIXME: remove this function by making callback types consistent | |
69 # (always use usual v8_types.cpp_type) | |
70 if idl_type == 'DOMString': | |
71 return 'const String&' | |
72 if idl_type == 'void': | |
73 return 'void' | |
74 # Callbacks use raw pointers, so used_as_argument=True | |
75 usual_cpp_type = v8_types.cpp_type(idl_type, used_as_argument=True) | |
76 if usual_cpp_type.startswith('Vector'): | |
77 return 'const %s&' % usual_cpp_type | |
78 return usual_cpp_type | |
79 | |
80 | |
81 def generate_callback_interface(callback_interface): | |
82 includes.clear() | |
83 includes.update(CALLBACK_INTERFACE_CPP_INCLUDES) | |
84 name = callback_interface.name | |
85 | |
86 methods = [generate_method(operation) | |
87 for operation in callback_interface.operations] | |
88 template_contents = { | |
89 'conditional_string': v8_utilities.conditional_string(callback_interface
), | |
90 'cpp_class': name, | |
91 'v8_class': v8_utilities.v8_class_name(callback_interface), | |
92 'header_includes': set(CALLBACK_INTERFACE_H_INCLUDES), | |
93 'methods': methods, | |
94 } | |
95 return template_contents | |
96 | |
97 | |
98 def add_includes_for_operation(operation): | |
99 v8_types.add_includes_for_type(operation.idl_type) | |
100 for argument in operation.arguments: | |
101 v8_types.add_includes_for_type(argument.idl_type) | |
102 | |
103 | |
104 def generate_method(operation): | |
105 extended_attributes = operation.extended_attributes | |
106 idl_type = operation.idl_type | |
107 if idl_type not in ['boolean', 'void']: | |
108 raise Exception('We only support callbacks that return boolean or void v
alues.') | |
109 is_custom = 'Custom' in extended_attributes | |
110 if not is_custom: | |
111 add_includes_for_operation(operation) | |
112 call_with = extended_attributes.get('CallWith') | |
113 call_with_this_handle = v8_utilities.extended_attribute_value_contains(call_
with, 'ThisValue') | |
114 contents = { | |
115 'call_with_this_handle': call_with_this_handle, | |
116 'custom': is_custom, | |
117 'name': operation.name, | |
118 'return_cpp_type': cpp_type(idl_type), | |
119 'return_idl_type': idl_type, | |
120 } | |
121 contents.update(generate_arguments_contents(operation.arguments, call_with_t
his_handle)) | |
122 return contents | |
123 | |
124 | |
125 def generate_arguments_contents(arguments, call_with_this_handle): | |
126 def generate_argument(argument): | |
127 return { | |
128 'name': argument.name, | |
129 'cpp_to_v8_conversion': cpp_to_v8_conversion(argument.idl_type, argu
ment.name), | |
130 } | |
131 | |
132 argument_declarations = [ | |
133 '%s %s' % (cpp_type(argument.idl_type), argument.name) | |
134 for argument in arguments] | |
135 if call_with_this_handle: | |
136 argument_declarations.insert(0, 'ScriptValue thisValue') | |
137 return { | |
138 'argument_declarations': argument_declarations, | |
139 'arguments': [generate_argument(argument) for argument in arguments], | |
140 'handles': ['%sHandle' % argument.name for argument in arguments], | |
141 } | |
OLD | NEW |