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