| 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 import re | |
| 30 | |
| 31 CPP_TYPE_SAME_AS_IDL_TYPE = set([ | |
| 32 'double', | |
| 33 'float', | |
| 34 'int', # FIXME: int is not an IDL type | |
| 35 'long long', | |
| 36 'unsigned long long', | |
| 37 ]) | |
| 38 CPP_INT_TYPES = set([ | |
| 39 'byte', | |
| 40 'long', | |
| 41 'short', | |
| 42 ]) | |
| 43 CPP_UNSIGNED_TYPES = set([ | |
| 44 'octet', | |
| 45 'unsigned int', | |
| 46 'unsigned long', | |
| 47 'unsigned short', | |
| 48 ]) | |
| 49 # Promise is not yet in the Web IDL spec but is going to be speced | |
| 50 # as primitive types in the future. | |
| 51 # Since V8 dosn't provide Promise primitive object currently, | |
| 52 # PRIMITIVE_TYPES doesn't contain Promise. | |
| 53 CPP_TYPE_SPECIAL_CONVERSION_RULES = { | |
| 54 'boolean': 'bool', | |
| 55 'DOMString': 'const String&', | |
| 56 'Promise': 'ScriptPromise', | |
| 57 } | |
| 58 PRIMITIVE_TYPES = set([ | |
| 59 # http://www.w3.org/TR/WebIDL/#dfn-primitive-type | |
| 60 'boolean', | |
| 61 'float', | |
| 62 # unrestricted float is not supported | |
| 63 'double', | |
| 64 # unrestricted double is not supported | |
| 65 # integer types | |
| 66 # http://www.w3.org/TR/WebIDL/#dfn-integer-type | |
| 67 'byte', | |
| 68 'octet', | |
| 69 'short', | |
| 70 'unsigned short', | |
| 71 # int and unsigned are not IDL types | |
| 72 'long', | |
| 73 'unsigned long', | |
| 74 'long long', | |
| 75 'unsigned long long', | |
| 76 # Blink-specific additions | |
| 77 'Date', | |
| 78 'void', | |
| 79 ]) | |
| 80 CPP_VALUE_TO_V8_VALUE_DICT = { | |
| 81 'boolean': 'v8Boolean({cpp_value}, {isolate})', | |
| 82 # long long and unsigned long long are not representable in ECMAScript. | |
| 83 'long long': 'v8::Number::New({isolate}, static_cast<double>({cpp_value}))', | |
| 84 'unsigned long long': 'v8::Number::New({isolate}, static_cast<double>({cpp_v
alue}))', | |
| 85 'float': 'v8::Number::New({isolate}, {cpp_value})', | |
| 86 'double': 'v8::Number::New({isolate}, {cpp_value})', | |
| 87 'DOMTimeStamp': 'v8::Number::New({isolate}, static_cast<double>({cpp_value})
)', | |
| 88 'DOMString': 'v8String({cpp_value}, {isolate})', | |
| 89 } | |
| 90 CPP_VALUE_TO_V8_VALUE_ARRAY_OR_SEQUENCE_TYPE = 'v8Array({cpp_value}, {isolate})' | |
| 91 CPP_VALUE_TO_V8_VALUE_DEFAULT = 'toV8({cpp_value}, {creation_context}, {isolate}
)' | |
| 92 V8_SET_RETURN_VALUE_DICT = { | |
| 93 'unsigned': 'v8SetReturnValueUnsigned({callback_info}, {cpp_value});', | |
| 94 } | |
| 95 | |
| 96 | |
| 97 def array_or_sequence_type(idl_type): | |
| 98 return array_type(idl_type) or sequence_type(idl_type) | |
| 99 | |
| 100 | |
| 101 def array_type(idl_type): | |
| 102 matched = re.match(r'([\w\s]+)\[\]', idl_type) | |
| 103 return matched and matched.group(1) | |
| 104 | |
| 105 | |
| 106 def primitive_type(idl_type): | |
| 107 return idl_type in PRIMITIVE_TYPES | |
| 108 | |
| 109 | |
| 110 def sequence_type(idl_type): | |
| 111 matched = re.match(r'sequence<([\w\s]+)>', idl_type) | |
| 112 return matched and matched.group(1) | |
| 113 | |
| 114 | |
| 115 def v8_type(interface_type): | |
| 116 return 'V8' + interface_type | |
| 117 | |
| 118 | |
| 119 def cpp_type(idl_type, pointer_type=None): | |
| 120 """Returns the C++ type corresponding to the IDL type. | |
| 121 | |
| 122 If unidentified, fall back to a pointer. | |
| 123 | |
| 124 Args: | |
| 125 idl_type: IDL type | |
| 126 pointer_type: If specified, return 'pointer_type<idl_type>' | |
| 127 (e.g. RefPtr<Foo>, PassRefPtr<Foo>) | |
| 128 else defaults to returning raw pointer form (e.g. 'Foo*'). | |
| 129 """ | |
| 130 if idl_type in CPP_TYPE_SAME_AS_IDL_TYPE: | |
| 131 return idl_type | |
| 132 if idl_type in CPP_INT_TYPES: | |
| 133 return 'int' | |
| 134 if idl_type in CPP_UNSIGNED_TYPES: | |
| 135 return 'unsigned' | |
| 136 if idl_type in CPP_TYPE_SPECIAL_CONVERSION_RULES: | |
| 137 return CPP_TYPE_SPECIAL_CONVERSION_RULES[idl_type] | |
| 138 this_array_or_sequence_type = array_or_sequence_type(idl_type) | |
| 139 if this_array_or_sequence_type: | |
| 140 return 'const Vector<{cpp_type} >&'.format(cpp_type=cpp_type(this_array_
or_sequence_type, 'RefPtr')) | |
| 141 if pointer_type: | |
| 142 return '{pointer_type}<{idl_type}>'.format(pointer_type=pointer_type, id
l_type=idl_type) | |
| 143 return idl_type + '*' # raw pointer | |
| 144 | |
| 145 | |
| 146 def cpp_value_to_v8_value(idl_type, cpp_value, isolate, creation_context=''): | |
| 147 """Returns an expression converting a C++ value to a V8 value.""" | |
| 148 if idl_type in CPP_VALUE_TO_V8_VALUE_DICT: | |
| 149 expression_format_string = CPP_VALUE_TO_V8_VALUE_DICT[idl_type] | |
| 150 elif primitive_type(idl_type): # primitive but not in dict | |
| 151 raise Exception('unexpected IDL type %s' % idl_type) | |
| 152 elif array_or_sequence_type(idl_type): | |
| 153 expression_format_string = CPP_VALUE_TO_V8_VALUE_ARRAY_OR_SEQUENCE_TYPE | |
| 154 else: | |
| 155 expression_format_string = CPP_VALUE_TO_V8_VALUE_DEFAULT | |
| 156 return expression_format_string.format(cpp_value=cpp_value, creation_context
=creation_context, isolate=isolate) | |
| 157 | |
| 158 | |
| 159 def v8_set_return_value(idl_type, cpp_value, callback_info=''): | |
| 160 """Returns a statement converting a C++ value to a V8 value and setting it a
s a return value.""" | |
| 161 this_cpp_type = cpp_type(idl_type) | |
| 162 if this_cpp_type in V8_SET_RETURN_VALUE_DICT: | |
| 163 expression_format_string = V8_SET_RETURN_VALUE_DICT[this_cpp_type] | |
| 164 else: | |
| 165 raise Exception('unexpected IDL type %s' % idl_type) | |
| 166 return expression_format_string.format(callback_info=callback_info, cpp_valu
e=cpp_value) | |
| 167 | |
| 168 | |
| 169 def includes_for_type(idl_type): | |
| 170 if primitive_type(idl_type) or idl_type == 'DOMString': | |
| 171 return set() | |
| 172 if idl_type == 'Promise': | |
| 173 return set(['ScriptPromise.h']) | |
| 174 this_array_or_sequence_type = array_or_sequence_type(idl_type) | |
| 175 if this_array_or_sequence_type: | |
| 176 return includes_for_type(this_array_or_sequence_type) | |
| 177 return set(['V8%s.h' % idl_type]) | |
| OLD | NEW |