Chromium Code Reviews| 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..547a6ea1a24cd667c575dbf897b554c4818f5049 |
| --- /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().go(); |
| +} |
| + |
| +class Legpad { |
| + // id of script element containing name of the main dart file |
|
floitsch
2012/03/26 22:07:05
Id and finish sentence with '.'.
|
| + // 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
|
| + static final String MAIN_ID = "main_id"; |
| + |
| + // accumulates diagnostic messages emitted by the leg compiler |
|
floitsch
2012/03/26 22:07:05
ditto: start with uppercase and finish with '.'.
|
| + String warnings = ""; |
|
floitsch
2012/03/26 22:07:05
Make it a StringBuffer?
mattsh
2012/03/27 02:30:25
Done.
|
| + |
| + // the generated javascript |
| + String output; |
| + |
| + String readAll(String filename) { |
| + // 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.
|
| + // are located |
| + 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)"); |
|
floitsch
2012/03/26 22:07:05
debug code?
|
| + return text; |
| + } |
| + |
| + void diagnosticHandler(uri_lib.Uri uri, int begin, int end, |
| + String message, bool fatal) { |
| + 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 go() { |
| + String mainFile = getText(MAIN_ID); |
| + setText("input", readAll(mainFile)); |
| + int time1 = new Date.now().value; |
|
floitsch
2012/03/26 22:07:05
use Stopwatch class instead.
|
| + 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"; |
|
floitsch
2012/03/26 22:07:05
toStringAsFixed?
|
| + 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"); |
| + 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; |
|
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.
|
| + } |
| + |
| + 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 |
|
floitsch
2012/03/26 22:07:05
TODO(ldap)
mattsh
2012/03/27 02:30:25
Done.
|
| + static String htmlEscape(String text) { |
| + return text.replaceAll('&', '&').replaceAll( |
| + '>', '>').replaceAll('<', '<'); |
| + } |
| +} |