| 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 compiler; | 5 library compiler; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'src/apiimpl.dart'; | 8 import 'src/apiimpl.dart'; |
| 9 | 9 |
| 10 // Unless explicitly allowed, passing [:null:] for any argument to the | 10 // Unless explicitly allowed, passing [:null:] for any argument to the |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 * [:null:]. If [uri] is not [:null:], neither are [begin] and | 58 * [:null:]. If [uri] is not [:null:], neither are [begin] and |
| 59 * [end]. [uri] indicates the compilation unit from where the | 59 * [end]. [uri] indicates the compilation unit from where the |
| 60 * diagnostic originates. [begin] and [end] are zero-based character | 60 * diagnostic originates. [begin] and [end] are zero-based character |
| 61 * offsets from the beginning of the compilaton unit. [message] is the | 61 * offsets from the beginning of the compilaton unit. [message] is the |
| 62 * diagnostic message, and [kind] indicates indicates what kind of | 62 * diagnostic message, and [kind] indicates indicates what kind of |
| 63 * diagnostic it is. | 63 * diagnostic it is. |
| 64 */ | 64 */ |
| 65 typedef void DiagnosticHandler(Uri uri, int begin, int end, | 65 typedef void DiagnosticHandler(Uri uri, int begin, int end, |
| 66 String message, Diagnostic kind); | 66 String message, Diagnostic kind); |
| 67 | 67 |
| 68 /// Information resulting from the compilation. | |
| 69 class CompilationResult { | |
| 70 /// `true` if the compilation succeeded, that is, compilation didn't fail due | |
| 71 /// to compile-time errors and/or internal errors. | |
| 72 final bool isSuccess; | |
| 73 | |
| 74 /// The compiler object used for the compilation. | |
| 75 /// | |
| 76 /// Note: The type of [compiler] is implementation dependent and may vary. | |
| 77 /// Use only for debugging and testing. | |
| 78 final compiler; | |
| 79 | |
| 80 CompilationResult(this.compiler, {this.isSuccess: true}); | |
| 81 } | |
| 82 | |
| 83 /** | 68 /** |
| 84 * Returns a future that completes to a non-null String when [script] | 69 * Returns a future that completes to a non-null String when [script] |
| 85 * has been successfully compiled. | 70 * has been successfully compiled. |
| 86 * | 71 * |
| 87 * The compiler output is obtained by providing an [outputProvider]. | 72 * The compiler output is obtained by providing an [outputProvider]. |
| 88 * | 73 * |
| 89 * If the compilation fails, the future's value will be [:null:] and | 74 * If the compilation fails, the future's value will be [:null:] and |
| 90 * [handler] will have been invoked at least once with [:kind == | 75 * [handler] will have been invoked at least once with [:kind == |
| 91 * Diagnostic.ERROR:] or [:kind == Diagnostic.CRASH:]. | 76 * Diagnostic.ERROR:] or [:kind == Diagnostic.CRASH:]. |
| 92 * | 77 * |
| 93 * Deprecated: if no [outputProvider] is given, the future completes | 78 * Deprecated: if no [outputProvider] is given, the future completes |
| 94 * to the compiled script. This behavior will be removed in the future | 79 * to the compiled script. This behavior will be removed in the future |
| 95 * as the compiler may create multiple files to support lazy loading | 80 * as the compiler may create multiple files to support lazy loading |
| 96 * of libraries. | 81 * of libraries. |
| 97 */ | 82 */ |
| 98 Future<CompilationResult> compile( | 83 Future<String> compile(Uri script, |
| 99 Uri script, | 84 Uri libraryRoot, |
| 100 Uri libraryRoot, | 85 Uri packageRoot, |
| 101 Uri packageRoot, | 86 CompilerInputProvider inputProvider, |
| 102 CompilerInputProvider inputProvider, | 87 DiagnosticHandler handler, |
| 103 DiagnosticHandler handler, | 88 [List<String> options = const [], |
| 104 [List<String> options = const [], | 89 CompilerOutputProvider outputProvider, |
| 105 CompilerOutputProvider outputProvider, | 90 Map<String, dynamic> environment = const {}]) { |
| 106 Map<String, dynamic> environment = const {}]) { | |
| 107 if (!libraryRoot.path.endsWith("/")) { | 91 if (!libraryRoot.path.endsWith("/")) { |
| 108 throw new ArgumentError("libraryRoot must end with a /"); | 92 throw new ArgumentError("libraryRoot must end with a /"); |
| 109 } | 93 } |
| 110 if (packageRoot != null && !packageRoot.path.endsWith("/")) { | 94 if (packageRoot != null && !packageRoot.path.endsWith("/")) { |
| 111 throw new ArgumentError("packageRoot must end with a /"); | 95 throw new ArgumentError("packageRoot must end with a /"); |
| 112 } | 96 } |
| 113 // TODO(ahe): Consider completing the future with an exception if | 97 // TODO(ahe): Consider completing the future with an exception if |
| 114 // code is null. | 98 // code is null. |
| 115 Compiler compiler = new Compiler(inputProvider, | 99 Compiler compiler = new Compiler(inputProvider, |
| 116 outputProvider, | 100 outputProvider, |
| 117 handler, | 101 handler, |
| 118 libraryRoot, | 102 libraryRoot, |
| 119 packageRoot, | 103 packageRoot, |
| 120 options, | 104 options, |
| 121 environment); | 105 environment); |
| 122 return compiler.run(script).then((bool success) { | 106 // TODO(ahe): Use the value of the future (which signals success or failure). |
| 123 return new CompilationResult(compiler, isSuccess: success); | 107 return compiler.run(script).then((_) { |
| 108 String code = compiler.assembledCode; |
| 109 if (code != null && outputProvider != null) { |
| 110 code = ''; // Non-null signals success. |
| 111 } |
| 112 return code; |
| 124 }); | 113 }); |
| 125 } | 114 } |
| 126 | 115 |
| 127 /** | 116 /** |
| 128 * Kind of diagnostics that the compiler can report. | 117 * Kind of diagnostics that the compiler can report. |
| 129 */ | 118 */ |
| 130 class Diagnostic { | 119 class Diagnostic { |
| 131 /** | 120 /** |
| 132 * An error as identified by the "Dart Programming Language | 121 * An error as identified by the "Dart Programming Language |
| 133 * Specification" [http://www.dartlang.org/docs/spec/]. | 122 * Specification" [http://www.dartlang.org/docs/spec/]. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 final String name; | 178 final String name; |
| 190 | 179 |
| 191 /** | 180 /** |
| 192 * This constructor is not private to support user-defined | 181 * This constructor is not private to support user-defined |
| 193 * diagnostic kinds. | 182 * diagnostic kinds. |
| 194 */ | 183 */ |
| 195 const Diagnostic(this.ordinal, this.name); | 184 const Diagnostic(this.ordinal, this.name); |
| 196 | 185 |
| 197 String toString() => name; | 186 String toString() => name; |
| 198 } | 187 } |
| OLD | NEW |