Chromium Code Reviews| 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 // accumulates diagnostic messages emitted by the leg compiler | |
| 29 String warnings = ""; | |
| 30 | |
| 31 // the generated javascript | |
| 32 String output; | |
| 33 | |
| 34 String readAll(String filename) { | |
| 35 // TODO - leg is confused about where this file is | |
|
ahe
2012/03/26 22:22:07
I think I know what the problem is. See below.
| |
| 36 if (filename == "dartdir/frog/leg/coreimpl.dart") { | |
| 37 filename = "dartdir/frog/leg/lib/coreimpl.dart"; | |
| 38 } | |
| 39 String text = getText(idOfFilename(filename)); | |
| 40 print("read $filename (${text.length} bytes)"); | |
| 41 return text; | |
| 42 } | |
| 43 | |
| 44 void diagnosticHandler(uri_lib.Uri uri, int begin, int end, | |
| 45 String message, bool fatal) { | |
| 46 // TODO format message with location info | |
| 47 warnings += message + "\n"; | |
| 48 } | |
| 49 | |
| 50 Future<String> readUriFromString(uri_lib.Uri uri) { | |
| 51 Completer<String> completer = new Completer<String>(); | |
| 52 completer.complete(readAll(uri.toString())); | |
| 53 return completer.future; | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Returns the id of the <script> element that contains | |
| 58 * the contents of this file. (Replace all slashes | |
| 59 * and dots in the file name with underscores.) | |
| 60 */ | |
| 61 String idOfFilename(String filename) { | |
| 62 return filename.replaceAll("/", "_").replaceAll(".", "_"); | |
| 63 } | |
| 64 | |
| 65 void run() { | |
| 66 String mainFile = getText(MAIN_ID); | |
| 67 setText("input", readAll(mainFile)); | |
| 68 int time1 = new Date.now().value; | |
| 69 runLeg(); | |
| 70 int time2 = new Date.now().value; | |
| 71 setText("output", output); | |
| 72 setText("warnings", warnings); | |
| 73 String timing = "generated ${output.length} characters in " + | |
| 74 "${((time2 - time1) / 1000).toStringAsPrecision(3)} seconds"; | |
| 75 setText("timing", timing); | |
| 76 } | |
| 77 | |
| 78 void runLeg() { | |
| 79 uri_lib.Uri mainUri = new uri_lib.Uri.fromString(getText(MAIN_ID)); | |
| 80 uri_lib.Uri libraryRoot = | |
| 81 new uri_lib.Uri.fromString("dartdir/frog/leg/lib"); | |
|
ahe
2012/03/26 22:22:07
Leg is not confused :-)
The URI must end with a s
| |
| 82 List<String> compilerArgs = [ | |
| 83 "--enable_type_checks", | |
| 84 "--enable_asserts" | |
| 85 ]; | |
| 86 Future<String> futureJavascript = api_lib.compile(mainUri, | |
| 87 libraryRoot, readUriFromString, diagnosticHandler, compilerArgs); | |
| 88 if (futureJavascript == null) { | |
| 89 output = "throw 'legpad compilation error';\n"; | |
| 90 return; | |
| 91 } | |
| 92 output = futureJavascript.value; | |
| 93 } | |
| 94 | |
| 95 void setText(String id, String text) { | |
| 96 html.Element element = html.document.query("#$id"); | |
| 97 if (element === null) { | |
| 98 throw new Exception("Can't find element $id"); | |
| 99 } | |
| 100 element.innerHTML = htmlEscape(text); | |
| 101 } | |
| 102 | |
| 103 String getText(String id) { | |
| 104 html.Element element = html.document.query("#$id"); | |
| 105 if (element === null) { | |
| 106 throw new Exception("Can't find element $id"); | |
| 107 } | |
| 108 return element.text.trim(); | |
| 109 } | |
| 110 | |
| 111 // TODO: should exist in standard lib somewhere | |
| 112 static String htmlEscape(String text) { | |
| 113 return text.replaceAll('&', '&').replaceAll( | |
| 114 '>', '>').replaceAll('<', '<'); | |
| 115 } | |
| 116 } | |
| OLD | NEW |