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 "include/dart_api.h" | 5 #include "include/dart_api.h" |
6 | 6 |
7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
10 #include "vm/dart.h" | 10 #include "vm/dart.h" |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 193 |
194 Dart_Handle Api::NewError(const char* format, ...) { | 194 Dart_Handle Api::NewError(const char* format, ...) { |
195 Isolate* isolate = Isolate::Current(); | 195 Isolate* isolate = Isolate::Current(); |
196 DARTSCOPE_NOCHECKS(isolate); | 196 DARTSCOPE_NOCHECKS(isolate); |
197 | 197 |
198 va_list args; | 198 va_list args; |
199 va_start(args, format); | 199 va_start(args, format); |
200 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 200 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
201 va_end(args); | 201 va_end(args); |
202 | 202 |
203 char* buffer = zone.Alloc<char>(len + 1); | 203 char* buffer = isolate->current_zone()->Alloc<char>(len + 1); |
204 va_list args2; | 204 va_list args2; |
205 va_start(args2, format); | 205 va_start(args2, format); |
206 OS::VSNPrint(buffer, (len + 1), format, args2); | 206 OS::VSNPrint(buffer, (len + 1), format, args2); |
207 va_end(args2); | 207 va_end(args2); |
208 | 208 |
209 const String& message = String::Handle(isolate, String::New(buffer)); | 209 const String& message = String::Handle(isolate, String::New(buffer)); |
210 return Api::NewHandle(isolate, ApiError::New(message)); | 210 return Api::NewHandle(isolate, ApiError::New(message)); |
211 } | 211 } |
212 | 212 |
213 | 213 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 // TODO(turnidge): Remove all uses and delete. | 403 // TODO(turnidge): Remove all uses and delete. |
404 DART_EXPORT Dart_Handle Dart_Error(const char* format, ...) { | 404 DART_EXPORT Dart_Handle Dart_Error(const char* format, ...) { |
405 Isolate* isolate = Isolate::Current(); | 405 Isolate* isolate = Isolate::Current(); |
406 DARTSCOPE(isolate); | 406 DARTSCOPE(isolate); |
407 | 407 |
408 va_list args; | 408 va_list args; |
409 va_start(args, format); | 409 va_start(args, format); |
410 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 410 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
411 va_end(args); | 411 va_end(args); |
412 | 412 |
413 char* buffer = zone.Alloc<char>(len + 1); | 413 char* buffer = isolate->current_zone()->Alloc<char>(len + 1); |
414 va_list args2; | 414 va_list args2; |
415 va_start(args2, format); | 415 va_start(args2, format); |
416 OS::VSNPrint(buffer, (len + 1), format, args2); | 416 OS::VSNPrint(buffer, (len + 1), format, args2); |
417 va_end(args2); | 417 va_end(args2); |
418 | 418 |
419 const String& message = String::Handle(isolate, String::New(buffer)); | 419 const String& message = String::Handle(isolate, String::New(buffer)); |
420 return Api::NewHandle(isolate, ApiError::New(message)); | 420 return Api::NewHandle(isolate, ApiError::New(message)); |
421 } | 421 } |
422 | 422 |
423 | 423 |
424 // TODO(turnidge): This clones Api::NewError. I need to use va_copy to | 424 // TODO(turnidge): This clones Api::NewError. I need to use va_copy to |
425 // fix this but not sure if it available on all of our builds. | 425 // fix this but not sure if it available on all of our builds. |
426 DART_EXPORT Dart_Handle Dart_NewApiError(const char* format, ...) { | 426 DART_EXPORT Dart_Handle Dart_NewApiError(const char* format, ...) { |
427 Isolate* isolate = Isolate::Current(); | 427 Isolate* isolate = Isolate::Current(); |
428 DARTSCOPE(isolate); | 428 DARTSCOPE(isolate); |
429 | 429 |
430 va_list args; | 430 va_list args; |
431 va_start(args, format); | 431 va_start(args, format); |
432 intptr_t len = OS::VSNPrint(NULL, 0, format, args); | 432 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
433 va_end(args); | 433 va_end(args); |
434 | 434 |
435 char* buffer = zone.Alloc<char>(len + 1); | 435 char* buffer = isolate->current_zone()->Alloc<char>(len + 1); |
436 va_list args2; | 436 va_list args2; |
437 va_start(args2, format); | 437 va_start(args2, format); |
438 OS::VSNPrint(buffer, (len + 1), format, args2); | 438 OS::VSNPrint(buffer, (len + 1), format, args2); |
439 va_end(args2); | 439 va_end(args2); |
440 | 440 |
441 const String& message = String::Handle(isolate, String::New(buffer)); | 441 const String& message = String::Handle(isolate, String::New(buffer)); |
442 return Api::NewHandle(isolate, ApiError::New(message)); | 442 return Api::NewHandle(isolate, ApiError::New(message)); |
443 } | 443 } |
444 | 444 |
445 | 445 |
446 DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception) { | 446 DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception) { |
447 Isolate* isolate = Isolate::Current(); | 447 Isolate* isolate = Isolate::Current(); |
448 DARTSCOPE(isolate); | 448 DARTSCOPE(isolate); |
449 const Instance& obj = Api::UnwrapInstanceHandle(isolate, exception); | 449 const Instance& obj = Api::UnwrapInstanceHandle(isolate, exception); |
450 if (obj.IsNull()) { | 450 if (obj.IsNull()) { |
451 RETURN_TYPE_ERROR(isolate, exception, Instance); | 451 RETURN_TYPE_ERROR(isolate, exception, Instance); |
452 } | 452 } |
453 const Instance& stacktrace = Instance::Handle(isolate); | 453 const Instance& stacktrace = Instance::Handle(isolate); |
454 return Api::NewHandle(isolate, UnhandledException::New(obj, stacktrace)); | 454 return Api::NewHandle(isolate, UnhandledException::New(obj, stacktrace)); |
455 } | 455 } |
456 | 456 |
457 | 457 |
458 DART_EXPORT Dart_Handle Dart_PropagateError(Dart_Handle handle) { | 458 DART_EXPORT Dart_Handle Dart_PropagateError(Dart_Handle handle) { |
459 Isolate* isolate = Isolate::Current(); | 459 Isolate* isolate = Isolate::Current(); |
460 CHECK_ISOLATE(isolate); | |
461 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle)); | 460 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle)); |
462 if (!obj.IsError()) { | 461 if (!obj.IsError()) { |
463 return Api::NewError( | 462 return Api::NewError( |
464 "%s expects argument 'handle' to be an error handle. " | 463 "%s expects argument 'handle' to be an error handle. " |
465 "Did you forget to check Dart_IsError first?", | 464 "Did you forget to check Dart_IsError first?", |
466 CURRENT_FUNC); | 465 CURRENT_FUNC); |
467 } | 466 } |
468 if (isolate->top_exit_frame_info() == 0) { | 467 if (isolate->top_exit_frame_info() == 0) { |
469 // There are no dart frames on the stack so it would be illegal to | 468 // There are no dart frames on the stack so it would be illegal to |
470 // propagate an error here. | 469 // propagate an error here. |
471 return Api::NewError("No Dart frames on stack, cannot propagate error."); | 470 return Api::NewError("No Dart frames on stack, cannot propagate error."); |
472 } | 471 } |
473 | 472 |
474 // Unwind all the API scopes till the exit frame before propagating. | 473 // Unwind all the API scopes till the exit frame before propagating. |
475 ApiState* state = isolate->api_state(); | 474 ApiState* state = isolate->api_state(); |
476 ASSERT(state != NULL); | 475 ASSERT(state != NULL); |
477 state->UnwindScopes(isolate->top_exit_frame_info()); | 476 const Error* error; |
478 Exceptions::PropagateError(Error::Cast(obj)); | 477 { |
| 478 // We need to preserve the error object across the destruction of zones |
| 479 // when the ApiScopes are unwound. By using NoGCScope, we can ensure |
| 480 // that GC won't touch the raw error object before creating a valid |
| 481 // handle for it in the surviving zone. |
| 482 NoGCScope no_gc; |
| 483 RawError* raw_error = static_cast<RawError*>(Api::UnwrapHandle(handle)); |
| 484 state->UnwindScopes(isolate->top_exit_frame_info()); |
| 485 error = &Error::Handle(isolate, raw_error); |
| 486 } |
| 487 Exceptions::PropagateError(*error); |
479 UNREACHABLE(); | 488 UNREACHABLE(); |
480 | |
481 return Api::NewError("Cannot reach here. Internal error."); | 489 return Api::NewError("Cannot reach here. Internal error."); |
482 } | 490 } |
483 | 491 |
484 | 492 |
485 DART_EXPORT void _Dart_ReportErrorHandle(const char* file, | 493 DART_EXPORT void _Dart_ReportErrorHandle(const char* file, |
486 int line, | 494 int line, |
487 const char* handle, | 495 const char* handle, |
488 const char* message) { | 496 const char* message) { |
489 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n", | 497 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n", |
490 file, line, handle, message); | 498 file, line, handle, message); |
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1135 CHECK_ISOLATE_SCOPE(isolate); | 1143 CHECK_ISOLATE_SCOPE(isolate); |
1136 ApiState* state = isolate->api_state(); | 1144 ApiState* state = isolate->api_state(); |
1137 ApiLocalScope* scope = state->top_scope(); | 1145 ApiLocalScope* scope = state->top_scope(); |
1138 | 1146 |
1139 state->set_top_scope(scope->previous()); // Reset top scope to previous. | 1147 state->set_top_scope(scope->previous()); // Reset top scope to previous. |
1140 delete scope; // Free up the old scope which we have just exited. | 1148 delete scope; // Free up the old scope which we have just exited. |
1141 } | 1149 } |
1142 | 1150 |
1143 | 1151 |
1144 DART_EXPORT uint8_t* Dart_ScopeAllocate(intptr_t size) { | 1152 DART_EXPORT uint8_t* Dart_ScopeAllocate(intptr_t size) { |
1145 ApiZone* zone; | 1153 Zone* zone; |
1146 Isolate* isolate = Isolate::Current(); | 1154 Isolate* isolate = Isolate::Current(); |
1147 if (isolate != NULL) { | 1155 if (isolate != NULL) { |
1148 ApiState* state = isolate->api_state(); | 1156 ApiState* state = isolate->api_state(); |
1149 if (state == NULL) return NULL; | 1157 if (state == NULL) return NULL; |
1150 ApiLocalScope* scope = state->top_scope(); | 1158 ApiLocalScope* scope = state->top_scope(); |
1151 zone = scope->zone(); | 1159 zone = scope->zone(); |
1152 } else { | 1160 } else { |
1153 ApiNativeScope* scope = ApiNativeScope::Current(); | 1161 ApiNativeScope* scope = ApiNativeScope::Current(); |
1154 if (scope == NULL) return NULL; | 1162 if (scope == NULL) return NULL; |
1155 zone = scope->zone(); | 1163 zone = scope->zone(); |
(...skipping 2734 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3890 instance.SetNativeField(index, value); | 3898 instance.SetNativeField(index, value); |
3891 return Api::Success(isolate); | 3899 return Api::Success(isolate); |
3892 } | 3900 } |
3893 | 3901 |
3894 | 3902 |
3895 // --- Exceptions ---- | 3903 // --- Exceptions ---- |
3896 | 3904 |
3897 | 3905 |
3898 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception) { | 3906 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception) { |
3899 Isolate* isolate = Isolate::Current(); | 3907 Isolate* isolate = Isolate::Current(); |
3900 DARTSCOPE(isolate); | 3908 CHECK_ISOLATE(isolate); |
3901 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception); | 3909 { |
3902 if (excp.IsNull()) { | 3910 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception); |
3903 RETURN_TYPE_ERROR(isolate, exception, Instance); | 3911 if (excp.IsNull()) { |
| 3912 RETURN_TYPE_ERROR(isolate, exception, Instance); |
| 3913 } |
3904 } | 3914 } |
3905 if (isolate->top_exit_frame_info() == 0) { | 3915 if (isolate->top_exit_frame_info() == 0) { |
3906 // There are no dart frames on the stack so it would be illegal to | 3916 // There are no dart frames on the stack so it would be illegal to |
3907 // throw an exception here. | 3917 // throw an exception here. |
3908 return Api::NewError("No Dart frames on stack, cannot throw exception"); | 3918 return Api::NewError("No Dart frames on stack, cannot throw exception"); |
3909 } | 3919 } |
3910 // Unwind all the API scopes till the exit frame before throwing an | 3920 // Unwind all the API scopes till the exit frame before throwing an |
3911 // exception. | 3921 // exception. |
3912 ApiState* state = isolate->api_state(); | 3922 ApiState* state = isolate->api_state(); |
3913 ASSERT(state != NULL); | 3923 ASSERT(state != NULL); |
3914 state->UnwindScopes(isolate->top_exit_frame_info()); | 3924 const Instance* saved_exception; |
3915 Exceptions::Throw(excp); | 3925 { |
| 3926 NoGCScope no_gc; |
| 3927 RawInstance* raw_exception = |
| 3928 static_cast<RawInstance*>(Api::UnwrapHandle(exception)); |
| 3929 state->UnwindScopes(isolate->top_exit_frame_info()); |
| 3930 saved_exception = &Instance::Handle(raw_exception); |
| 3931 } |
| 3932 Exceptions::Throw(*saved_exception); |
3916 return Api::NewError("Exception was not thrown, internal error"); | 3933 return Api::NewError("Exception was not thrown, internal error"); |
3917 } | 3934 } |
3918 | 3935 |
3919 | 3936 |
3920 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception, | 3937 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception, |
3921 Dart_Handle stacktrace) { | 3938 Dart_Handle stacktrace) { |
3922 Isolate* isolate = Isolate::Current(); | 3939 Isolate* isolate = Isolate::Current(); |
3923 CHECK_ISOLATE(isolate); | 3940 CHECK_ISOLATE(isolate); |
3924 DARTSCOPE(isolate); | 3941 { |
3925 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception); | 3942 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception); |
3926 if (excp.IsNull()) { | 3943 if (excp.IsNull()) { |
3927 RETURN_TYPE_ERROR(isolate, exception, Instance); | 3944 RETURN_TYPE_ERROR(isolate, exception, Instance); |
3928 } | 3945 } |
3929 const Instance& stk = Api::UnwrapInstanceHandle(isolate, stacktrace); | 3946 const Instance& stk = Api::UnwrapInstanceHandle(isolate, stacktrace); |
3930 if (stk.IsNull()) { | 3947 if (stk.IsNull()) { |
3931 RETURN_TYPE_ERROR(isolate, stacktrace, Instance); | 3948 RETURN_TYPE_ERROR(isolate, stacktrace, Instance); |
| 3949 } |
3932 } | 3950 } |
3933 if (isolate->top_exit_frame_info() == 0) { | 3951 if (isolate->top_exit_frame_info() == 0) { |
3934 // There are no dart frames on the stack so it would be illegal to | 3952 // There are no dart frames on the stack so it would be illegal to |
3935 // throw an exception here. | 3953 // throw an exception here. |
3936 return Api::NewError("No Dart frames on stack, cannot throw exception"); | 3954 return Api::NewError("No Dart frames on stack, cannot throw exception"); |
3937 } | 3955 } |
3938 // Unwind all the API scopes till the exit frame before throwing an | 3956 // Unwind all the API scopes till the exit frame before throwing an |
3939 // exception. | 3957 // exception. |
3940 ApiState* state = isolate->api_state(); | 3958 ApiState* state = isolate->api_state(); |
3941 ASSERT(state != NULL); | 3959 ASSERT(state != NULL); |
3942 state->UnwindScopes(isolate->top_exit_frame_info()); | 3960 const Instance* saved_exception; |
3943 Exceptions::ReThrow(excp, stk); | 3961 const Instance* saved_stacktrace; |
| 3962 { |
| 3963 NoGCScope no_gc; |
| 3964 RawInstance* raw_exception = |
| 3965 static_cast<RawInstance*>(Api::UnwrapHandle(exception)); |
| 3966 RawInstance* raw_stacktrace = |
| 3967 static_cast<RawInstance*>(Api::UnwrapHandle(stacktrace)); |
| 3968 state->UnwindScopes(isolate->top_exit_frame_info()); |
| 3969 saved_exception = &Instance::Handle(raw_exception); |
| 3970 saved_stacktrace = &Instance::Handle(raw_stacktrace); |
| 3971 } |
| 3972 Exceptions::ReThrow(*saved_exception, *saved_stacktrace); |
3944 return Api::NewError("Exception was not re thrown, internal error"); | 3973 return Api::NewError("Exception was not re thrown, internal error"); |
3945 } | 3974 } |
3946 | 3975 |
3947 | 3976 |
3948 // --- Native functions --- | 3977 // --- Native functions --- |
3949 | 3978 |
3950 | 3979 |
3951 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, | 3980 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, |
3952 int index) { | 3981 int index) { |
3953 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 3982 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4416 | 4445 |
4417 DART_EXPORT void Dart_GetPprofSymbolInfo(void** buffer, int* buffer_size) { | 4446 DART_EXPORT void Dart_GetPprofSymbolInfo(void** buffer, int* buffer_size) { |
4418 Isolate* isolate = Isolate::Current(); | 4447 Isolate* isolate = Isolate::Current(); |
4419 DebugInfo* pprof_symbol_generator = Dart::pprof_symbol_generator(); | 4448 DebugInfo* pprof_symbol_generator = Dart::pprof_symbol_generator(); |
4420 if (pprof_symbol_generator != NULL) { | 4449 if (pprof_symbol_generator != NULL) { |
4421 DebugInfo::ByteBuffer* debug_region = new DebugInfo::ByteBuffer(); | 4450 DebugInfo::ByteBuffer* debug_region = new DebugInfo::ByteBuffer(); |
4422 ASSERT(debug_region != NULL); | 4451 ASSERT(debug_region != NULL); |
4423 pprof_symbol_generator->WriteToMemory(debug_region); | 4452 pprof_symbol_generator->WriteToMemory(debug_region); |
4424 *buffer_size = debug_region->size(); | 4453 *buffer_size = debug_region->size(); |
4425 if (*buffer_size != 0) { | 4454 if (*buffer_size != 0) { |
4426 ApiZone* zone = Api::TopScope(isolate)->zone(); | 4455 Zone* zone = Api::TopScope(isolate)->zone(); |
4427 *buffer = reinterpret_cast<void*>(zone->AllocUnsafe(*buffer_size)); | 4456 *buffer = reinterpret_cast<void*>(zone->AllocUnsafe(*buffer_size)); |
4428 memmove(*buffer, debug_region->data(), *buffer_size); | 4457 memmove(*buffer, debug_region->data(), *buffer_size); |
4429 } else { | 4458 } else { |
4430 *buffer = NULL; | 4459 *buffer = NULL; |
4431 } | 4460 } |
4432 delete debug_region; | 4461 delete debug_region; |
4433 } else { | 4462 } else { |
4434 *buffer = NULL; | 4463 *buffer = NULL; |
4435 *buffer_size = 0; | 4464 *buffer_size = 0; |
4436 } | 4465 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4477 } | 4506 } |
4478 { | 4507 { |
4479 NoGCScope no_gc; | 4508 NoGCScope no_gc; |
4480 RawObject* raw_obj = obj.raw(); | 4509 RawObject* raw_obj = obj.raw(); |
4481 isolate->heap()->SetPeer(raw_obj, peer); | 4510 isolate->heap()->SetPeer(raw_obj, peer); |
4482 } | 4511 } |
4483 return Api::Success(isolate); | 4512 return Api::Success(isolate); |
4484 } | 4513 } |
4485 | 4514 |
4486 } // namespace dart | 4515 } // namespace dart |
OLD | NEW |