| 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 library mock_compiler; | 5 library mock_compiler; |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:collection'; | 9 import 'dart:collection'; |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 ErroneousElementX, | 24 ErroneousElementX, |
| 25 FunctionElementX; | 25 FunctionElementX; |
| 26 | 26 |
| 27 import 'package:compiler/implementation/dart2jslib.dart' | 27 import 'package:compiler/implementation/dart2jslib.dart' |
| 28 hide TreeElementMapping; | 28 hide TreeElementMapping; |
| 29 | 29 |
| 30 import 'package:compiler/implementation/deferred_load.dart' | 30 import 'package:compiler/implementation/deferred_load.dart' |
| 31 show DeferredLoadTask, | 31 show DeferredLoadTask, |
| 32 OutputUnit; | 32 OutputUnit; |
| 33 | 33 |
| 34 import 'mock_libraries.dart'; |
| 35 |
| 34 class WarningMessage { | 36 class WarningMessage { |
| 35 Spannable node; | 37 Spannable node; |
| 36 Message message; | 38 Message message; |
| 37 WarningMessage(this.node, this.message); | 39 WarningMessage(this.node, this.message); |
| 38 | 40 |
| 39 toString() => message.toString(); | 41 toString() => message.toString(); |
| 40 } | 42 } |
| 41 | 43 |
| 42 const String DEFAULT_HELPERLIB = r''' | |
| 43 const patch = 0; | |
| 44 wrapException(x) { return x; } | |
| 45 iae(x) { throw x; } ioore(x) { throw x; } | |
| 46 guard$array(x) { return x; } | |
| 47 guard$num(x) { return x; } | |
| 48 guard$string(x) { return x; } | |
| 49 guard$stringOrArray(x) { return x; } | |
| 50 makeLiteralMap(List keyValuePairs) {} | |
| 51 setRuntimeTypeInfo(a, b) {} | |
| 52 getRuntimeTypeInfo(a) {} | |
| 53 stringTypeCheck(x) {} | |
| 54 stringTypeCast(x) {} | |
| 55 propertyTypeCast(x) {} | |
| 56 boolConversionCheck(x) {} | |
| 57 abstract class JavaScriptIndexingBehavior {} | |
| 58 class JSInvocationMirror {} | |
| 59 abstract class BoundClosure extends Closure { | |
| 60 var self; | |
| 61 var target; | |
| 62 var receiver; | |
| 63 } | |
| 64 abstract class Closure implements Function { } | |
| 65 class ConstantMap {} | |
| 66 class ConstantStringMap {} | |
| 67 class TypeImpl {} | |
| 68 S() {} | |
| 69 throwCyclicInit() {} | |
| 70 throwExpression(e) {} | |
| 71 unwrapException(e) {} | |
| 72 assertHelper(a) {} | |
| 73 isJsIndexable(a, b) {} | |
| 74 createRuntimeType(a) {} | |
| 75 createInvocationMirror(a0, a1, a2, a3, a4, a5) {} | |
| 76 throwNoSuchMethod(obj, name, arguments, expectedArgumentNames) {} | |
| 77 throwAbstractClassInstantiationError(className) {} | |
| 78 boolTypeCheck(value) {} | |
| 79 propertyTypeCheck(value, property) {} | |
| 80 interceptedTypeCheck(value, property) {} | |
| 81 functionSubtypeCast(Object object, String signatureName, | |
| 82 String contextName, var context) {} | |
| 83 checkFunctionSubtype(var target, String signatureName, | |
| 84 String contextName, var context, | |
| 85 var typeArguments) {} | |
| 86 computeSignature(var signature, var context, var contextName) {} | |
| 87 getRuntimeTypeArguments(target, substitutionName) {} | |
| 88 voidTypeCheck(value) {}'''; | |
| 89 | |
| 90 const String FOREIGN_LIBRARY = r''' | |
| 91 dynamic JS(String typeDescription, String codeTemplate, | |
| 92 [var arg0, var arg1, var arg2, var arg3, var arg4, var arg5, var arg6, | |
| 93 var arg7, var arg8, var arg9, var arg10, var arg11]) {}'''; | |
| 94 | |
| 95 const String DEFAULT_INTERCEPTORSLIB = r''' | |
| 96 class Interceptor { | |
| 97 toString() {} | |
| 98 bool operator==(other) => identical(this, other); | |
| 99 get hashCode => throw "Interceptor.hashCode not implemented."; | |
| 100 noSuchMethod(im) { throw im; } | |
| 101 } | |
| 102 abstract class JSIndexable { | |
| 103 get length; | |
| 104 operator[](index); | |
| 105 } | |
| 106 abstract class JSMutableIndexable extends JSIndexable {} | |
| 107 class JSArray<E> extends Interceptor implements List<E>, JSIndexable { | |
| 108 JSArray(); | |
| 109 factory JSArray.typed(a) => a; | |
| 110 var length; | |
| 111 operator[](index) => this[index]; | |
| 112 operator[]=(index, value) { this[index] = value; } | |
| 113 add(value) { this[length + 1] = value; } | |
| 114 insert(index, value) {} | |
| 115 E get first => this[0]; | |
| 116 E get last => this[0]; | |
| 117 E get single => this[0]; | |
| 118 E removeLast() => this[0]; | |
| 119 E removeAt(index) => this[0]; | |
| 120 E elementAt(index) => this[0]; | |
| 121 E singleWhere(f) => this[0]; | |
| 122 } | |
| 123 class JSMutableArray extends JSArray implements JSMutableIndexable {} | |
| 124 class JSFixedArray extends JSMutableArray {} | |
| 125 class JSExtendableArray extends JSMutableArray {} | |
| 126 class JSString extends Interceptor implements String, JSIndexable { | |
| 127 var length; | |
| 128 operator[](index) {} | |
| 129 toString() {} | |
| 130 operator+(other) => this; | |
| 131 } | |
| 132 class JSPositiveInt extends JSInt {} | |
| 133 class JSUInt32 extends JSPositiveInt {} | |
| 134 class JSUInt31 extends JSUInt32 {} | |
| 135 class JSNumber extends Interceptor implements num { | |
| 136 // All these methods return a number to please type inferencing. | |
| 137 operator-() => (this is JSInt) ? 42 : 42.2; | |
| 138 operator +(other) => (this is JSInt) ? 42 : 42.2; | |
| 139 operator -(other) => (this is JSInt) ? 42 : 42.2; | |
| 140 operator ~/(other) => _tdivFast(other); | |
| 141 operator /(other) => (this is JSInt) ? 42 : 42.2; | |
| 142 operator *(other) => (this is JSInt) ? 42 : 42.2; | |
| 143 operator %(other) => (this is JSInt) ? 42 : 42.2; | |
| 144 operator <<(other) => _shlPositive(other); | |
| 145 operator >>(other) { | |
| 146 return _shrBothPositive(other) + _shrReceiverPositive(other) + | |
| 147 _shrOtherPositive(other); | |
| 148 } | |
| 149 operator |(other) => 42; | |
| 150 operator &(other) => 42; | |
| 151 operator ^(other) => 42; | |
| 152 | |
| 153 operator >(other) => true; | |
| 154 operator >=(other) => true; | |
| 155 operator <(other) => true; | |
| 156 operator <=(other) => true; | |
| 157 operator ==(other) => true; | |
| 158 get hashCode => throw "JSNumber.hashCode not implemented."; | |
| 159 | |
| 160 // We force side effects on _tdivFast to mimic the shortcomings of | |
| 161 // the effect analysis: because the `_tdivFast` implementation of | |
| 162 // the core library has calls that may not already be analyzed, | |
| 163 // the analysis will conclude that `_tdivFast` may have side | |
| 164 // effects. | |
| 165 _tdivFast(other) => new List()..length = 42; | |
| 166 _shlPositive(other) => 42; | |
| 167 _shrBothPositive(other) => 42; | |
| 168 _shrReceiverPositive(other) => 42; | |
| 169 _shrOtherPositive(other) => 42; | |
| 170 | |
| 171 abs() => (this is JSInt) ? 42 : 42.2; | |
| 172 remainder(other) => (this is JSInt) ? 42 : 42.2; | |
| 173 truncate() => 42; | |
| 174 } | |
| 175 class JSInt extends JSNumber implements int { | |
| 176 } | |
| 177 class JSDouble extends JSNumber implements double { | |
| 178 } | |
| 179 class JSNull extends Interceptor { | |
| 180 bool operator==(other) => identical(null, other); | |
| 181 get hashCode => throw "JSNull.hashCode not implemented."; | |
| 182 String toString() => 'Null'; | |
| 183 Type get runtimeType => null; | |
| 184 noSuchMethod(x) => super.noSuchMethod(x); | |
| 185 } | |
| 186 class JSBool extends Interceptor implements bool { | |
| 187 } | |
| 188 abstract class JSFunction extends Interceptor implements Function { | |
| 189 } | |
| 190 class ObjectInterceptor { | |
| 191 } | |
| 192 getInterceptor(x) {} | |
| 193 getNativeInterceptor(x) {} | |
| 194 var dispatchPropertyName; | |
| 195 var mapTypeToInterceptor; | |
| 196 getDispatchProperty(o) {} | |
| 197 initializeDispatchProperty(f,p,i) {} | |
| 198 initializeDispatchPropertyCSP(f,p,i) {} | |
| 199 '''; | |
| 200 | |
| 201 const String DEFAULT_CORELIB = r''' | |
| 202 print(var obj) {} | |
| 203 abstract class num {} | |
| 204 abstract class int extends num { } | |
| 205 abstract class double extends num { | |
| 206 static var NAN = 0; | |
| 207 static parse(s) {} | |
| 208 } | |
| 209 class bool {} | |
| 210 class String implements Pattern {} | |
| 211 class Object { | |
| 212 const Object(); | |
| 213 operator ==(other) { return true; } | |
| 214 get hashCode => throw "Object.hashCode not implemented."; | |
| 215 String toString() { return null; } | |
| 216 noSuchMethod(im) { throw im; } | |
| 217 } | |
| 218 class Null {} | |
| 219 abstract class StackTrace {} | |
| 220 class Type {} | |
| 221 class Function {} | |
| 222 class List<E> { | |
| 223 var length; | |
| 224 List([length]); | |
| 225 List.filled(length, element); | |
| 226 E get first => null; | |
| 227 E get last => null; | |
| 228 E get single => null; | |
| 229 E removeLast() => null; | |
| 230 E removeAt(i) => null; | |
| 231 E elementAt(i) => null; | |
| 232 E singleWhere(f) => null; | |
| 233 } | |
| 234 abstract class Map<K,V> {} | |
| 235 class LinkedHashMap { | |
| 236 factory LinkedHashMap._empty() => null; | |
| 237 factory LinkedHashMap._literal(elements) => null; | |
| 238 } | |
| 239 class DateTime { | |
| 240 DateTime(year); | |
| 241 DateTime.utc(year); | |
| 242 } | |
| 243 abstract class Pattern {} | |
| 244 bool identical(Object a, Object b) { return true; } | |
| 245 const proxy = 0;'''; | |
| 246 | |
| 247 final Uri PATCH_CORE = new Uri(scheme: 'patch', path: 'core'); | 44 final Uri PATCH_CORE = new Uri(scheme: 'patch', path: 'core'); |
| 248 | 45 |
| 249 const String PATCH_CORE_SOURCE = r''' | |
| 250 import 'dart:_js_helper'; | |
| 251 import 'dart:_interceptors'; | |
| 252 import 'dart:_isolate_helper'; | |
| 253 '''; | |
| 254 | |
| 255 const String DEFAULT_ISOLATE_HELPERLIB = r''' | |
| 256 var startRootIsolate; | |
| 257 var _currentIsolate; | |
| 258 var _callInIsolate; | |
| 259 class _WorkerBase {}'''; | |
| 260 | |
| 261 const String DEFAULT_MIRRORS = r''' | |
| 262 class Comment {} | |
| 263 class MirrorSystem {} | |
| 264 class MirrorsUsed {} | |
| 265 '''; | |
| 266 | |
| 267 class MockCompiler extends Compiler { | 46 class MockCompiler extends Compiler { |
| 268 api.DiagnosticHandler diagnosticHandler; | 47 api.DiagnosticHandler diagnosticHandler; |
| 269 List<WarningMessage> warnings; | 48 List<WarningMessage> warnings; |
| 270 List<WarningMessage> errors; | 49 List<WarningMessage> errors; |
| 271 List<WarningMessage> hints; | 50 List<WarningMessage> hints; |
| 272 List<WarningMessage> infos; | 51 List<WarningMessage> infos; |
| 273 List<WarningMessage> crashes; | 52 List<WarningMessage> crashes; |
| 274 /// Expected number of warnings. If `null`, the number of warnings is | 53 /// Expected number of warnings. If `null`, the number of warnings is |
| 275 /// not checked. | 54 /// not checked. |
| 276 final int expectedWarnings; | 55 final int expectedWarnings; |
| 277 /// Expected number of errors. If `null`, the number of errors is not checked. | 56 /// Expected number of errors. If `null`, the number of errors is not checked. |
| 278 final int expectedErrors; | 57 final int expectedErrors; |
| 279 final Map<String, SourceFile> sourceFiles; | 58 final Map<String, SourceFile> sourceFiles; |
| 280 Node parsedTree; | 59 Node parsedTree; |
| 281 | 60 |
| 282 MockCompiler.internal( | 61 MockCompiler.internal( |
| 283 {String coreSource: DEFAULT_CORELIB, | 62 {Map<String, String> coreSource, |
| 284 String interceptorsSource: DEFAULT_INTERCEPTORSLIB, | |
| 285 bool enableTypeAssertions: false, | 63 bool enableTypeAssertions: false, |
| 286 bool enableMinification: false, | 64 bool enableMinification: false, |
| 287 bool enableConcreteTypeInference: false, | 65 bool enableConcreteTypeInference: false, |
| 288 int maxConcreteTypeSize: 5, | 66 int maxConcreteTypeSize: 5, |
| 289 bool disableTypeInference: false, | 67 bool disableTypeInference: false, |
| 290 bool analyzeAll: false, | 68 bool analyzeAll: false, |
| 291 bool analyzeOnly: false, | 69 bool analyzeOnly: false, |
| 292 bool emitJavaScript: true, | 70 bool emitJavaScript: true, |
| 293 bool preserveComments: false, | 71 bool preserveComments: false, |
| 294 // Our unit tests check code generation output that is | 72 // Our unit tests check code generation output that is |
| (...skipping 11 matching lines...) Expand all Loading... |
| 306 analyzeOnly: analyzeOnly, | 84 analyzeOnly: analyzeOnly, |
| 307 emitJavaScript: emitJavaScript, | 85 emitJavaScript: emitJavaScript, |
| 308 preserveComments: preserveComments, | 86 preserveComments: preserveComments, |
| 309 showPackageWarnings: true) { | 87 showPackageWarnings: true) { |
| 310 this.disableInlining = disableInlining; | 88 this.disableInlining = disableInlining; |
| 311 | 89 |
| 312 deferredLoadTask = new MockDeferredLoadTask(this); | 90 deferredLoadTask = new MockDeferredLoadTask(this); |
| 313 | 91 |
| 314 clearMessages(); | 92 clearMessages(); |
| 315 | 93 |
| 316 registerSource(Compiler.DART_CORE, coreSource); | 94 registerSource(Compiler.DART_CORE, |
| 317 registerSource(PATCH_CORE, PATCH_CORE_SOURCE); | 95 buildLibrarySource(DEFAULT_CORE_LIBRARY, coreSource)); |
| 96 registerSource(PATCH_CORE, DEFAULT_PATCH_CORE_SOURCE); |
| 318 | 97 |
| 319 registerSource(JavaScriptBackend.DART_JS_HELPER, DEFAULT_HELPERLIB); | 98 registerSource(JavaScriptBackend.DART_JS_HELPER, |
| 320 registerSource(JavaScriptBackend.DART_FOREIGN_HELPER, FOREIGN_LIBRARY); | 99 buildLibrarySource(DEFAULT_JS_HELPER_LIBRARY)); |
| 321 registerSource(JavaScriptBackend.DART_INTERCEPTORS, interceptorsSource); | 100 registerSource(JavaScriptBackend.DART_FOREIGN_HELPER, |
| 101 buildLibrarySource(DEFAULT_FOREIGN_HELPER_LIBRARY)); |
| 102 registerSource(JavaScriptBackend.DART_INTERCEPTORS, |
| 103 buildLibrarySource(DEFAULT_INTERCEPTORS_LIBRARY)); |
| 322 registerSource(JavaScriptBackend.DART_ISOLATE_HELPER, | 104 registerSource(JavaScriptBackend.DART_ISOLATE_HELPER, |
| 323 DEFAULT_ISOLATE_HELPERLIB); | 105 buildLibrarySource(DEFAULT_ISOLATE_HELPER_LIBRARY)); |
| 324 registerSource(Compiler.DART_MIRRORS, DEFAULT_MIRRORS); | 106 registerSource(Compiler.DART_MIRRORS, |
| 107 buildLibrarySource(DEFAULT_MIRRORS_LIBRARY)); |
| 325 } | 108 } |
| 326 | 109 |
| 327 /// Initialize the mock compiler with an empty main library. | 110 /// Initialize the mock compiler with an empty main library. |
| 328 Future init([String mainSource = ""]) { | 111 Future init([String mainSource = ""]) { |
| 329 Uri uri = new Uri(scheme: "mock"); | 112 Uri uri = new Uri(scheme: "mock"); |
| 330 registerSource(uri, mainSource); | 113 registerSource(uri, mainSource); |
| 331 return libraryLoader.loadLibrary(uri) | 114 return libraryLoader.loadLibrary(uri) |
| 332 .then((LibraryElement library) { | 115 .then((LibraryElement library) { |
| 333 mainApp = library; | 116 mainApp = library; |
| 334 // We need to make sure the Object class is resolved. When registering a | 117 // We need to make sure the Object class is resolved. When registering a |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 | 380 |
| 598 class MockElement extends FunctionElementX { | 381 class MockElement extends FunctionElementX { |
| 599 MockElement(Element enclosingElement) | 382 MockElement(Element enclosingElement) |
| 600 : super('', ElementKind.FUNCTION, Modifiers.EMPTY, | 383 : super('', ElementKind.FUNCTION, Modifiers.EMPTY, |
| 601 enclosingElement, false); | 384 enclosingElement, false); |
| 602 | 385 |
| 603 get node => null; | 386 get node => null; |
| 604 | 387 |
| 605 parseNode(_) => null; | 388 parseNode(_) => null; |
| 606 } | 389 } |
| OLD | NEW |