| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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/dartutils.h" |
| 16 #include "bin/file.h" | 16 #include "bin/file.h" |
| 17 #include "platform/globals.h" | 17 #include "platform/globals.h" |
| 18 | 18 |
| 19 #define CHECK_RESULT(result) \ | 19 #define CHECK_RESULT(result) \ |
| 20 if (Dart_IsError(result)) { \ | 20 if (Dart_IsError(result)) { \ |
| 21 free(snapshot_buffer); \ |
| 21 fprintf(stderr, "Error: %s", Dart_GetError(result)); \ | 22 fprintf(stderr, "Error: %s", Dart_GetError(result)); \ |
| 22 Dart_ExitScope(); \ | 23 Dart_ExitScope(); \ |
| 23 Dart_ShutdownIsolate(); \ | 24 Dart_ShutdownIsolate(); \ |
| 24 exit(255); \ | 25 exit(255); \ |
| 25 } \ | 26 } \ |
| 26 | 27 |
| 27 | 28 |
| 28 // Global state that indicates whether a snapshot is to be created and | 29 // Global state that indicates whether a snapshot is to be created and |
| 29 // if so which file to write the snapshot into. | 30 // if so which file to write the snapshot into. |
| 30 static const char* snapshot_filename = NULL; | 31 static const char* snapshot_filename = NULL; |
| 31 static bool script_snapshot = false; | 32 static bool script_snapshot = false; |
| 32 static const char* package_root = NULL; | 33 static const char* package_root = NULL; |
| 34 static uint8_t* snapshot_buffer = NULL; |
| 33 | 35 |
| 34 | 36 |
| 35 // Global state which contains a pointer to the script name for which | 37 // Global state which contains a pointer to the script name for which |
| 36 // a snapshot needs to be created (NULL would result in the creation | 38 // a snapshot needs to be created (NULL would result in the creation |
| 37 // of a generic snapshot that contains only the corelibs). | 39 // of a generic snapshot that contains only the corelibs). |
| 38 static char* app_script_name = NULL; | 40 static char* app_script_name = NULL; |
| 39 | 41 |
| 40 | 42 |
| 41 // Global state that captures the URL mappings specified on the command line. | 43 // Global state that captures the URL mappings specified on the command line. |
| 42 static CommandLineOptions* url_mapping = NULL; | 44 static CommandLineOptions* url_mapping = NULL; |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 // this case, use the generic library tag handler. | 338 // this case, use the generic library tag handler. |
| 337 | 339 |
| 338 // First setup and create a generic full snapshot. | 340 // First setup and create a generic full snapshot. |
| 339 SetupForGenericSnapshotCreation(); | 341 SetupForGenericSnapshotCreation(); |
| 340 uint8_t* buffer = NULL; | 342 uint8_t* buffer = NULL; |
| 341 intptr_t size = 0; | 343 intptr_t size = 0; |
| 342 result = Dart_CreateSnapshot(&buffer, &size); | 344 result = Dart_CreateSnapshot(&buffer, &size); |
| 343 CHECK_RESULT(result); | 345 CHECK_RESULT(result); |
| 344 | 346 |
| 345 // Save the snapshot buffer as we are about to shutdown the isolate. | 347 // Save the snapshot buffer as we are about to shutdown the isolate. |
| 346 uint8_t* snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size)); | 348 snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size)); |
| 347 ASSERT(snapshot_buffer != NULL); | 349 ASSERT(snapshot_buffer != NULL); |
| 348 memmove(snapshot_buffer, buffer, size); | 350 memmove(snapshot_buffer, buffer, size); |
| 349 | 351 |
| 350 // Shutdown the isolate. | 352 // Shutdown the isolate. |
| 351 Dart_ExitScope(); | 353 Dart_ExitScope(); |
| 352 Dart_ShutdownIsolate(); | 354 Dart_ShutdownIsolate(); |
| 353 | 355 |
| 354 // Now load the specified script and create a script snapshot. | 356 // Now load the specified script and create a script snapshot. |
| 355 Dart_Isolate isolate = Dart_CreateIsolate(NULL, | 357 Dart_Isolate isolate = Dart_CreateIsolate(NULL, |
| 356 NULL, | 358 NULL, |
| 357 snapshot_buffer, | 359 snapshot_buffer, |
| 358 NULL, | 360 NULL, |
| 359 &error); | 361 &error); |
| 360 if (isolate == NULL) { | 362 if (isolate == NULL) { |
| 361 fprintf(stderr, "%s", error); | 363 fprintf(stderr, "%s", error); |
| 362 free(error); | 364 free(error); |
| 365 free(snapshot_buffer); |
| 363 exit(255); | 366 exit(255); |
| 364 } | 367 } |
| 365 Dart_EnterScope(); | 368 Dart_EnterScope(); |
| 366 | 369 |
| 367 // Setup generic library tag handler. | 370 // Setup generic library tag handler. |
| 368 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 371 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
| 369 CHECK_RESULT(result); | 372 CHECK_RESULT(result); |
| 370 | 373 |
| 371 // Get handle to builtin library. | 374 // Get handle to builtin library. |
| 372 Dart_Handle builtin_lib = | 375 Dart_Handle builtin_lib = |
| (...skipping 12 matching lines...) Expand all Loading... |
| 385 CreateAndWriteSnapshot(true); | 388 CreateAndWriteSnapshot(true); |
| 386 | 389 |
| 387 free(snapshot_buffer); | 390 free(snapshot_buffer); |
| 388 } | 391 } |
| 389 } else { | 392 } else { |
| 390 SetupForGenericSnapshotCreation(); | 393 SetupForGenericSnapshotCreation(); |
| 391 CreateAndWriteSnapshot(false); | 394 CreateAndWriteSnapshot(false); |
| 392 } | 395 } |
| 393 return 0; | 396 return 0; |
| 394 } | 397 } |
| OLD | NEW |