| 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('frog_leg'); | 5 #library('frog_leg'); |
| 6 | 6 |
| 7 #import('../../lib/uri/uri.dart'); | 7 #import('../../lib/uri/uri.dart'); |
| 8 #import('../lang.dart', prefix: 'frog'); | 8 #import('../lang.dart', prefix: 'frog'); |
| 9 #import('elements/elements.dart'); | 9 #import('api.dart', prefix: 'api'); |
| 10 #import('io/io.dart', prefix: 'io'); | 10 #import('io/io.dart', prefix: 'io'); |
| 11 #import('leg.dart'); | |
| 12 #import('tree/tree.dart'); | |
| 13 #import('ssa/tracer.dart'); | |
| 14 | 11 |
| 15 bool compile(frog.World world) { | 12 bool compile(frog.World world) { |
| 16 final throwOnError = frog.options.throwOnErrors; | 13 final throwOnError = frog.options.throwOnErrors; |
| 17 final compiler = new WorldCompiler(world, throwOnError); | 14 // final compiler = new WorldCompiler(world, throwOnError); |
| 18 Uri cwd = new Uri(scheme: 'file', path: compiler.currentDirectory); | 15 Uri cwd = new Uri(scheme: 'file', path: io.getCurrentDirectory()); |
| 19 Uri uri = cwd.resolve(frog.options.dartScript); | 16 Uri uri = cwd.resolve(frog.options.dartScript); |
| 20 return compiler.run(uri); | 17 String frogLibDir = frog.options.libDir; |
| 21 } | 18 if (!frogLibDir.endsWith("/")) frogLibDir = "$frogLibDir/"; |
| 19 Uri frogLib = new Uri(scheme: 'file', path: frogLibDir); |
| 20 Uri libraryRoot = frogLib.resolve('../leg/lib/'); |
| 22 | 21 |
| 23 class WorldCompiler extends Compiler { | 22 Future<String> provider(Uri uri) { |
| 24 final frog.World world; | 23 if (uri.scheme != 'file') { |
| 25 final bool throwOnError; | 24 throw new IllegalArgumentException(uri); |
| 26 | |
| 27 WorldCompiler(this.world, this.throwOnError) | |
| 28 : super.withCurrentDirectory(io.getCurrentDirectory(), | |
| 29 tracer: new HTracer()); | |
| 30 | |
| 31 void log(message) { | |
| 32 if (frog.options.showInfo) { | |
| 33 // Avoid calling message.toString() unless showInfo is on. It | |
| 34 // could be slow. | |
| 35 world.info('[leg] $message'); | |
| 36 } | 25 } |
| 26 String source = world.files.readAll(uri.path); |
| 27 world.dartBytesRead += source.length; |
| 28 Completer<String> completer = new Completer<String>(); |
| 29 completer.complete(source); |
| 30 return completer.future; |
| 37 } | 31 } |
| 38 | 32 |
| 39 bool run(Uri uri) { | 33 void handler(Uri uri, int begin, int end, String message, bool fatal) { |
| 40 bool success = super.run(uri); | 34 if (uri === null && !fatal) { |
| 41 if (success) { | 35 world.info('[leg] $message'); |
| 42 var code = assembledCode; | 36 return; |
| 43 world.legCode = code; | |
| 44 world.jsBytesWritten = code.length; | |
| 45 for (final task in tasks) { | |
| 46 log('${task.name} took ${task.timing}msec'); | |
| 47 } | |
| 48 } | 37 } |
| 49 return success; | 38 print(message); |
| 39 if (fatal && throwOnError) new AbortLeg(message); |
| 50 } | 40 } |
| 51 | 41 |
| 52 spanFromNode(Node node, Script current) { | 42 // TODO(ahe): We expect the future to be complete and call value |
| 53 final begin = node.getBeginToken(); | 43 // directly. In effect, we don't support truly asynchronous API. |
| 54 final end = node.getEndToken(); | 44 String code = api.compile(uri, libraryRoot, provider, handler).value; |
| 55 if (begin === null || end === null) { | 45 if (code === null) return false; |
| 56 cancel('cannot find tokens to produce error message for $node.'); | 46 world.legCode = code; |
| 57 } | 47 world.jsBytesWritten = code.length; |
| 58 final startOffset = begin.charOffset; | 48 return true; |
| 59 // TODO(ahe): Compute proper end offset. | |
| 60 final endOffset = end.charOffset + 1; | |
| 61 return new frog.SourceSpan(current.file, startOffset, endOffset); | |
| 62 } | |
| 63 | |
| 64 currentScript() { | |
| 65 if (currentElement === null) return null; | |
| 66 CompilationUnitElement compilationUnit = | |
| 67 currentElement.getCompilationUnit(); | |
| 68 if (compilationUnit === null) return null; | |
| 69 return compilationUnit.script; | |
| 70 } | |
| 71 | |
| 72 reportWarning(Node node, var message) { | |
| 73 frog.SourceSpan span; | |
| 74 Script current = currentScript(); | |
| 75 if (current === null) { | |
| 76 world.fatal('No current script'); | |
| 77 } else if (node === null) { | |
| 78 span = new frog.SourceSpan(current.file, 0, 0); | |
| 79 } else { | |
| 80 span = spanFromNode(node, current); | |
| 81 } | |
| 82 world.warning('$message.', span); | |
| 83 } | |
| 84 | |
| 85 reportError(Node node, var message) { | |
| 86 cancel(message.toString(), node); | |
| 87 } | |
| 88 | |
| 89 Uri getUriFor(String fileName) { | |
| 90 Uri cwd = new Uri(scheme: 'file', path: currentDirectory); | |
| 91 return cwd.resolve(fileName); | |
| 92 } | |
| 93 | |
| 94 Script readScript(Uri uri, [ScriptTag node]) { | |
| 95 String uriName = uri.toString(); | |
| 96 // TODO(ahe): Clean this up. | |
| 97 if (uriName == 'dart:dom') { | |
| 98 uri = getUriFor(io.join([legDirectory, '..', '..', | |
| 99 'client', 'dom', 'frog', 'dom_frog.dart'])); | |
| 100 } else if (uriName == 'dart:html') { | |
| 101 uri = getUriFor(io.join([legDirectory, '..', '..', | |
| 102 'client', 'html', 'frog', 'html_frog.dart'])); | |
| 103 } else if (uriName == 'dart:json') { | |
| 104 uri = getUriFor(io.join([legDirectory, '..', '..', | |
| 105 'lib', 'json', 'json.dart'])); | |
| 106 } | |
| 107 if (uri.scheme != 'file') cancel('cannot read $uri', node: node); | |
| 108 String text = ""; | |
| 109 try { | |
| 110 text = frog.world.files.readAll(uri.path); | |
| 111 } catch (var exception) { | |
| 112 cancel("${uri.path}: $exception", node: node); | |
| 113 } | |
| 114 world.dartBytesRead += text.length; | |
| 115 frog.SourceFile sourceFile = new frog.SourceFile(uri.toString(), text); | |
| 116 return new Script(uri, sourceFile); | |
| 117 } | |
| 118 | |
| 119 String get legDirectory() => io.join([frog.options.libDir, '..', 'leg']); | |
| 120 | |
| 121 void cancel([String reason, Node node, token, instruction, element]) { | |
| 122 Script script = currentScript(); | |
| 123 if (node !== null) { | |
| 124 print(spanFromNode(node, script).toMessageString("cancel leg: $reason")); | |
| 125 } else if (token !== null) { | |
| 126 int begin = token.charOffset; | |
| 127 int end = begin + 1; // TODO(ahe): Compute proper length. | |
| 128 print(script.file.getLocationMessage("cancel leg: $reason", | |
| 129 begin, end, true)); | |
| 130 } else if (element !== null) { | |
| 131 if (element.position() === null) { | |
| 132 // Sometimes, the backend fakes up elements that have no | |
| 133 // position. So we use the enclosing element instead. It is | |
| 134 // not a good error location, but cancel really is "internal | |
| 135 // error" or "not implemented yet", so the vicinity is good | |
| 136 // enough for now. | |
| 137 element = element.enclosingElement; | |
| 138 // TODO(ahe): I plan to overhaul this infrastructure anyways. | |
| 139 } | |
| 140 if (element !== null) { | |
| 141 withCurrentElement(element, | |
| 142 () => cancel(reason: reason, token: element.position())); | |
| 143 } | |
| 144 } else if (currentElement !== null) { | |
| 145 cancel(reason, element: currentElement); | |
| 146 return; | |
| 147 } | |
| 148 if (throwOnError) { | |
| 149 throw new AbortLeg(reason); | |
| 150 } | |
| 151 super.cancel(reason, node, token, instruction); | |
| 152 } | |
| 153 | |
| 154 LibraryElement scanBuiltinLibrary(String filename) { | |
| 155 String fileName = io.join([legDirectory, 'lib', filename]); | |
| 156 Uri cwd = new Uri(scheme: 'file', path: currentDirectory); | |
| 157 Uri uri = cwd.resolve(fileName); | |
| 158 LibraryElement library = scanner.loadLibrary(uri, null); | |
| 159 return library; | |
| 160 } | |
| 161 } | 49 } |
| 162 | 50 |
| 163 class AbortLeg { | 51 class AbortLeg { |
| 164 final message; | 52 final message; |
| 165 AbortLeg(this.message); | 53 AbortLeg(this.message); |
| 166 toString() => 'Aborted due to --throw-on-error: $message'; | 54 toString() => 'Aborted due to --throw-on-error: $message'; |
| 167 } | 55 } |
| OLD | NEW |