OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 """This module provides shared functionality for the system to generate | 6 """This module provides shared functionality for the system to generate |
7 dart:html APIs from the IDL database.""" | 7 dart:html APIs from the IDL database.""" |
8 | 8 |
9 import emitter | 9 import emitter |
10 from generator import AnalyzeOperation, ConstantOutputOrder, \ | 10 from generator import AnalyzeOperation, ConstantOutputOrder, \ |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 generate_call(stmts_emitter, call_emitter, | 228 generate_call(stmts_emitter, call_emitter, |
229 version[0], signature_index, argument_count) | 229 version[0], signature_index, argument_count) |
230 | 230 |
231 def GenerateChecksAndCall(signature_index, argument_count): | 231 def GenerateChecksAndCall(signature_index, argument_count): |
232 checks = [] | 232 checks = [] |
233 for i in range(0, argument_count): | 233 for i in range(0, argument_count): |
234 argument = signatures[signature_index][i] | 234 argument = signatures[signature_index][i] |
235 parameter_name = parameter_names[i] | 235 parameter_name = parameter_names[i] |
236 test_type = self._DartType(argument.type.id) | 236 test_type = self._DartType(argument.type.id) |
237 if test_type in ['dynamic', 'Object']: | 237 if test_type in ['dynamic', 'Object']: |
238 checks.append('?%s' % parameter_name) | 238 checks.append('%s != null' % parameter_name) |
239 elif not can_omit_type_check(test_type, i): | 239 elif not can_omit_type_check(test_type, i): |
240 checks.append('(%s is %s || %s == null)' % ( | 240 checks.append('(%s is %s || %s == null)' % ( |
241 parameter_name, test_type, parameter_name)) | 241 parameter_name, test_type, parameter_name)) |
242 # There can be multiple presence checks. We need them all since a later | 242 # There can be multiple presence checks. We need them all since a later |
243 # optional argument could have been passed by name, leaving 'holes'. | 243 # optional argument could have been passed by name, leaving 'holes'. |
244 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]] ) | 244 checks.extend(['%s == null' % name for name in parameter_names[argument_co unt:]]) |
blois
2013/05/09 22:35:22
line length
Anton Muhin
2013/05/14 14:48:23
BTW, another potential source of semantic differen
| |
245 | 245 |
246 GenerateCall(signature_index, argument_count, checks) | 246 GenerateCall(signature_index, argument_count, checks) |
247 | 247 |
248 # TODO: Optimize the dispatch to avoid repeated checks. | 248 # TODO: Optimize the dispatch to avoid repeated checks. |
249 if len(signatures) > 1: | 249 if len(signatures) > 1: |
250 index_swaps = {} | 250 index_swaps = {} |
251 for signature_index, signature in enumerate(signatures): | 251 for signature_index, signature in enumerate(signatures): |
252 for argument_position, argument in enumerate(signature): | 252 for argument_position, argument in enumerate(signature): |
253 if argument.type.id != 'ArrayBuffer': | 253 if argument.type.id != 'ArrayBuffer': |
254 continue | 254 continue |
(...skipping 17 matching lines...) Expand all Loading... | |
272 GenerateChecksAndCall(signature_index, argument_position) | 272 GenerateChecksAndCall(signature_index, argument_position) |
273 GenerateChecksAndCall(signature_index, len(signature)) | 273 GenerateChecksAndCall(signature_index, len(signature)) |
274 body_emitter.Emit( | 274 body_emitter.Emit( |
275 ' throw new ArgumentError("Incorrect number or type of arguments"); ' | 275 ' throw new ArgumentError("Incorrect number or type of arguments"); ' |
276 '\n'); | 276 '\n'); |
277 else: | 277 else: |
278 signature = signatures[0] | 278 signature = signatures[0] |
279 argument_count = len(signature) | 279 argument_count = len(signature) |
280 for argument_position, argument in list(enumerate(signature))[::-1]: | 280 for argument_position, argument in list(enumerate(signature))[::-1]: |
281 if is_optional(0, argument): | 281 if is_optional(0, argument): |
282 check = '?%s' % parameter_names[argument_position] | 282 check = '%s != null' % parameter_names[argument_position] |
283 # argument_count instead of argument_position + 1 is used here to cove r one | 283 # argument_count instead of argument_position + 1 is used here to cove r one |
284 # complicated case with the effectively optional argument in the middl e. | 284 # complicated case with the effectively optional argument in the middl e. |
285 # Consider foo(x, optional y, [Default=NullString] optional z) | 285 # Consider foo(x, optional y, [Default=NullString] optional z) |
286 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). | 286 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). |
287 # y is optional in WebCore, while z is not. | 287 # y is optional in WebCore, while z is not. |
288 # In this case, if y was actually passed, we'd like to emit foo(x, y, z) invocation, | 288 # In this case, if y was actually passed, we'd like to emit foo(x, y, z) invocation, |
289 # not foo(x, y). | 289 # not foo(x, y). |
290 GenerateCall(0, argument_count, [check]) | 290 GenerateCall(0, argument_count, [check]) |
291 argument_count = argument_position | 291 argument_count = argument_position |
292 GenerateCall(0, argument_count, []) | 292 GenerateCall(0, argument_count, []) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 constructor_info, factory_name, factory_constructor_name): | 355 constructor_info, factory_name, factory_constructor_name): |
356 if self.GenerateCustomFactory(constructor_info): | 356 if self.GenerateCustomFactory(constructor_info): |
357 return | 357 return |
358 | 358 |
359 metadata = self._metadata.GetFormattedMetadata( | 359 metadata = self._metadata.GetFormattedMetadata( |
360 self._library_name, self._interface, self._interface.id, ' ') | 360 self._library_name, self._interface, self._interface.id, ' ') |
361 | 361 |
362 if not factory_constructor_name: | 362 if not factory_constructor_name: |
363 factory_constructor_name = '_create' | 363 factory_constructor_name = '_create' |
364 factory_parameters = constructor_info.ParametersAsArgumentList() | 364 factory_parameters = constructor_info.ParametersAsArgumentList() |
365 has_factory_provider = True | |
366 else: | 365 else: |
367 factory_parameters = ', '.join(constructor_info.factory_parameters) | 366 factory_parameters = ', '.join(constructor_info.factory_parameters) |
368 has_factory_provider = False | |
369 | 367 |
370 if constructor_info.pure_dart_constructor: | 368 if constructor_info.pure_dart_constructor: |
371 # TODO(antonm): use common dispatcher generation for this case as well. | 369 # TODO(antonm): use common dispatcher generation for this case as well. |
372 has_optional = any(param_info.is_optional | 370 has_optional = any(param_info.is_optional |
373 for param_info in constructor_info.param_infos) | 371 for param_info in constructor_info.param_infos) |
374 | 372 |
375 if not has_optional: | 373 if not has_optional: |
376 self._members_emitter.Emit( | 374 self._members_emitter.Emit( |
377 '\n $(METADATA)' | 375 '\n $(METADATA)' |
378 'factory $CTOR($PARAMS) => ' | 376 'factory $CTOR($PARAMS) => ' |
379 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', | 377 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', |
380 CTOR=constructor_info._ConstructorFullName(self._DartType), | 378 CTOR=constructor_info._ConstructorFullName(self._DartType), |
381 PARAMS=constructor_info.ParametersDeclaration(self._DartType), | 379 PARAMS=constructor_info.ParametersDeclaration(self._DartType), |
382 FACTORY=factory_name, | 380 FACTORY=factory_name, |
383 METADATA=metadata, | 381 METADATA=metadata, |
384 CTOR_FACTORY_NAME=factory_constructor_name, | 382 CTOR_FACTORY_NAME=factory_constructor_name, |
385 FACTORY_PARAMS=factory_parameters) | 383 FACTORY_PARAMS=factory_parameters) |
386 else: | 384 else: |
387 if has_factory_provider: | 385 inits = self._members_emitter.Emit( |
Anton Muhin
2013/05/14 14:48:23
please, a separate change or rebase if not too lat
| |
388 dispatcher_emitter = self._members_emitter.Emit( | 386 '\n $(METADATA)' |
389 '\n $(METADATA)' | 387 'factory $CONSTRUCTOR($PARAMS) {\n' |
390 'factory $CTOR($PARAMS) {\n' | 388 ' var e = $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
391 '$!DISPATCHER' | 389 '$!INITS' |
392 ' return $FACTORY._create($FACTORY_PARAMS);\n' | 390 ' return e;\n' |
393 ' }\n', | 391 ' }\n', |
394 CTOR=constructor_info._ConstructorFullName(self._DartType), | 392 CONSTRUCTOR=constructor_info._ConstructorFullName(self._DartType), |
395 PARAMS=constructor_info.ParametersDeclaration(self._DartType), | 393 METADATA=metadata, |
396 FACTORY=factory_name, | 394 FACTORY=factory_name, |
397 METADATA=metadata, | 395 CTOR_FACTORY_NAME=factory_constructor_name, |
398 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList()) | 396 PARAMS=constructor_info.ParametersDeclaration(self._DartType), |
397 FACTORY_PARAMS=factory_parameters) | |
399 | 398 |
400 for index, param_info in enumerate(constructor_info.param_infos): | 399 for index, param_info in enumerate(constructor_info.param_infos): |
401 if param_info.is_optional: | 400 if param_info.is_optional: |
402 dispatcher_emitter.Emit( | 401 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name) |
403 ' if ($OPT_PARAM_NAME == null) {\n' | |
404 ' return $FACTORY._create($FACTORY_PARAMS);\n' | |
405 ' }\n', | |
406 OPT_PARAM_NAME=param_info.name, | |
407 FACTORY=factory_name, | |
408 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList(index)) | |
409 else: | |
410 inits = self._members_emitter.Emit( | |
411 '\n $(METADATA)' | |
412 'factory $CONSTRUCTOR($PARAMS) {\n' | |
413 ' var e = $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | |
414 '$!INITS' | |
415 ' return e;\n' | |
416 ' }\n', | |
417 CONSTRUCTOR=constructor_info._ConstructorFullName(self._DartType), | |
418 METADATA=metadata, | |
419 FACTORY=factory_name, | |
420 CTOR_FACTORY_NAME=factory_constructor_name, | |
421 PARAMS=constructor_info.ParametersDeclaration(self._DartType), | |
422 FACTORY_PARAMS=factory_parameters) | |
423 | |
424 for index, param_info in enumerate(constructor_info.param_infos): | |
425 if param_info.is_optional: | |
426 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name) | |
427 else: | 402 else: |
428 def GenerateCall( | 403 def GenerateCall( |
429 stmts_emitter, call_emitter, | 404 stmts_emitter, call_emitter, |
430 version, signature_index, argument_count): | 405 version, signature_index, argument_count): |
431 name = emitter.Format('_create_$VERSION', VERSION=version) | 406 name = emitter.Format('_create_$VERSION', VERSION=version) |
432 call_emitter.Emit('$FACTORY.$NAME($FACTORY_PARAMS)', | 407 call_emitter.Emit('$FACTORY.$NAME($FACTORY_PARAMS)', |
433 FACTORY=factory_name, | 408 FACTORY=factory_name, |
434 NAME=name, | 409 NAME=name, |
435 FACTORY_PARAMS= \ | 410 FACTORY_PARAMS= \ |
436 constructor_info.ParametersAsArgumentList(argument_count)) | 411 constructor_info.ParametersAsArgumentList(argument_count)) |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
597 if interface.parents: | 572 if interface.parents: |
598 parent = interface.parents[0] | 573 parent = interface.parents[0] |
599 if IsPureInterface(parent.type.id): | 574 if IsPureInterface(parent.type.id): |
600 walk(interface.parents) | 575 walk(interface.parents) |
601 else: | 576 else: |
602 walk(interface.parents[1:]) | 577 walk(interface.parents[1:]) |
603 return result | 578 return result |
604 | 579 |
605 def _DartType(self, type_name): | 580 def _DartType(self, type_name): |
606 return self._type_registry.DartType(type_name) | 581 return self._type_registry.DartType(type_name) |
OLD | NEW |