| 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 #import("dart:html", prefix:"html"); | 5 #import("dart:html", prefix:"html"); |
| 6 #import('../../../lib/compiler/compiler.dart', prefix: "compiler_lib"); | 6 #import('../../../lib/compiler/compiler.dart', prefix: "compiler_lib"); |
| 7 #import('../../../lib/uri/uri.dart', prefix:"uri_lib"); | 7 #import('../../../lib/uri/uri.dart', prefix:"uri_lib"); |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * This is the entrypoint for a version of the leg compiler that | 10 * This is the entrypoint for a version of the leg compiler that |
| 11 * runs in the browser. | 11 * runs in the browser. |
| 12 * | 12 * |
| 13 * Because this is running in the browser, we do not access | 13 * Because this is running in the browser, we do not access |
| 14 * the file system. Instead, we assume that all necessary files | 14 * the file system. Instead, we assume that all necessary files |
| 15 * have been placed in inert <script> elements somewhere on the html page. | 15 * have been placed in inert <script> elements somewhere on the html page. |
| 16 * | 16 * |
| 17 * (See legpad.py for more details on how the html page is constructed.) | 17 * (See legpad.py for more details on how the html page is constructed.) |
| 18 */ | 18 */ |
| 19 void main() { | 19 void main() { |
| 20 new Legpad().run(); | 20 new Legpad().run(); |
| 21 } | 21 } |
| 22 | 22 |
| 23 class Legpad { | 23 class Legpad { |
| 24 // id of script element containing name of the main dart file | 24 // id of script element containing name of the main dart file |
| 25 // to compile | 25 // to compile |
| 26 static final String MAIN_ID = "main_id"; | 26 static final String MAIN_ID = "main_id"; |
| 27 | 27 |
| 28 Legpad() : warnings = "" {} | 28 Legpad() : warnings = new StringBuffer(); |
| 29 | 29 |
| 30 // accumulates diagnostic messages emitted by the leg compiler | 30 // accumulates diagnostic messages emitted by the leg compiler |
| 31 String warnings; | 31 StringBuffer warnings; |
| 32 | 32 |
| 33 // the generated javascript | 33 // the generated javascript |
| 34 String output; | 34 String output; |
| 35 | 35 |
| 36 String readAll(String filename) { | 36 String readAll(String filename) { |
| 37 String text = getText(idOfFilename(filename)); | 37 String text = getText(idOfFilename(filename)); |
| 38 print("read $filename (${text.length} bytes)"); | 38 print("read $filename (${text.length} bytes)"); |
| 39 return text; | 39 return text; |
| 40 } | 40 } |
| 41 | 41 |
| 42 void diagnosticHandler(uri_lib.Uri uri, int begin, int end, | 42 void diagnosticHandler(uri_lib.Uri uri, int begin, int end, |
| 43 String message, bool fatal) { | 43 String message, bool fatal) { |
| 44 StringBuffer sb = new StringBuffer(); | 44 warnings.add(message); |
| 45 sb.add(message); | |
| 46 if (uri !== null) { | 45 if (uri !== null) { |
| 47 sb.add(" (${uri.toString()}: $begin, $end)"); | 46 warnings.add(" ($uri: $begin, $end)"); |
| 48 } | 47 } |
| 49 if (fatal) { | 48 if (fatal) { |
| 50 sb.add(" (fatal)"); | 49 warnings.add(" (fatal)"); |
| 51 } | 50 } |
| 52 sb.add("\n"); | 51 warnings.add("\n"); |
| 53 warnings += sb.toString(); | |
| 54 } | 52 } |
| 55 | 53 |
| 56 Future<String> readUriFromString(uri_lib.Uri uri) { | 54 Future<String> readUriFromString(uri_lib.Uri uri) { |
| 57 Completer<String> completer = new Completer<String>(); | 55 Completer<String> completer = new Completer<String>(); |
| 58 completer.complete(readAll(uri.toString())); | 56 completer.complete(readAll(uri.toString())); |
| 59 return completer.future; | 57 return completer.future; |
| 60 } | 58 } |
| 61 | 59 |
| 62 /** | 60 /** |
| 63 * Returns the id of the <script> element that contains | 61 * Returns the id of the <script> element that contains |
| 64 * the contents of this file. (Replace all slashes | 62 * the contents of this file. (Replace all slashes |
| 65 * and dots in the file name with underscores.) | 63 * and dots in the file name with underscores.) |
| 66 */ | 64 */ |
| 67 String idOfFilename(String filename) { | 65 String idOfFilename(String filename) { |
| 68 return filename.replaceAll("/", "_").replaceAll(".", "_"); | 66 return filename.replaceAll("/", "_").replaceAll(".", "_"); |
| 69 } | 67 } |
| 70 | 68 |
| 71 void run() { | 69 void run() { |
| 72 String mainFile = getText(MAIN_ID); | 70 String mainFile = getText(MAIN_ID); |
| 73 setText("input", readAll(mainFile)); | 71 setText("input", readAll(mainFile)); |
| 74 Stopwatch stopwatch = new Stopwatch.start(); | 72 Stopwatch stopwatch = new Stopwatch.start(); |
| 75 runLeg(); | 73 runLeg(); |
| 76 int elapsedMillis = stopwatch.elapsedInMs(); | 74 int elapsedMillis = stopwatch.elapsedInMs(); |
| 77 if (output === null) { | 75 if (output === null) { |
| 78 output = "throw 'dart2js compilation error';\n"; | 76 output = "throw 'dart2js compilation error';\n"; |
| 79 } | 77 } |
| 80 | 78 |
| 81 setText("output", output); | 79 setText("output", output); |
| 82 setText("warnings", warnings); | 80 setText("warnings", warnings.toString()); |
| 83 int lineCount = lineCount(output); | 81 int lineCount = lineCount(output); |
| 84 String timing = "generated $lineCount lines " + | 82 String timing = "generated $lineCount lines " + |
| 85 "(${output.length} characters) in " + | 83 "(${output.length} characters) in " + |
| 86 "${((elapsedMillis) / 1000).toStringAsPrecision(3)} seconds"; | 84 "${((elapsedMillis) / 1000).toStringAsPrecision(3)} seconds"; |
| 87 setText("timing", timing); | 85 setText("timing", timing); |
| 88 } | 86 } |
| 89 | 87 |
| 90 static int lineCount(String s) { | 88 static int lineCount(String s) { |
| 91 Iterable<Match> matches = "\n".allMatches(s); | 89 Iterable<Match> matches = "\n".allMatches(s); |
| 92 int n = 0; | 90 int n = 0; |
| 93 for (Match m in matches) { | 91 for (Match m in matches) { |
| 94 n++; | 92 n++; |
| 95 } | 93 } |
| 96 return n; | 94 return n; |
| 97 } | 95 } |
| 98 | 96 |
| 99 void runLeg() { | 97 void runLeg() { |
| 100 uri_lib.Uri mainUri = new uri_lib.Uri.fromString(getText(MAIN_ID)); | 98 uri_lib.Uri mainUri = new uri_lib.Uri.fromString(getText(MAIN_ID)); |
| 101 uri_lib.Uri libraryRoot = new uri_lib.Uri.fromString("dartdir/"); | 99 uri_lib.Uri libraryRoot = new uri_lib.Uri.fromString("dartdir/"); |
| 102 List<String> compilerArgs = [ | 100 List<String> compilerArgs = [ |
| 103 "--enable_type_checks", | 101 "--enable_type_checks", |
| 104 "--enable_asserts" | 102 "--enable_asserts" |
| 105 ]; | 103 ]; |
| 106 | 104 |
| 107 // TODO(mattsh): dart2js api should be synchronous | 105 // TODO(mattsh): dart2js api should be synchronous |
| (...skipping 21 matching lines...) Expand all Loading... |
| 129 } | 127 } |
| 130 return element.text.trim(); | 128 return element.text.trim(); |
| 131 } | 129 } |
| 132 | 130 |
| 133 // TODO(mattsh): should exist in standard lib somewhere | 131 // TODO(mattsh): should exist in standard lib somewhere |
| 134 static String htmlEscape(String text) { | 132 static String htmlEscape(String text) { |
| 135 return text.replaceAll('&', '&').replaceAll( | 133 return text.replaceAll('&', '&').replaceAll( |
| 136 '>', '>').replaceAll('<', '<'); | 134 '>', '>').replaceAll('<', '<'); |
| 137 } | 135 } |
| 138 } | 136 } |
| OLD | NEW |