Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tools/testing/legpad/legpad.dart

Issue 9866007: leg pad (similar to frogpad, but for leg) (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: small fixes Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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().go();
21 }
22
23 class Legpad {
24 // id of script element containing name of the main dart file
floitsch 2012/03/26 22:07:05 Id and finish sentence with '.'.
25 // to compile
floitsch 2012/03/26 22:07:05 Unless I'm wrong this is the Id of the script elem
mattsh 2012/03/27 02:30:25 (just chatted offline, yes, this is in fact the fi
26 static final String MAIN_ID = "main_id";
27
28 // accumulates diagnostic messages emitted by the leg compiler
floitsch 2012/03/26 22:07:05 ditto: start with uppercase and finish with '.'.
29 String warnings = "";
floitsch 2012/03/26 22:07:05 Make it a StringBuffer?
mattsh 2012/03/27 02:30:25 Done.
30
31 // the generated javascript
32 String output;
33
34 String readAll(String filename) {
35 // TODO - currently leg is confused about where these two files
floitsch 2012/03/26 22:07:05 TODO(ldap) or TODO(issue-number). Followed by ":".
mattsh 2012/03/27 02:30:25 Done.
36 // are located
37 if (filename == "dartdir/frog/leg/coreimpl.dart") {
38 filename = "dartdir/frog/leg/lib/coreimpl.dart";
39 }
40 String text = getText(idOfFilename(filename));
41 print("read $filename (${text.length} bytes)");
floitsch 2012/03/26 22:07:05 debug code?
42 return text;
43 }
44
45 void diagnosticHandler(uri_lib.Uri uri, int begin, int end,
46 String message, bool fatal) {
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 go() {
66 String mainFile = getText(MAIN_ID);
67 setText("input", readAll(mainFile));
68 int time1 = new Date.now().value;
floitsch 2012/03/26 22:07:05 use Stopwatch class instead.
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";
floitsch 2012/03/26 22:07:05 toStringAsFixed?
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");
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;
floitsch 2012/03/26 22:07:05 You can't assume that the future has completed. Ei
mattsh 2012/03/27 02:30:25 added TODO for compiler to add synchronous API.
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
floitsch 2012/03/26 22:07:05 TODO(ldap)
mattsh 2012/03/27 02:30:25 Done.
112 static String htmlEscape(String text) {
113 return text.replaceAll('&', '&amp;').replaceAll(
114 '>', '&gt;').replaceAll('<', '&lt;');
115 }
116 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698