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 #include "bin/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 | 6 |
| 7 #include "bin/crypto.h" | 7 #include "bin/crypto.h" |
| 8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
| 9 #include "bin/extensions.h" | 9 #include "bin/extensions.h" |
| 10 #include "bin/file.h" | 10 #include "bin/file.h" |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 } | 332 } |
| 333 | 333 |
| 334 Dart_Handle DartUtils::ResolveScript(Dart_Handle url) { | 334 Dart_Handle DartUtils::ResolveScript(Dart_Handle url) { |
| 335 const int kNumArgs = 1; | 335 const int kNumArgs = 1; |
| 336 Dart_Handle dart_args[kNumArgs]; | 336 Dart_Handle dart_args[kNumArgs]; |
| 337 dart_args[0] = url; | 337 dart_args[0] = url; |
| 338 return Dart_Invoke(DartUtils::BuiltinLib(), NewString("_resolveScriptUri"), | 338 return Dart_Invoke(DartUtils::BuiltinLib(), NewString("_resolveScriptUri"), |
| 339 kNumArgs, dart_args); | 339 kNumArgs, dart_args); |
| 340 } | 340 } |
| 341 | 341 |
| 342 static Dart_Handle LoadDataAsync_Invoke(Dart_Handle tag, | |
| 343 Dart_Handle url, | |
| 344 Dart_Handle library_url) { | |
| 345 const int kNumArgs = 3; | |
| 346 Dart_Handle dart_args[kNumArgs]; | |
| 347 dart_args[0] = tag; | |
| 348 dart_args[1] = url; | |
| 349 dart_args[2] = library_url; | |
| 350 return Dart_Invoke(DartUtils::BuiltinLib(), | |
| 351 DartUtils::NewString("_loadDataAsync"), kNumArgs, | |
|
zra
2017/07/26 16:14:24
I couldn't find this Dart function. Is it already
| |
| 352 dart_args); | |
| 353 } | |
| 354 | |
| 355 Dart_Handle DartUtils::LibraryTagHandler(Dart_LibraryTag tag, | |
| 356 Dart_Handle library, | |
| 357 Dart_Handle url) { | |
| 358 Dart_Handle library_url = Dart_LibraryUrl(library); | |
| 359 if (Dart_IsError(library_url)) { | |
| 360 return library_url; | |
| 361 } | |
| 362 if (tag == Dart_kCanonicalizeUrl) { | |
| 363 return Dart_DefaultCanonicalizeUrl(library_url, url); | |
| 364 } | |
| 365 if (!Dart_IsString(url)) { | |
| 366 return Dart_NewApiError("url is not a string"); | |
| 367 } | |
| 368 const char* url_string = NULL; | |
| 369 Dart_Handle result = Dart_StringToCString(url, &url_string); | |
| 370 if (Dart_IsError(result)) { | |
| 371 return result; | |
| 372 } | |
| 373 const char* library_url_string = NULL; | |
| 374 result = Dart_StringToCString(library_url, &library_url_string); | |
| 375 if (Dart_IsError(result)) { | |
| 376 return result; | |
| 377 } | |
| 378 | |
| 379 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string); | |
| 380 bool is_dart_library = DartUtils::IsDartSchemeURL(library_url_string); | |
| 381 | |
| 382 // Handle canonicalization, 'import' and 'part' of 'dart:' libraries. | |
| 383 if (is_dart_scheme_url || is_dart_library) { | |
| 384 if (tag == Dart_kImportTag) { | |
| 385 Builtin::BuiltinLibraryId id = Builtin::FindId(url_string); | |
| 386 if (id == Builtin::kInvalidLibrary) { | |
| 387 return NewError( | |
| 388 "The built-in library '%s' is not available" | |
| 389 " on the stand-alone VM.\n", | |
| 390 url_string); | |
| 391 } | |
| 392 return Builtin::LoadLibrary(url, id); | |
| 393 } else { | |
| 394 ASSERT(tag == Dart_kSourceTag); | |
| 395 Builtin::BuiltinLibraryId id = Builtin::FindId(library_url_string); | |
| 396 if (id == Builtin::kInvalidLibrary) { | |
| 397 return NewError( | |
| 398 "The built-in library '%s' is not available" | |
| 399 " on the stand-alone VM. Trying to load" | |
| 400 " '%s'.\n", | |
| 401 library_url_string, url_string); | |
| 402 } | |
| 403 // Prepend the library URI to form a unique script URI for the part. | |
| 404 intptr_t len = snprintf(NULL, 0, "%s/%s", library_url_string, url_string); | |
| 405 char* part_uri = reinterpret_cast<char*>(malloc(len + 1)); | |
| 406 snprintf(part_uri, len + 1, "%s/%s", library_url_string, url_string); | |
| 407 Dart_Handle part_uri_obj = DartUtils::NewString(part_uri); | |
| 408 free(part_uri); | |
| 409 return Dart_LoadSource(library, part_uri_obj, Dart_Null(), | |
| 410 Builtin::PartSource(id, url_string), 0, 0); | |
| 411 } | |
| 412 // All cases should have been handled above. | |
| 413 UNREACHABLE(); | |
| 414 } | |
| 415 | |
| 416 if (DartUtils::IsDartExtensionSchemeURL(url_string)) { | |
| 417 // Load a native code shared library to use in a native extension | |
| 418 if (tag != Dart_kImportTag) { | |
| 419 return NewError("Dart extensions must use import: '%s'", url_string); | |
| 420 } | |
| 421 Dart_Handle library_file_path = DartUtils::LibraryFilePath(library_url); | |
| 422 const char* lib_path_str = NULL; | |
| 423 Dart_StringToCString(library_file_path, &lib_path_str); | |
| 424 const char* extension_path = DartUtils::RemoveScheme(url_string); | |
| 425 if (strchr(extension_path, '/') != NULL || | |
| 426 (IsWindowsHost() && strchr(extension_path, '\\') != NULL)) { | |
| 427 return NewError( | |
| 428 "Relative paths for dart extensions are not supported: '%s'", | |
| 429 extension_path); | |
| 430 } | |
| 431 return Extensions::LoadExtension(lib_path_str, extension_path, library); | |
| 432 } | |
| 433 | |
| 434 // Handle 'import' or 'part' requests for all other URIs. Call dart code to | |
| 435 // read the source code asynchronously. | |
| 436 return LoadDataAsync_Invoke(Dart_NewInteger(tag), url, library_url); | |
| 437 } | |
| 438 | |
| 439 static bool CheckMagicNumber(const uint8_t** buf, | 342 static bool CheckMagicNumber(const uint8_t** buf, |
| 440 intptr_t* len, | 343 intptr_t* len, |
| 441 const MagicNumberData& magic_number) { | 344 const MagicNumberData& magic_number) { |
| 442 if ((*len >= MagicNumberData::kLength) && | 345 if ((*len >= MagicNumberData::kLength) && |
| 443 (memcmp(*buf, magic_number.bytes, MagicNumberData::kLength) == 0)) { | 346 (memcmp(*buf, magic_number.bytes, MagicNumberData::kLength) == 0)) { |
| 444 if (magic_number.should_skip) { | 347 if (magic_number.should_skip) { |
| 445 *buf += MagicNumberData::kLength; | 348 *buf += MagicNumberData::kLength; |
| 446 *len -= MagicNumberData::kLength; | 349 *len -= MagicNumberData::kLength; |
| 447 } | 350 } |
| 448 return true; | 351 return true; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 463 return kUnknownMagicNumber; | 366 return kUnknownMagicNumber; |
| 464 } | 367 } |
| 465 | 368 |
| 466 void DartUtils::WriteMagicNumber(File* file) { | 369 void DartUtils::WriteMagicNumber(File* file) { |
| 467 // Write a magic number and version information into the snapshot file. | 370 // Write a magic number and version information into the snapshot file. |
| 468 bool bytes_written = | 371 bool bytes_written = |
| 469 file->WriteFully(snapshot_magic_number.bytes, MagicNumberData::kLength); | 372 file->WriteFully(snapshot_magic_number.bytes, MagicNumberData::kLength); |
| 470 ASSERT(bytes_written); | 373 ASSERT(bytes_written); |
| 471 } | 374 } |
| 472 | 375 |
| 473 Dart_Handle DartUtils::LoadScript(const char* script_uri) { | |
| 474 Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(), | |
| 475 Dart_GetMainPortId(), Dart_Timeline_Event_Async_Begin, 0, | |
| 476 NULL, NULL); | |
| 477 Dart_Handle uri = Dart_NewStringFromCString(script_uri); | |
| 478 return LoadDataAsync_Invoke(Dart_Null(), uri, Dart_Null()); | |
| 479 } | |
| 480 | |
| 481 void FUNCTION_NAME(Builtin_GetCurrentDirectory)(Dart_NativeArguments args) { | 376 void FUNCTION_NAME(Builtin_GetCurrentDirectory)(Dart_NativeArguments args) { |
| 482 const char* current = Directory::Current(); | 377 const char* current = Directory::Current(); |
| 483 if (current != NULL) { | 378 if (current != NULL) { |
| 484 Dart_SetReturnValue(args, DartUtils::NewString(current)); | 379 Dart_SetReturnValue(args, DartUtils::NewString(current)); |
| 485 } else { | 380 } else { |
| 486 Dart_Handle err = DartUtils::NewError("Failed to get current directory."); | 381 Dart_Handle err = DartUtils::NewError("Failed to get current directory."); |
| 487 Dart_PropagateError(err); | 382 Dart_PropagateError(err); |
| 488 } | 383 } |
| 489 } | 384 } |
| 490 | 385 |
| (...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1059 new CObjectString(CObject::NewString(os_error->message())); | 954 new CObjectString(CObject::NewString(os_error->message())); |
| 1060 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 955 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 1061 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 956 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 1062 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 957 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 1063 result->SetAt(2, error_message); | 958 result->SetAt(2, error_message); |
| 1064 return result; | 959 return result; |
| 1065 } | 960 } |
| 1066 | 961 |
| 1067 } // namespace bin | 962 } // namespace bin |
| 1068 } // namespace dart | 963 } // namespace dart |
| OLD | NEW |