OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of js_backend; | 5 part of js_backend; |
6 | 6 |
7 class NativeEmitter { | 7 class NativeEmitter { |
8 | 8 |
9 CodeEmitterTask emitter; | 9 CodeEmitterTask emitter; |
10 CodeBuffer nativeBuffer; | 10 CodeBuffer nativeBuffer; |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 | 406 |
407 if (!element.isClass()) { | 407 if (!element.isClass()) { |
408 compiler.cancel("Is check does not handle element", element: element); | 408 compiler.cancel("Is check does not handle element", element: element); |
409 return false; | 409 return false; |
410 } | 410 } |
411 | 411 |
412 return subtypes[element] != null; | 412 return subtypes[element] != null; |
413 } | 413 } |
414 | 414 |
415 bool requiresNativeIsCheck(Element element) { | 415 bool requiresNativeIsCheck(Element element) { |
| 416 // TODO(sra): Remove this function. It determines if a native type may |
| 417 // satisfy a check against [element], in whcih case an interceptor must be |
| 418 // used. We should also use an interceptor if the check can't be satisfied |
| 419 // by a native class in case we get a natibe instance that tries to spoof |
| 420 // the type info. i.e the criteria for whether or not to use an interceptor |
| 421 // is whether the receiver can be native, not the type of the test. |
416 if (!element.isClass()) return false; | 422 if (!element.isClass()) return false; |
417 ClassElement cls = element; | 423 ClassElement cls = element; |
418 if (cls.isNative()) return true; | 424 if (cls.isNative()) return true; |
419 return isSupertypeOfNativeClass(element); | 425 return isSupertypeOfNativeClass(element); |
420 } | 426 } |
421 | 427 |
422 void assembleCode(CodeBuffer targetBuffer) { | 428 void assembleCode(CodeBuffer targetBuffer) { |
423 List<jsAst.Property> objectProperties = <jsAst.Property>[]; | 429 List<jsAst.Property> objectProperties = <jsAst.Property>[]; |
424 | 430 |
425 void addProperty(String name, jsAst.Expression value) { | 431 void addProperty(String name, jsAst.Expression value) { |
426 objectProperties.add(new jsAst.Property(js.string(name), value)); | 432 objectProperties.add(new jsAst.Property(js.string(name), value)); |
427 } | 433 } |
428 | 434 |
429 // Because of native classes, we have to generate some is checks | |
430 // by calling a method, instead of accessing a property. So we | |
431 // attach to the JS Object prototype these methods that return | |
432 // false, and will be overridden by subclasses when they have to | |
433 // return true. | |
434 void emitIsChecks() { | |
435 for (ClassElement element in | |
436 Elements.sortedByPosition(emitter.checkedClasses)) { | |
437 if (!requiresNativeIsCheck(element)) continue; | |
438 if (element.isObject(compiler)) continue; | |
439 // Add function for the is-test. | |
440 String name = backend.namer.operatorIs(element); | |
441 addProperty(name, | |
442 js.fun([], js.return_(js('false')))); | |
443 // Add a function for the (trivial) substitution. | |
444 addProperty(backend.namer.substitutionName(element), | |
445 js.fun([], js.return_(js('null')))); | |
446 } | |
447 } | |
448 emitIsChecks(); | |
449 | |
450 if (!nativeClasses.isEmpty) { | 435 if (!nativeClasses.isEmpty) { |
451 // If the native emitter has been asked to take care of the | 436 // If the native emitter has been asked to take care of the |
452 // noSuchMethod handlers, we do that now. | 437 // noSuchMethod handlers, we do that now. |
453 if (handleNoSuchMethod) { | 438 if (handleNoSuchMethod) { |
454 emitter.emitNoSuchMethodHandlers(addProperty); | 439 emitter.emitNoSuchMethodHandlers(addProperty); |
455 } | 440 } |
456 } | 441 } |
457 | 442 |
458 // If we have any properties to add to Object.prototype, we run | 443 // If we have any properties to add to Object.prototype, we run |
459 // through them and add them using defineProperty. | 444 // through them and add them using defineProperty. |
(...skipping 13 matching lines...) Expand all Loading... |
473 if (emitter.compiler.enableMinification) targetBuffer.add(';'); | 458 if (emitter.compiler.enableMinification) targetBuffer.add(';'); |
474 targetBuffer.add(jsAst.prettyPrint( | 459 targetBuffer.add(jsAst.prettyPrint( |
475 new jsAst.ExpressionStatement(init), compiler)); | 460 new jsAst.ExpressionStatement(init), compiler)); |
476 targetBuffer.add('\n'); | 461 targetBuffer.add('\n'); |
477 } | 462 } |
478 | 463 |
479 targetBuffer.add(nativeBuffer); | 464 targetBuffer.add(nativeBuffer); |
480 targetBuffer.add('\n'); | 465 targetBuffer.add('\n'); |
481 } | 466 } |
482 } | 467 } |
OLD | NEW |