| 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('../../../lib/compiler/compiler.dart', prefix: "compiler_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 const 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 warnings.add(message); | |
| 45 if (uri != null) { | |
| 46 warnings.add(" ($uri: $begin, $end)"); | |
| 47 } | |
| 48 if (fatal) { | |
| 49 warnings.add(" (fatal)"); | |
| 50 } | |
| 51 warnings.add("\n"); | |
| 52 } | |
| 53 | |
| 54 Future<String> readUriFromString(uri_lib.Uri uri) { | |
| 55 Completer<String> completer = new Completer<String>(); | |
| 56 completer.complete(readAll(uri.toString())); | |
| 57 return completer.future; | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * Returns the id of the <script> element that contains | |
| 62 * the contents of this file. (Replace all slashes | |
| 63 * and dots in the file name with underscores.) | |
| 64 */ | |
| 65 String idOfFilename(String filename) { | |
| 66 return filename.replaceAll("/", "_").replaceAll(".", "_"); | |
| 67 } | |
| 68 | |
| 69 void run() { | |
| 70 String mainFile = getText(MAIN_ID); | |
| 71 setText("input", readAll(mainFile)); | |
| 72 Stopwatch stopwatch = new Stopwatch()..start(); | |
| 73 runLeg(); | |
| 74 int elapsedMillis = stopwatch.elapsedMilliseconds; | |
| 75 if (output == null) { | |
| 76 output = "throw 'dart2js compilation error';\n"; | |
| 77 } | |
| 78 | |
| 79 setText("output", output); | |
| 80 setText("warnings", warnings.toString()); | |
| 81 int lineCount = lineCount(output); | |
| 82 String timing = "generated $lineCount lines " | |
| 83 "(${output.length} characters) in " | |
| 84 "${((elapsedMillis) / 1000).toStringAsPrecision(3)} seconds"; | |
| 85 setText("timing", timing); | |
| 86 } | |
| 87 | |
| 88 static int lineCount(String s) { | |
| 89 Iterable<Match> matches = "\n".allMatches(s); | |
| 90 int n = 0; | |
| 91 for (Match m in matches) { | |
| 92 n++; | |
| 93 } | |
| 94 return n; | |
| 95 } | |
| 96 | |
| 97 void runLeg() { | |
| 98 uri_lib.Uri mainUri = new uri_lib.Uri.fromString(getText(MAIN_ID)); | |
| 99 uri_lib.Uri libraryRoot = new uri_lib.Uri.fromString("dartdir/"); | |
| 100 List<String> compilerArgs = [ | |
| 101 "--enable_type_checks", | |
| 102 "--enable_asserts" | |
| 103 ]; | |
| 104 | |
| 105 // TODO(mattsh): dart2js api should be synchronous | |
| 106 Future<String> futureJavascript = compiler_lib.compile(mainUri, | |
| 107 libraryRoot, readUriFromString, diagnosticHandler, compilerArgs); | |
| 108 | |
| 109 if (futureJavascript == null) { | |
| 110 return; | |
| 111 } | |
| 112 output = futureJavascript.value; | |
| 113 } | |
| 114 | |
| 115 void setText(String id, String text) { | |
| 116 html.Element element = html.document.query("#$id"); | |
| 117 if (element == null) { | |
| 118 throw new Exception("Can't find element $id"); | |
| 119 } | |
| 120 element.innerHTML = htmlEscape(text); | |
| 121 } | |
| 122 | |
| 123 String getText(String id) { | |
| 124 html.Element element = html.document.query("#$id"); | |
| 125 if (element == null) { | |
| 126 throw new Exception("Can't find element $id"); | |
| 127 } | |
| 128 return element.text.trim(); | |
| 129 } | |
| 130 | |
| 131 // TODO(mattsh): should exist in standard lib somewhere | |
| 132 static String htmlEscape(String text) { | |
| 133 return text.replaceAll('&', '&').replaceAll( | |
| 134 '>', '>').replaceAll('<', '<'); | |
| 135 } | |
| 136 } | |
| OLD | NEW |