| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #library('mock_compiler'); | |
| 6 | |
| 7 #import("dart:uri"); | |
| 8 | |
| 9 #import("../../../lib/compiler/implementation/elements/elements.dart"); | |
| 10 #import("../../../lib/compiler/implementation/leg.dart"); | |
| 11 #import("../../../lib/compiler/implementation/tree/tree.dart"); | |
| 12 #import("../../../lib/compiler/implementation/util/util.dart"); | |
| 13 #import("parser_helper.dart"); | |
| 14 | |
| 15 class WarningMessage { | |
| 16 Node node; | |
| 17 Message message; | |
| 18 WarningMessage(this.node, this.message); | |
| 19 | |
| 20 toString() => message.toString(); | |
| 21 } | |
| 22 | |
| 23 final String DEFAULT_HELPERLIB = @''' | |
| 24 lt() {} add(var a, var b) {} sub() {} mul() {} div() {} tdiv() {} mod() {} | |
| 25 neg() {} shl() {} shr() {} eq() {} le() {} gt() {} ge() {} | |
| 26 or() {} and() {} not() {} eqNull(a) {} eqq() {} | |
| 27 ltB() {} leB() {} eqB() {} gtB() {} geB() {} eqNullB(a) {} | |
| 28 iae(x) {throw x;} ioore(x) {throw x;} | |
| 29 guard$array(x) { return x; } | |
| 30 guard$num(x) { return x; } | |
| 31 guard$string(x) { return x; } | |
| 32 guard$stringOrArray(x) { return x; } | |
| 33 index(a, index) {} | |
| 34 indexSet(a, index, value) {} | |
| 35 setRuntimeTypeInfo(a, b) {} | |
| 36 getRuntimeTypeInfo(a) {} | |
| 37 interface JavaScriptIndexingBehavior {} | |
| 38 S() {}'''; | |
| 39 | |
| 40 final String DEFAULT_INTERCEPTORSLIB = @''' | |
| 41 add$1(receiver, value) {} | |
| 42 get$length(receiver) {} | |
| 43 filter(receiver, predicate) {} | |
| 44 removeLast(receiver) {} | |
| 45 iterator(receiver) {} | |
| 46 next(receiver) {} | |
| 47 hasNext(receiver) {}'''; | |
| 48 | |
| 49 final String DEFAULT_CORELIB = @''' | |
| 50 print(var obj) {} | |
| 51 interface int extends num {} | |
| 52 interface double extends num {} | |
| 53 class bool {} | |
| 54 class String {} | |
| 55 class Object {} | |
| 56 interface num {} | |
| 57 class Function {} | |
| 58 class List {} | |
| 59 class Closure {} | |
| 60 class Null {} | |
| 61 class Dynamic {}'''; | |
| 62 | |
| 63 class MockCompiler extends Compiler { | |
| 64 List<WarningMessage> warnings; | |
| 65 List<WarningMessage> errors; | |
| 66 final Map<String, String> sources; | |
| 67 Node parsedTree; | |
| 68 | |
| 69 MockCompiler([String coreSource = DEFAULT_CORELIB, | |
| 70 String helperSource = DEFAULT_HELPERLIB, | |
| 71 String interceptorsSource = DEFAULT_INTERCEPTORSLIB]) | |
| 72 : warnings = [], errors = [], | |
| 73 sources = new Map<String, String>(), | |
| 74 super() { | |
| 75 Uri uri = new Uri(scheme: "source"); | |
| 76 var script = new Script(uri, new MockFile(coreSource)); | |
| 77 coreLibrary = new LibraryElement(script); | |
| 78 parseScript(coreSource, coreLibrary); | |
| 79 | |
| 80 script = new Script(uri, new MockFile(helperSource)); | |
| 81 jsHelperLibrary = new LibraryElement(script); | |
| 82 parseScript(helperSource, jsHelperLibrary); | |
| 83 | |
| 84 script = new Script(uri, new MockFile(interceptorsSource)); | |
| 85 interceptorsLibrary = new LibraryElement(script); | |
| 86 parseScript(interceptorsSource, interceptorsLibrary); | |
| 87 | |
| 88 scanner.importLibrary(jsHelperLibrary, coreLibrary, null); | |
| 89 scanner.importLibrary(interceptorsLibrary, coreLibrary, null); | |
| 90 mainApp = mockLibrary(this, ""); | |
| 91 initializeSpecialClasses(); | |
| 92 } | |
| 93 | |
| 94 void reportWarning(Node node, var message) { | |
| 95 warnings.add(new WarningMessage(node, message.message)); | |
| 96 } | |
| 97 | |
| 98 void reportError(Node node, var message) { | |
| 99 if (message is String && message.startsWith("no #library tag found in")) { | |
| 100 // TODO(ahe): Fix the MockCompiler to not have this problem. | |
| 101 return; | |
| 102 } | |
| 103 errors.add(new WarningMessage(node, message.message)); | |
| 104 } | |
| 105 | |
| 106 void reportDiagnostic(SourceSpan span, String message, bool fatal) { | |
| 107 print(message); | |
| 108 } | |
| 109 | |
| 110 void clearWarnings() { | |
| 111 warnings = []; | |
| 112 } | |
| 113 | |
| 114 void clearErrors() { | |
| 115 errors = []; | |
| 116 } | |
| 117 | |
| 118 TreeElementMapping resolveStatement(String text) { | |
| 119 parsedTree = parseStatement(text); | |
| 120 return resolveNodeStatement(parsedTree, mainApp); | |
| 121 } | |
| 122 | |
| 123 TreeElementMapping resolveNodeStatement(Node tree, Element element) { | |
| 124 ResolverVisitor visitor = new ResolverVisitor(this, element); | |
| 125 if (visitor.context is TopScope) { | |
| 126 visitor.context = new BlockScope(visitor.context); | |
| 127 } | |
| 128 visitor.visit(tree); | |
| 129 visitor.context = new TopScope(element.getLibrary()); | |
| 130 // Resolve the type annotations encountered in the code. | |
| 131 while (!resolver.toResolve.isEmpty()) { | |
| 132 resolver.toResolve.removeFirst().ensureResolved(this); | |
| 133 } | |
| 134 return visitor.mapping; | |
| 135 } | |
| 136 | |
| 137 resolverVisitor() { | |
| 138 Element mockElement = | |
| 139 new Element(buildSourceString(''), ElementKind.FUNCTION, mainApp); | |
| 140 ResolverVisitor visitor = new ResolverVisitor(this, mockElement); | |
| 141 visitor.context = new BlockScope(visitor.context); | |
| 142 return visitor; | |
| 143 } | |
| 144 | |
| 145 parseScript(String text, [LibraryElement library]) { | |
| 146 if (library === null) library = mainApp; | |
| 147 parseUnit(text, this, library); | |
| 148 } | |
| 149 | |
| 150 void scanBuiltinLibraries() { | |
| 151 // Do nothing. The mock core library is already handled in the constructor. | |
| 152 } | |
| 153 | |
| 154 LibraryElement scanBuiltinLibrary(String name) { | |
| 155 // Do nothing. The mock core library is already handled in the constructor. | |
| 156 } | |
| 157 | |
| 158 Script readScript(Uri uri, [ScriptTag node]) { | |
| 159 String code = sources[uri.toString()]; | |
| 160 if (code === null) throw new IllegalArgumentException(uri); | |
| 161 return new StringScript(code); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 void compareWarningKinds(String text, expectedWarnings, foundWarnings) { | |
| 166 var fail = (message) => Expect.fail('$text: $message'); | |
| 167 Iterator<MessageKind> expected = expectedWarnings.iterator(); | |
| 168 Iterator<WarningMessage> found = foundWarnings.iterator(); | |
| 169 while (expected.hasNext() && found.hasNext()) { | |
| 170 Expect.equals(expected.next(), found.next().message.kind); | |
| 171 } | |
| 172 if (expected.hasNext()) { | |
| 173 do { | |
| 174 print('Expected warning "${expected.next()}" did not occur'); | |
| 175 } while (expected.hasNext()); | |
| 176 fail('Too few warnings'); | |
| 177 } | |
| 178 if (found.hasNext()) { | |
| 179 do { | |
| 180 print('Additional warning "${found.next()}"'); | |
| 181 } while (found.hasNext()); | |
| 182 fail('Too many warnings'); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 void importLibrary(LibraryElement target, LibraryElement imported, | |
| 187 Compiler compiler) { | |
| 188 for (Link<Element> link = imported.topLevelElements; !link.isEmpty(); | |
| 189 link = link.tail) { | |
| 190 compiler.withCurrentElement(link.head, () { | |
| 191 target.define(link.head, compiler); | |
| 192 }); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 LibraryElement mockLibrary(Compiler compiler, String source) { | |
| 197 Uri uri = new Uri(scheme: "source"); | |
| 198 var library = new LibraryElement(new Script(uri, new MockFile(source))); | |
| 199 importLibrary(library, compiler.coreLibrary, compiler); | |
| 200 return library; | |
| 201 } | |
| 202 | |
| 203 class StringScript extends Script { | |
| 204 final String code; | |
| 205 StringScript(this.code) : super(null, null); | |
| 206 String get text() => code; | |
| 207 String get name() => "mock script"; | |
| 208 } | |
| OLD | NEW |