Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: tests/compiler/dart2js_native/error_safeToString_test.dart

Issue 1362553002: dart2js runtime: Harden objectTypeName. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sdk/lib/_internal/js_runtime/lib/js_helper.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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.
4
5 import "package:expect/expect.dart";
6 import 'dart:_foreign_helper' show JS_INTERCEPTOR_CONSTANT, JS;
7 import 'dart:_js_helper' show Native, Creates;
8 import 'dart:_interceptors' show
9 Interceptor,
10 JavaScriptObject,
11 PlainJavaScriptObject,
12 UnknownJavaScriptObject;
13
14 // Test for safe formatting of JavaScript objects by Error.safeToString.
15
16 @Native('PPPP')
17 class Purple {}
18
19 @Native('QQQQ')
20 class Q {}
21
22 @Native('RRRR')
23 class Rascal {
24 toString() => 'RRRRRRRR';
25 }
26
27 makeA() native;
28 makeB() native;
29 makeC() native;
30 makeD() native;
31 makeE() native;
32 makeP() native;
33 makeQ() native;
34 makeR() native;
35
36 void setup() native r"""
37 makeA = function(){return {hello: 123};};
38
39 function BB(){}
40 makeB = function(){return new BB();};
41
42 function CC(){}
43 makeC = function(){
44 var x = new CC();
45 x.constructor = null; // Foils constructor lookup.
46 return x;
47 };
48
49 function DD(){}
50 makeD = function(){
51 var x = new DD();
52 x.constructor = {name: 'DDxxx'}; // Foils constructor lookup.
53 return x;
54 };
55
56 function EE(){}
57 makeE = function(){
58 var x = new EE();
59 x.constructor = function Liar(){}; // Looks like a legitimate constructor.
60 return x;
61 };
62
63 function PPPP(){}
64 makeP = function(){return new PPPP();};
65
66 function QQQQ(){}
67 makeQ = function(){return new QQQQ();};
68
69 function RRRR(){}
70 makeR = function(){return new RRRR();};
71
72 """;
73
74
75 expectTypeName(expectedName, s) {
76 var m = new RegExp(r"Instance of '(.*)'").firstMatch(s);
77 Expect.isNotNull(m);
78 var name = m.group(1);
79 Expect.isTrue(expectedName == name || name.length <= 3,
80 "Is '$expectedName' or minified: '$name'");
81 }
82
83 final plainJsString =
84 Error.safeToString(JS_INTERCEPTOR_CONSTANT(PlainJavaScriptObject));
85
86 final unknownJsString =
87 Error.safeToString(JS_INTERCEPTOR_CONSTANT(UnknownJavaScriptObject));
88
89 final interceptorString =
90 Error.safeToString(JS_INTERCEPTOR_CONSTANT(Interceptor));
91
92
93 testDistinctInterceptors() {
94 // Test invariants needed for the other tests.
95
96 Expect.notEquals(plainJsString, unknownJsString);
97 Expect.notEquals(plainJsString, interceptorString);
98 Expect.notEquals(unknownJsString, interceptorString);
99
100 expectTypeName('PlainJavaScriptObject', plainJsString);
101 expectTypeName('UnknownJavaScriptObject', unknownJsString);
102 expectTypeName('Interceptor', interceptorString);
103
104 // Sometimes interceptor *objects* are used instead of the prototypes. Check
105 // these work too.
106 var plain2 = Error.safeToString(const PlainJavaScriptObject());
107 Expect.equals(plainJsString, plain2);
108
109 var unk2 = Error.safeToString(const UnknownJavaScriptObject());
110 Expect.equals(unknownJsString, unk2);
111 }
112
113
114 testExternal() {
115 var x = makeA();
116 Expect.equals(plainJsString, Error.safeToString(x));
117
118 x = makeB();
119 // Gets name from constructor, regardless of minification.
120 Expect.equals("Instance of 'BB'", Error.safeToString(x));
121
122 x = makeC();
123 Expect.equals(unknownJsString, Error.safeToString(x));
124
125 x = makeD();
126 Expect.equals(unknownJsString, Error.safeToString(x));
127
128 x = makeE();
129 Expect.equals("Instance of 'Liar'", Error.safeToString(x));
130 }
131
132 testNative() {
133 var x = makeP();
134 Expect.isTrue(x is Purple); // This test forces Purple to be distinguished.
135 Expect.notEquals(plainJsString, Error.safeToString(x));
136 Expect.notEquals(unknownJsString, Error.safeToString(x));
137 Expect.notEquals(interceptorString, Error.safeToString(x));
138 // And not the native class constructor.
139 Expect.notEquals("Instance of 'PPPP'", Error.safeToString(x));
140 expectTypeName('Purple', Error.safeToString(x));
141
142 x = makeQ();
143 print('Q: $x ${Error.safeToString(x)}');
144 // We are going to get either the general interceptor or the JavaScript
145 // constructor.
146 Expect.isTrue(
147 "Instance of 'QQQQ'" == Error.safeToString(x) ||
148 interceptorString == Error.safeToString(x));
149
150 x = makeR();
151
152 // Rascal overrides 'toString'. The toString() call causes Rascal to be
153 // distinguished.
154 x.toString();
155 Expect.notEquals(plainJsString, Error.safeToString(x));
156 Expect.notEquals(unknownJsString, Error.safeToString(x));
157 Expect.notEquals(interceptorString, Error.safeToString(x));
158 // And not the native class constructor.
159 Expect.notEquals("Instance of 'RRRR'", Error.safeToString(x));
160 expectTypeName('Rascal', Error.safeToString(x));
161 }
162
163 main() {
164 setup();
165
166 testDistinctInterceptors();
167 testExternal();
168 testNative();
169 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/js_runtime/lib/js_helper.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698