Chromium Code Reviews| 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 "bin/log.h" | |
|
siva
2012/11/15 18:46:48
Please add a blank line after this (would like to
gram
2012/11/15 21:16:50
Done.
| |
| 17 #include "platform/globals.h" | 18 #include "platform/globals.h" |
| 18 | 19 |
| 19 #define CHECK_RESULT(result) \ | 20 #define CHECK_RESULT(result) \ |
| 20 if (Dart_IsError(result)) { \ | 21 if (Dart_IsError(result)) { \ |
| 21 free(snapshot_buffer); \ | 22 free(snapshot_buffer); \ |
| 22 fprintf(stderr, "Error: %s", Dart_GetError(result)); \ | 23 Log::PrintErr("Error: %s", Dart_GetError(result)); \ |
| 23 Dart_ExitScope(); \ | 24 Dart_ExitScope(); \ |
| 24 Dart_ShutdownIsolate(); \ | 25 Dart_ShutdownIsolate(); \ |
| 25 exit(255); \ | 26 exit(255); \ |
| 26 } \ | 27 } \ |
| 27 | 28 |
| 28 | 29 |
| 29 // Global state that indicates whether a snapshot is to be created and | 30 // Global state that indicates whether a snapshot is to be created and |
| 30 // if so which file to write the snapshot into. | 31 // if so which file to write the snapshot into. |
| 31 static const char* snapshot_filename = NULL; | 32 static const char* snapshot_filename = NULL; |
| 32 static bool script_snapshot = false; | 33 static bool script_snapshot = false; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 Dart_Handle lib; | 199 Dart_Handle lib; |
| 199 // Load the builtin library to make it available in the snapshot | 200 // Load the builtin library to make it available in the snapshot |
| 200 // for importing. | 201 // for importing. |
| 201 lib = Builtin::LoadAndCheckLibrary(id); | 202 lib = Builtin::LoadAndCheckLibrary(id); |
| 202 ASSERT(!Dart_IsError(lib)); | 203 ASSERT(!Dart_IsError(lib)); |
| 203 return lib; | 204 return lib; |
| 204 } | 205 } |
| 205 | 206 |
| 206 | 207 |
| 207 static void PrintUsage() { | 208 static void PrintUsage() { |
| 208 fprintf(stderr, | 209 Log::PrintErr("dart [<vm-flags>] [<dart-script-file>]\n"); |
| 209 "dart [<vm-flags>] " | |
| 210 "[<dart-script-file>]\n"); | |
| 211 } | 210 } |
| 212 | 211 |
| 213 | 212 |
| 214 static void VerifyLoaded(Dart_Handle library) { | 213 static void VerifyLoaded(Dart_Handle library) { |
| 215 if (Dart_IsError(library)) { | 214 if (Dart_IsError(library)) { |
| 216 const char* err_msg = Dart_GetError(library); | 215 const char* err_msg = Dart_GetError(library); |
| 217 fprintf(stderr, "Errors encountered while loading: %s\n", err_msg); | 216 Log::PrintErr("Errors encountered while loading: %s\n", err_msg); |
| 218 Dart_ExitScope(); | 217 Dart_ExitScope(); |
| 219 Dart_ShutdownIsolate(); | 218 Dart_ShutdownIsolate(); |
| 220 exit(255); | 219 exit(255); |
| 221 } | 220 } |
| 222 ASSERT(Dart_IsLibrary(library)); | 221 ASSERT(Dart_IsLibrary(library)); |
| 223 } | 222 } |
| 224 | 223 |
| 225 | 224 |
| 226 static void CreateAndWriteSnapshot(bool script_snapshot) { | 225 static void CreateAndWriteSnapshot(bool script_snapshot) { |
| 227 Dart_Handle result; | 226 Dart_Handle result; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 244 | 243 |
| 245 // Shutdown the isolate. | 244 // Shutdown the isolate. |
| 246 Dart_ShutdownIsolate(); | 245 Dart_ShutdownIsolate(); |
| 247 } | 246 } |
| 248 | 247 |
| 249 | 248 |
| 250 static void SetupForGenericSnapshotCreation() { | 249 static void SetupForGenericSnapshotCreation() { |
| 251 // Set up the library tag handler for this isolate. | 250 // Set up the library tag handler for this isolate. |
| 252 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 251 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
| 253 if (Dart_IsError(result)) { | 252 if (Dart_IsError(result)) { |
| 254 fprintf(stderr, "%s", Dart_GetError(result)); | 253 Log::PrintErr("%s", Dart_GetError(result)); |
| 255 Dart_ExitScope(); | 254 Dart_ExitScope(); |
| 256 Dart_ShutdownIsolate(); | 255 Dart_ShutdownIsolate(); |
| 257 exit(255); | 256 exit(255); |
| 258 } | 257 } |
| 259 // This is a generic dart snapshot which needs builtin library setup. | 258 // This is a generic dart snapshot which needs builtin library setup. |
| 260 Dart_Handle library = | 259 Dart_Handle library = |
| 261 LoadGenericSnapshotCreationScript(Builtin::kBuiltinLibrary); | 260 LoadGenericSnapshotCreationScript(Builtin::kBuiltinLibrary); |
| 262 VerifyLoaded(library); | 261 VerifyLoaded(library); |
| 263 library = LoadGenericSnapshotCreationScript(Builtin::kJsonLibrary); | 262 library = LoadGenericSnapshotCreationScript(Builtin::kJsonLibrary); |
| 264 VerifyLoaded(library); | 263 VerifyLoaded(library); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 283 // Parse command line arguments. | 282 // Parse command line arguments. |
| 284 if (ParseArguments(argc, | 283 if (ParseArguments(argc, |
| 285 argv, | 284 argv, |
| 286 &vm_options, | 285 &vm_options, |
| 287 &app_script_name) < 0) { | 286 &app_script_name) < 0) { |
| 288 PrintUsage(); | 287 PrintUsage(); |
| 289 return 255; | 288 return 255; |
| 290 } | 289 } |
| 291 | 290 |
| 292 if (snapshot_filename == NULL) { | 291 if (snapshot_filename == NULL) { |
| 293 fprintf(stderr, "No snapshot output file specified\n"); | 292 Log::PrintErr("No snapshot output file specified\n"); |
| 294 return 255; | 293 return 255; |
| 295 } | 294 } |
| 296 | 295 |
| 297 DartUtils::SetOriginalWorkingDirectory(); | 296 DartUtils::SetOriginalWorkingDirectory(); |
| 298 | 297 |
| 299 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); | 298 Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
| 300 | 299 |
| 301 // Initialize the Dart VM. | 300 // Initialize the Dart VM. |
| 302 // Note: We don't expect isolates to be created from dart code during | 301 // Note: We don't expect isolates to be created from dart code during |
| 303 // snapshot generation. | 302 // snapshot generation. |
| 304 if (!Dart_Initialize(NULL, NULL, NULL)) { | 303 if (!Dart_Initialize(NULL, NULL, NULL)) { |
| 305 fprintf(stderr, "VM initialization failed\n"); | 304 Log::PrintErr("VM initialization failed\n"); |
| 306 return 255; | 305 return 255; |
| 307 } | 306 } |
| 308 | 307 |
| 309 char* error; | 308 char* error; |
| 310 Dart_Isolate isolate = Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error); | 309 Dart_Isolate isolate = Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error); |
| 311 if (isolate == NULL) { | 310 if (isolate == NULL) { |
| 312 fprintf(stderr, "Error: %s", error); | 311 Log::PrintErr("Error: %s", error); |
| 313 free(error); | 312 free(error); |
| 314 exit(255); | 313 exit(255); |
| 315 } | 314 } |
| 316 | 315 |
| 317 Dart_Handle result; | 316 Dart_Handle result; |
| 318 Dart_Handle library; | 317 Dart_Handle library; |
| 319 Dart_EnterScope(); | 318 Dart_EnterScope(); |
| 320 | 319 |
| 321 ASSERT(snapshot_filename != NULL); | 320 ASSERT(snapshot_filename != NULL); |
| 322 // Load up the script before a snapshot is created. | 321 // Load up the script before a snapshot is created. |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 353 Dart_ExitScope(); | 352 Dart_ExitScope(); |
| 354 Dart_ShutdownIsolate(); | 353 Dart_ShutdownIsolate(); |
| 355 | 354 |
| 356 // Now load the specified script and create a script snapshot. | 355 // Now load the specified script and create a script snapshot. |
| 357 Dart_Isolate isolate = Dart_CreateIsolate(NULL, | 356 Dart_Isolate isolate = Dart_CreateIsolate(NULL, |
| 358 NULL, | 357 NULL, |
| 359 snapshot_buffer, | 358 snapshot_buffer, |
| 360 NULL, | 359 NULL, |
| 361 &error); | 360 &error); |
| 362 if (isolate == NULL) { | 361 if (isolate == NULL) { |
| 363 fprintf(stderr, "%s", error); | 362 Log::PrintErr("%s", error); |
| 364 free(error); | 363 free(error); |
| 365 free(snapshot_buffer); | 364 free(snapshot_buffer); |
| 366 exit(255); | 365 exit(255); |
| 367 } | 366 } |
| 368 Dart_EnterScope(); | 367 Dart_EnterScope(); |
| 369 | 368 |
| 370 // Setup generic library tag handler. | 369 // Setup generic library tag handler. |
| 371 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 370 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
| 372 CHECK_RESULT(result); | 371 CHECK_RESULT(result); |
| 373 | 372 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 388 CreateAndWriteSnapshot(true); | 387 CreateAndWriteSnapshot(true); |
| 389 | 388 |
| 390 free(snapshot_buffer); | 389 free(snapshot_buffer); |
| 391 } | 390 } |
| 392 } else { | 391 } else { |
| 393 SetupForGenericSnapshotCreation(); | 392 SetupForGenericSnapshotCreation(); |
| 394 CreateAndWriteSnapshot(false); | 393 CreateAndWriteSnapshot(false); |
| 395 } | 394 } |
| 396 return 0; | 395 return 0; |
| 397 } | 396 } |
| OLD | NEW |