| 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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 | 227 |
| 228 if is_void: | 228 if is_void: |
| 229 call_emitter = call_emitter.Emit('$(INDENT)$!CALL;\n$(INDENT)return;\n') | 229 call_emitter = call_emitter.Emit('$(INDENT)$!CALL;\n$(INDENT)return;\n') |
| 230 else: | 230 else: |
| 231 call_emitter = call_emitter.Emit('$(INDENT)return $!CALL;\n') | 231 call_emitter = call_emitter.Emit('$(INDENT)return $!CALL;\n') |
| 232 | 232 |
| 233 version[0] += 1 | 233 version[0] += 1 |
| 234 generate_call(stmts_emitter, call_emitter, | 234 generate_call(stmts_emitter, call_emitter, |
| 235 version[0], signature_index, argument_count) | 235 version[0], signature_index, argument_count) |
| 236 | 236 |
| 237 def IsDartTypeNullable(type_signature): | |
| 238 return type_signature not in ['int', 'double', 'num', 'bool'] | |
| 239 | |
| 240 def GenerateChecksAndCall(signature_index, argument_count): | 237 def GenerateChecksAndCall(signature_index, argument_count): |
| 241 checks = [] | 238 checks = [] |
| 242 for i in range(0, argument_count): | 239 for i in range(0, argument_count): |
| 243 argument = signatures[signature_index][i] | 240 argument = signatures[signature_index][i] |
| 244 parameter_name = parameter_names[i] | 241 parameter_name = parameter_names[i] |
| 245 test_type = self._DartType(argument.type.id) | 242 test_type = self._DartType(argument.type.id) |
| 246 nullable = argument.type.nullable | |
| 247 | 243 |
| 248 if test_type in ['dynamic', 'Object']: | 244 if test_type in ['dynamic', 'Object']: |
| 249 checks.append('?%s' % parameter_name) | 245 checks.append('?%s' % parameter_name) |
| 250 elif not can_omit_type_check(test_type, i): | 246 elif not can_omit_type_check(test_type, i): |
| 251 nullable = True | |
| 252 checks.append('(%s is %s || %s == null)' % ( | 247 checks.append('(%s is %s || %s == null)' % ( |
| 253 parameter_name, test_type, parameter_name)) | 248 parameter_name, test_type, parameter_name)) |
| 254 | 249 else: |
| 255 if not nullable and not IsDartTypeNullable(test_type): | |
| 256 for signature in signatures: | 250 for signature in signatures: |
| 257 if (len(signature) <= i or signature[i].id not in | 251 if (len(signature) <= i or signature[i].id not in |
| 258 parameter_name.split('_OR_')): | 252 parameter_name.split('_OR_')): |
| 259 | 253 |
| 260 checks.append('%s != null' % parameter_name) | 254 checks.append('?%s' % parameter_name) |
| 261 break | 255 break |
| 262 | 256 |
| 263 # There can be multiple presence checks. We need them all since a later | 257 # There can be multiple presence checks. We need them all since a later |
| 264 # optional argument could have been passed by name, leaving 'holes'. | 258 # optional argument could have been passed by name, leaving 'holes'. |
| 265 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) | 259 checks.extend(['!?%s' % name for name in parameter_names[argument_count:]]
) |
| 266 | 260 |
| 267 GenerateCall(signature_index, argument_count, checks) | 261 GenerateCall(signature_index, argument_count, checks) |
| 268 | 262 |
| 269 # TODO: Optimize the dispatch to avoid repeated checks. | 263 # TODO: Optimize the dispatch to avoid repeated checks. |
| 270 if len(signatures) > 1: | 264 if len(signatures) > 1: |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 if interface.parents: | 589 if interface.parents: |
| 596 parent = interface.parents[0] | 590 parent = interface.parents[0] |
| 597 if IsPureInterface(parent.type.id): | 591 if IsPureInterface(parent.type.id): |
| 598 walk(interface.parents) | 592 walk(interface.parents) |
| 599 else: | 593 else: |
| 600 walk(interface.parents[1:]) | 594 walk(interface.parents[1:]) |
| 601 return result | 595 return result |
| 602 | 596 |
| 603 def _DartType(self, type_name): | 597 def _DartType(self, type_name): |
| 604 return self._type_registry.DartType(type_name) | 598 return self._type_registry.DartType(type_name) |
| OLD | NEW |