| 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('leg_apiimpl'); | 5 #library('leg_apiimpl'); |
| 6 | 6 |
| 7 #import('dart:uri'); |
| 8 |
| 9 #import('../compiler.dart', prefix: 'api'); |
| 7 #import('leg.dart', prefix: 'leg'); | 10 #import('leg.dart', prefix: 'leg'); |
| 8 #import('elements/elements.dart', prefix: 'leg'); | 11 #import('tree/tree.dart', prefix: 'tree'); |
| 9 #import('tree/tree.dart', prefix: 'leg'); | 12 #import('elements/elements.dart', prefix: 'elements'); |
| 10 #import('ssa/tracer.dart', prefix: 'ssa'); | 13 #import('ssa/tracer.dart', prefix: 'ssa'); |
| 11 // TODO(ahe): Remove dependency on frog. | |
| 12 #import('../../../frog/lang.dart', prefix: 'frog'); | |
| 13 #import('../compiler.dart'); | |
| 14 #import('../../uri/uri.dart'); | |
| 15 #import('library_map.dart'); | 14 #import('library_map.dart'); |
| 16 | 15 #import('source_file.dart'); |
| 17 | 16 |
| 18 class Compiler extends leg.Compiler { | 17 class Compiler extends leg.Compiler { |
| 19 ReadUriFromString provider; | 18 api.ReadUriFromString provider; |
| 20 DiagnosticHandler handler; | 19 api.DiagnosticHandler handler; |
| 21 Uri libraryRoot; | 20 Uri libraryRoot; |
| 22 Uri packageRoot; | 21 Uri packageRoot; |
| 23 List<String> options; | 22 List<String> options; |
| 24 bool mockableLibraryUsed = false; | 23 bool mockableLibraryUsed = false; |
| 25 | 24 |
| 26 Compiler(this.provider, this.handler, this.libraryRoot, this.options) | 25 Compiler(this.provider, this.handler, this.libraryRoot, this.options) |
| 27 : super(tracer: new ssa.HTracer()); | 26 : super(tracer: new ssa.HTracer()); |
| 28 | 27 |
| 29 leg.LibraryElement scanBuiltinLibrary(String path) { | 28 elements.LibraryElement scanBuiltinLibrary(String path) { |
| 30 Uri uri = libraryRoot.resolve(DART2JS_LIBRARY_MAP[path]); | 29 Uri uri = libraryRoot.resolve(DART2JS_LIBRARY_MAP[path]); |
| 31 leg.LibraryElement library = scanner.loadLibrary(uri, null); | 30 elements.LibraryElement library = scanner.loadLibrary(uri, null); |
| 32 return library; | 31 return library; |
| 33 } | 32 } |
| 34 | 33 |
| 35 void log(message) { | 34 void log(message) { |
| 36 handler(null, null, null, message, false); | 35 handler(null, null, null, message, false); |
| 37 } | 36 } |
| 38 | 37 |
| 39 leg.Script readScript(Uri uri, [leg.ScriptTag node]) { | 38 leg.Script readScript(Uri uri, [tree.ScriptTag node]) { |
| 40 if (uri.scheme == 'dart') { | 39 if (uri.scheme == 'dart') { |
| 41 uri = translateDartUri(uri, node); | 40 uri = translateDartUri(uri, node); |
| 42 } else if (uri.scheme == 'package') { | 41 } else if (uri.scheme == 'package') { |
| 43 uri = translatePackageUri(uri, node); | 42 uri = translatePackageUri(uri, node); |
| 44 } | 43 } |
| 45 String text = ""; | 44 String text = ""; |
| 46 try { | 45 try { |
| 47 // TODO(ahe): We expect the future to be complete and call value | 46 // TODO(ahe): We expect the future to be complete and call value |
| 48 // directly. In effect, we don't support truly asynchronous API. | 47 // directly. In effect, we don't support truly asynchronous API. |
| 49 text = provider(uri).value; | 48 text = provider(uri).value; |
| 50 } catch (var exception) { | 49 } catch (var exception) { |
| 51 cancel("${uri}: $exception", node: node); | 50 cancel("${uri}: $exception", node: node); |
| 52 } | 51 } |
| 53 frog.SourceFile sourceFile = new frog.SourceFile(uri.toString(), text); | 52 SourceFile sourceFile = new SourceFile(uri.toString(), text); |
| 54 return new leg.Script(uri, sourceFile); | 53 return new leg.Script(uri, sourceFile); |
| 55 } | 54 } |
| 56 | 55 |
| 57 translateDartUri(Uri uri, leg.ScriptTag node) { | 56 translateDartUri(Uri uri, tree.ScriptTag node) { |
| 58 String path = DART2JS_LIBRARY_MAP[uri.path]; | 57 String path = DART2JS_LIBRARY_MAP[uri.path]; |
| 59 if (path === null || uri.path.startsWith('_')) { | 58 if (path === null || uri.path.startsWith('_')) { |
| 60 reportError(node, 'library not found ${uri}'); | 59 reportError(node, 'library not found ${uri}'); |
| 61 return null; | 60 return null; |
| 62 } | 61 } |
| 63 if (uri.path == 'dom' || uri.path == 'html' || uri.path == 'io') { | 62 if (uri.path == 'dom' || uri.path == 'html' || uri.path == 'io') { |
| 64 // TODO(ahe): Get rid of mockableLibraryUsed when test.dart | 63 // TODO(ahe): Get rid of mockableLibraryUsed when test.dart |
| 65 // supports this use case better. | 64 // supports this use case better. |
| 66 mockableLibraryUsed = true; | 65 mockableLibraryUsed = true; |
| 67 } | 66 } |
| 68 return libraryRoot.resolve(path); | 67 return libraryRoot.resolve(path); |
| 69 } | 68 } |
| 70 | 69 |
| 71 translatePackageUri(Uri uri, leg.ScriptTag node) => | 70 translatePackageUri(Uri uri, tree.ScriptTag node) => |
| 72 packageRoot.resolve(uri.path); | 71 packageRoot.resolve(uri.path); |
| 73 | 72 |
| 74 bool run(Uri uri) { | 73 bool run(Uri uri) { |
| 75 try { | 74 try { |
| 76 packageRoot = uri.resolve('packages/'); | 75 packageRoot = uri.resolve('packages/'); |
| 77 bool success = super.run(uri); | 76 bool success = super.run(uri); |
| 78 for (final task in tasks) { | 77 for (final task in tasks) { |
| 79 log('${task.name} took ${task.timing}msec'); | 78 log('${task.name} took ${task.timing}msec'); |
| 80 } | 79 } |
| 81 return success; | 80 return success; |
| 82 } finally { | 81 } finally { |
| 83 packageRoot = null; | 82 packageRoot = null; |
| 84 } | 83 } |
| 85 } | 84 } |
| 86 | 85 |
| 87 void reportDiagnostic(leg.SourceSpan span, String message, bool fatal) { | 86 void reportDiagnostic(leg.SourceSpan span, String message, bool fatal) { |
| 88 if (span === null) { | 87 if (span === null) { |
| 89 handler(null, null, null, message, fatal); | 88 handler(null, null, null, message, fatal); |
| 90 } else { | 89 } else { |
| 91 handler(span.uri, span.begin, span.end, message, fatal); | 90 handler(span.uri, span.begin, span.end, message, fatal); |
| 92 } | 91 } |
| 93 } | 92 } |
| 94 | 93 |
| 95 bool get isMockCompilation() { | 94 bool get isMockCompilation() { |
| 96 return mockableLibraryUsed | 95 return mockableLibraryUsed |
| 97 && (options.indexOf('--allow-mock-compilation') !== -1); | 96 && (options.indexOf('--allow-mock-compilation') !== -1); |
| 98 } | 97 } |
| 99 } | 98 } |
| OLD | NEW |