| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 import "native_testing.dart"; | 5 import "native_testing.dart"; |
| 6 | 6 |
| 7 // Test to see if resolving a hidden native class's method interferes with | 7 // Test to see if resolving a hidden native class's method interferes with |
| 8 // subsequent resolving the subclass's method. This might happen if the | 8 // subsequent resolving the subclass's method. This might happen if the |
| 9 // superclass caches the method in the prototype, so shadowing the dispatcher | 9 // superclass caches the method in the prototype, so shadowing the dispatcher |
| 10 // stored on Object.prototype. | 10 // stored on Object.prototype. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 } | 23 } |
| 24 | 24 |
| 25 @Native("D") | 25 @Native("D") |
| 26 class D extends C {} | 26 class D extends C {} |
| 27 | 27 |
| 28 makeA() native; | 28 makeA() native; |
| 29 makeB() native; | 29 makeB() native; |
| 30 makeC() native; | 30 makeC() native; |
| 31 makeD() native; | 31 makeD() native; |
| 32 | 32 |
| 33 void setup() native """ | 33 void setup() { |
| 34 // This code is all inside 'setup' and so not accessible from the global scope. | 34 JS('', r""" |
| 35 function inherits(child, parent) { | 35 (function(){ |
| 36 if (child.prototype.__proto__) { | 36 // This code is inside 'setup' and so not accessible from the global scope. |
| 37 child.prototype.__proto__ = parent.prototype; | 37 function inherits(child, parent) { |
| 38 } else { | 38 if (child.prototype.__proto__) { |
| 39 function tmp() {}; | 39 child.prototype.__proto__ = parent.prototype; |
| 40 tmp.prototype = parent.prototype; | 40 } else { |
| 41 child.prototype = new tmp(); | 41 function tmp() {}; |
| 42 child.prototype.constructor = child; | 42 tmp.prototype = parent.prototype; |
| 43 child.prototype = new tmp(); |
| 44 child.prototype.constructor = child; |
| 45 } |
| 43 } | 46 } |
| 47 |
| 48 function A(){} |
| 49 function B(){} |
| 50 inherits(B, A); |
| 51 function C(){} |
| 52 inherits(C, B); |
| 53 function D(){} |
| 54 inherits(D, C); |
| 55 |
| 56 A.prototype.foo = function(a){return 'A.foo(' + a + ')'}; |
| 57 C.prototype.foo = function(z){return 'C.foo(' + z + ')'}; |
| 58 |
| 59 makeA = function(){return new A()}; |
| 60 makeB = function(){return new B()}; |
| 61 makeC = function(){return new C()}; |
| 62 makeD = function(){return new D()}; |
| 63 |
| 64 self.nativeConstructor(A); |
| 65 self.nativeConstructor(B); |
| 66 self.nativeConstructor(C); |
| 67 self.nativeConstructor(D); |
| 68 })()"""); |
| 44 } | 69 } |
| 45 | 70 |
| 46 function A(){} | |
| 47 function B(){} | |
| 48 inherits(B, A); | |
| 49 function C(){} | |
| 50 inherits(C, B); | |
| 51 function D(){} | |
| 52 inherits(D, C); | |
| 53 | |
| 54 A.prototype.foo = function(a){return 'A.foo(' + a + ')';} | |
| 55 C.prototype.foo = function(z){return 'C.foo(' + z + ')';} | |
| 56 | |
| 57 makeA = function(){return new A}; | |
| 58 makeB = function(){return new B}; | |
| 59 makeC = function(){return new C}; | |
| 60 makeD = function(){return new D}; | |
| 61 | |
| 62 self.nativeConstructor(A); | |
| 63 self.nativeConstructor(B); | |
| 64 self.nativeConstructor(C); | |
| 65 self.nativeConstructor(D); | |
| 66 """; | |
| 67 | |
| 68 main() { | 71 main() { |
| 69 nativeTesting(); | 72 nativeTesting(); |
| 70 setup(); | 73 setup(); |
| 71 | 74 |
| 72 var a = makeA(); | 75 var a = makeA(); |
| 73 var b = makeB(); | 76 var b = makeB(); |
| 74 var c = makeC(); | 77 var c = makeC(); |
| 75 var d = makeD(); | 78 var d = makeD(); |
| 76 | 79 |
| 77 Expect.equals('A.foo(100)', b.foo()); | 80 Expect.equals('A.foo(100)', b.foo()); |
| 78 Expect.equals('C.foo(300)', d.foo()); | 81 Expect.equals('C.foo(300)', d.foo()); |
| 79 // If the above line fails with C.foo(100) then the dispatch to fill in the | 82 // If the above line fails with C.foo(100) then the dispatch to fill in the |
| 80 // default got the wrong one, followed by a second dispatch that resolved to | 83 // default got the wrong one, followed by a second dispatch that resolved to |
| 81 // the correct native method. | 84 // the correct native method. |
| 82 | 85 |
| 83 Expect.equals('A.foo(1)', a.foo(1)); | 86 Expect.equals('A.foo(1)', a.foo(1)); |
| 84 Expect.equals('A.foo(2)', b.foo(2)); | 87 Expect.equals('A.foo(2)', b.foo(2)); |
| 85 Expect.equals('C.foo(3)', c.foo(3)); | 88 Expect.equals('C.foo(3)', c.foo(3)); |
| 86 Expect.equals('C.foo(4)', d.foo(4)); | 89 Expect.equals('C.foo(4)', d.foo(4)); |
| 87 } | 90 } |
| OLD | NEW |