Chromium Code Reviews| 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, |
|
Anton Muhin
2011/11/17 18:15:36
maybe consider passing only const char** url_chars
siva
2011/11/17 19:09:46
I prefer keeping the input read only and not overw
| |
| 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 } |
| 131 | 132 |
| 132 | 133 |
| 133 static Dart_Handle CanonicalizeUrl(const char* library_url_chars, | 134 static Dart_Handle LoadSourceFile(const char* name, |
| 134 const char* url_chars) { | 135 Dart_LibraryTag tag, |
| 135 // Calculate the canonical path based on the importing library and the url. | 136 Dart_Handle library, |
| 136 const char* canonical_filename = GetCanonicalPath(library_url_chars, | 137 Dart_Handle url) { |
| 137 url_chars); | 138 // The tag is either an import or a source tag. Read the file based on the |
| 138 Dart_Handle canon_url = Dart_NewString(canonical_filename); | 139 // url chars. |
| 139 free(const_cast<char*>(canonical_filename)); | 140 Dart_Handle source = ReadStringFromFile(name); |
| 140 return canon_url; | 141 if (Dart_IsError(source)) { |
| 142 return source; // source contains the error string. | |
| 143 } | |
| 144 if (tag == kImportTag) { | |
| 145 return Dart_LoadLibrary(url, source); | |
| 146 } else if (tag == kSourceTag) { | |
| 147 return Dart_LoadSource(library, url, source); | |
| 148 } | |
| 149 return Dart_Error("wrong tag"); | |
| 141 } | 150 } |
| 142 | 151 |
| 143 | 152 |
| 144 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, | 153 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, |
| 145 Dart_Handle library, | 154 Dart_Handle library, |
| 146 Dart_Handle url) { | 155 Dart_Handle url) { |
| 147 if (!Dart_IsLibrary(library)) { | 156 if (!Dart_IsLibrary(library)) { |
| 148 return Dart_Error("not a library"); | 157 return Dart_Error("not a library"); |
| 149 } | 158 } |
| 150 if (!Dart_IsString8(url)) { | 159 if (!Dart_IsString8(url)) { |
| 151 return Dart_Error("url is not a string"); | 160 return Dart_Error("url is not a string"); |
| 152 } | 161 } |
| 153 const char* url_chars = NULL; | 162 const char* url_chars = NULL; |
| 154 Dart_Handle result = Dart_StringToCString(url, &url_chars); | 163 Dart_Handle result = Dart_StringToCString(url, &url_chars); |
| 155 if (Dart_IsError(result)) { | 164 if (Dart_IsError(result)) { |
| 156 return Dart_Error("accessing url characters failed"); | 165 return Dart_Error("accessing url characters failed"); |
| 157 } | 166 } |
| 158 | 167 |
| 159 // If the URL starts with "dart:" then it is handled specially. | 168 // If the URL starts with "dart:" then it is handled specially. |
| 160 static const char* kDartScheme = "dart:"; | 169 static const char* kDartScheme = "dart:"; |
| 161 static const intptr_t kDartSchemeLen = strlen(kDartScheme); | 170 static const intptr_t kDartSchemeLen = strlen(kDartScheme); |
| 162 if (strncmp(url_chars, kDartScheme, kDartSchemeLen) == 0) { | 171 if (strncmp(url_chars, kDartScheme, kDartSchemeLen) == 0) { |
| 163 if (tag == kCanonicalizeUrl) { | 172 if (tag == kCanonicalizeUrl) { |
|
Anton Muhin
2011/11/17 18:15:36
just a suggestion (feel free ignore). Control flo
siva
2011/11/17 19:09:46
Redid the loop per your suggestion.
On 2011/11/17
| |
| 164 return Dart_NewString(url_chars); | 173 return url; |
| 165 } | 174 } |
| 166 const char* mapped_url_chars = MapLibraryUrl(url_chars); | 175 const char* mapped_url_chars; |
| 167 if (mapped_url_chars != NULL) { | 176 bool url_is_mapped = MapLibraryUrl(url_chars, &mapped_url_chars); |
| 168 // We have a URL mapping specified, just return the mapped version. | 177 if (url_is_mapped) { |
| 169 return Dart_NewString(mapped_url_chars); | 178 // We have a URL mapping specified, just read the file that the |
| 179 // URL mapping specifies and load it. | |
| 180 return LoadSourceFile(mapped_url_chars, tag, library, url); | |
| 170 } | 181 } |
| 171 } | 182 return Dart_Error("Do not know how to load %s", url_chars); |
| 172 // Get the url of the calling library. | |
| 173 Dart_Handle library_url = Dart_LibraryUrl(library); | |
| 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 } | 183 } |
| 190 if (tag == kCanonicalizeUrl) { | 184 if (tag == kCanonicalizeUrl) { |
| 191 return canon_url; | 185 // Get the url of the calling library. |
| 192 } | 186 Dart_Handle library_url = Dart_LibraryUrl(library); |
| 193 result = Dart_StringToCString(canon_url, &url_chars); | 187 if (Dart_IsError(library_url)) { |
| 194 if (Dart_IsError(result)) { | 188 return Dart_Error("accessing library url failed"); |
| 195 return Dart_Error("accessing canon url characters failed"); | 189 } |
| 190 if (!Dart_IsString8(library_url)) { | |
| 191 return Dart_Error("library url is not a string"); | |
| 192 } | |
| 193 const char* library_url_chars = NULL; | |
| 194 result = Dart_StringToCString(library_url, &library_url_chars); | |
| 195 if (Dart_IsError(result)) { | |
| 196 return Dart_Error("accessing library url characters failed"); | |
| 197 } | |
| 198 const char* mapped_library_url_chars; | |
| 199 MapLibraryUrl(library_url_chars, &mapped_library_url_chars); | |
| 200 const char* canon_url_chars = GetCanonicalPath(library_url_chars, | |
| 201 url_chars); | |
| 202 Dart_Handle canon_url = Dart_NewString(canon_url_chars); | |
| 203 free(const_cast<char*>(canon_url_chars)); | |
| 204 | |
| 205 return canon_url; // canon_url has error string in case of errors. | |
| 196 } | 206 } |
| 197 // The tag is either an import or a source tag. Read the file based on the | 207 // The tag is either an import or a source tag. Read the file based on the |
| 198 // url chars. | 208 // url chars and load it. |
| 199 Dart_Handle source = ReadStringFromFile(url_chars); | 209 return LoadSourceFile(url_chars, tag, library, url); |
| 200 if (Dart_IsError(source)) { | |
| 201 return source; // source contains the error string. | |
| 202 } | |
| 203 if (tag == kImportTag) { | |
| 204 return Dart_LoadLibrary(url, source); | |
| 205 } else if (tag == kSourceTag) { | |
| 206 return Dart_LoadSource(library, url, source); | |
| 207 } | |
| 208 return Dart_Error("wrong tag"); | |
| 209 } | 210 } |
| 210 | 211 |
| 211 | 212 |
| 212 static Dart_Handle LoadSnapshotCreationScript(const char* script_name) { | 213 static Dart_Handle LoadSnapshotCreationScript(const char* script_name) { |
| 213 Dart_Handle source = ReadStringFromFile(script_name); | 214 Dart_Handle source = ReadStringFromFile(script_name); |
| 214 if (Dart_IsError(source)) { | 215 if (Dart_IsError(source)) { |
| 215 return source; // source contains the error string. | 216 return source; // source contains the error string. |
| 216 } | 217 } |
| 217 Dart_Handle url = Dart_NewString(script_name); | 218 Dart_Handle url = Dart_NewString(script_name); |
| 218 | 219 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 // and writes out a snapshot. | 311 // and writes out a snapshot. |
| 311 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); | 312 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); |
| 312 if (isolate == NULL) { | 313 if (isolate == NULL) { |
| 313 return 255; | 314 return 255; |
| 314 } | 315 } |
| 315 | 316 |
| 316 // Shutdown the isolate. | 317 // Shutdown the isolate. |
| 317 Dart_ShutdownIsolate(); | 318 Dart_ShutdownIsolate(); |
| 318 return 0; | 319 return 0; |
| 319 } | 320 } |
| OLD | NEW |