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

Unified 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: fixed comments Created 8 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/testing/legpad/legpad.dart
diff --git a/tools/testing/legpad/legpad.dart b/tools/testing/legpad/legpad.dart
new file mode 100644
index 0000000000000000000000000000000000000000..86a30528782f3a05cb9650fd61919c8b3c718653
--- /dev/null
+++ b/tools/testing/legpad/legpad.dart
@@ -0,0 +1,116 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#import("dart:html", prefix:"html");
+#import('../../../frog/leg/api.dart', prefix: "api_lib");
+#import('../../../lib/uri/uri.dart', prefix:"uri_lib");
+
+/**
+ * This is the entrypoint for a version of the leg compiler that
+ * runs in the browser.
+ *
+ * Because this is running in the browser, we do not access
+ * the file system. Instead, we assume that all necessary files
+ * have been placed in inert <script> elements somewhere on the html page.
+ *
+ * (See legpad.py for more details on how the html page is constructed.)
+ */
+void main() {
+ new Legpad().run();
+}
+
+class Legpad {
+ // id of script element containing name of the main dart file
+ // to compile
+ static final String MAIN_ID = "main_id";
+
+ // accumulates diagnostic messages emitted by the leg compiler
+ String warnings = "";
+
+ // the generated javascript
+ String output;
+
+ String readAll(String filename) {
+ // 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.
+ if (filename == "dartdir/frog/leg/coreimpl.dart") {
+ filename = "dartdir/frog/leg/lib/coreimpl.dart";
+ }
+ String text = getText(idOfFilename(filename));
+ print("read $filename (${text.length} bytes)");
+ return text;
+ }
+
+ void diagnosticHandler(uri_lib.Uri uri, int begin, int end,
+ String message, bool fatal) {
+ // TODO format message with location info
+ warnings += message + "\n";
+ }
+
+ Future<String> readUriFromString(uri_lib.Uri uri) {
+ Completer<String> completer = new Completer<String>();
+ completer.complete(readAll(uri.toString()));
+ return completer.future;
+ }
+
+ /**
+ * Returns the id of the <script> element that contains
+ * the contents of this file. (Replace all slashes
+ * and dots in the file name with underscores.)
+ */
+ String idOfFilename(String filename) {
+ return filename.replaceAll("/", "_").replaceAll(".", "_");
+ }
+
+ void run() {
+ String mainFile = getText(MAIN_ID);
+ setText("input", readAll(mainFile));
+ int time1 = new Date.now().value;
+ runLeg();
+ int time2 = new Date.now().value;
+ setText("output", output);
+ setText("warnings", warnings);
+ String timing = "generated ${output.length} characters in " +
+ "${((time2 - time1) / 1000).toStringAsPrecision(3)} seconds";
+ setText("timing", timing);
+ }
+
+ void runLeg() {
+ uri_lib.Uri mainUri = new uri_lib.Uri.fromString(getText(MAIN_ID));
+ uri_lib.Uri libraryRoot =
+ 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
+ List<String> compilerArgs = [
+ "--enable_type_checks",
+ "--enable_asserts"
+ ];
+ Future<String> futureJavascript = api_lib.compile(mainUri,
+ libraryRoot, readUriFromString, diagnosticHandler, compilerArgs);
+ if (futureJavascript == null) {
+ output = "throw 'legpad compilation error';\n";
+ return;
+ }
+ output = futureJavascript.value;
+ }
+
+ void setText(String id, String text) {
+ html.Element element = html.document.query("#$id");
+ if (element === null) {
+ throw new Exception("Can't find element $id");
+ }
+ element.innerHTML = htmlEscape(text);
+ }
+
+ String getText(String id) {
+ html.Element element = html.document.query("#$id");
+ if (element === null) {
+ throw new Exception("Can't find element $id");
+ }
+ return element.text.trim();
+ }
+
+ // TODO: should exist in standard lib somewhere
+ static String htmlEscape(String text) {
+ return text.replaceAll('&', '&amp;').replaceAll(
+ '>', '&gt;').replaceAll('<', '&lt;');
+ }
+}
« 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