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> |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 static void WriteSnapshotFile(const uint8_t* buffer, const intptr_t size) { | 142 static void WriteSnapshotFile(const uint8_t* buffer, const intptr_t size) { |
| 143 File* file = File::Open(snapshot_filename, File::kWriteTruncate); | 143 File* file = File::Open(snapshot_filename, File::kWriteTruncate); |
| 144 ASSERT(file != NULL); | 144 ASSERT(file != NULL); |
| 145 for (intptr_t i = 0; i < size; i++) { | 145 for (intptr_t i = 0; i < size; i++) { |
| 146 file->WriteByte(buffer[i]); | 146 file->WriteByte(buffer[i]); |
| 147 } | 147 } |
| 148 delete file; | 148 delete file; |
| 149 } | 149 } |
| 150 | 150 |
| 151 | 151 |
| 152 class UriResolverIsolateScope { | |
| 153 public: | |
| 154 UriResolverIsolateScope() { | |
| 155 ASSERT(isolate != NULL); | |
| 156 snapshotted_isolate_ = Dart_CurrentIsolate(); | |
| 157 Dart_ExitIsolate(); | |
| 158 Dart_EnterIsolate(isolate); | |
| 159 Dart_EnterScope(); | |
| 160 } | |
| 161 | |
| 162 ~UriResolverIsolateScope() { | |
| 163 ASSERT(snapshotted_isolate_ != NULL); | |
| 164 Dart_ExitScope(); | |
| 165 Dart_ExitIsolate(); | |
| 166 Dart_EnterIsolate(snapshotted_isolate_); | |
| 167 } | |
| 168 | |
| 169 static Dart_Isolate isolate; | |
| 170 | |
| 171 private: | |
| 172 Dart_Isolate snapshotted_isolate_; | |
| 173 | |
| 174 DISALLOW_COPY_AND_ASSIGN(UriResolverIsolateScope); | |
| 175 }; | |
| 176 | |
| 177 Dart_Isolate UriResolverIsolateScope::isolate = NULL; | |
| 178 | |
| 179 | |
| 180 static Dart_Handle ResolveScriptUri(const char* script_uri) { | |
| 181 bool failed = false; | |
| 182 char* result_string = NULL; | |
| 183 | |
| 184 { | |
| 185 UriResolverIsolateScope scope; | |
| 186 | |
| 187 // Run DartUtils::ResolveScriptUri in context of uri resolver isolate. | |
| 188 Dart_Handle builtin_lib = | |
| 189 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | |
| 190 CHECK_RESULT(builtin_lib); | |
| 191 | |
| 192 Dart_Handle result = DartUtils::ResolveScriptUri( | |
| 193 DartUtils::NewString(script_uri), builtin_lib); | |
| 194 if (Dart_IsError(result)) { | |
| 195 failed = true; | |
| 196 result_string = strdup(Dart_GetError(result)); | |
| 197 } else { | |
| 198 result_string = strdup(DartUtils::GetStringValue(result)); | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 Dart_Handle result = failed ? Dart_NewApiError(result_string) : | |
| 203 DartUtils::NewString(result_string); | |
| 204 free(result_string); | |
| 205 return result; | |
| 206 } | |
| 207 | |
| 208 | |
| 209 static Dart_Handle FilePathFromUri(const char* script_uri) { | |
| 210 bool failed = false; | |
| 211 char* result_string = NULL; | |
| 212 | |
| 213 { | |
| 214 UriResolverIsolateScope scope; | |
| 215 | |
| 216 // Run DartUtils::FilePathFromUri in context of uri resolver isolate. | |
| 217 Dart_Handle builtin_lib = | |
| 218 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | |
| 219 CHECK_RESULT(builtin_lib); | |
| 220 | |
| 221 Dart_Handle result = DartUtils::FilePathFromUri( | |
| 222 DartUtils::NewString(script_uri), builtin_lib); | |
| 223 if (Dart_IsError(result)) { | |
| 224 failed = true; | |
| 225 result_string = strdup(Dart_GetError(result)); | |
| 226 } else { | |
| 227 result_string = strdup(DartUtils::GetStringValue(result)); | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 Dart_Handle result = failed ? Dart_NewApiError(result_string) : | |
| 232 DartUtils::NewString(result_string); | |
| 233 free(result_string); | |
| 234 return result; | |
| 235 } | |
| 236 | |
| 237 | |
| 238 static Dart_Handle ResolveUri(const char* library_uri, const char* uri) { | |
| 239 bool failed = false; | |
| 240 char* result_string = NULL; | |
| 241 | |
| 242 { | |
| 243 UriResolverIsolateScope scope; | |
| 244 | |
| 245 // Run DartUtils::ResolveUri in context of uri resolver isolate. | |
| 246 Dart_Handle builtin_lib = | |
| 247 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | |
| 248 CHECK_RESULT(builtin_lib); | |
| 249 | |
| 250 Dart_Handle result = DartUtils::ResolveUri( | |
| 251 DartUtils::NewString(library_uri), | |
| 252 DartUtils::NewString(uri), | |
| 253 builtin_lib); | |
| 254 if (Dart_IsError(result)) { | |
| 255 failed = true; | |
| 256 result_string = strdup(Dart_GetError(result)); | |
| 257 } else { | |
| 258 result_string = strdup(DartUtils::GetStringValue(result)); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 Dart_Handle result = failed ? Dart_NewApiError(result_string) : | |
| 263 DartUtils::NewString(result_string); | |
| 264 free(result_string); | |
| 265 return result; | |
| 266 } | |
| 267 | |
| 268 | |
| 152 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, | 269 static Dart_Handle CreateSnapshotLibraryTagHandler(Dart_LibraryTag tag, |
| 153 Dart_Handle library, | 270 Dart_Handle library, |
| 154 Dart_Handle url) { | 271 Dart_Handle url) { |
| 155 if (!Dart_IsLibrary(library)) { | 272 if (!Dart_IsLibrary(library)) { |
| 156 return Dart_Error("not a library"); | 273 return Dart_Error("not a library"); |
| 157 } | 274 } |
| 275 Dart_Handle library_url = Dart_LibraryUrl(library); | |
| 276 if (Dart_IsError(library_url)) { | |
| 277 return Dart_Error("accessing library url failed"); | |
| 278 } | |
| 279 const char* library_url_string = DartUtils::GetStringValue(library_url); | |
| 280 const char* mapped_library_url_string = DartUtils::MapLibraryUrl( | |
| 281 url_mapping, library_url_string); | |
| 282 if (mapped_library_url_string != NULL) { | |
| 283 library_url = ResolveScriptUri(mapped_library_url_string); | |
| 284 library_url_string = DartUtils::GetStringValue(library_url); | |
| 285 } | |
| 286 | |
| 158 if (!Dart_IsString(url)) { | 287 if (!Dart_IsString(url)) { |
| 159 return Dart_Error("url is not a string"); | 288 return Dart_Error("url is not a string"); |
| 160 } | 289 } |
| 161 const char* url_string = NULL; | 290 const char* url_string = DartUtils::GetStringValue(url); |
| 162 Dart_Handle result = Dart_StringToCString(url, &url_string); | 291 const char* mapped_url_string = DartUtils::MapLibraryUrl(url_mapping, |
| 163 if (Dart_IsError(result)) { | 292 url_string); |
| 164 return result; | 293 |
| 294 if (tag == kCanonicalizeUrl) { | |
| 295 // Keep original names for libraries that have a mapping (e.g. dart:io). | |
| 296 if (mapped_url_string) { | |
| 297 return url; | |
| 298 } | |
| 299 return ResolveUri(library_url_string, url_string); | |
| 165 } | 300 } |
| 166 | 301 |
| 167 // If the URL starts with "dart:" then it is handled specially. | 302 Dart_Handle resolved_url = url; |
| 168 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); | 303 if (mapped_url_string != NULL) { |
| 169 if (tag == kCanonicalizeUrl) { | 304 // Mapped urls are relative to working directory. |
| 170 if (is_dart_scheme_url) { | 305 resolved_url = ResolveScriptUri(mapped_url_string); |
| 171 return url; | 306 if (Dart_IsError(resolved_url)) { |
| 307 return resolved_url; | |
| 172 } | 308 } |
| 173 return DartUtils::CanonicalizeURL(url_mapping, library, url_string); | |
| 174 } | 309 } |
| 175 return DartUtils::LoadSource(url_mapping, | 310 |
| 311 // Get the file path out of the url. | |
| 312 Dart_Handle file_path = FilePathFromUri( | |
| 313 DartUtils::GetStringValue(resolved_url)); | |
| 314 if (Dart_IsError(file_path)) { | |
| 315 return file_path; | |
| 316 } | |
| 317 | |
| 318 return DartUtils::LoadSource(NULL, | |
| 176 library, | 319 library, |
| 177 url, | 320 url, |
| 178 tag, | 321 tag, |
| 179 url_string); | 322 DartUtils::GetStringValue(file_path)); |
| 180 } | 323 } |
| 181 | 324 |
| 182 | 325 |
| 183 static Dart_Handle LoadSnapshotCreationScript(const char* script_name) { | 326 static Dart_Handle LoadSnapshotCreationScript(const char* script_name) { |
| 184 Dart_Handle source = DartUtils::ReadStringFromFile(script_name); | 327 Dart_Handle resolved_script_uri = ResolveScriptUri(script_name); |
| 328 if (Dart_IsError(resolved_script_uri)) { | |
| 329 return resolved_script_uri; | |
| 330 } | |
| 331 Dart_Handle script_path = FilePathFromUri( | |
| 332 DartUtils::GetStringValue(resolved_script_uri)); | |
| 333 if (Dart_IsError(script_path)) { | |
| 334 return script_path; | |
| 335 } | |
| 336 Dart_Handle source = DartUtils::ReadStringFromFile( | |
| 337 DartUtils::GetStringValue(script_path)); | |
| 185 if (Dart_IsError(source)) { | 338 if (Dart_IsError(source)) { |
| 186 return source; // source contains the error string. | 339 return source; |
| 187 } | 340 } |
| 188 Dart_Handle url = DartUtils::NewString(script_name); | 341 return Dart_LoadScript(resolved_script_uri, source); |
| 189 | |
| 190 return Dart_LoadScript(url, source); | |
| 191 } | 342 } |
| 192 | 343 |
| 193 | 344 |
| 194 static Dart_Handle LoadGenericSnapshotCreationScript( | 345 static Dart_Handle LoadGenericSnapshotCreationScript( |
| 195 Builtin::BuiltinLibraryId id) { | 346 Builtin::BuiltinLibraryId id) { |
| 196 Dart_Handle source = Builtin::Source(id); | 347 Dart_Handle source = Builtin::Source(id); |
| 197 if (Dart_IsError(source)) { | 348 if (Dart_IsError(source)) { |
| 198 return source; // source contains the error string. | 349 return source; // source contains the error string. |
| 199 } | 350 } |
| 200 Dart_Handle lib; | 351 Dart_Handle lib; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 | 391 |
| 241 // Now write the snapshot out to specified file and exit. | 392 // Now write the snapshot out to specified file and exit. |
| 242 WriteSnapshotFile(buffer, size); | 393 WriteSnapshotFile(buffer, size); |
| 243 Dart_ExitScope(); | 394 Dart_ExitScope(); |
| 244 | 395 |
| 245 // Shutdown the isolate. | 396 // Shutdown the isolate. |
| 246 Dart_ShutdownIsolate(); | 397 Dart_ShutdownIsolate(); |
| 247 } | 398 } |
| 248 | 399 |
| 249 | 400 |
| 250 static void SetupForGenericSnapshotCreation() { | 401 static void SetupForGenericSnapshotCreation(bool script_snapshot) { |
|
siva
2012/12/04 20:00:02
It would probably be more readable if you split th
podivilov
2012/12/04 20:20:02
Done.
| |
| 251 // Set up the library tag handler for this isolate. | 402 // Set up the library tag handler for this isolate. |
| 252 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 403 Dart_Handle result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
| 253 if (Dart_IsError(result)) { | 404 if (Dart_IsError(result)) { |
| 254 Log::PrintErr("%s", Dart_GetError(result)); | 405 Log::PrintErr("%s", Dart_GetError(result)); |
| 255 Dart_ExitScope(); | 406 Dart_ExitScope(); |
| 256 Dart_ShutdownIsolate(); | 407 Dart_ShutdownIsolate(); |
| 257 exit(255); | 408 exit(255); |
| 258 } | 409 } |
| 259 // This is a generic dart snapshot which needs builtin library setup. | 410 // This is a generic dart snapshot which needs builtin library setup. |
| 260 Dart_Handle library = | 411 Dart_Handle library = |
| 261 LoadGenericSnapshotCreationScript(Builtin::kBuiltinLibrary); | 412 LoadGenericSnapshotCreationScript(Builtin::kBuiltinLibrary); |
| 262 VerifyLoaded(library); | 413 VerifyLoaded(library); |
| 263 library = LoadGenericSnapshotCreationScript(Builtin::kJsonLibrary); | |
| 264 VerifyLoaded(library); | |
| 265 library = LoadGenericSnapshotCreationScript(Builtin::kUriLibrary); | 414 library = LoadGenericSnapshotCreationScript(Builtin::kUriLibrary); |
| 266 VerifyLoaded(library); | 415 VerifyLoaded(library); |
| 267 library = LoadGenericSnapshotCreationScript(Builtin::kCryptoLibrary); | |
| 268 VerifyLoaded(library); | |
| 269 library = LoadGenericSnapshotCreationScript(Builtin::kIOLibrary); | |
| 270 VerifyLoaded(library); | |
| 271 library = LoadGenericSnapshotCreationScript(Builtin::kUtfLibrary); | 416 library = LoadGenericSnapshotCreationScript(Builtin::kUtfLibrary); |
| 272 VerifyLoaded(library); | 417 VerifyLoaded(library); |
| 418 if (script_snapshot) { | |
| 419 library = LoadGenericSnapshotCreationScript(Builtin::kJsonLibrary); | |
| 420 VerifyLoaded(library); | |
| 421 library = LoadGenericSnapshotCreationScript(Builtin::kCryptoLibrary); | |
| 422 VerifyLoaded(library); | |
| 423 library = LoadGenericSnapshotCreationScript(Builtin::kIOLibrary); | |
| 424 VerifyLoaded(library); | |
| 425 } | |
| 273 } | 426 } |
| 274 | 427 |
| 275 | 428 |
| 276 int main(int argc, char** argv) { | 429 int main(int argc, char** argv) { |
| 277 CommandLineOptions vm_options(argc); | 430 CommandLineOptions vm_options(argc); |
| 278 | 431 |
| 279 // Initialize the URL mapping array. | 432 // Initialize the URL mapping array. |
| 280 CommandLineOptions url_mapping_array(argc); | 433 CommandLineOptions url_mapping_array(argc); |
| 281 url_mapping = &url_mapping_array; | 434 url_mapping = &url_mapping_array; |
| 282 | 435 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 | 469 |
| 317 Dart_Handle result; | 470 Dart_Handle result; |
| 318 Dart_Handle library; | 471 Dart_Handle library; |
| 319 Dart_EnterScope(); | 472 Dart_EnterScope(); |
| 320 | 473 |
| 321 ASSERT(snapshot_filename != NULL); | 474 ASSERT(snapshot_filename != NULL); |
| 322 // Load up the script before a snapshot is created. | 475 // Load up the script before a snapshot is created. |
| 323 if (app_script_name != NULL) { | 476 if (app_script_name != NULL) { |
| 324 if (!script_snapshot) { | 477 if (!script_snapshot) { |
| 325 // This is the case of a custom embedder (e.g: dartium) trying to | 478 // This is the case of a custom embedder (e.g: dartium) trying to |
| 326 // create a full snapshot. Set up the library tag handler for this case | 479 // create a full snapshot. The current isolate is set up so that we can |
| 327 // in such a manner that it will use the URL mapping specified on the | 480 // invoke the dart uri resolution code like _resolveURI. App script is |
| 328 // command line to load the libraries. | 481 // loaded into a separate isolate. |
| 482 | |
| 483 SetupForGenericSnapshotCreation(false); | |
| 484 | |
| 485 // Get handle to builtin library. | |
| 486 Dart_Handle builtin_lib = | |
| 487 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | |
| 488 CHECK_RESULT(builtin_lib); | |
| 489 | |
| 490 // Prepare for script loading by setting up the 'print' and 'timer' | |
| 491 // closures and setting up 'package root' for URI resolution. | |
| 492 result = DartUtils::PrepareForScriptLoading(package_root, builtin_lib); | |
| 493 CHECK_RESULT(result); | |
| 494 Dart_ExitScope(); | |
| 495 | |
| 496 UriResolverIsolateScope::isolate = isolate; | |
| 497 | |
| 498 // Now we create an isolate into which we load all the code that needs to | |
| 499 // be in the snapshot. | |
| 500 if (Dart_CreateIsolate(NULL, NULL, NULL, NULL, &error) == NULL) { | |
| 501 fprintf(stderr, "%s", error); | |
| 502 free(error); | |
| 503 exit(255); | |
| 504 } | |
| 505 Dart_EnterScope(); | |
| 506 | |
| 507 // Set up the library tag handler in such a manner that it will use the | |
| 508 // URL mapping specified on the command line to load the libraries. | |
| 329 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); | 509 result = Dart_SetLibraryTagHandler(CreateSnapshotLibraryTagHandler); |
| 330 CHECK_RESULT(result); | 510 CHECK_RESULT(result); |
| 331 // Load the specified script. | 511 // Load the specified script. |
| 332 library = LoadSnapshotCreationScript(app_script_name); | 512 library = LoadSnapshotCreationScript(app_script_name); |
| 333 VerifyLoaded(library); | 513 VerifyLoaded(library); |
| 334 CreateAndWriteSnapshot(false); | 514 CreateAndWriteSnapshot(false); |
| 515 | |
| 516 Dart_EnterIsolate(UriResolverIsolateScope::isolate); | |
| 517 Dart_ShutdownIsolate(); | |
| 335 } else { | 518 } else { |
| 336 // This is the case where we want to create a script snapshot of | 519 // This is the case where we want to create a script snapshot of |
| 337 // the specified script. There will be no URL mapping specified for | 520 // the specified script. There will be no URL mapping specified for |
| 338 // this case, use the generic library tag handler. | 521 // this case, use the generic library tag handler. |
| 339 | 522 |
| 340 // First setup and create a generic full snapshot. | 523 // First setup and create a generic full snapshot. |
| 341 SetupForGenericSnapshotCreation(); | 524 SetupForGenericSnapshotCreation(true); |
| 342 uint8_t* buffer = NULL; | 525 uint8_t* buffer = NULL; |
| 343 intptr_t size = 0; | 526 intptr_t size = 0; |
| 344 result = Dart_CreateSnapshot(&buffer, &size); | 527 result = Dart_CreateSnapshot(&buffer, &size); |
| 345 CHECK_RESULT(result); | 528 CHECK_RESULT(result); |
| 346 | 529 |
| 347 // Save the snapshot buffer as we are about to shutdown the isolate. | 530 // Save the snapshot buffer as we are about to shutdown the isolate. |
| 348 snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size)); | 531 snapshot_buffer = reinterpret_cast<uint8_t*>(malloc(size)); |
| 349 ASSERT(snapshot_buffer != NULL); | 532 ASSERT(snapshot_buffer != NULL); |
| 350 memmove(snapshot_buffer, buffer, size); | 533 memmove(snapshot_buffer, buffer, size); |
| 351 | 534 |
| 352 // Shutdown the isolate. | 535 // Shutdown the isolate. |
| 353 Dart_ExitScope(); | 536 Dart_ExitScope(); |
| 354 Dart_ShutdownIsolate(); | 537 Dart_ShutdownIsolate(); |
| 355 | 538 |
| 356 // Now load the specified script and create a script snapshot. | 539 // Now load the specified script and create a script snapshot. |
| 357 Dart_Isolate isolate = Dart_CreateIsolate(NULL, | 540 if (Dart_CreateIsolate( |
| 358 NULL, | 541 NULL, NULL, snapshot_buffer, NULL, &error) == NULL) { |
| 359 snapshot_buffer, | |
| 360 NULL, | |
| 361 &error); | |
| 362 if (isolate == NULL) { | |
| 363 Log::PrintErr("%s", error); | 542 Log::PrintErr("%s", error); |
| 364 free(error); | 543 free(error); |
| 365 free(snapshot_buffer); | 544 free(snapshot_buffer); |
| 366 exit(255); | 545 exit(255); |
| 367 } | 546 } |
| 368 Dart_EnterScope(); | 547 Dart_EnterScope(); |
| 369 | 548 |
| 370 // Setup generic library tag handler. | 549 // Setup generic library tag handler. |
| 371 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); | 550 result = Dart_SetLibraryTagHandler(DartUtils::LibraryTagHandler); |
| 372 CHECK_RESULT(result); | 551 CHECK_RESULT(result); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 383 | 562 |
| 384 // Load specified script. | 563 // Load specified script. |
| 385 library = DartUtils::LoadScript(app_script_name, builtin_lib); | 564 library = DartUtils::LoadScript(app_script_name, builtin_lib); |
| 386 | 565 |
| 387 // Now create and write snapshot of script. | 566 // Now create and write snapshot of script. |
| 388 CreateAndWriteSnapshot(true); | 567 CreateAndWriteSnapshot(true); |
| 389 | 568 |
| 390 free(snapshot_buffer); | 569 free(snapshot_buffer); |
| 391 } | 570 } |
| 392 } else { | 571 } else { |
| 393 SetupForGenericSnapshotCreation(); | 572 SetupForGenericSnapshotCreation(false); |
|
siva
2012/12/04 20:00:02
For a full standalone snapshot we need to load all
podivilov
2012/12/04 20:20:02
Done.
| |
| 394 CreateAndWriteSnapshot(false); | 573 CreateAndWriteSnapshot(false); |
| 395 } | 574 } |
| 396 return 0; | 575 return 0; |
| 397 } | 576 } |
| OLD | NEW |