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

Side by Side Diff: Source/bindings/scripts/v8_types.py

Issue 345893002: Implement an infrastructure of Blink-in-JS Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 'Promise': 'ScriptPromise', 112 'Promise': 'ScriptPromise',
113 'ScriptValue': 'ScriptValue', 113 'ScriptValue': 'ScriptValue',
114 # FIXME: Eliminate custom bindings for XPathNSResolver http://crbug.com/345 529 114 # FIXME: Eliminate custom bindings for XPathNSResolver http://crbug.com/345 529
115 'XPathNSResolver': 'RefPtrWillBeRawPtr<XPathNSResolver>', 115 'XPathNSResolver': 'RefPtrWillBeRawPtr<XPathNSResolver>',
116 'boolean': 'bool', 116 'boolean': 'bool',
117 'unrestricted double': 'double', 117 'unrestricted double': 'double',
118 'unrestricted float': 'float', 118 'unrestricted float': 'float',
119 } 119 }
120 120
121 121
122 def cpp_type(idl_type, extended_attributes=None, used_as_argument=False, used_as _variadic_argument=False, used_in_cpp_sequence=False): 122 def cpp_type(idl_type, extended_attributes=None, used_as_primitive_type=False, u sed_as_argument=False, used_as_variadic_argument=False, used_in_cpp_sequence=Fal se):
123 """Returns C++ type corresponding to IDL type. 123 """Returns C++ type corresponding to IDL type.
124 124
125 |idl_type| argument is of type IdlType, while return value is a string 125 |idl_type| argument is of type IdlType, while return value is a string
126 126
127 Args: 127 Args:
128 idl_type: 128 idl_type:
129 IdlType 129 IdlType
130 used_as_primitive_type:
Nils Barth (inactive) 2014/06/23 03:14:29 "raw type" sounds better Also, generally "primiti
131 bool, True if idl_type's raw/primitive C++ type should be returned.
130 used_as_argument: 132 used_as_argument:
131 bool, True if idl_type's raw/primitive C++ type should be returned. 133 bool, True if the C++ type is used as an argument of a method.
134 used_as_variadic_argument:
135 bool, True if the C++ type is used as a variadic argument of a metho d.
132 used_in_cpp_sequence: 136 used_in_cpp_sequence:
133 bool, True if the C++ type is used as an element of an array or sequ ence. 137 bool, True if the C++ type is used as an element of an array or sequ ence.
134 """ 138 """
135 def string_mode(): 139 def string_mode():
136 # FIXME: the Web IDL spec requires 'EmptyString', not 'NullString', 140 # FIXME: the Web IDL spec requires 'EmptyString', not 'NullString',
137 # but we use NullString for performance. 141 # but we use NullString for performance.
138 if extended_attributes.get('TreatNullAs') != 'NullString': 142 if extended_attributes.get('TreatNullAs') != 'NullString':
139 return '' 143 return ''
140 if extended_attributes.get('TreatUndefinedAs') != 'NullString': 144 if extended_attributes.get('TreatUndefinedAs') != 'NullString':
141 return 'WithNullCheck' 145 return 'WithNullCheck'
(...skipping 17 matching lines...) Expand all
159 if base_idl_type in CPP_TYPE_SAME_AS_IDL_TYPE: 163 if base_idl_type in CPP_TYPE_SAME_AS_IDL_TYPE:
160 return base_idl_type 164 return base_idl_type
161 if base_idl_type in CPP_INT_TYPES: 165 if base_idl_type in CPP_INT_TYPES:
162 return 'int' 166 return 'int'
163 if base_idl_type in CPP_UNSIGNED_TYPES: 167 if base_idl_type in CPP_UNSIGNED_TYPES:
164 return 'unsigned' 168 return 'unsigned'
165 if base_idl_type in CPP_SPECIAL_CONVERSION_RULES: 169 if base_idl_type in CPP_SPECIAL_CONVERSION_RULES:
166 return CPP_SPECIAL_CONVERSION_RULES[base_idl_type] 170 return CPP_SPECIAL_CONVERSION_RULES[base_idl_type]
167 171
168 if base_idl_type in NON_WRAPPER_TYPES: 172 if base_idl_type in NON_WRAPPER_TYPES:
169 return 'RefPtr<%s>' % base_idl_type 173 return ('PassRefPtr<%s>' if used_as_argument else 'RefPtr<%s>') % base_i dl_type
170 if base_idl_type in ('DOMString', 'ByteString', 'ScalarValueString'): 174 if base_idl_type in ('DOMString', 'ByteString', 'ScalarValueString'):
171 if not used_as_argument: 175 if not used_as_primitive_type:
172 return 'String' 176 return 'String'
173 return 'V8StringResource<%s>' % string_mode() 177 return 'V8StringResource<%s>' % string_mode()
174 178
175 if idl_type.is_typed_array_type and used_as_argument: 179 if idl_type.is_typed_array_type and used_as_primitive_type:
176 return base_idl_type + '*' 180 return base_idl_type + '*'
177 if idl_type.is_interface_type: 181 if idl_type.is_interface_type:
178 implemented_as_class = idl_type.implemented_as 182 implemented_as_class = idl_type.implemented_as
179 if used_as_argument: 183 if used_as_primitive_type:
180 return implemented_as_class + '*' 184 return implemented_as_class + '*'
181 new_type = 'Member' if used_in_cpp_sequence else 'RawPtr' 185 new_type = 'Member' if used_in_cpp_sequence else 'RawPtr'
182 ptr_type = cpp_ptr_type('RefPtr', new_type, idl_type.gc_type) 186 ptr_type = cpp_ptr_type(('PassRefPtr' if used_as_argument else 'RefPtr') , new_type, idl_type.gc_type)
183 return cpp_template_type(ptr_type, implemented_as_class) 187 return cpp_template_type(ptr_type, implemented_as_class)
184 # Default, assume native type is a pointer with same type name as idl type 188 # Default, assume native type is a pointer with same type name as idl type
185 return base_idl_type + '*' 189 return base_idl_type + '*'
186 190
187 191
188 def cpp_type_union(idl_type, extended_attributes=None, used_as_argument=False): 192 def cpp_type_union(idl_type, extended_attributes=None):
189 return (member_type.cpp_type for member_type in idl_type.member_types) 193 return (member_type.cpp_type for member_type in idl_type.member_types)
190 194
191 195
192 # Allow access as idl_type.cpp_type if no arguments 196 # Allow access as idl_type.cpp_type if no arguments
193 IdlType.cpp_type = property(cpp_type) 197 IdlType.cpp_type = property(cpp_type)
194 IdlUnionType.cpp_type = property(cpp_type_union) 198 IdlUnionType.cpp_type = property(cpp_type_union)
195 IdlType.cpp_type_args = cpp_type 199 IdlType.cpp_type_args = cpp_type
196 IdlUnionType.cpp_type_args = cpp_type_union 200 IdlUnionType.cpp_type_args = cpp_type_union
197 201
198 202
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 'byte': 'toInt8({arguments})', 389 'byte': 'toInt8({arguments})',
386 'octet': 'toUInt8({arguments})', 390 'octet': 'toUInt8({arguments})',
387 'short': 'toInt16({arguments})', 391 'short': 'toInt16({arguments})',
388 'unsigned short': 'toUInt16({arguments})', 392 'unsigned short': 'toUInt16({arguments})',
389 'long': 'toInt32({arguments})', 393 'long': 'toInt32({arguments})',
390 'unsigned long': 'toUInt32({arguments})', 394 'unsigned long': 'toUInt32({arguments})',
391 'long long': 'toInt64({arguments})', 395 'long long': 'toInt64({arguments})',
392 'unsigned long long': 'toUInt64({arguments})', 396 'unsigned long long': 'toUInt64({arguments})',
393 # Interface types 397 # Interface types
394 'CompareHow': 'static_cast<Range::CompareHow>({v8_value}->Int32Value())', 398 'CompareHow': 'static_cast<Range::CompareHow>({v8_value}->Int32Value())',
395 'Dictionary': 'Dictionary({v8_value}, info.GetIsolate())', 399 'Dictionary': 'Dictionary({v8_value}, {isolate})',
396 'EventTarget': 'V8DOMWrapper::isDOMWrapper({v8_value}) ? toWrapperTypeInfo(v 8::Handle<v8::Object>::Cast({v8_value}))->toEventTarget(v8::Handle<v8::Object>:: Cast({v8_value})) : 0', 400 'EventTarget': 'V8DOMWrapper::isDOMWrapper({v8_value}) ? toWrapperTypeInfo(v 8::Handle<v8::Object>::Cast({v8_value}))->toEventTarget(v8::Handle<v8::Object>:: Cast({v8_value})) : 0',
397 'MediaQueryListListener': 'MediaQueryListListener::create(ScriptState::curre nt(info.GetIsolate()), ScriptValue(ScriptState::current(info.GetIsolate()), {v8_ value}))', 401 'MediaQueryListListener': 'MediaQueryListListener::create(ScriptState::curre nt({isolate}), ScriptValue(ScriptState::current({isolate}), {v8_value}))',
398 'NodeFilter': 'toNodeFilter({v8_value}, info.Holder(), ScriptState::current( info.GetIsolate()))', 402 'NodeFilter': 'toNodeFilter({v8_value}, info.Holder(), ScriptState::current( {isolate}))',
399 'Promise': 'ScriptPromise::cast(ScriptState::current(info.GetIsolate()), {v8 _value})', 403 'Promise': 'ScriptPromise::cast(ScriptState::current({isolate}), {v8_value}) ',
400 'SerializedScriptValue': 'SerializedScriptValue::create({v8_value}, info.Get Isolate())', 404 'SerializedScriptValue': 'SerializedScriptValue::create({v8_value}, {isolate })',
401 'ScriptValue': 'ScriptValue(ScriptState::current(info.GetIsolate()), {v8_val ue})', 405 'ScriptValue': 'ScriptValue(ScriptState::current({isolate}), {v8_value})',
402 'Window': 'toDOMWindow({v8_value}, info.GetIsolate())', 406 'Window': 'toDOMWindow({v8_value}, {isolate})',
403 'XPathNSResolver': 'toXPathNSResolver({v8_value}, info.GetIsolate())', 407 'XPathNSResolver': 'toXPathNSResolver({v8_value}, {isolate})',
404 } 408 }
405 409
406 410
407 def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index): 411 def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index, isolat e='info.GetIsolate()'):
408 # Composite types 412 # Composite types
409 array_or_sequence_type = idl_type.array_or_sequence_type 413 array_or_sequence_type = idl_type.array_or_sequence_type
410 if array_or_sequence_type: 414 if array_or_sequence_type:
411 return v8_value_to_cpp_value_array_or_sequence(array_or_sequence_type, v 8_value, index) 415 return v8_value_to_cpp_value_array_or_sequence(array_or_sequence_type, v 8_value, index)
412 416
413 # Simple types 417 # Simple types
414 idl_type = idl_type.preprocessed_type 418 idl_type = idl_type.preprocessed_type
415 add_includes_for_type(idl_type) 419 add_includes_for_type(idl_type)
416 base_idl_type = idl_type.base_type 420 base_idl_type = idl_type.base_type
417 421
418 if 'EnforceRange' in extended_attributes: 422 if 'EnforceRange' in extended_attributes:
419 arguments = ', '.join([v8_value, 'EnforceRange', 'exceptionState']) 423 arguments = ', '.join([v8_value, 'EnforceRange', 'exceptionState'])
420 elif (idl_type.is_integer_type or # NormalConversion 424 elif (idl_type.is_integer_type or # NormalConversion
421 idl_type.name in ('ByteString', 'ScalarValueString')): 425 idl_type.name in ('ByteString', 'ScalarValueString')):
422 arguments = ', '.join([v8_value, 'exceptionState']) 426 arguments = ', '.join([v8_value, 'exceptionState'])
423 else: 427 else:
424 arguments = v8_value 428 arguments = v8_value
425 429
426 if base_idl_type in V8_VALUE_TO_CPP_VALUE: 430 if base_idl_type in V8_VALUE_TO_CPP_VALUE:
427 cpp_expression_format = V8_VALUE_TO_CPP_VALUE[base_idl_type] 431 cpp_expression_format = V8_VALUE_TO_CPP_VALUE[base_idl_type]
428 elif idl_type.is_typed_array_type: 432 elif idl_type.is_typed_array_type:
429 cpp_expression_format = ( 433 cpp_expression_format = (
430 '{v8_value}->Is{idl_type}() ? ' 434 '{v8_value}->Is{idl_type}() ? '
431 'V8{idl_type}::toNative(v8::Handle<v8::{idl_type}>::Cast({v8_value}) ) : 0') 435 'V8{idl_type}::toNative(v8::Handle<v8::{idl_type}>::Cast({v8_value}) ) : 0')
432 else: 436 else:
433 cpp_expression_format = ( 437 cpp_expression_format = (
434 'V8{idl_type}::toNativeWithTypeCheck(info.GetIsolate(), {v8_value})' ) 438 'V8{idl_type}::toNativeWithTypeCheck({isolate}, {v8_value})')
435 439
436 return cpp_expression_format.format(arguments=arguments, idl_type=base_idl_t ype, v8_value=v8_value) 440 return cpp_expression_format.format(arguments=arguments, idl_type=base_idl_t ype, v8_value=v8_value, isolate=isolate)
437 441
438 442
439 def v8_value_to_cpp_value_array_or_sequence(array_or_sequence_type, v8_value, in dex): 443 def v8_value_to_cpp_value_array_or_sequence(array_or_sequence_type, v8_value, in dex, isolate='info.GetIsolate()'):
440 # Index is None for setters, index (starting at 0) for method arguments, 444 # Index is None for setters, index (starting at 0) for method arguments,
441 # and is used to provide a human-readable exception message 445 # and is used to provide a human-readable exception message
442 if index is None: 446 if index is None:
443 index = 0 # special case, meaning "setter" 447 index = 0 # special case, meaning "setter"
444 else: 448 else:
445 index += 1 # human-readable index 449 index += 1 # human-readable index
446 if (array_or_sequence_type.is_interface_type and 450 if (array_or_sequence_type.is_interface_type and
447 array_or_sequence_type.name != 'Dictionary'): 451 array_or_sequence_type.name != 'Dictionary'):
448 this_cpp_type = None 452 this_cpp_type = None
449 ref_ptr_type = cpp_ptr_type('RefPtr', 'Member', array_or_sequence_type.g c_type) 453 ref_ptr_type = cpp_ptr_type('RefPtr', 'Member', array_or_sequence_type.g c_type)
450 expression_format = '(to{ref_ptr_type}NativeArray<{array_or_sequence_typ e}, V8{array_or_sequence_type}>({v8_value}, {index}, info.GetIsolate()))' 454 expression_format = '(to{ref_ptr_type}NativeArray<{array_or_sequence_typ e}, V8{array_or_sequence_type}>({v8_value}, {index}, {isolate}))'
451 add_includes_for_type(array_or_sequence_type) 455 add_includes_for_type(array_or_sequence_type)
452 else: 456 else:
453 ref_ptr_type = None 457 ref_ptr_type = None
454 this_cpp_type = array_or_sequence_type.cpp_type 458 this_cpp_type = array_or_sequence_type.cpp_type
455 expression_format = 'toNativeArray<{cpp_type}>({v8_value}, {index}, info .GetIsolate())' 459 expression_format = 'toNativeArray<{cpp_type}>({v8_value}, {index}, {iso late})'
456 expression = expression_format.format(array_or_sequence_type=array_or_sequen ce_type.name, cpp_type=this_cpp_type, index=index, ref_ptr_type=ref_ptr_type, v8 _value=v8_value) 460 expression = expression_format.format(array_or_sequence_type=array_or_sequen ce_type.name, cpp_type=this_cpp_type, index=index, ref_ptr_type=ref_ptr_type, v8 _value=v8_value, isolate=isolate)
457 return expression 461 return expression
458 462
459 463
460 def v8_value_to_local_cpp_value(idl_type, extended_attributes, v8_value, variabl e_name, index=None, declare_variable=True): 464 def v8_value_to_local_cpp_value(idl_type, extended_attributes, v8_value, variabl e_name, index=None, declare_variable=True):
461 """Returns an expression that converts a V8 value to a C++ value and stores it as a local value.""" 465 """Returns an expression that converts a V8 value to a C++ value and stores it as a local value."""
462 this_cpp_type = idl_type.cpp_type_args(extended_attributes=extended_attribut es, used_as_argument=True) 466 this_cpp_type = idl_type.cpp_type_args(extended_attributes=extended_attribut es, used_as_primitive_type=True)
463 467
464 idl_type = idl_type.preprocessed_type 468 idl_type = idl_type.preprocessed_type
465 cpp_value = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, i ndex) 469 cpp_value = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, i ndex)
466 args = [variable_name, cpp_value] 470 args = [variable_name, cpp_value]
467 if idl_type.base_type == 'DOMString' and not idl_type.array_or_sequence_type : 471 if idl_type.base_type == 'DOMString' and not idl_type.array_or_sequence_type :
468 macro = 'TOSTRING_VOID' 472 macro = 'TOSTRING_VOID'
469 elif (idl_type.is_integer_type or 473 elif (idl_type.is_integer_type or
470 idl_type.name in ('ByteString', 'ScalarValueString')): 474 idl_type.name in ('ByteString', 'ScalarValueString')):
471 macro = 'TONATIVE_VOID_EXCEPTIONSTATE' 475 macro = 'TONATIVE_VOID_EXCEPTIONSTATE'
472 args.append('exceptionState') 476 args.append('exceptionState')
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 def cpp_value_to_v8_value(idl_type, cpp_value, isolate='info.GetIsolate()', crea tion_context='info.Holder()', extended_attributes=None): 686 def cpp_value_to_v8_value(idl_type, cpp_value, isolate='info.GetIsolate()', crea tion_context='info.Holder()', extended_attributes=None):
683 """Returns an expression that converts a C++ value to a V8 value.""" 687 """Returns an expression that converts a C++ value to a V8 value."""
684 # the isolate parameter is needed for callback interfaces 688 # the isolate parameter is needed for callback interfaces
685 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext ended_attributes) 689 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext ended_attributes)
686 this_v8_conversion_type = idl_type.v8_conversion_type(extended_attributes) 690 this_v8_conversion_type = idl_type.v8_conversion_type(extended_attributes)
687 format_string = CPP_VALUE_TO_V8_VALUE[this_v8_conversion_type] 691 format_string = CPP_VALUE_TO_V8_VALUE[this_v8_conversion_type]
688 statement = format_string.format(cpp_value=cpp_value, isolate=isolate, creat ion_context=creation_context) 692 statement = format_string.format(cpp_value=cpp_value, isolate=isolate, creat ion_context=creation_context)
689 return statement 693 return statement
690 694
691 IdlType.cpp_value_to_v8_value = cpp_value_to_v8_value 695 IdlType.cpp_value_to_v8_value = cpp_value_to_v8_value
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698