| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 3 // for details. All rights reserved. Use of this source code is governed by a | |
| 4 // BSD-style license that can be found in the LICENSE file. | |
| 5 | |
| 6 /** | |
| 7 * This is the entrypoint for a version of the frog compiler that | |
| 8 * can run in the browser. | |
| 9 * Because this is running in the browser, frog cannot access | |
| 10 * the file system. Instead, we assume the all necessary files | |
| 11 * have been placed in inert <script> elements somewhere on the page. | |
| 12 * (See frogpad.py for more details on how the html page is constructed.) | |
| 13 */ | |
| 14 | |
| 15 #import("dart:html", prefix:"html"); | |
| 16 #import("../../../frog/lang.dart"); | |
| 17 #import("../../../frog/file_system.dart"); | |
| 18 | |
| 19 // id of script element containing name of the main dart file | |
| 20 // to compile. | |
| 21 final String MAIN_ID = "main_id"; | |
| 22 | |
| 23 // id of script element containing name of the frog directory | |
| 24 final String FROGDIR_ID = "frogdir_id"; | |
| 25 | |
| 26 void main() { | |
| 27 StringBuffer warnings = new StringBuffer(); | |
| 28 HtmlFileSystem fs = new HtmlFileSystem(); | |
| 29 String frogDir = getText(FROGDIR_ID); | |
| 30 String mainFile = getText(MAIN_ID); | |
| 31 setText("input", fs.readAll(mainFile)); | |
| 32 | |
| 33 int time1 = new Date.now().value; | |
| 34 List<String> args = [ | |
| 35 "dummy_arg1", | |
| 36 "dummy_arg2", | |
| 37 "--enable_type_checks", | |
| 38 "--enable_asserts", | |
| 39 mainFile]; | |
| 40 parseOptions(frogDir, args, fs); | |
| 41 | |
| 42 options.useColors = false; | |
| 43 | |
| 44 initializeWorld(fs); | |
| 45 world.messageHandler = void _( | |
| 46 String prefix, String message, SourceSpan span) { | |
| 47 String location = ""; | |
| 48 if (span !== null) { | |
| 49 location = span.locationText; | |
| 50 } | |
| 51 warnings.add('$prefix$message$location\n'); | |
| 52 }; | |
| 53 bool success = world.compile(); | |
| 54 int time2 = new Date.now().value; | |
| 55 String output = "throw 'frogpad compilation error';\n"; | |
| 56 if (success) { | |
| 57 output = world.getGeneratedCode(); | |
| 58 } | |
| 59 setText("output", output); | |
| 60 setText("warnings", warnings.toString()); | |
| 61 | |
| 62 String timing = "generated ${output.length} characters in " | |
| 63 "${((time2 - time1) / 1000).toStringAsPrecision(3)} seconds"; | |
| 64 setText("timing", timing); | |
| 65 } | |
| 66 | |
| 67 void setText(String id, String text) { | |
| 68 html.Element element = html.document.query("#$id"); | |
| 69 if (element === null) { | |
| 70 throw new Exception("Can't find element $id"); | |
| 71 } | |
| 72 element.innerHTML = htmlEscape(text); | |
| 73 } | |
| 74 | |
| 75 String getText(String id) { | |
| 76 html.Element element = html.document.query("#$id"); | |
| 77 if (element === null) { | |
| 78 throw new Exception("Can't find element $id"); | |
| 79 } | |
| 80 return element.text.trim(); | |
| 81 } | |
| 82 | |
| 83 // TODO(rnystrom): should exist in standard lib somewhere | |
| 84 String htmlEscape(String text) { | |
| 85 return text.replaceAll('&', '&') | |
| 86 .replaceAll('>', '>') | |
| 87 .replaceAll('<', '<'); | |
| 88 } | |
| 89 | |
| 90 class HtmlFileSystem implements FileSystem { | |
| 91 | |
| 92 HtmlFileSystem() {} | |
| 93 | |
| 94 String readAll(String filename) { | |
| 95 String text = getText(idOfFilename(filename)); | |
| 96 print("read $filename (${text.length} bytes)"); | |
| 97 return text; | |
| 98 } | |
| 99 | |
| 100 /** | |
| 101 * Returns the id of the <script> element that contains | |
| 102 * the contents of this file. | |
| 103 * The id is constructed by taking the filename and replacing | |
| 104 * all slashes and dots with underscores. For example, the file name: | |
| 105 * | |
| 106 * "/usr/local/src/dart/frog/lang.dart" | |
| 107 * | |
| 108 * becomes: | |
| 109 * | |
| 110 * "_usr_local_src_dart_frog_lang_dart" | |
| 111 * | |
| 112 * And, so this file's contents will be found in a <script> | |
| 113 * element that looks like this: | |
| 114 * | |
| 115 * <script type=application/inert id="_usr_local_src_dart_frog_lang_dart"> | |
| 116 * ... contents of file lang.dart placed here ... | |
| 117 * etc. | |
| 118 */ | |
| 119 String idOfFilename(String filename) { | |
| 120 return filename.replaceAll("/", "_").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-existent 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 |