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 "include/dart_api.h" | 7 #include "include/dart_api.h" |
8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
9 | 9 |
10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 static bool IsWindowsHost() { | 38 static bool IsWindowsHost() { |
39 #if defined(TARGET_OS_WINDOWS) | 39 #if defined(TARGET_OS_WINDOWS) |
40 return true; | 40 return true; |
41 #else // defined(TARGET_OS_WINDOWS) | 41 #else // defined(TARGET_OS_WINDOWS) |
42 return false; | 42 return false; |
43 #endif // defined(TARGET_OS_WINDOWS) | 43 #endif // defined(TARGET_OS_WINDOWS) |
44 } | 44 } |
45 | 45 |
46 | 46 |
47 static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib, | |
48 const char* method) { | |
49 const int kNumArgs = 1; | |
50 Dart_Handle dart_args[kNumArgs]; | |
51 dart_args[0] = arg; | |
52 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args); | |
53 } | |
54 | |
55 | |
56 const char* DartUtils::MapLibraryUrl(CommandLineOptions* url_mapping, | 47 const char* DartUtils::MapLibraryUrl(CommandLineOptions* url_mapping, |
57 const char* url_string) { | 48 const char* url_string) { |
58 ASSERT(url_mapping != NULL); | 49 ASSERT(url_mapping != NULL); |
59 // We need to check if the passed in url is found in the url_mapping array, | 50 // We need to check if the passed in url is found in the url_mapping array, |
60 // in that case use the mapped entry. | 51 // in that case use the mapped entry. |
61 int len = strlen(url_string); | 52 int len = strlen(url_string); |
62 for (int idx = 0; idx < url_mapping->count(); idx++) { | 53 for (int idx = 0; idx < url_mapping->count(); idx++) { |
63 const char* url_name = url_mapping->GetArgument(idx); | 54 const char* url_name = url_mapping->GetArgument(idx); |
64 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { | 55 if (!strncmp(url_string, url_name, len) && (url_name[len] == ',')) { |
65 const char* url_mapped_name = url_name + len + 1; | 56 const char* url_mapped_name = url_name + len + 1; |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 bool bytes_written = file_stream->WriteFully(buffer, num_bytes); | 236 bool bytes_written = file_stream->WriteFully(buffer, num_bytes); |
246 ASSERT(bytes_written); | 237 ASSERT(bytes_written); |
247 } | 238 } |
248 | 239 |
249 | 240 |
250 void DartUtils::CloseFile(void* stream) { | 241 void DartUtils::CloseFile(void* stream) { |
251 delete reinterpret_cast<File*>(stream); | 242 delete reinterpret_cast<File*>(stream); |
252 } | 243 } |
253 | 244 |
254 | 245 |
255 // Writes string into socket. | 246 static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib, |
256 // Return < 0 indicates an error. | 247 const char* method) { |
257 // Return >= 0 number of bytes written. | 248 const int kNumArgs = 1; |
258 static intptr_t SocketWriteString(intptr_t socket, const char* str, | 249 Dart_Handle dart_args[kNumArgs]; |
259 intptr_t len) { | 250 dart_args[0] = arg; |
260 int r; | 251 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args); |
261 intptr_t cursor = 0; | |
262 do { | |
263 r = Socket::Write(socket, &str[cursor], len); | |
264 if (r < 0) { | |
265 return r; | |
266 } | |
267 cursor += r; | |
268 len -= r; | |
269 } while (len > 0); | |
270 ASSERT(len == 0); | |
271 return cursor; | |
272 } | 252 } |
273 | 253 |
274 | 254 |
275 static uint8_t* SocketReadUntilEOF(intptr_t socket, intptr_t* response_len) { | |
276 const intptr_t kInitialBufferSize = 16 * KB; | |
277 intptr_t buffer_size = kInitialBufferSize; | |
278 uint8_t* buffer = reinterpret_cast<uint8_t*>(malloc(buffer_size)); | |
279 ASSERT(buffer != NULL); | |
280 intptr_t buffer_cursor = 0; | |
281 do { | |
282 int bytes_read = Socket::Read(socket, &buffer[buffer_cursor], | |
283 buffer_size - buffer_cursor - 1); | |
284 if (bytes_read < 0) { | |
285 free(buffer); | |
286 return NULL; | |
287 } | |
288 | |
289 buffer_cursor += bytes_read; | |
290 | |
291 if (bytes_read == 0) { | |
292 *response_len = buffer_cursor; | |
293 buffer[buffer_cursor] = '\0'; | |
294 break; | |
295 } | |
296 | |
297 // There is still more data to be read, check that we have room in the | |
298 // buffer for more data. | |
299 if (buffer_cursor == buffer_size - 1) { | |
300 // Buffer is full. Increase buffer size. | |
301 buffer_size *= 2; | |
302 buffer = reinterpret_cast<uint8_t*>(realloc(buffer, buffer_size)); | |
303 ASSERT(buffer != NULL); | |
304 } | |
305 } while (true); | |
306 return buffer; | |
307 } | |
308 | |
309 | |
310 static bool HttpGetRequestOkay(const char* response) { | |
311 static const char* kOkayReply = "HTTP/1.0 200 OK"; | |
312 static const intptr_t kOkayReplyLen = strlen(kOkayReply); | |
313 return (strncmp(response, kOkayReply, kOkayReplyLen) == 0); | |
314 } | |
315 | |
316 | |
317 static const uint8_t* HttpRequestGetPayload(const char* response) { | |
318 const char* split = strstr(response, "\r\n\r\n"); | |
319 if (split != NULL) { | |
320 return reinterpret_cast<const uint8_t*>(split+4); | |
321 } | |
322 return NULL; | |
323 } | |
324 | |
325 | |
326 // TODO(iposva): Allocate from the zone instead of leaking error string | 255 // TODO(iposva): Allocate from the zone instead of leaking error string |
327 // here. On the other hand the binary is about the exit anyway. | 256 // here. On the other hand the binary is about the exit anyway. |
328 #define SET_ERROR_MSG(error_msg, format, ...) \ | 257 #define SET_ERROR_MSG(error_msg, format, ...) \ |
329 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \ | 258 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \ |
330 char *msg = reinterpret_cast<char*>(malloc(len + 1)); \ | 259 char *msg = reinterpret_cast<char*>(malloc(len + 1)); \ |
331 snprintf(msg, len + 1, format, __VA_ARGS__); \ | 260 snprintf(msg, len + 1, format, __VA_ARGS__); \ |
332 *error_msg = msg | 261 *error_msg = msg |
333 | 262 |
334 | 263 |
335 static const uint8_t* HttpGetRequest(const char* host, const char* path, | 264 Dart_Handle MakeHttpRequest(Dart_Handle uri, Dart_Handle builtin_lib, |
336 int port, intptr_t* response_len, | 265 uint8_t** buffer, intptr_t* buffer_len) { |
337 const char** error_msg) { | 266 const intptr_t HttpResponseCodeOK = 200; |
338 OSError* error = NULL; | 267 ASSERT(buffer != NULL); |
339 SocketAddresses* addresses = Socket::LookupAddress(host, -1, &error); | 268 ASSERT(buffer_len != NULL); |
340 if (addresses == NULL || addresses->count() == 0) { | 269 ASSERT(!Dart_HasLivePorts()); |
341 SET_ERROR_MSG(error_msg, "Unable to resolve %s", host); | 270 SingleArgDart_Invoke(uri, builtin_lib, "_makeHttpRequest"); |
342 return NULL; | 271 // Run until all ports to isolate are closed. |
343 } | 272 Dart_Handle result = Dart_RunLoop(); |
344 | |
345 int preferred_address = 0; | |
346 for (int i = 0; i < addresses->count(); i++) { | |
347 SocketAddress* address = addresses->GetAt(i); | |
348 if (address->GetType() == SocketAddress::ADDRESS_LOOPBACK_IP_V4) { | |
349 // Prefer the IP_V4 loop back. | |
350 preferred_address = i; | |
351 break; | |
352 } | |
353 } | |
354 | |
355 RawAddr addr = addresses->GetAt(preferred_address)->addr(); | |
356 intptr_t tcp_client = Socket::Create(addr); | |
357 if (tcp_client < 0) { | |
358 SET_ERROR_MSG(error_msg, "Unable to create socket to %s:%d", host, port); | |
359 return NULL; | |
360 } | |
361 Socket::Connect(tcp_client, addr, port); | |
362 if (tcp_client < 0) { | |
363 SET_ERROR_MSG(error_msg, "Unable to connect to %s:%d", host, port); | |
364 return NULL; | |
365 } | |
366 // Send get request. | |
367 { | |
368 const char* format = | |
369 "GET %s HTTP/1.0\r\nUser-Agent: Dart VM\r\nHost: %s\r\n\r\n"; | |
370 intptr_t len = snprintf(NULL, 0, format, path, host); | |
371 char* get_request = reinterpret_cast<char*>(malloc(len + 1)); | |
372 snprintf(get_request, len + 1, format, path, host); | |
373 intptr_t r = SocketWriteString(tcp_client, get_request, len); | |
374 free(get_request); | |
375 if (r < len) { | |
376 SET_ERROR_MSG(error_msg, "Unable to write to %s:%d - %d", host, port, | |
377 static_cast<int>(r)); | |
378 Socket::Close(tcp_client); | |
379 return NULL; | |
380 } | |
381 ASSERT(r == len); | |
382 } | |
383 // Consume response. | |
384 uint8_t* response = SocketReadUntilEOF(tcp_client, response_len); | |
385 // Close socket. | |
386 Socket::Close(tcp_client); | |
387 if (response == NULL) { | |
388 SET_ERROR_MSG(error_msg, "Unable to read from %s:%d", host, port); | |
389 return NULL; | |
390 } | |
391 if (HttpGetRequestOkay(reinterpret_cast<const char*>(response)) == false) { | |
392 SET_ERROR_MSG(error_msg, "Invalid HTTP response from %s:%d", host, port); | |
393 free(response); | |
394 return NULL; | |
395 } | |
396 return response; | |
397 } | |
398 | |
399 | |
400 static Dart_Handle ParseHttpUri(Dart_Handle uri, const char** host_str, | |
401 int64_t* port_int, const char** path_str) { | |
402 ASSERT(host_str != NULL); | |
403 ASSERT(port_int != NULL); | |
404 ASSERT(path_str != NULL); | |
405 Dart_Handle result; | |
406 Dart_Handle builtin_lib = | |
407 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); | |
408 Dart_Handle path = DartUtils::PathFromUri(uri, builtin_lib); | |
409 if (Dart_IsError(path)) { | |
410 return path; | |
411 } | |
412 Dart_Handle host = DartUtils::HostFromUri(uri, builtin_lib); | |
413 if (Dart_IsError(host)) { | |
414 return host; | |
415 } | |
416 Dart_Handle port = DartUtils::PortFromUri(uri, builtin_lib); | |
417 if (Dart_IsError(port)) { | |
418 return port; | |
419 } | |
420 result = Dart_StringToCString(path, path_str); | |
421 if (Dart_IsError(result)) { | 273 if (Dart_IsError(result)) { |
422 return result; | 274 return result; |
423 } | 275 } |
424 result = Dart_StringToCString(host, host_str); | 276 intptr_t responseCode = |
425 if (Dart_IsError(result)) { | 277 DartUtils::GetIntegerField(builtin_lib, "_httpRequestResponseCode"); |
426 return result; | 278 if (responseCode != HttpResponseCodeOK) { |
| 279 // Return error. |
| 280 Dart_Handle responseStatus = |
| 281 Dart_GetField(builtin_lib, |
| 282 DartUtils::NewString("_httpRequestStatusString")); |
| 283 if (Dart_IsError(responseStatus)) { |
| 284 return responseStatus; |
| 285 } |
| 286 if (Dart_IsNull(responseStatus)) { |
| 287 return Dart_Error("HTTP error."); |
| 288 } |
| 289 return Dart_Error(DartUtils::GetStringValue(responseStatus)); |
427 } | 290 } |
428 if (DartUtils::GetInt64Value(port, port_int) == false) { | 291 Dart_Handle response = |
429 return Dart_Error("Invalid port"); | 292 Dart_GetField(builtin_lib, DartUtils::NewString("_httpRequestResponse")); |
| 293 if (Dart_IsError(response)) { |
| 294 return response; |
| 295 } |
| 296 if (Dart_IsString(response)) { |
| 297 // Received response as string. |
| 298 uint8_t* responseString = NULL; |
| 299 intptr_t responseStringLength; |
| 300 Dart_Handle r = Dart_StringToUTF8(response, &responseString, |
| 301 &responseStringLength); |
| 302 if (Dart_IsError(r)) { |
| 303 *buffer = NULL; |
| 304 *buffer_len = 0; |
| 305 return r; |
| 306 } |
| 307 // Get payload as bytes. |
| 308 *buffer_len = responseStringLength; |
| 309 *buffer = reinterpret_cast<uint8_t*>(malloc(responseStringLength)); |
| 310 memmove(*buffer, responseString, responseStringLength); |
| 311 } else { |
| 312 // Received response as list of bytes. |
| 313 ASSERT(Dart_IsList(response)); |
| 314 // Query list length. |
| 315 result = Dart_ListLength(response, buffer_len); |
| 316 if (Dart_IsError(result)) { |
| 317 *buffer_len = 0; |
| 318 *buffer = NULL; |
| 319 return result; |
| 320 } |
| 321 // Get payload as bytes. |
| 322 *buffer = reinterpret_cast<uint8_t*>(malloc(*buffer_len)); |
| 323 result = Dart_ListGetAsBytes(response, 0, *buffer, *buffer_len); |
| 324 if (Dart_IsError(result)) { |
| 325 free(*buffer); |
| 326 *buffer_len = 0; |
| 327 *buffer = NULL; |
| 328 return result; |
| 329 } |
430 } | 330 } |
431 return result; | 331 return result; |
432 } | 332 } |
433 | 333 |
434 | 334 |
435 Dart_Handle DartUtils::ReadStringFromHttp(const char* filename) { | 335 Dart_Handle DartUtils::ReadStringFromHttp(const char* script_uri) { |
436 Dart_Handle uri = NewString(filename); | 336 Dart_Handle uri = NewString(script_uri); |
437 if (Dart_IsError(uri)) { | 337 if (Dart_IsError(uri)) { |
438 return uri; | 338 return uri; |
439 } | 339 } |
440 const char* host_str = NULL; | 340 Dart_Handle builtin_lib = |
441 int64_t port_int = 0; | 341 Builtin::LoadAndCheckLibrary(Builtin::kBuiltinLibrary); |
442 const char* path_str = NULL; | 342 uint8_t* buffer; |
443 Dart_Handle result = ParseHttpUri(uri, &host_str, &port_int, | 343 intptr_t bufferLen; |
444 &path_str); | 344 Dart_Handle result = MakeHttpRequest(uri, builtin_lib, &buffer, &bufferLen); |
445 if (Dart_IsError(result)) { | 345 if (Dart_IsError(result)) { |
446 return result; | 346 return result; |
447 } | 347 } |
448 const char* error_msg = NULL; | 348 Dart_Handle str = Dart_NewStringFromUTF8(buffer, |
449 intptr_t len; | 349 bufferLen); |
450 const uint8_t* text_buffer = HttpGetRequest(host_str, path_str, port_int, | 350 free(buffer); |
451 &len, &error_msg); | |
452 if (text_buffer == NULL) { | |
453 return Dart_Error(error_msg); | |
454 } | |
455 const uint8_t* payload = HttpRequestGetPayload( | |
456 reinterpret_cast<const char*>(text_buffer)); | |
457 if (payload == NULL) { | |
458 return Dart_Error("Invalid HTTP response."); | |
459 } | |
460 // Subtract HTTP response from length. | |
461 len -= (payload-text_buffer); | |
462 ASSERT(len >= 0); | |
463 Dart_Handle str = Dart_NewStringFromUTF8(payload, len); | |
464 return str; | 351 return str; |
465 } | 352 } |
466 | 353 |
467 | 354 |
468 static const uint8_t* ReadFileFully(const char* filename, | 355 static const uint8_t* ReadFileFully(const char* filename, |
469 intptr_t* file_len, | 356 intptr_t* file_len, |
470 const char** error_msg) { | 357 const char** error_msg) { |
471 void* stream = DartUtils::OpenFile(filename, false); | 358 void* stream = DartUtils::OpenFile(filename, false); |
472 if (stream == NULL) { | 359 if (stream == NULL) { |
473 SET_ERROR_MSG(error_msg, "Unable to open file: %s", filename); | 360 SET_ERROR_MSG(error_msg, "Unable to open file: %s", filename); |
(...skipping 16 matching lines...) Expand all Loading... |
490 intptr_t len; | 377 intptr_t len; |
491 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); | 378 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); |
492 if (text_buffer == NULL) { | 379 if (text_buffer == NULL) { |
493 return Dart_Error(error_msg); | 380 return Dart_Error(error_msg); |
494 } | 381 } |
495 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); | 382 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); |
496 return str; | 383 return str; |
497 } | 384 } |
498 | 385 |
499 | 386 |
500 Dart_Handle DartUtils::SetWorkingDirectory(Dart_Handle builtin_lib) { | |
501 Dart_Handle directory = NewString(original_working_directory); | |
502 return SingleArgDart_Invoke(directory, builtin_lib, "_setWorkingDirectory"); | |
503 } | |
504 | |
505 | |
506 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, | 387 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, |
507 Dart_Handle builtin_lib) { | 388 Dart_Handle builtin_lib) { |
508 return SingleArgDart_Invoke(script_uri, builtin_lib, "_resolveScriptUri"); | 389 const int kNumArgs = 3; |
| 390 Dart_Handle dart_args[kNumArgs]; |
| 391 dart_args[0] = NewString(original_working_directory); |
| 392 dart_args[1] = script_uri; |
| 393 dart_args[2] = (IsWindowsHost() ? Dart_True() : Dart_False()); |
| 394 return Dart_Invoke(builtin_lib, |
| 395 NewString("_resolveScriptUri"), |
| 396 kNumArgs, |
| 397 dart_args); |
509 } | 398 } |
510 | 399 |
511 | 400 |
512 Dart_Handle DartUtils::FilePathFromUri(Dart_Handle script_uri, | 401 Dart_Handle DartUtils::FilePathFromUri(Dart_Handle script_uri, |
513 Dart_Handle builtin_lib) { | 402 Dart_Handle builtin_lib) { |
514 return SingleArgDart_Invoke(script_uri, builtin_lib, "_filePathFromUri"); | 403 const int kNumArgs = 2; |
| 404 Dart_Handle dart_args[kNumArgs]; |
| 405 dart_args[0] = script_uri; |
| 406 dart_args[1] = (IsWindowsHost() ? Dart_True() : Dart_False()); |
| 407 return Dart_Invoke(builtin_lib, |
| 408 NewString("_filePathFromUri"), |
| 409 kNumArgs, |
| 410 dart_args); |
515 } | 411 } |
516 | 412 |
517 | 413 |
518 Dart_Handle DartUtils::PathFromUri(Dart_Handle script_uri, | |
519 Dart_Handle builtin_lib) { | |
520 return SingleArgDart_Invoke(script_uri, builtin_lib, "_pathFromHttpUri"); | |
521 } | |
522 | |
523 | |
524 Dart_Handle DartUtils::HostFromUri(Dart_Handle script_uri, | |
525 Dart_Handle builtin_lib) { | |
526 return SingleArgDart_Invoke(script_uri, builtin_lib, "_hostFromHttpUri"); | |
527 } | |
528 | |
529 | |
530 Dart_Handle DartUtils::PortFromUri(Dart_Handle script_uri, | |
531 Dart_Handle builtin_lib) { | |
532 return SingleArgDart_Invoke(script_uri, builtin_lib, "_portFromHttpUri"); | |
533 } | |
534 | |
535 | |
536 Dart_Handle DartUtils::ResolveUri(Dart_Handle library_url, | 414 Dart_Handle DartUtils::ResolveUri(Dart_Handle library_url, |
537 Dart_Handle url, | 415 Dart_Handle url, |
538 Dart_Handle builtin_lib) { | 416 Dart_Handle builtin_lib) { |
539 const int kNumArgs = 2; | 417 const int kNumArgs = 2; |
540 Dart_Handle dart_args[kNumArgs]; | 418 Dart_Handle dart_args[kNumArgs]; |
541 dart_args[0] = library_url; | 419 dart_args[0] = library_url; |
542 dart_args[1] = url; | 420 dart_args[1] = url; |
543 return Dart_Invoke( | 421 return Dart_Invoke( |
544 builtin_lib, NewString("_resolveUri"), kNumArgs, dart_args); | 422 builtin_lib, NewString("_resolveUri"), kNumArgs, dart_args); |
545 } | 423 } |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 | 533 |
656 void DartUtils::WriteMagicNumber(File* file) { | 534 void DartUtils::WriteMagicNumber(File* file) { |
657 // Write a magic number and version information into the snapshot file. | 535 // Write a magic number and version information into the snapshot file. |
658 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); | 536 bool bytes_written = file->WriteFully(magic_number, sizeof(magic_number)); |
659 ASSERT(bytes_written); | 537 ASSERT(bytes_written); |
660 } | 538 } |
661 | 539 |
662 | 540 |
663 Dart_Handle DartUtils::LoadScriptHttp(Dart_Handle uri, | 541 Dart_Handle DartUtils::LoadScriptHttp(Dart_Handle uri, |
664 Dart_Handle builtin_lib) { | 542 Dart_Handle builtin_lib) { |
665 const char* host_str = NULL; | 543 intptr_t len = 0; |
666 int64_t port_int = 0; | 544 uint8_t* buffer = NULL; |
667 const char* path_str = NULL; | 545 Dart_Handle result = MakeHttpRequest(uri, builtin_lib, &buffer, &len); |
668 Dart_Handle result = ParseHttpUri(uri, &host_str, &port_int, | |
669 &path_str); | |
670 if (Dart_IsError(result)) { | 546 if (Dart_IsError(result)) { |
671 return result; | 547 return result; |
672 } | 548 } |
673 const char* error_msg = NULL; | 549 const uint8_t* payload = buffer; |
674 intptr_t len; | |
675 const uint8_t* text_buffer; | |
676 text_buffer = HttpGetRequest(host_str, path_str, port_int, &len, | |
677 &error_msg); | |
678 if (text_buffer == NULL) { | |
679 return Dart_Error(error_msg); | |
680 } | |
681 const uint8_t* payload = HttpRequestGetPayload( | |
682 reinterpret_cast<const char*>(text_buffer)); | |
683 if (payload == NULL) { | |
684 return Dart_Error("Invalid HTTP response."); | |
685 } | |
686 // Subtract HTTP response from length. | |
687 len -= (payload-text_buffer); | |
688 ASSERT(len >= 0); | |
689 // At this point we have received a valid HTTP 200 reply and | |
690 // payload points at the beginning of the script or snapshot. | |
691 bool is_snapshot = false; | 550 bool is_snapshot = false; |
692 payload = SniffForMagicNumber(payload, &len, &is_snapshot); | 551 payload = SniffForMagicNumber(payload, &len, &is_snapshot); |
693 if (is_snapshot) { | 552 if (is_snapshot) { |
694 return Dart_LoadScriptFromSnapshot(payload, len); | 553 return Dart_LoadScriptFromSnapshot(payload, len); |
695 } else { | 554 } else { |
696 Dart_Handle source = Dart_NewStringFromUTF8(payload, len); | 555 Dart_Handle source = Dart_NewStringFromUTF8(payload, len); |
| 556 free(buffer); |
697 if (Dart_IsError(source)) { | 557 if (Dart_IsError(source)) { |
698 return source; | 558 return source; |
699 } | 559 } |
700 return Dart_LoadScript(uri, source, 0, 0); | 560 return Dart_LoadScript(uri, source, 0, 0); |
701 } | 561 } |
702 } | 562 } |
703 | 563 |
704 | 564 |
705 Dart_Handle DartUtils::LoadScript(const char* script_uri, | 565 Dart_Handle DartUtils::LoadScript(const char* script_uri, |
706 Dart_Handle builtin_lib) { | 566 Dart_Handle builtin_lib) { |
| 567 // Always call ResolveScriptUri because as a side effect it sets |
| 568 // the script entry path which is used when automatically resolving |
| 569 // package root. |
707 Dart_Handle resolved_script_uri = | 570 Dart_Handle resolved_script_uri = |
708 ResolveScriptUri(NewString(script_uri), builtin_lib); | 571 ResolveScriptUri(NewString(script_uri), builtin_lib); |
709 if (Dart_IsError(resolved_script_uri)) { | 572 if (Dart_IsError(resolved_script_uri)) { |
710 return resolved_script_uri; | 573 return resolved_script_uri; |
711 } | 574 } |
712 // Handle http: requests separately. | 575 // Handle http: requests separately. |
713 if (DartUtils::IsHttpSchemeURL(script_uri)) { | 576 if (DartUtils::IsHttpSchemeURL(script_uri)) { |
714 return LoadScriptHttp(resolved_script_uri, builtin_lib); | 577 return LoadScriptHttp(resolved_script_uri, builtin_lib); |
715 } | 578 } |
716 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, | 579 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 Dart_Handle async_lib = Dart_LookupLibrary(url); | 659 Dart_Handle async_lib = Dart_LookupLibrary(url); |
797 DART_CHECK_VALID(async_lib); | 660 DART_CHECK_VALID(async_lib); |
798 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); | 661 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); |
799 Dart_Handle timer_closure = | 662 Dart_Handle timer_closure = |
800 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL); | 663 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL); |
801 Dart_Handle args[1]; | 664 Dart_Handle args[1]; |
802 args[0] = timer_closure; | 665 args[0] = timer_closure; |
803 DART_CHECK_VALID(Dart_Invoke( | 666 DART_CHECK_VALID(Dart_Invoke( |
804 async_lib, NewString("_setTimerFactoryClosure"), 1, args)); | 667 async_lib, NewString("_setTimerFactoryClosure"), 1, args)); |
805 | 668 |
806 | |
807 if (IsWindowsHost()) { | |
808 result = Dart_Invoke(builtin_lib, NewString("_setWindows"), 0, 0); | |
809 if (Dart_IsError(result)) { | |
810 return result; | |
811 } | |
812 } | |
813 | |
814 if (Dart_IsError(result)) { | |
815 return result; | |
816 } | |
817 // Set current working directory. | |
818 result = SetWorkingDirectory(builtin_lib); | |
819 if (Dart_IsError(result)) { | |
820 return result; | |
821 } | |
822 | |
823 // Set up package root if specified. | 669 // Set up package root if specified. |
824 if (package_root != NULL) { | 670 if (package_root != NULL) { |
825 result = NewString(package_root); | 671 result = NewString(package_root); |
826 if (!Dart_IsError(result)) { | 672 if (!Dart_IsError(result)) { |
827 const int kNumArgs = 1; | 673 const int kNumArgs = 1; |
828 Dart_Handle dart_args[kNumArgs]; | 674 Dart_Handle dart_args[kNumArgs]; |
829 dart_args[0] = result; | 675 dart_args[0] = result; |
830 return Dart_Invoke(builtin_lib, | 676 return Dart_Invoke(builtin_lib, |
831 NewString("_setPackageRoot"), | 677 NewString("_setPackageRoot"), |
832 kNumArgs, | 678 kNumArgs, |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1112 new CObjectString(CObject::NewString(os_error->message())); | 958 new CObjectString(CObject::NewString(os_error->message())); |
1113 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 959 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
1114 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 960 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
1115 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 961 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
1116 result->SetAt(2, error_message); | 962 result->SetAt(2, error_message); |
1117 return result; | 963 return result; |
1118 } | 964 } |
1119 | 965 |
1120 } // namespace bin | 966 } // namespace bin |
1121 } // namespace dart | 967 } // namespace dart |
OLD | NEW |