| 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 from v8_types import cpp_type, cpp_value_to_v8_value, includes_for_type |  | 
| 32 from v8_utilities import v8_class_name |  | 
| 33 |  | 
| 34 CALLBACK_INTERFACE_H_INCLUDES = set([ |  | 
| 35     'bindings/v8/ActiveDOMCallback.h', |  | 
| 36     'bindings/v8/DOMWrapperWorld.h', |  | 
| 37     'bindings/v8/ScopedPersistent.h', |  | 
| 38 ]) |  | 
| 39 CALLBACK_INTERFACE_CPP_INCLUDES = set([ |  | 
| 40     'core/dom/ScriptExecutionContext.h', |  | 
| 41     'bindings/v8/V8Binding.h', |  | 
| 42     'bindings/v8/V8Callback.h', |  | 
| 43     'wtf/Assertions.h', |  | 
| 44 ]) |  | 
| 45 CPP_TO_V8_CONVERSION = 'v8::Handle<v8::Value> {name}Handle = {cpp_value_to_v8_va
     lue};' |  | 
| 46 |  | 
| 47 |  | 
| 48 def cpp_to_v8_conversion(idl_type, name): |  | 
| 49     this_cpp_value_to_v8_value = cpp_value_to_v8_value(idl_type, name, 'isolate'
     , creation_context='v8::Handle<v8::Object>()') |  | 
| 50     return CPP_TO_V8_CONVERSION.format(name=name, cpp_value_to_v8_value=this_cpp
     _value_to_v8_value) |  | 
| 51 |  | 
| 52 |  | 
| 53 def generate_callback_interface(callback_interface): |  | 
| 54     cpp_includes = CALLBACK_INTERFACE_CPP_INCLUDES |  | 
| 55 |  | 
| 56     def generate_method(operation): |  | 
| 57         method_contents, method_cpp_includes = generate_method_and_includes(oper
     ation) |  | 
| 58         cpp_includes.update(method_cpp_includes) |  | 
| 59         return method_contents |  | 
| 60 |  | 
| 61     methods = [generate_method(operation) for operation in callback_interface.op
     erations] |  | 
| 62     template_contents = { |  | 
| 63         'cpp_class_name': callback_interface.name, |  | 
| 64         'v8_class_name': v8_class_name(callback_interface), |  | 
| 65         'header_includes': CALLBACK_INTERFACE_H_INCLUDES, |  | 
| 66         'cpp_includes': cpp_includes, |  | 
| 67         'methods': methods, |  | 
| 68     } |  | 
| 69     return template_contents |  | 
| 70 |  | 
| 71 |  | 
| 72 def generate_method_and_includes(operation): |  | 
| 73     if 'Custom' in operation.extended_attributes: |  | 
| 74         return generate_method_contents(operation), [] |  | 
| 75     if operation.data_type != 'boolean': |  | 
| 76         raise Exception("We don't yet support callbacks that return non-boolean 
     values.") |  | 
| 77     cpp_includes = includes_for_operation(operation) |  | 
| 78     return generate_method_contents(operation), cpp_includes |  | 
| 79 |  | 
| 80 |  | 
| 81 def includes_for_operation(operation): |  | 
| 82     includes = includes_for_type(operation.data_type) |  | 
| 83     for argument in operation.arguments: |  | 
| 84         includes.update(includes_for_type(argument.data_type)) |  | 
| 85     return includes |  | 
| 86 |  | 
| 87 |  | 
| 88 def generate_method_contents(operation): |  | 
| 89     contents = { |  | 
| 90         'name': operation.name, |  | 
| 91         'return_cpp_type': cpp_type(operation.data_type, 'RefPtr'), |  | 
| 92         'custom': 'Custom' in operation.extended_attributes, |  | 
| 93     } |  | 
| 94     contents.update(generate_arguments_contents(operation.arguments)) |  | 
| 95     return contents |  | 
| 96 |  | 
| 97 |  | 
| 98 def generate_arguments_contents(arguments): |  | 
| 99     def argument_declaration(argument): |  | 
| 100         return '%s %s' % (cpp_type(argument.data_type), argument.name) |  | 
| 101 |  | 
| 102     def generate_argument(argument): |  | 
| 103         return { |  | 
| 104             'name': argument.name, |  | 
| 105             'cpp_to_v8_conversion': cpp_to_v8_conversion(argument.data_type, arg
     ument.name), |  | 
| 106         } |  | 
| 107 |  | 
| 108     return  { |  | 
| 109         'argument_declarations': [argument_declaration(argument) for argument in
      arguments], |  | 
| 110         'arguments': [generate_argument(argument) for argument in arguments], |  | 
| 111         'handles': ['%sHandle' % argument.name for argument in arguments], |  | 
| 112     } |  | 
| OLD | NEW | 
|---|