| 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("../../../frog/lang.dart"); |
| 16 #import("../../../frog/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 /** |
| 90 * Returns the id of the <script> element that contains |
| 91 * the contents of this file. |
| 92 * The id is constructed by taking the last directory |
| 93 * of the path, plus the file name, and using _ as a |
| 94 * separator. So, for example, the file name: |
| 95 * |
| 96 * "/usr/local/src/dart/frog/lang.dart" |
| 97 * |
| 98 * becomes: |
| 99 * |
| 100 * "frog_lang_dart" |
| 101 * |
| 102 * And, so this file's contents will be found in a <script> |
| 103 * element that looks like this: |
| 104 * |
| 105 * <script type=application/inert id="frog_lang_dart"> |
| 106 * ... contents of file lang.dart placed here ... |
| 107 * </script> |
| 108 */ |
| 109 String idOfFilename(String filename) { |
| 110 List<String> components = filename.split("/"); |
| 111 if (components.isEmpty()) { |
| 112 throw new Exception("bad filename"); |
| 113 } |
| 114 // Grab the last two components (the directory name and file name). |
| 115 int startIndex = Math.max(0, components.length - 2); |
| 116 int length = components.length - startIndex;; |
| 117 components = components.getRange(startIndex, length); |
| 118 |
| 119 // Join components with underscore, and replace dots with underscore. |
| 120 return Strings.join(components, "_").replaceAll(".", "_"); |
| 121 } |
| 122 |
| 123 void writeString(String outfile, String text) { |
| 124 throw new UnsupportedOperationException(""); |
| 125 } |
| 126 |
| 127 bool fileExists(String filename) { |
| 128 // frog calls this to check if files exist before reading them. We return |
| 129 // true here for all files, and let it fail later if frog attempts to read |
| 130 // the contents of a non-existant file. |
| 131 return true; |
| 132 } |
| 133 |
| 134 void createDirectory(String path, [bool recursive]) { |
| 135 throw new UnsupportedOperationException(""); |
| 136 } |
| 137 |
| 138 void removeDirectory(String path, [bool recursive]) { |
| 139 throw new UnsupportedOperationException(""); |
| 140 } |
| 141 } |
| OLD | NEW |