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