| 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 package com.google.dart.runner; | 5 package com.google.dart.runner; |
| 6 | 6 |
| 7 import com.google.common.base.Joiner; | 7 import com.google.common.base.Joiner; |
| 8 import com.google.common.collect.Lists; | 8 import com.google.common.collect.Lists; |
| 9 import com.google.common.io.CharStreams; | 9 import com.google.common.io.CharStreams; |
| 10 import com.google.common.io.Files; | 10 import com.google.common.io.Files; |
| 11 import com.google.dart.compiler.Backend; | 11 import com.google.dart.compiler.Backend; |
| 12 import com.google.dart.compiler.CommandLineOptions; | 12 import com.google.dart.compiler.CommandLineOptions; |
| 13 import com.google.dart.compiler.CommandLineOptions.DartRunnerOptions; | 13 import com.google.dart.compiler.CommandLineOptions.DartRunnerOptions; |
| 14 import com.google.dart.compiler.CompilerConfiguration; | 14 import com.google.dart.compiler.CompilerConfiguration; |
| 15 import com.google.dart.compiler.DartArtifactProvider; | 15 import com.google.dart.compiler.DartArtifactProvider; |
| 16 import com.google.dart.compiler.DartCompiler; | 16 import com.google.dart.compiler.DartCompiler; |
| 17 import com.google.dart.compiler.DartCompilerListener; | 17 import com.google.dart.compiler.DartCompilerListener; |
| 18 import com.google.dart.compiler.DefaultCompilerConfiguration; | 18 import com.google.dart.compiler.DefaultCompilerConfiguration; |
| 19 import com.google.dart.compiler.DefaultDartCompilerListener; | 19 import com.google.dart.compiler.DefaultDartCompilerListener; |
| 20 import com.google.dart.compiler.LibrarySource; | 20 import com.google.dart.compiler.LibrarySource; |
| 21 import com.google.dart.compiler.Source; | 21 import com.google.dart.compiler.Source; |
| 22 import com.google.dart.compiler.UnitTestBatchRunner; | 22 import com.google.dart.compiler.UnitTestBatchRunner; |
| 23 import com.google.dart.compiler.UnitTestBatchRunner.Invocation; | 23 import com.google.dart.compiler.UnitTestBatchRunner.Invocation; |
| 24 import com.google.dart.compiler.UrlLibrarySource; | 24 import com.google.dart.compiler.UrlLibrarySource; |
| 25 import com.google.dart.compiler.backend.js.ClosureJsBackend; | |
| 26 import com.google.dart.compiler.backend.js.JavascriptBackend; | 25 import com.google.dart.compiler.backend.js.JavascriptBackend; |
| 27 import com.google.debugging.sourcemap.SourceMapConsumerFactory; | |
| 28 import com.google.debugging.sourcemap.SourceMapParseException; | |
| 29 import com.google.debugging.sourcemap.SourceMapSupplier; | |
| 30 import com.google.debugging.sourcemap.SourceMapping; | |
| 31 | 26 |
| 32 import org.kohsuke.args4j.CmdLineException; | 27 import org.kohsuke.args4j.CmdLineException; |
| 33 import org.kohsuke.args4j.CmdLineParser; | 28 import org.kohsuke.args4j.CmdLineParser; |
| 34 | 29 |
| 35 import java.io.ByteArrayOutputStream; | 30 import java.io.ByteArrayOutputStream; |
| 36 import java.io.File; | 31 import java.io.File; |
| 37 import java.io.IOException; | 32 import java.io.IOException; |
| 38 import java.io.OutputStream; | 33 import java.io.OutputStream; |
| 39 import java.io.PrintStream; | 34 import java.io.PrintStream; |
| 40 import java.io.Reader; | 35 import java.io.Reader; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 String script = options.getSourceFiles().get(0); | 114 String script = options.getSourceFiles().get(0); |
| 120 ArrayList<String> scriptArguments = new ArrayList<String>(); | 115 ArrayList<String> scriptArguments = new ArrayList<String>(); |
| 121 | 116 |
| 122 LibrarySource app = new UrlLibrarySource(new File(script)); | 117 LibrarySource app = new UrlLibrarySource(new File(script)); |
| 123 | 118 |
| 124 File outFile = options.getOutputFilename(); | 119 File outFile = options.getOutputFilename(); |
| 125 | 120 |
| 126 DefaultDartCompilerListener listener = | 121 DefaultDartCompilerListener listener = |
| 127 new DefaultDartCompilerListener(stderr, options.printErrorFormat()); | 122 new DefaultDartCompilerListener(stderr, options.printErrorFormat()); |
| 128 | 123 |
| 129 CompilationResult compiled; | 124 String compiled; |
| 130 compiled = compileApp(app, imports, options, listener); | 125 compiled = compileApp(app, imports, options, listener); |
| 131 | 126 |
| 132 if (listener.getErrorCount() != 0) { | 127 if (listener.getErrorCount() != 0) { |
| 133 throw new RunnerError("Compilation failed."); | 128 throw new RunnerError("Compilation failed."); |
| 134 } | 129 } |
| 135 | 130 |
| 136 if (outFile != null) { | 131 if (outFile != null) { |
| 137 File dir = outFile.getParentFile(); | 132 File dir = outFile.getParentFile(); |
| 138 if (dir != null) { | 133 if (dir != null) { |
| 139 if (!dir.exists()) { | 134 if (!dir.exists()) { |
| 140 throw new RunnerError("Cannot create: " + outFile.getName() | 135 throw new RunnerError("Cannot create: " + outFile.getName() |
| 141 + ". " + dir + " does not exist"); | 136 + ". " + dir + " does not exist"); |
| 142 } | 137 } |
| 143 if (!dir.canWrite()) { | 138 if (!dir.canWrite()) { |
| 144 throw new RunnerError("Cannot write " + outFile.getName() + " to " | 139 throw new RunnerError("Cannot write " + outFile.getName() + " to " |
| 145 + dir + ": Permission denied."); | 140 + dir + ": Permission denied."); |
| 146 } | 141 } |
| 147 } else { | 142 } else { |
| 148 dir = new File ("."); | 143 dir = new File ("."); |
| 149 if (!dir.canWrite()) { | 144 if (!dir.canWrite()) { |
| 150 throw new RunnerError("Cannot write " + outFile.getName() + " to " | 145 throw new RunnerError("Cannot write " + outFile.getName() + " to " |
| 151 + dir + ": Permission denied."); | 146 + dir + ": Permission denied."); |
| 152 } | 147 } |
| 153 } | 148 } |
| 154 try { | 149 try { |
| 155 Files.write(compiled.js, outFile, Charset.defaultCharset()); | 150 Files.write(compiled, outFile, Charset.defaultCharset()); |
| 156 } catch (IOException e) { | 151 } catch (IOException e) { |
| 157 throw new RunnerError(e); | 152 throw new RunnerError(e); |
| 158 } | 153 } |
| 159 } | 154 } |
| 160 | 155 |
| 161 if (!options.shouldCompileOnly() && !options.checkOnly()) { | 156 if (!options.shouldCompileOnly() && !options.checkOnly()) { |
| 162 runApp(compiled, app.getName(), options, scriptArguments.toArray(new Strin
g[0]), | 157 runApp(compiled, app.getName(), options, scriptArguments.toArray(new Strin
g[0]), |
| 163 stdout, stderr); | 158 stdout, stderr); |
| 164 } | 159 } |
| 165 } | 160 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 } | 229 } |
| 235 | 230 |
| 236 public static void compileAndRunApp(LibrarySource app, | 231 public static void compileAndRunApp(LibrarySource app, |
| 237 DartRunnerOptions options, | 232 DartRunnerOptions options, |
| 238 CompilerConfiguration config, | 233 CompilerConfiguration config, |
| 239 DartCompilerListener listener, | 234 DartCompilerListener listener, |
| 240 String[] dartArguments, | 235 String[] dartArguments, |
| 241 PrintStream stdout, | 236 PrintStream stdout, |
| 242 PrintStream stderr) | 237 PrintStream stderr) |
| 243 throws RunnerError { | 238 throws RunnerError { |
| 244 CompilationResult compiled = compileApp( | 239 String compiled = compileApp( |
| 245 app, options, Collections.<LibrarySource>emptyList(), config, listener)
; | 240 app, options, Collections.<LibrarySource>emptyList(), config, listener)
; |
| 246 runApp(compiled, app.getName(), options, dartArguments, stdout, stderr); | 241 runApp(compiled, app.getName(), options, dartArguments, stdout, stderr); |
| 247 } | 242 } |
| 248 | 243 |
| 249 private static void runApp(CompilationResult compiled, | 244 private static void runApp(String compiled, |
| 250 String sourceName, | 245 String sourceName, |
| 251 DartRunnerOptions options, | 246 DartRunnerOptions options, |
| 252 String[] scriptArguments, | 247 String[] scriptArguments, |
| 253 PrintStream stdout, | 248 PrintStream stdout, |
| 254 PrintStream stderr) | 249 PrintStream stderr) |
| 255 throws RunnerError { | 250 throws RunnerError { |
| 256 | 251 |
| 257 if (options.useRhino()) { | 252 new V8Launcher().execute(compiled, sourceName, scriptArguments, options, |
| 258 new RhinoLauncher().execute(compiled.js, sourceName, scriptArguments, opti
ons, stdout, stderr); | 253 stdout, stderr); |
| 259 } else { | |
| 260 new V8Launcher(compiled.mapping).execute(compiled.js, sourceName, scriptAr
guments, options, | |
| 261 stdout, stderr); | |
| 262 } | |
| 263 } | 254 } |
| 264 | 255 |
| 265 private static class CompilationResult { | 256 private static String compileApp (LibrarySource app, List<LibrarySource> impor
ts, |
| 266 final SourceMapping mapping; | |
| 267 final String js; | |
| 268 | |
| 269 public CompilationResult(String js, SourceMapping mapping) { | |
| 270 this.mapping = mapping; | |
| 271 this.js = js; | |
| 272 } | |
| 273 } | |
| 274 | |
| 275 private static CompilationResult compileApp (LibrarySource app, List<LibrarySo
urce> imports, | |
| 276 final DartRunnerOptions options, DartCompilerListener listener) throws Run
nerError { | 257 final DartRunnerOptions options, DartCompilerListener listener) throws Run
nerError { |
| 277 Backend backend; | 258 Backend backend = new JavascriptBackend(); |
| 278 if (options.shouldOptimize()) { | |
| 279 backend = new ClosureJsBackend( | |
| 280 options.developerModeChecks(), | |
| 281 options.generateHumanReadableOutput()); | |
| 282 } else { | |
| 283 backend = new JavascriptBackend(); | |
| 284 } | |
| 285 CompilerConfiguration config = new DefaultCompilerConfiguration(backend, opt
ions) { | 259 CompilerConfiguration config = new DefaultCompilerConfiguration(backend, opt
ions) { |
| 286 @Override | 260 @Override |
| 287 public boolean expectEntryPoint() { | 261 public boolean expectEntryPoint() { |
| 288 return true; | 262 return true; |
| 289 } | 263 } |
| 290 | 264 |
| 291 @Override | 265 @Override |
| 292 public boolean typeErrorsAreFatal() { | 266 public boolean typeErrorsAreFatal() { |
| 293 return options.typeErrorsAreFatal(); | 267 return options.typeErrorsAreFatal(); |
| 294 } | 268 } |
| 295 }; | 269 }; |
| 296 return compileApp(app, options, imports, config, listener); | 270 return compileApp(app, options, imports, config, listener); |
| 297 } | 271 } |
| 298 | 272 |
| 299 /** | 273 /** |
| 300 * Parses and compiles an application to Javascript. | 274 * Parses and compiles an application to Javascript. |
| 301 */ | 275 */ |
| 302 private static CompilationResult compileApp(LibrarySource app, | 276 private static String compileApp(LibrarySource app, |
| 303 final DartRunnerOptions options, | 277 final DartRunnerOptions options, |
| 304 List<LibrarySource> imports, | 278 List<LibrarySource> imports, |
| 305 CompilerConfiguration config, | 279 CompilerConfiguration config, |
| 306 DartCompilerListener listener) th
rows RunnerError { | 280 DartCompilerListener listener) throws Runner
Error { |
| 307 try { | 281 try { |
| 308 final RunnerDartArtifactProvider provider = new RunnerDartArtifactProvider
(); | 282 final RunnerDartArtifactProvider provider = new RunnerDartArtifactProvider
(); |
| 309 String errmsg = DartCompiler.compileLib(app, imports, config, provider, li
stener); | 283 String errmsg = DartCompiler.compileLib(app, imports, config, provider, li
stener); |
| 310 if (errmsg != null) { | 284 if (errmsg != null) { |
| 311 throw new RunnerError(errmsg); | 285 throw new RunnerError(errmsg); |
| 312 } | 286 } |
| 313 Backend backend = config.getBackends().get(0); | 287 Backend backend = config.getBackends().get(0); |
| 314 | 288 |
| 315 SourceMapping mapping = null; | |
| 316 if (config.getCompilerOptions().generateSourceMaps()) { | |
| 317 Reader mr = provider.getArtifactReader(app, "", backend.getSourceMapExte
nsion()); | |
| 318 if (mr != null) { | |
| 319 try { | |
| 320 String mapContents = CharStreams.toString(mr); | |
| 321 mapping = SourceMapConsumerFactory.parse(mapContents, new SourceMapS
upplier() { | |
| 322 | |
| 323 @Override | |
| 324 public String getSourceMap(String url) { | |
| 325 String contents = provider.getGeneratedFileContents(url); | |
| 326 if (contents == null || contents.isEmpty()) { | |
| 327 return null; | |
| 328 } | |
| 329 return contents; | |
| 330 } | |
| 331 | |
| 332 }); | |
| 333 } catch (SourceMapParseException e) { | |
| 334 throw new AssertionError(e); | |
| 335 } finally { | |
| 336 mr.close(); | |
| 337 } | |
| 338 } | |
| 339 } | |
| 340 | |
| 341 if (!options.checkOnly()) { | 289 if (!options.checkOnly()) { |
| 342 Reader r = provider.getArtifactReader(app, "", backend.getAppExtension()
); | 290 Reader r = provider.getArtifactReader(app, "", backend.getAppExtension()
); |
| 343 String js = CharStreams.toString(r); | 291 String js = CharStreams.toString(r); |
| 344 r.close(); | 292 r.close(); |
| 345 return new CompilationResult(js, mapping); | 293 return js; |
| 346 } | 294 } |
| 347 return null; | 295 return null; |
| 348 } catch (IOException e) { | 296 } catch (IOException e) { |
| 349 // This can't happen; it's just a StringWriter. | 297 // This can't happen; it's just a StringWriter. |
| 350 throw new AssertionError(e); | 298 throw new AssertionError(e); |
| 351 } | 299 } |
| 352 } | 300 } |
| 353 } | 301 } |
| OLD | NEW |