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> |
11 | 11 |
12 #include "include/dart_api.h" | 12 #include "include/dart_api.h" |
13 | 13 |
14 #include "bin/builtin.h" | 14 #include "bin/builtin.h" |
| 15 #include "bin/dartutils.h" |
15 #include "bin/file.h" | 16 #include "bin/file.h" |
16 #include "bin/globals.h" | 17 #include "bin/globals.h" |
17 #include "bin/process_script.h" | 18 #include "bin/process_script.h" |
18 | 19 |
19 // Global state that indicates whether a snapshot is to be created and | 20 // Global state that indicates whether a snapshot is to be created and |
20 // if so which file to write the snapshot into. | 21 // if so which file to write the snapshot into. |
21 static const char* snapshot_filename = NULL; | 22 static const char* snapshot_filename = NULL; |
22 | 23 |
23 // Global state that captures the URL mappings specified on the command line. | 24 // Global state that captures the URL mappings specified on the command line. |
24 static CommandLineOptions* url_mapping = NULL; | 25 static CommandLineOptions* url_mapping = NULL; |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
219 return Dart_LoadScript(url, source, CreateSnapshotLibraryTagHandler); | 220 return Dart_LoadScript(url, source, CreateSnapshotLibraryTagHandler); |
220 } | 221 } |
221 | 222 |
222 | 223 |
| 224 static Dart_Handle BuiltinSnapshotLibraryTagHandler(Dart_LibraryTag tag, |
| 225 Dart_Handle library, |
| 226 Dart_Handle url) { |
| 227 if (!Dart_IsLibrary(library)) { |
| 228 return Dart_Error("not a library"); |
| 229 } |
| 230 if (!Dart_IsString8(url)) { |
| 231 return Dart_Error("url is not a string"); |
| 232 } |
| 233 const char* url_chars = NULL; |
| 234 Dart_Handle result = Dart_StringToCString(url, &url_chars); |
| 235 if (Dart_IsError(result)) { |
| 236 return Dart_Error("accessing url characters failed"); |
| 237 } |
| 238 // We only support canonicalization of "dart:". |
| 239 static const char* kDartScheme = "dart:"; |
| 240 static const intptr_t kDartSchemeLen = strlen(kDartScheme); |
| 241 if (strncmp(url_chars, kDartScheme, kDartSchemeLen) == 0) { |
| 242 if (tag == kCanonicalizeUrl) { |
| 243 return url; |
| 244 } |
| 245 return Dart_Error("unexpected tag encountered %d", tag); |
| 246 } |
| 247 return Dart_Error("unsupported url encountered %s", url_chars); |
| 248 } |
| 249 |
| 250 |
| 251 static Dart_Handle LoadGenericSnapshotCreationScript() { |
| 252 Dart_Handle source = Builtin_Source(); |
| 253 if (Dart_IsError(source)) { |
| 254 return source; // source contains the error string. |
| 255 } |
| 256 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); |
| 257 Dart_Handle lib = Dart_LoadScript(url, |
| 258 source, |
| 259 BuiltinSnapshotLibraryTagHandler); |
| 260 if (!Dart_IsError(lib)) { |
| 261 Builtin_SetupLibrary(lib); |
| 262 } |
| 263 return lib; |
| 264 } |
| 265 |
| 266 |
223 static void* SnapshotCreateCallback(void* data) { | 267 static void* SnapshotCreateCallback(void* data) { |
224 const char* script_name = reinterpret_cast<const char*>(data); | 268 const char* script_name = reinterpret_cast<const char*>(data); |
225 Dart_Handle result; | 269 Dart_Handle result; |
| 270 Dart_Handle library; |
226 Dart_EnterScope(); | 271 Dart_EnterScope(); |
227 | 272 |
228 ASSERT(snapshot_filename != NULL); | 273 ASSERT(snapshot_filename != NULL); |
229 | 274 |
230 // If a file is specified on the command line, load it up before a snapshot | 275 // Load up the script before a snapshot is created. |
231 // is created. | |
232 if (script_name != NULL) { | 276 if (script_name != NULL) { |
233 // Load the specified script. | 277 // Load the specified script. |
234 Dart_Handle library = LoadSnapshotCreationScript(script_name); | 278 library = LoadSnapshotCreationScript(script_name); |
235 if (Dart_IsError(library)) { | |
236 const char* err_msg = Dart_GetError(library); | |
237 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); | |
238 Dart_ExitScope(); | |
239 exit(255); | |
240 } | |
241 | |
242 if (!Dart_IsLibrary(library)) { | |
243 fprintf(stderr, | |
244 "Expected a library when loading script: %s", | |
245 script_name); | |
246 Dart_ExitScope(); | |
247 exit(255); | |
248 } | |
249 } else { | 279 } else { |
250 // Implicitly load builtin library. | 280 // This is a generic dart snapshot which needs builtin library setup. |
251 Builtin_LoadLibrary(); | 281 library = LoadGenericSnapshotCreationScript(); |
252 // Setup the native resolver for built in library functions. | |
253 Builtin_SetNativeResolver(); | |
254 } | 282 } |
255 | 283 if (Dart_IsError(library)) { |
| 284 const char* err_msg = Dart_GetError(library); |
| 285 fprintf(stderr, "Errors encountered while loading script: %s\n", err_msg); |
| 286 Dart_ExitScope(); |
| 287 exit(255); |
| 288 } |
| 289 ASSERT(Dart_IsLibrary(library)); |
256 uint8_t* buffer = NULL; | 290 uint8_t* buffer = NULL; |
257 intptr_t size = 0; | 291 intptr_t size = 0; |
258 // First create the snapshot. | 292 // First create the snapshot. |
259 result = Dart_CreateSnapshot(&buffer, &size); | 293 result = Dart_CreateSnapshot(&buffer, &size); |
260 if (Dart_IsError(result)) { | 294 if (Dart_IsError(result)) { |
261 const char* err_msg = Dart_GetError(result); | 295 const char* err_msg = Dart_GetError(result); |
262 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg); | 296 fprintf(stderr, "Error while creating snapshot: %s\n", err_msg); |
263 Dart_ExitScope(); | 297 Dart_ExitScope(); |
264 exit(255); | 298 exit(255); |
265 } | 299 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 // and writes out a snapshot. | 344 // and writes out a snapshot. |
311 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); | 345 Dart_Isolate isolate = Dart_CreateIsolate(NULL, script_name); |
312 if (isolate == NULL) { | 346 if (isolate == NULL) { |
313 return 255; | 347 return 255; |
314 } | 348 } |
315 | 349 |
316 // Shutdown the isolate. | 350 // Shutdown the isolate. |
317 Dart_ShutdownIsolate(); | 351 Dart_ShutdownIsolate(); |
318 return 0; | 352 return 0; |
319 } | 353 } |
OLD | NEW |