Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: runtime/bin/dartutils.cc

Issue 17183008: HTTP loading cleanups (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« runtime/bin/builtin.dart ('K') | « runtime/bin/dartutils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 bool bytes_written = file_stream->WriteFully(buffer, num_bytes); 236 bool bytes_written = file_stream->WriteFully(buffer, num_bytes);
237 ASSERT(bytes_written); 237 ASSERT(bytes_written);
238 } 238 }
239 239
240 240
241 void DartUtils::CloseFile(void* stream) { 241 void DartUtils::CloseFile(void* stream) {
242 delete reinterpret_cast<File*>(stream); 242 delete reinterpret_cast<File*>(stream);
243 } 243 }
244 244
245 245
246 static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib, 246 static Dart_Handle SingleArgDart_Invoke(Dart_Handle arg, Dart_Handle lib,
Ivan Posva 2013/06/18 22:24:00 Please fix the argument order. Maybe in a differen
Cutch 2013/06/18 22:28:22 Done.
247 const char* method) { 247 const char* method) {
248 const int kNumArgs = 1; 248 const int kNumArgs = 1;
249 Dart_Handle dart_args[kNumArgs]; 249 Dart_Handle dart_args[kNumArgs];
250 dart_args[0] = arg; 250 dart_args[0] = arg;
251 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args); 251 return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args);
252 } 252 }
253 253
254 254
255 // TODO(iposva): Allocate from the zone instead of leaking error string 255 // TODO(iposva): Allocate from the zone instead of leaking error string
256 // 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.
257 #define SET_ERROR_MSG(error_msg, format, ...) \ 257 #define SET_ERROR_MSG(error_msg, format, ...) \
258 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \ 258 intptr_t len = snprintf(NULL, 0, format, __VA_ARGS__); \
259 char *msg = reinterpret_cast<char*>(malloc(len + 1)); \ 259 char *msg = reinterpret_cast<char*>(malloc(len + 1)); \
260 snprintf(msg, len + 1, format, __VA_ARGS__); \ 260 snprintf(msg, len + 1, format, __VA_ARGS__); \
261 *error_msg = msg 261 *error_msg = msg
262 262
263 263
264 Dart_Handle MakeHttpRequest(Dart_Handle uri, Dart_Handle builtin_lib, 264 Dart_Handle MakeHttpRequest(Dart_Handle uri, Dart_Handle builtin_lib,
265 uint8_t** buffer, intptr_t* buffer_len) { 265 uint8_t** buffer, intptr_t* buffer_len) {
266 const intptr_t HttpResponseCodeOK = 200; 266 const intptr_t HttpResponseCodeOK = 200;
267 ASSERT(buffer != NULL); 267 ASSERT(buffer != NULL);
268 ASSERT(buffer_len != NULL); 268 ASSERT(buffer_len != NULL);
269 ASSERT(!Dart_HasLivePorts()); 269 ASSERT(!Dart_HasLivePorts());
270 SingleArgDart_Invoke(uri, builtin_lib, "_makeHttpRequest"); 270 SingleArgDart_Invoke(uri, builtin_lib, "_makeHttpRequest");
271 // Run until all ports to isolate are closed. 271 // Run until all ports to isolate are closed.
272 Dart_Handle result = Dart_RunLoop(); 272 Dart_Handle result = Dart_RunLoop();
273 if (Dart_IsError(result)) { 273 if (Dart_IsError(result)) {
274 return result; 274 return result;
275 } 275 }
276 intptr_t responseCode = 276 result = Dart_Invoke(builtin_lib,
277 DartUtils::GetIntegerField(builtin_lib, "_httpRequestResponseCode"); 277 DartUtils::NewString("_getHttpRequestResponseCode"),
278 0,
279 0);
Ivan Posva 2013/06/18 22:24:00 NULL it is typed as a pointer.
Cutch 2013/06/18 22:28:22 Done.
280 if (Dart_IsError(result)) {
281 return result;
282 }
283 intptr_t responseCode = DartUtils::GetIntegerValue(result);
278 if (responseCode != HttpResponseCodeOK) { 284 if (responseCode != HttpResponseCodeOK) {
279 // Return error. 285 // Return error.
280 Dart_Handle responseStatus = 286 Dart_Handle responseStatus =
281 Dart_GetField(builtin_lib, 287 Dart_Invoke(builtin_lib,
282 DartUtils::NewString("_httpRequestStatusString")); 288 DartUtils::NewString("_getHttpRequestStatusString"),
289 0,
290 0);
Ivan Posva 2013/06/18 22:24:00 ditto.
Cutch 2013/06/18 22:28:22 Done.
283 if (Dart_IsError(responseStatus)) { 291 if (Dart_IsError(responseStatus)) {
284 return responseStatus; 292 return responseStatus;
285 } 293 }
286 if (Dart_IsNull(responseStatus)) { 294 if (Dart_IsNull(responseStatus)) {
287 return Dart_Error("HTTP error."); 295 return Dart_Error("HTTP error.");
288 } 296 }
289 return Dart_Error(DartUtils::GetStringValue(responseStatus)); 297 return Dart_Error(DartUtils::GetStringValue(responseStatus));
290 } 298 }
291 Dart_Handle response = 299 Dart_Handle response =
292 Dart_GetField(builtin_lib, DartUtils::NewString("_httpRequestResponse")); 300 Dart_Invoke(builtin_lib, DartUtils::NewString("_getHttpRequestResponse"),
301 0, 0);
Ivan Posva 2013/06/18 22:24:00 ditto.
Cutch 2013/06/18 22:28:22 Done.
293 if (Dart_IsError(response)) { 302 if (Dart_IsError(response)) {
294 return response; 303 return response;
295 } 304 }
296 if (Dart_IsString(response)) { 305 if (Dart_IsString(response)) {
297 // Received response as string. 306 // Received response as string.
298 uint8_t* responseString = NULL; 307 uint8_t* responseString = NULL;
299 intptr_t responseStringLength; 308 intptr_t responseStringLength;
300 Dart_Handle r = Dart_StringToUTF8(response, &responseString, 309 Dart_Handle r = Dart_StringToUTF8(response, &responseString,
301 &responseStringLength); 310 &responseStringLength);
302 if (Dart_IsError(r)) { 311 if (Dart_IsError(r)) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 intptr_t len; 386 intptr_t len;
378 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); 387 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg);
379 if (text_buffer == NULL) { 388 if (text_buffer == NULL) {
380 return Dart_Error(error_msg); 389 return Dart_Error(error_msg);
381 } 390 }
382 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); 391 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len);
383 return str; 392 return str;
384 } 393 }
385 394
386 395
396 Dart_Handle DartUtils::SetWorkingDirectory(Dart_Handle builtin_lib) {
397 Dart_Handle directory = NewString(original_working_directory);
398 return SingleArgDart_Invoke(directory, builtin_lib, "_setWorkingDirectory");
399 }
400
401
387 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, 402 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri,
388 Dart_Handle builtin_lib) { 403 Dart_Handle builtin_lib) {
389 const int kNumArgs = 3; 404 const int kNumArgs = 1;
390 Dart_Handle dart_args[kNumArgs]; 405 Dart_Handle dart_args[kNumArgs];
391 dart_args[0] = NewString(original_working_directory); 406 dart_args[0] = script_uri;
392 dart_args[1] = script_uri;
393 dart_args[2] = (IsWindowsHost() ? Dart_True() : Dart_False());
394 return Dart_Invoke(builtin_lib, 407 return Dart_Invoke(builtin_lib,
395 NewString("_resolveScriptUri"), 408 NewString("_resolveScriptUri"),
396 kNumArgs, 409 kNumArgs,
397 dart_args); 410 dart_args);
398 } 411 }
399 412
400 413
401 Dart_Handle DartUtils::FilePathFromUri(Dart_Handle script_uri, 414 Dart_Handle DartUtils::FilePathFromUri(Dart_Handle script_uri,
402 Dart_Handle builtin_lib) { 415 Dart_Handle builtin_lib) {
403 const int kNumArgs = 2; 416 const int kNumArgs = 1;
404 Dart_Handle dart_args[kNumArgs]; 417 Dart_Handle dart_args[kNumArgs];
405 dart_args[0] = script_uri; 418 dart_args[0] = script_uri;
406 dart_args[1] = (IsWindowsHost() ? Dart_True() : Dart_False());
407 return Dart_Invoke(builtin_lib, 419 return Dart_Invoke(builtin_lib,
408 NewString("_filePathFromUri"), 420 NewString("_filePathFromUri"),
409 kNumArgs, 421 kNumArgs,
410 dart_args); 422 dart_args);
411 } 423 }
412 424
413 425
414 Dart_Handle DartUtils::ResolveUri(Dart_Handle library_url, 426 Dart_Handle DartUtils::ResolveUri(Dart_Handle library_url,
415 Dart_Handle url, 427 Dart_Handle url,
416 Dart_Handle builtin_lib) { 428 Dart_Handle builtin_lib) {
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 if (Dart_IsError(source)) { 569 if (Dart_IsError(source)) {
558 return source; 570 return source;
559 } 571 }
560 return Dart_LoadScript(uri, source, 0, 0); 572 return Dart_LoadScript(uri, source, 0, 0);
561 } 573 }
562 } 574 }
563 575
564 576
565 Dart_Handle DartUtils::LoadScript(const char* script_uri, 577 Dart_Handle DartUtils::LoadScript(const char* script_uri,
566 Dart_Handle builtin_lib) { 578 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.
570 Dart_Handle resolved_script_uri = 579 Dart_Handle resolved_script_uri =
571 ResolveScriptUri(NewString(script_uri), builtin_lib); 580 ResolveScriptUri(NewString(script_uri), builtin_lib);
572 if (Dart_IsError(resolved_script_uri)) { 581 if (Dart_IsError(resolved_script_uri)) {
573 return resolved_script_uri; 582 return resolved_script_uri;
574 } 583 }
575 // Handle http: requests separately. 584 // Handle http: requests separately.
576 if (DartUtils::IsHttpSchemeURL(script_uri)) { 585 if (DartUtils::IsHttpSchemeURL(script_uri)) {
577 return LoadScriptHttp(resolved_script_uri, builtin_lib); 586 return LoadScriptHttp(resolved_script_uri, builtin_lib);
578 } 587 }
579 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, 588 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri,
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 return Dart_LoadSource(library, url, source); 649 return Dart_LoadSource(library, url, source);
641 } 650 }
642 return Dart_Error("wrong tag"); 651 return Dart_Error("wrong tag");
643 } 652 }
644 653
645 654
646 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root, 655 Dart_Handle DartUtils::PrepareForScriptLoading(const char* package_root,
647 Dart_Handle builtin_lib) { 656 Dart_Handle builtin_lib) {
648 // Setup the corelib 'print' function. 657 // Setup the corelib 'print' function.
649 Dart_Handle print = Dart_Invoke( 658 Dart_Handle print = Dart_Invoke(
650 builtin_lib, NewString("_getPrintClosure"), 0, 0); 659 builtin_lib, NewString("_getPrintClosure"), 0, 0);
Ivan Posva 2013/06/18 22:24:00 ditto.
Cutch 2013/06/18 22:28:22 Done.
651 Dart_Handle corelib = Dart_LookupLibrary(NewString("dart:core")); 660 Dart_Handle corelib = Dart_LookupLibrary(NewString("dart:core"));
652 Dart_Handle result = Dart_SetField(corelib, 661 Dart_Handle result = Dart_SetField(corelib,
653 NewString("_printClosure"), 662 NewString("_printClosure"),
654 print); 663 print);
655 664
656 // Setup the 'timer' factory. 665 // Setup the 'timer' factory.
657 Dart_Handle url = NewString(kAsyncLibURL); 666 Dart_Handle url = NewString(kAsyncLibURL);
658 DART_CHECK_VALID(url); 667 DART_CHECK_VALID(url);
659 Dart_Handle async_lib = Dart_LookupLibrary(url); 668 Dart_Handle async_lib = Dart_LookupLibrary(url);
660 DART_CHECK_VALID(async_lib); 669 DART_CHECK_VALID(async_lib);
661 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary); 670 Dart_Handle io_lib = Builtin::LoadAndCheckLibrary(Builtin::kIOLibrary);
662 Dart_Handle timer_closure = 671 Dart_Handle timer_closure =
663 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL); 672 Dart_Invoke(io_lib, NewString("_getTimerFactoryClosure"), 0, NULL);
664 Dart_Handle args[1]; 673 Dart_Handle args[1];
665 args[0] = timer_closure; 674 args[0] = timer_closure;
666 DART_CHECK_VALID(Dart_Invoke( 675 DART_CHECK_VALID(Dart_Invoke(
667 async_lib, NewString("_setTimerFactoryClosure"), 1, args)); 676 async_lib, NewString("_setTimerFactoryClosure"), 1, args));
668 677
678
679 if (IsWindowsHost()) {
680 // Set running on Windows flag.
681 result = Dart_Invoke(builtin_lib, NewString("_setWindows"), 0, 0);
Ivan Posva 2013/06/18 22:24:00 ditto.
Cutch 2013/06/18 22:28:22 Done.
682 if (Dart_IsError(result)) {
683 return result;
684 }
685 }
686
687 // Set current working directory.
688 result = SetWorkingDirectory(builtin_lib);
689 if (Dart_IsError(result)) {
690 return result;
691 }
692
669 // Set up package root if specified. 693 // Set up package root if specified.
670 if (package_root != NULL) { 694 if (package_root != NULL) {
671 result = NewString(package_root); 695 result = NewString(package_root);
672 if (!Dart_IsError(result)) { 696 if (!Dart_IsError(result)) {
673 const int kNumArgs = 1; 697 const int kNumArgs = 1;
674 Dart_Handle dart_args[kNumArgs]; 698 Dart_Handle dart_args[kNumArgs];
675 dart_args[0] = result; 699 dart_args[0] = result;
676 return Dart_Invoke(builtin_lib, 700 return Dart_Invoke(builtin_lib,
677 NewString("_setPackageRoot"), 701 NewString("_setPackageRoot"),
678 kNumArgs, 702 kNumArgs,
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
958 new CObjectString(CObject::NewString(os_error->message())); 982 new CObjectString(CObject::NewString(os_error->message()));
959 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); 983 CObjectArray* result = new CObjectArray(CObject::NewArray(3));
960 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); 984 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError)));
961 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); 985 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code())));
962 result->SetAt(2, error_message); 986 result->SetAt(2, error_message);
963 return result; 987 return result;
964 } 988 }
965 989
966 } // namespace bin 990 } // namespace bin
967 } // namespace dart 991 } // namespace dart
OLDNEW
« runtime/bin/builtin.dart ('K') | « runtime/bin/dartutils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698