| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Generate a snapshot file after loading all the scripts specified on the | 5 // Generate a snapshot file after loading all the scripts specified on the |
| 6 // command line. | 6 // command line. |
| 7 | 7 |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 const bool kWritable = true; | 101 const bool kWritable = true; |
| 102 File* file = File::Open(snapshot_filename, kWritable); | 102 File* file = File::Open(snapshot_filename, kWritable); |
| 103 ASSERT(file != NULL); | 103 ASSERT(file != NULL); |
| 104 for (intptr_t i = 0; i < size; i++) { | 104 for (intptr_t i = 0; i < size; i++) { |
| 105 file->WriteByte(buffer[i]); | 105 file->WriteByte(buffer[i]); |
| 106 } | 106 } |
| 107 delete file; | 107 delete file; |
| 108 } | 108 } |
| 109 | 109 |
| 110 | 110 |
| 111 static const char* MapLibraryUrl(const char* library_url_chars) { | 111 static bool MapLibraryUrl(const char* url_chars, |
| 112 const char* mapped_url_chars = NULL; | 112 const char** mapped_url_chars) { |
| 113 *mapped_url_chars = url_chars; |
| 113 if (url_mapping != NULL) { | 114 if (url_mapping != NULL) { |
| 114 // We need to check if the passed in url is found in the url_mapping array, | 115 // We need to check if the passed in url is found in the url_mapping array, |
| 115 // in that case use the mapped entry. | 116 // in that case use the mapped entry. |
| 116 int len = strlen(library_url_chars); | 117 int len = strlen(url_chars); |
| 117 for (int idx = 0; idx < url_mapping->count(); idx++) { | 118 for (int idx = 0; idx < url_mapping->count(); idx++) { |
| 118 const char* url_name = url_mapping->GetArgument(idx); | 119 const char* url_name = url_mapping->GetArgument(idx); |
| 119 if (!strncmp(library_url_chars, url_name, len) && | 120 if (!strncmp(url_chars, url_name, len) && |
| 120 (url_name[len] == ',')) { | 121 (url_name[len] == ',')) { |
| 121 const char* url_mapped_name = url_name + len + 1; | 122 const char* url_mapped_name = url_name + len + 1; |
| 122 if (strlen(url_mapped_name) != 0) { | 123 if (strlen(url_mapped_name) != 0) { |
| 123 mapped_url_chars = url_mapped_name; | 124 *mapped_url_chars = url_mapped_name; |
| 125 return true; // Found a mapping for this URL. |
| 124 } | 126 } |
| 125 break; | |
| 126 } | 127 } |
| 127 } | 128 } |
| 128 } | 129 } |
| 129 return mapped_url_chars; | 130 return false; // Did not find any mapping for this URL. |
| 130 } | |
| 131 | |
| 132 | |
| 133 static Dart_Handle CanonicalizeUrl(const char* library_url_chars, | |
| 134 const char* url_chars) { | |
| 135 // Calculate the canonical path based on the importing library and the url. | |
| 136 const char* canonical_filename = GetCanonicalPath(library_url_chars, | |
| 137 url_chars); | |
| 138 Dart_Handle canon_url = Dart_NewString(canonical_filename); | |
| 139 free(const_cast<char*>(canonical_filename)); | |
| 140 return canon_url; | |
| 141 } | 131 } |
| 142 | 132 |
| 143 | 133 |
| 144 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, | 134 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, |
| 145 Dart_Handle library, | 135 Dart_Handle library, |
| 146 Dart_Handle url) { | 136 Dart_Handle url) { |
| 147 if (!Dart_IsLibrary(library)) { | 137 if (!Dart_IsLibrary(library)) { |
| 148 return Dart_Error("not a library"); | 138 return Dart_Error("not a library"); |
| 149 } | 139 } |
| 150 if (!Dart_IsString8(url)) { | 140 if (!Dart_IsString8(url)) { |
| 151 return Dart_Error("url is not a string"); | 141 return Dart_Error("url is not a string"); |
| 152 } | 142 } |
| 153 const char* url_chars = NULL; | 143 const char* url_chars = NULL; |
| 154 Dart_Handle result = Dart_StringToCString(url, &url_chars); | 144 Dart_Handle result = Dart_StringToCString(url, &url_chars); |
| 155 if (Dart_IsError(result)) { | 145 if (Dart_IsError(result)) { |
| 156 return Dart_Error("accessing url characters failed"); | 146 return Dart_Error("accessing url characters failed"); |
| 157 } | 147 } |
| 158 | 148 |
| 159 // If the URL starts with "dart:" then it is handled specially. | 149 // If the URL starts with "dart:" then it is handled specially. |
| 160 static const char* kDartScheme = "dart:"; | 150 static const char* kDartScheme = "dart:"; |
| 161 static const intptr_t kDartSchemeLen = strlen(kDartScheme); | 151 static const intptr_t kDartSchemeLen = strlen(kDartScheme); |
| 162 if (strncmp(url_chars, kDartScheme, kDartSchemeLen) == 0) { | 152 bool is_dart_scheme_url = |
| 163 if (tag == kCanonicalizeUrl) { | 153 (strncmp(url_chars, kDartScheme, kDartSchemeLen) == 0); |
| 164 return Dart_NewString(url_chars); | 154 |
| 155 if (tag == kCanonicalizeUrl) { |
| 156 if (is_dart_scheme_url) { |
| 157 return url; |
| 165 } | 158 } |
| 166 const char* mapped_url_chars = MapLibraryUrl(url_chars); | 159 // Get the url of the calling library. |
| 167 if (mapped_url_chars != NULL) { | 160 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 168 // We have a URL mapping specified, just return the mapped version. | 161 if (Dart_IsError(library_url)) { |
| 169 return Dart_NewString(mapped_url_chars); | 162 return Dart_Error("accessing library url failed"); |
| 163 } |
| 164 if (!Dart_IsString8(library_url)) { |
| 165 return Dart_Error("library url is not a string"); |
| 166 } |
| 167 const char* library_url_chars = NULL; |
| 168 result = Dart_StringToCString(library_url, &library_url_chars); |
| 169 if (Dart_IsError(result)) { |
| 170 return Dart_Error("accessing library url characters failed"); |
| 171 } |
| 172 const char* mapped_library_url_chars; |
| 173 MapLibraryUrl(library_url_chars, &mapped_library_url_chars); |
| 174 const char* canon_url_chars = GetCanonicalPath(mapped_library_url_chars, |
| 175 url_chars); |
| 176 Dart_Handle canon_url = Dart_NewString(canon_url_chars); |
| 177 free(const_cast<char*>(canon_url_chars)); |
| 178 |
| 179 return canon_url; // canon_url has error string in case of errors. |
| 180 } |
| 181 if (is_dart_scheme_url) { |
| 182 const char* mapped_url_chars; |
| 183 bool url_is_mapped = MapLibraryUrl(url_chars, &mapped_url_chars); |
| 184 if (url_is_mapped) { |
| 185 // We have a URL mapping specified, just read the file that the |
| 186 // URL mapping specifies and load it. |
| 187 url_chars = mapped_url_chars; |
| 188 } else { |
| 189 return Dart_Error("Do not know how to load %s", url_chars); |
| 170 } | 190 } |
| 171 } | 191 } |
| 172 // Get the url of the calling library. | 192 // The tag is either an import or a source tag. Read the file pointed to by |
| 173 Dart_Handle library_url = Dart_LibraryUrl(library); | 193 // url_chars and load it. |
| 174 if (Dart_IsError(library_url)) { | |
| 175 return Dart_Error("accessing library url failed"); | |
| 176 } | |
| 177 if (!Dart_IsString8(library_url)) { | |
| 178 return Dart_Error("library url is not a string"); | |
| 179 } | |
| 180 const char* library_url_chars = NULL; | |
| 181 result = Dart_StringToCString(library_url, &library_url_chars); | |
| 182 if (Dart_IsError(result)) { | |
| 183 return Dart_Error("accessing library url characters failed"); | |
| 184 } | |
| 185 library_url_chars = MapLibraryUrl(library_url_chars); | |
| 186 Dart_Handle canon_url = CanonicalizeUrl(library_url_chars, url_chars); | |
| 187 if (Dart_IsError(canon_url)) { | |
| 188 return canon_url; // canon_url contains the error string. | |
| 189 } | |
| 190 if (tag == kCanonicalizeUrl) { | |
| 191 return canon_url; | |
| 192 } | |
| 193 result = Dart_StringToCString(canon_url, &url_chars); | |
| 194 if (Dart_IsError(result)) { | |
| 195 return Dart_Error("accessing canon url characters failed"); | |
| 196 } | |
| 197 // The tag is either an import or a source tag. Read the file based on the | |
| 198 // url chars. | |
| 199 Dart_Handle source = ReadStringFromFile(url_chars); | 194 Dart_Handle source = ReadStringFromFile(url_chars); |
| 200 if (Dart_IsError(source)) { | 195 if (Dart_IsError(source)) { |
| 201 return source; // source contains the error string. | 196 return source; // source contains the error string. |
| 202 } | 197 } |
| 203 if (tag == kImportTag) { | 198 if (tag == kImportTag) { |
| 204 return Dart_LoadLibrary(url, source); | 199 return Dart_LoadLibrary(url, source); |
| 205 } else if (tag == kSourceTag) { | 200 } else if (tag == kSourceTag) { |
| 206 return Dart_LoadSource(library, url, source); | 201 return Dart_LoadSource(library, url, source); |
| 207 } | 202 } |
| 208 return Dart_Error("wrong tag"); | 203 return Dart_Error("wrong tag"); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 // and writes out a snapshot. | 305 // and writes out a snapshot. |
| 311 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); | 306 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); |
| 312 if (isolate == NULL) { | 307 if (isolate == NULL) { |
| 313 return 255; | 308 return 255; |
| 314 } | 309 } |
| 315 | 310 |
| 316 // Shutdown the isolate. | 311 // Shutdown the isolate. |
| 317 Dart_ShutdownIsolate(); | 312 Dart_ShutdownIsolate(); |
| 318 return 0; | 313 return 0; |
| 319 } | 314 } |
| OLD | NEW |