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/extensions.h" | 7 #include "bin/extensions.h" |
| 8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
| 9 #include "bin/file.h" | 9 #include "bin/file.h" |
| 10 #include "bin/io_buffer.h" | 10 #include "bin/io_buffer.h" |
| 11 #include "include/dart_api.h" | 11 #include "include/dart_api.h" |
| 12 #include "platform/assert.h" | 12 #include "platform/assert.h" |
| 13 #include "platform/globals.h" | 13 #include "platform/globals.h" |
| 14 | 14 |
| 15 const char* DartUtils::original_working_directory = NULL; | 15 const char* DartUtils::original_working_directory = NULL; |
| 16 const char* DartUtils::kDartScheme = "dart:"; | 16 const char* DartUtils::kDartScheme = "dart:"; |
| 17 const char* DartUtils::kDartExtensionScheme = "dart-ext:"; | 17 const char* DartUtils::kDartExtensionScheme = "dart-ext:"; |
| 18 const char* DartUtils::kAsyncLibURL = "dart:async"; | 18 const char* DartUtils::kAsyncLibURL = "dart:async"; |
| 19 const char* DartUtils::kBuiltinLibURL = "dart:builtin"; | 19 const char* DartUtils::kBuiltinLibURL = "dart:builtin"; |
| 20 const char* DartUtils::kCoreLibURL = "dart:core"; | 20 const char* DartUtils::kCoreLibURL = "dart:core"; |
| 21 const char* DartUtils::kIOLibURL = "dart:io"; | 21 const char* DartUtils::kIOLibURL = "dart:io"; |
| 22 const char* DartUtils::kIOLibPatchURL = "dart:io-patch"; | 22 const char* DartUtils::kIOLibPatchURL = "dart:io-patch"; |
| 23 const char* DartUtils::kUriLibURL = "dart:uri"; | 23 const char* DartUtils::kUriLibURL = "dart:uri"; |
| 24 const char* DartUtils::kUtfLibURL = "dart:utf"; | 24 const char* DartUtils::kUtfLibURL = "dart:utf"; |
| 25 | 25 |
| 26 const char* DartUtils::kIdFieldName = "_id"; | 26 const char* DartUtils::kIdFieldName = "_id"; |
| 27 | 27 |
| 28 uint8_t DartUtils::magic_number[] = { 0xf5, 0xf5, 0xdc, 0xdc }; | |
| 28 | 29 |
| 29 static bool IsWindowsHost() { | 30 static bool IsWindowsHost() { |
| 30 #if defined(TARGET_OS_WINDOWS) | 31 #if defined(TARGET_OS_WINDOWS) |
| 31 return true; | 32 return true; |
| 32 #else // defined(TARGET_OS_WINDOWS) | 33 #else // defined(TARGET_OS_WINDOWS) |
| 33 return false; | 34 return false; |
| 34 #endif // defined(TARGET_OS_WINDOWS) | 35 #endif // defined(TARGET_OS_WINDOWS) |
| 35 } | 36 } |
| 36 | 37 |
| 37 | 38 |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 } | 181 } |
| 181 // Calculate the canonical path. | 182 // Calculate the canonical path. |
| 182 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str); | 183 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str); |
| 183 Dart_Handle canon_url = NewString(canon_url_str); | 184 Dart_Handle canon_url = NewString(canon_url_str); |
| 184 free(const_cast<char*>(canon_url_str)); | 185 free(const_cast<char*>(canon_url_str)); |
| 185 | 186 |
| 186 return canon_url; | 187 return canon_url; |
| 187 } | 188 } |
| 188 | 189 |
| 189 | 190 |
| 190 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { | 191 static const uint8_t* ReadFile(const char* filename, |
| 192 intptr_t* file_len, | |
| 193 const char** error_msg) { | |
| 191 File* file = File::Open(filename, File::kRead); | 194 File* file = File::Open(filename, File::kRead); |
| 192 if (file == NULL) { | 195 if (file == NULL) { |
| 193 const char* format = "Unable to open file: %s"; | 196 const char* format = "Unable to open file: %s"; |
| 194 intptr_t len = snprintf(NULL, 0, format, filename); | 197 intptr_t len = snprintf(NULL, 0, format, filename); |
| 195 // TODO(iposva): Allocate from the zone instead of leaking error string | 198 // TODO(iposva): Allocate from the zone instead of leaking error string |
| 196 // here. On the other hand the binary is about the exit anyway. | 199 // here. On the other hand the binary is about the exit anyway. |
| 197 char* error_msg = reinterpret_cast<char*>(malloc(len + 1)); | 200 char* msg = reinterpret_cast<char*>(malloc(len + 1)); |
| 198 snprintf(error_msg, len + 1, format, filename); | 201 snprintf(msg, len + 1, format, filename); |
| 202 *error_msg = msg; | |
| 203 return NULL; | |
| 204 } | |
| 205 *file_len = file->Length(); | |
| 206 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(*file_len)); | |
| 207 if (text_buffer == NULL) { | |
| 208 delete file; | |
| 209 *error_msg = "Unable to allocate buffer"; | |
| 210 return NULL; | |
| 211 } | |
| 212 if (!file->ReadFully(text_buffer, *file_len)) { | |
| 213 delete file; | |
| 214 free(text_buffer); | |
| 215 *error_msg = "Unable to fully read contents"; | |
| 216 return NULL; | |
| 217 } | |
| 218 delete file; | |
| 219 return text_buffer; | |
| 220 } | |
| 221 | |
| 222 | |
| 223 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { | |
| 224 const char* error_msg = NULL; | |
| 225 intptr_t len; | |
| 226 const uint8_t* text_buffer = ReadFile(filename, &len, &error_msg); | |
| 227 if (text_buffer == NULL) { | |
| 199 return Dart_Error(error_msg); | 228 return Dart_Error(error_msg); |
| 200 } | 229 } |
| 201 intptr_t len = file->Length(); | |
| 202 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(len)); | |
| 203 if (text_buffer == NULL) { | |
| 204 delete file; | |
| 205 return Dart_Error("Unable to allocate buffer"); | |
| 206 } | |
| 207 if (!file->ReadFully(text_buffer, len)) { | |
| 208 delete file; | |
| 209 free(text_buffer); | |
| 210 return Dart_Error("Unable to fully read contents"); | |
| 211 } | |
| 212 delete file; | |
| 213 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); | 230 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); |
| 214 free(text_buffer); | |
| 215 return str; | 231 return str; |
| 216 } | 232 } |
| 217 | 233 |
| 218 | 234 |
| 219 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, | 235 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, |
| 220 Dart_Handle builtin_lib) { | 236 Dart_Handle builtin_lib) { |
| 221 const int kNumArgs = 3; | 237 const int kNumArgs = 3; |
| 222 Dart_Handle dart_args[kNumArgs]; | 238 Dart_Handle dart_args[kNumArgs]; |
| 223 dart_args[0] = NewString(original_working_directory); | 239 dart_args[0] = NewString(original_working_directory); |
| 224 dart_args[1] = script_uri; | 240 dart_args[1] = script_uri; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 } | 334 } |
| 319 result = DartUtils::LoadSource(NULL, | 335 result = DartUtils::LoadSource(NULL, |
| 320 library, | 336 library, |
| 321 url, | 337 url, |
| 322 tag, | 338 tag, |
| 323 url_string); | 339 url_string); |
| 324 return result; | 340 return result; |
| 325 } | 341 } |
| 326 | 342 |
| 327 | 343 |
| 328 static Dart_Handle ReadSource(Dart_Handle script_uri, | 344 const uint8_t* DartUtils::SniffForMagicNumber(const uint8_t* text_buffer, |
| 329 Dart_Handle builtin_lib) { | 345 bool* is_snapshot) { |
| 330 Dart_Handle script_path = DartUtils::FilePathFromUri(script_uri, builtin_lib); | 346 intptr_t len = sizeof(magic_number); |
| 331 if (Dart_IsError(script_path)) { | 347 for (intptr_t i = 0; i < len; i++) { |
| 332 return script_path; | 348 if (text_buffer[i] != magic_number[i]) { |
| 349 *is_snapshot = false; | |
| 350 return text_buffer; | |
| 351 } | |
| 333 } | 352 } |
| 334 const char* script_path_cstr; | 353 *is_snapshot = true; |
| 335 Dart_StringToCString(script_path, &script_path_cstr); | 354 return text_buffer + len; |
| 336 Dart_Handle source = DartUtils::ReadStringFromFile(script_path_cstr); | 355 } |
| 337 return source; | 356 |
| 357 | |
| 358 void DartUtils::WriteMagicNumber(File* file) { | |
| 359 // Write a magic number and version information into the snapshot file. | |
| 360 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); | |
| 361 ASSERT(bytes_written); | |
| 338 } | 362 } |
| 339 | 363 |
| 340 | 364 |
| 341 Dart_Handle DartUtils::LoadScript(const char* script_uri, | 365 Dart_Handle DartUtils::LoadScript(const char* script_uri, |
| 342 Dart_Handle builtin_lib) { | 366 Dart_Handle builtin_lib) { |
| 343 Dart_Handle resolved_script_uri; | 367 Dart_Handle resolved_script_uri; |
| 344 resolved_script_uri = ResolveScriptUri(NewString(script_uri), builtin_lib); | 368 resolved_script_uri = ResolveScriptUri(NewString(script_uri), builtin_lib); |
| 345 if (Dart_IsError(resolved_script_uri)) { | 369 if (Dart_IsError(resolved_script_uri)) { |
| 346 return resolved_script_uri; | 370 return resolved_script_uri; |
| 347 } | 371 } |
| 348 Dart_Handle source = ReadSource(resolved_script_uri, builtin_lib); | 372 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, |
|
Ivan Posva
2013/03/20 06:18:00
Does this mean that script snapshots can be loaded
siva
2013/03/20 20:57:20
Yes, that was the intent. I wanted to make the cod
| |
| 349 if (Dart_IsError(source)) { | 373 builtin_lib); |
| 350 return source; | 374 if (Dart_IsError(script_path)) { |
| 375 return script_path; | |
| 351 } | 376 } |
| 352 return Dart_LoadEmbeddedScript(resolved_script_uri, source, 0, 0); | 377 const char* script_path_cstr; |
| 378 Dart_StringToCString(script_path, &script_path_cstr); | |
| 379 const char* error_msg = NULL; | |
| 380 intptr_t len; | |
| 381 const uint8_t* text_buffer = ReadFile(script_path_cstr, &len, &error_msg); | |
| 382 if (text_buffer == NULL) { | |
| 383 return Dart_Error(error_msg); | |
| 384 } | |
| 385 bool is_snapshot = false; | |
| 386 text_buffer = SniffForMagicNumber(text_buffer, &is_snapshot); | |
| 387 if (is_snapshot) { | |
| 388 return Dart_LoadScriptFromSnapshot(text_buffer); | |
|
Ivan Posva
2013/03/20 06:18:00
I am wondering whether we should be passing the le
siva
2013/03/20 20:57:20
Done.
| |
| 389 } else { | |
| 390 Dart_Handle source = Dart_NewStringFromUTF8(text_buffer, len); | |
| 391 return Dart_LoadEmbeddedScript(resolved_script_uri, source, 0, 0); | |
| 392 } | |
| 353 } | 393 } |
| 354 | 394 |
| 355 | 395 |
| 356 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping, | 396 Dart_Handle DartUtils::LoadSource(CommandLineOptions* url_mapping, |
| 357 Dart_Handle library, | 397 Dart_Handle library, |
| 358 Dart_Handle url, | 398 Dart_Handle url, |
| 359 Dart_LibraryTag tag, | 399 Dart_LibraryTag tag, |
| 360 const char* url_string) { | 400 const char* url_string) { |
| 361 if (url_mapping != NULL && IsDartSchemeURL(url_string)) { | 401 if (url_mapping != NULL && IsDartSchemeURL(url_string)) { |
| 362 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string); | 402 const char* mapped_url_string = MapLibraryUrl(url_mapping, url_string); |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 694 | 734 |
| 695 CObject* CObject::NewOSError(OSError* os_error) { | 735 CObject* CObject::NewOSError(OSError* os_error) { |
| 696 CObject* error_message = | 736 CObject* error_message = |
| 697 new CObjectString(CObject::NewString(os_error->message())); | 737 new CObjectString(CObject::NewString(os_error->message())); |
| 698 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 738 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
| 699 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 739 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
| 700 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 740 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| 701 result->SetAt(2, error_message); | 741 result->SetAt(2, error_message); |
| 702 return result; | 742 return result; |
| 703 } | 743 } |
| OLD | NEW |