| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 for dartNativeDispatchHooksTransformer, getTag hook. | 7 // Test for dartNativeDispatchHooksTransformer, getTag hook. |
| 8 | 8 |
| 9 @Native("T1A") | 9 @Native("T1A") |
| 10 class T1A {} | 10 class T1A {} |
| 11 | 11 |
| 12 @Native("T1B") | 12 @Native("T1B") |
| 13 class T1B {} | 13 class T1B {} |
| 14 | 14 |
| 15 @Native("T1C") | 15 @Native("T1C") |
| 16 class T1C {} | 16 class T1C {} |
| 17 | 17 |
| 18 makeT1A() native; | 18 makeT1A() native; |
| 19 makeT1B() native; | 19 makeT1B() native; |
| 20 makeT1C() native; | 20 makeT1C() native; |
| 21 | 21 |
| 22 int getTagCallCount() native; | 22 int getTagCallCount() native; |
| 23 | 23 |
| 24 void setup() native r''' | 24 void setup() { |
| 25 function T1A() { } // Normal native class. | 25 JS('', r''' |
| 26 function T1CrazyB() { } // Native class with different constructor name. | 26 (function(){ |
| 27 function T1A() { } // Normal native class. |
| 28 function T1CrazyB() { } // Native class with different constructor name. |
| 27 | 29 |
| 28 var T1fakeA = (function(){ | 30 var T1fakeA = (function(){ |
| 29 function T1A() {} // Native class with adversarial constructor name. | 31 function T1A() {} // Native class with adversarial constructor name. |
| 30 return T1A; | 32 return T1A; |
| 31 })(); | 33 })(); |
| 32 | 34 |
| 33 // Make constructors visible on 'window' for prepatching. | 35 // Make constructors visible on 'window' for prepatching. |
| 34 if (typeof window == "undefined") window = {} | 36 if (typeof window == "undefined") window = {}; |
| 35 window.T1A = T1A; | 37 window.T1A = T1A; |
| 36 window.T1CrazyB = T1CrazyB; | 38 window.T1CrazyB = T1CrazyB; |
| 37 | 39 |
| 38 makeT1A = function(){return new T1A;}; | 40 makeT1A = function(){return new T1A()}; |
| 39 makeT1B = function(){return new T1CrazyB;}; | 41 makeT1B = function(){return new T1CrazyB()}; |
| 40 makeT1C = function(){return new T1fakeA;}; | 42 makeT1C = function(){return new T1fakeA()}; |
| 41 | 43 |
| 42 self.nativeConstructor(T1A); | 44 self.nativeConstructor(T1A); |
| 43 self.nativeConstructor(T1CrazyB); | 45 self.nativeConstructor(T1CrazyB); |
| 44 self.nativeConstructor(T1fakeA); | 46 self.nativeConstructor(T1fakeA); |
| 45 | 47 |
| 46 var getTagCount = 0; | 48 var getTagCount = 0; |
| 47 getTagCallCount = function() { return getTagCount; } | 49 getTagCallCount = function() { return getTagCount; }; |
| 48 | 50 |
| 49 function transformer1(hooks) { | 51 function transformer1(hooks) { |
| 50 var getTag = hooks.getTag; | 52 var getTag = hooks.getTag; |
| 51 | 53 |
| 52 function getTagNew(obj) { | 54 function getTagNew(obj) { |
| 53 ++getTagCount; | 55 ++getTagCount; |
| 54 | 56 |
| 55 // If something looks like a different native type we can check in advance | 57 // If something looks like a different native type we can check in advance |
| 56 // of the default algorithm. | 58 // of the default algorithm. |
| 57 if (obj instanceof T1fakeA) return "T1C"; | 59 if (obj instanceof T1fakeA) return "T1C"; |
| 58 | 60 |
| 59 var tag = getTag(obj); | 61 var tag = getTag(obj); |
| 60 | 62 |
| 61 // New constructor names can be mapped here. | 63 // New constructor names can be mapped here. |
| 62 if (tag == "T1CrazyB") return "T1B"; | 64 if (tag == "T1CrazyB") return "T1B"; |
| 63 | 65 |
| 64 return tag; | 66 return tag; |
| 67 } |
| 68 |
| 69 hooks.getTag = getTagNew; |
| 65 } | 70 } |
| 66 | 71 |
| 67 hooks.getTag = getTagNew; | 72 dartNativeDispatchHooksTransformer = [transformer1]; |
| 73 })()'''); |
| 68 } | 74 } |
| 69 | 75 |
| 70 dartNativeDispatchHooksTransformer = [transformer1]; | |
| 71 '''; | |
| 72 | |
| 73 main() { | 76 main() { |
| 74 nativeTesting(); | 77 nativeTesting(); |
| 75 setup(); | 78 setup(); |
| 76 | 79 |
| 77 var t1a = makeT1A(); | 80 var t1a = makeT1A(); |
| 78 var t1b = makeT1B(); | 81 var t1b = makeT1B(); |
| 79 var t1c = makeT1C(); | 82 var t1c = makeT1C(); |
| 80 | 83 |
| 81 Expect.equals(true, t1a is T1A, '$t1a is T1A'); | 84 Expect.equals(true, t1a is T1A, '$t1a is T1A'); |
| 82 Expect.equals(true, t1b is T1B, '$t1b is T1B'); | 85 Expect.equals(true, t1b is T1B, '$t1b is T1B'); |
| 83 Expect.equals(true, t1c is T1C, '$t1c is T1C'); | 86 Expect.equals(true, t1c is T1C, '$t1c is T1C'); |
| 84 | 87 |
| 85 Expect.equals(2, getTagCallCount()); | 88 Expect.equals(2, getTagCallCount()); |
| 86 | 89 |
| 87 Expect.equals(true, confuse(t1a) is T1A, '$t1a is T1A'); | 90 Expect.equals(true, confuse(t1a) is T1A, '$t1a is T1A'); |
| 88 Expect.equals(true, confuse(t1b) is T1B, '$t1b is T1B'); | 91 Expect.equals(true, confuse(t1b) is T1B, '$t1b is T1B'); |
| 89 Expect.equals(true, confuse(t1c) is T1C, '$t1c is T1C'); | 92 Expect.equals(true, confuse(t1c) is T1C, '$t1c is T1C'); |
| 90 | 93 |
| 91 Expect.equals(2, getTagCallCount()); | 94 Expect.equals(2, getTagCallCount()); |
| 92 } | 95 } |
| OLD | NEW |