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 /** | |
| 6 * This is the entrypoint for a version of the frog compiler that | |
| 7 * can run in the browser. | |
| 8 * Because this is running in the browser, frog cannot access | |
| 9 * the file system. Instead, we assume the all necessary files | |
| 10 * have been placed in inert <script> elements somewhere on the page. | |
| 11 * (See frogpad.py for more details on how the html page is constructed.) | |
| 12 */ | |
| 13 | |
| 14 #import("dart:html", prefix:"html"); | |
| 15 #import("../lang.dart"); | |
| 16 #import("../file_system.dart"); | |
| 17 | |
| 18 // id of script element containing name of the main dart file | |
| 19 // to compile. | |
| 20 final String MAIN_ID = "main_id"; | |
| 21 | |
| 22 void main() { | |
| 23 String warnings = ""; | |
| 24 HtmlFileSystem fs = new HtmlFileSystem(); | |
| 25 String mainFile = getText(MAIN_ID); | |
| 26 setText("input", fs.readAll(mainFile)); | |
| 27 | |
| 28 int time1 = new Date.now().value; | |
| 29 List<String> args = ["dummy_arg1", "dummy_arg2", mainFile]; | |
| 30 parseOptions("dummy_home_dir", args, fs); | |
| 31 | |
| 32 options.useColors = false; | |
| 33 | |
| 34 initializeWorld(fs); | |
| 35 world.messageHandler = void _( | |
| 36 String prefix, String message, SourceSpan span) { | |
| 37 String location = ""; | |
| 38 if (span !== null) { | |
| 39 location = span.locationText; | |
| 40 } | |
| 41 warnings += prefix + message + location + "\n"; | |
| 42 }; | |
| 43 bool success = world.compile(); | |
| 44 int time2 = new Date.now().value; | |
| 45 String output = world.getGeneratedCode(); | |
| 46 if (success) { | |
| 47 setText("output", output); | |
| 48 } | |
| 49 setText("warnings", warnings); | |
| 50 | |
| 51 ((time2 - time1) / 1000).toStringAsPrecision(3); | |
| 52 String timing = "generated ${output.length} characters in " + | |
| 53 "${((time2 - time1) / 1000).toStringAsPrecision(3)} seconds"; | |
| 54 setText("timing", timing); | |
| 55 } | |
| 56 | |
| 57 void setText(String id, String text) { | |
| 58 html.Element element = html.document.query("#$id"); | |
| 59 if (element === null) { | |
| 60 throw new Exception("Can't find element $id"); | |
| 61 } | |
| 62 element.innerHTML = htmlEscape(text); | |
| 63 } | |
| 64 | |
| 65 String getText(String id) { | |
| 66 html.Element element = html.document.query("#$id"); | |
| 67 if (element === null) { | |
| 68 throw new Exception("Can't find element $id"); | |
| 69 } | |
| 70 return element.text.trim(); | |
| 71 } | |
| 72 | |
| 73 // TODO(rnystrom): should exist in standard lib somewhere | |
| 74 String htmlEscape(String text) { | |
| 75 return text.replaceAll('&', '&').replaceAll( | |
| 76 '>', '>').replaceAll('<', '<'); | |
| 77 } | |
| 78 | |
| 79 class HtmlFileSystem implements FileSystem { | |
| 80 | |
| 81 HtmlFileSystem() {} | |
| 82 | |
| 83 String readAll(String filename) { | |
| 84 String text = getText(idOfFilename(filename)); | |
| 85 print("read $filename (${text.length} bytes)"); | |
| 86 return text; | |
| 87 } | |
| 88 | |
| 89 String idOfFilename(String filename) { | |
|
Emily Fortuna
2012/02/14 19:56:53
Why'd the comments for this function go away? I th
mattsh
2012/02/14 22:13:40
Good catch. I'm not sure how it got deleted, but
| |
| 90 List<String> components = filename.split("/"); | |
| 91 if (components.isEmpty()) { | |
| 92 throw new Exception("bad filename"); | |
| 93 } | |
| 94 // Grab the last two components (the directory name and file name). | |
| 95 int startIndex = Math.max(0, components.length - 2); | |
| 96 int length = components.length - startIndex;; | |
| 97 components = components.getRange(startIndex, length); | |
| 98 | |
| 99 // Join components with underscore, and replace dots with underscore. | |
| 100 return Strings.join(components, "_").replaceAll(".", "_"); | |
| 101 } | |
| 102 | |
| 103 void writeString(String outfile, String text) { | |
| 104 throw new UnsupportedOperationException(""); | |
| 105 } | |
| 106 | |
| 107 bool fileExists(String filename) { | |
| 108 // frog calls this to check if files exist before reading them. We return | |
| 109 // true here for all files, and let it fail later if frog attempts to read | |
| 110 // the contents of a non-existant file. | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 void createDirectory(String path, [bool recursive]) { | |
| 115 throw new UnsupportedOperationException(""); | |
| 116 } | |
| 117 | |
| 118 void removeDirectory(String path, [bool recursive]) { | |
| 119 throw new UnsupportedOperationException(""); | |
| 120 } | |
| 121 } | |
| OLD | NEW |