Chromium Code Reviews| 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('../../../frog/leg/api.dart', prefix: "api_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 |
|
ahe
2012/03/30 08:55:32
Not a proper comment, and not a documentation comm
| |
| 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 = new StringBuffer() {} | 28 Legpad() : warnings = "" {} |
| 29 | 29 |
| 30 // accumulates diagnostic messages emitted by the leg compiler | 30 // accumulates diagnostic messages emitted by the leg compiler |
|
ahe
2012/03/30 08:55:32
Ditto.
| |
| 31 StringBuffer warnings; | 31 String warnings; |
| 32 | 32 |
| 33 // the generated javascript | 33 // the generated javascript |
|
ahe
2012/03/30 08:55:32
Ditto.
| |
| 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 // TODO(mattsh): format message with location info | 44 StringBuffer sb = new StringBuffer(); |
| 45 warnings.add(message).add("\n"); | 45 sb.add(message); |
| 46 if (uri !== null) { | |
| 47 sb.add(" (${uri.toString()}: $begin, $end)"); | |
| 48 } | |
| 49 if (fatal) { | |
| 50 sb.add(" (fatal)"); | |
| 51 } | |
| 52 sb.add("\n"); | |
| 53 warnings += sb.toString(); | |
|
ahe
2012/03/30 08:55:32
I think this is a weird change. You clearly need a
| |
| 46 } | 54 } |
| 47 | 55 |
| 48 Future<String> readUriFromString(uri_lib.Uri uri) { | 56 Future<String> readUriFromString(uri_lib.Uri uri) { |
| 49 Completer<String> completer = new Completer<String>(); | 57 Completer<String> completer = new Completer<String>(); |
| 50 completer.complete(readAll(uri.toString())); | 58 completer.complete(readAll(uri.toString())); |
| 51 return completer.future; | 59 return completer.future; |
| 52 } | 60 } |
| 53 | 61 |
| 54 /** | 62 /** |
| 55 * Returns the id of the <script> element that contains | 63 * Returns the id of the <script> element that contains |
| 56 * the contents of this file. (Replace all slashes | 64 * the contents of this file. (Replace all slashes |
| 57 * and dots in the file name with underscores.) | 65 * and dots in the file name with underscores.) |
| 58 */ | 66 */ |
| 59 String idOfFilename(String filename) { | 67 String idOfFilename(String filename) { |
| 60 return filename.replaceAll("/", "_").replaceAll(".", "_"); | 68 return filename.replaceAll("/", "_").replaceAll(".", "_"); |
| 61 } | 69 } |
| 62 | 70 |
| 63 void run() { | 71 void run() { |
| 64 String mainFile = getText(MAIN_ID); | 72 String mainFile = getText(MAIN_ID); |
| 65 setText("input", readAll(mainFile)); | 73 setText("input", readAll(mainFile)); |
| 66 Stopwatch stopwatch = new Stopwatch.start(); | 74 Stopwatch stopwatch = new Stopwatch.start(); |
| 67 runLeg(); | 75 runLeg(); |
| 68 int elapsedMillis = stopwatch.elapsedInMs(); | 76 int elapsedMillis = stopwatch.elapsedInMs(); |
| 77 if (output === null) { | |
| 78 output = "throw 'legpad compilation error';\n"; | |
|
ahe
2012/03/30 08:55:32
legpad -> dart2js.
| |
| 79 } | |
| 69 setText("output", output); | 80 setText("output", output); |
| 70 setText("warnings", warnings); | 81 setText("warnings", warnings); |
| 71 String timing = "generated ${output.length} characters in " + | 82 String timing = "generated ${output.length} characters in " + |
| 72 "${((elapsedMillis) / 1000).toStringAsPrecision(3)} seconds"; | 83 "${((elapsedMillis) / 1000).toStringAsPrecision(3)} seconds"; |
| 73 setText("timing", timing); | 84 setText("timing", timing); |
| 74 } | 85 } |
| 75 | 86 |
| 76 void runLeg() { | 87 void runLeg() { |
| 77 uri_lib.Uri mainUri = new uri_lib.Uri.fromString(getText(MAIN_ID)); | 88 uri_lib.Uri mainUri = new uri_lib.Uri.fromString(getText(MAIN_ID)); |
| 78 uri_lib.Uri libraryRoot = | 89 uri_lib.Uri libraryRoot = new uri_lib.Uri.fromString("dartdir/"); |
| 79 new uri_lib.Uri.fromString("dartdir/frog/leg/lib/"); | |
| 80 List<String> compilerArgs = [ | 90 List<String> compilerArgs = [ |
| 81 "--enable_type_checks", | 91 "--enable_type_checks", |
| 82 "--enable_asserts" | 92 "--enable_asserts" |
| 83 ]; | 93 ]; |
| 84 | 94 |
| 85 // TODO(mattsh) - dart2js api should be synchronous | 95 // TODO(mattsh) - dart2js api should be synchronous |
|
ahe
2012/03/30 08:55:32
The form is this:
TODO(ldap): Blah blah blah.
A
| |
| 86 Future<String> futureJavascript = api_lib.compile(mainUri, | 96 Future<String> futureJavascript = compiler_lib.compile(mainUri, |
| 87 libraryRoot, readUriFromString, diagnosticHandler, compilerArgs); | 97 libraryRoot, readUriFromString, diagnosticHandler, compilerArgs); |
| 88 | 98 |
| 89 if (futureJavascript == null) { | 99 if (futureJavascript == null) { |
| 90 output = "throw 'legpad compilation error';\n"; | 100 output = "throw 'legpad compilation error';\n"; |
|
ahe
2012/03/30 08:55:32
legpad -> dart2js
| |
| 91 return; | 101 return; |
| 92 } | 102 } |
| 93 output = futureJavascript.value; | 103 output = futureJavascript.value; |
| 94 } | 104 } |
| 95 | 105 |
| 96 void setText(String id, String text) { | 106 void setText(String id, String text) { |
| 97 html.Element element = html.document.query("#$id"); | 107 html.Element element = html.document.query("#$id"); |
| 98 if (element === null) { | 108 if (element === null) { |
| 99 throw new Exception("Can't find element $id"); | 109 throw new Exception("Can't find element $id"); |
| 100 } | 110 } |
| 101 element.innerHTML = htmlEscape(text); | 111 element.innerHTML = htmlEscape(text); |
| 102 } | 112 } |
| 103 | 113 |
| 104 String getText(String id) { | 114 String getText(String id) { |
| 105 html.Element element = html.document.query("#$id"); | 115 html.Element element = html.document.query("#$id"); |
| 106 if (element === null) { | 116 if (element === null) { |
| 107 throw new Exception("Can't find element $id"); | 117 throw new Exception("Can't find element $id"); |
| 108 } | 118 } |
| 109 return element.text.trim(); | 119 return element.text.trim(); |
| 110 } | 120 } |
| 111 | 121 |
| 112 // TODO(mattsh): should exist in standard lib somewhere | 122 // TODO(mattsh): should exist in standard lib somewhere |
|
ahe
2012/03/30 08:55:32
Not a proper sentence.
| |
| 113 static String htmlEscape(String text) { | 123 static String htmlEscape(String text) { |
| 114 return text.replaceAll('&', '&').replaceAll( | 124 return text.replaceAll('&', '&').replaceAll( |
| 115 '>', '>').replaceAll('<', '<'); | 125 '>', '>').replaceAll('<', '<'); |
| 116 } | 126 } |
| 117 } | 127 } |
| OLD | NEW |