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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 static Dart_Handle SingleArgDart_Invoke(Dart_Handle lib, const char* method, | 246 static Dart_Handle SingleArgDart_Invoke(Dart_Handle lib, const char* method, |
247 Dart_Handle arg) { | 247 Dart_Handle arg) { |
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 to 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()); |
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
778 Dart_Handle DartUtils::NewDartOSError(OSError* os_error) { | 778 Dart_Handle DartUtils::NewDartOSError(OSError* os_error) { |
779 // Create a dart:io OSError object with the information retrieved from the OS. | 779 // Create a dart:io OSError object with the information retrieved from the OS. |
780 Dart_Handle type = GetDartType(kIOLibURL, "OSError"); | 780 Dart_Handle type = GetDartType(kIOLibURL, "OSError"); |
781 Dart_Handle args[2]; | 781 Dart_Handle args[2]; |
782 args[0] = NewString(os_error->message()); | 782 args[0] = NewString(os_error->message()); |
783 args[1] = Dart_NewInteger(os_error->code()); | 783 args[1] = Dart_NewInteger(os_error->code()); |
784 return Dart_New(type, Dart_Null(), 2, args); | 784 return Dart_New(type, Dart_Null(), 2, args); |
785 } | 785 } |
786 | 786 |
787 | 787 |
788 Dart_Handle DartUtils::NewDartSocketException(const char* message, | 788 Dart_Handle DartUtils::NewDartExceptionWithOSError(const char* library_url, |
789 Dart_Handle os_error) { | 789 const char* exception_name, |
790 // Create a dart:io SocketException object. | 790 const char* message, |
791 Dart_Handle type = GetDartType(kIOLibURL, "SocketException"); | 791 Dart_Handle os_error) { |
| 792 // Create a Dart Exception object with a message and an OSError. |
| 793 Dart_Handle type = GetDartType(library_url, exception_name); |
792 Dart_Handle args[2]; | 794 Dart_Handle args[2]; |
793 args[0] = NewString(message); | 795 args[0] = NewString(message); |
794 args[1] = os_error; | 796 args[1] = os_error; |
795 return Dart_New(type, Dart_Null(), 2, args); | 797 return Dart_New(type, Dart_Null(), 2, args); |
796 } | 798 } |
797 | 799 |
798 | 800 |
799 Dart_Handle DartUtils::NewDartExceptionWithMessage(const char* library_url, | 801 Dart_Handle DartUtils::NewDartExceptionWithMessage(const char* library_url, |
800 const char* exception_name, | 802 const char* exception_name, |
801 const char* message) { | 803 const char* message) { |
802 // Create a Dart Exception object with a message. | 804 // Create a Dart Exception object with a message. |
803 Dart_Handle type = GetDartType(library_url, exception_name); | 805 Dart_Handle type = GetDartType(library_url, exception_name); |
804 if (message != NULL) { | 806 if (message != NULL) { |
805 Dart_Handle args[1]; | 807 Dart_Handle args[1]; |
806 args[0] = NewString(message); | 808 args[0] = NewString(message); |
807 return Dart_New(type, Dart_Null(), 1, args); | 809 return Dart_New(type, Dart_Null(), 1, args); |
808 } else { | 810 } else { |
809 return Dart_New(type, Dart_Null(), 0, NULL); | 811 return Dart_New(type, Dart_Null(), 0, NULL); |
810 } | 812 } |
811 } | 813 } |
812 | 814 |
813 | 815 |
814 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { | 816 Dart_Handle DartUtils::NewDartArgumentError(const char* message) { |
815 return NewDartExceptionWithMessage(kCoreLibURL, | 817 return NewDartExceptionWithMessage(kCoreLibURL, |
816 "ArgumentError", | 818 "ArgumentError", |
817 message); | 819 message); |
818 } | 820 } |
819 | 821 |
820 | 822 |
| 823 Dart_Handle DartUtils::NewDartIOException(const char* exception_name, |
| 824 const char* message, |
| 825 Dart_Handle os_error) { |
| 826 // Create a dart:io exception object of the given type. |
| 827 return NewDartExceptionWithOSError(kIOLibURL, |
| 828 exception_name, |
| 829 message, |
| 830 os_error); |
| 831 } |
| 832 |
| 833 |
821 Dart_Handle DartUtils::NewInternalError(const char* message) { | 834 Dart_Handle DartUtils::NewInternalError(const char* message) { |
822 return NewDartExceptionWithMessage(kCoreLibURL, "InternalError", message); | 835 return NewDartExceptionWithMessage(kCoreLibURL, "InternalError", message); |
823 } | 836 } |
824 | 837 |
825 | 838 |
826 void DartUtils::SetOriginalWorkingDirectory() { | 839 void DartUtils::SetOriginalWorkingDirectory() { |
827 original_working_directory = Directory::Current(); | 840 original_working_directory = Directory::Current(); |
828 } | 841 } |
829 | 842 |
830 | 843 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
982 new CObjectString(CObject::NewString(os_error->message())); | 995 new CObjectString(CObject::NewString(os_error->message())); |
983 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 996 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
984 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 997 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
985 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 998 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
986 result->SetAt(2, error_message); | 999 result->SetAt(2, error_message); |
987 return result; | 1000 return result; |
988 } | 1001 } |
989 | 1002 |
990 } // namespace bin | 1003 } // namespace bin |
991 } // namespace dart | 1004 } // namespace dart |
OLD | NEW |