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

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 11028145: Changed StackZone and ApiZone to be containers for Zone. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Reworked ApiScope unwinding. Created 8 years, 2 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
« no previous file with comments | « runtime/vm/code_descriptors_test.cc ('k') | runtime/vm/dart_api_impl_test.cc » ('j') | 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 "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
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
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 // Unwind all the API scopes till the exit frame, and reallocate handle.
459 DART_EXPORT Dart_Handle UnwindApiScopes(Isolate* isolate, Dart_Handle handle) {
siva 2012/10/15 17:50:35 Why not make this a static method?
Ivan Posva 2012/10/15 18:26:01 There should be no need to return a Dart_Handle he
Tom Ball 2012/10/15 19:51:06 Done.
Tom Ball 2012/10/15 19:51:06 Inlined function.
460 NoGCScope no_gc;
461 ApiState* state = isolate->api_state();
462 ASSERT(state != NULL);
463 RawObject* raw_obj = Api::UnwrapHandle(handle);
464 state->UnwindScopes(isolate->top_exit_frame_info());
465 return Api::NewHandle(isolate, raw_obj);
siva 2012/10/15 17:50:35 These new handles are created in the outer API sco
Tom Ball 2012/10/15 19:51:06 Removed function, as Ivan described.
466 }
467
468
458 DART_EXPORT Dart_Handle Dart_PropagateError(Dart_Handle handle) { 469 DART_EXPORT Dart_Handle Dart_PropagateError(Dart_Handle handle) {
459 Isolate* isolate = Isolate::Current(); 470 Isolate* isolate = Isolate::Current();
460 CHECK_ISOLATE(isolate); 471 {
461 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle)); 472 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle));
462 if (!obj.IsError()) { 473 if (!obj.IsError()) {
463 return Api::NewError( 474 return Api::NewError(
464 "%s expects argument 'handle' to be an error handle. " 475 "%s expects argument 'handle' to be an error handle. "
465 "Did you forget to check Dart_IsError first?", 476 "Did you forget to check Dart_IsError first?",
466 CURRENT_FUNC); 477 CURRENT_FUNC);
478 }
467 } 479 }
468 if (isolate->top_exit_frame_info() == 0) { 480 if (isolate->top_exit_frame_info() == 0) {
469 // There are no dart frames on the stack so it would be illegal to 481 // There are no dart frames on the stack so it would be illegal to
470 // propagate an error here. 482 // propagate an error here.
471 return Api::NewError("No Dart frames on stack, cannot propagate error."); 483 return Api::NewError("No Dart frames on stack, cannot propagate error.");
472 } 484 }
473 485
474 // Unwind all the API scopes till the exit frame before propagating. 486 // Unwind all the API scopes till the exit frame before propagating.
475 ApiState* state = isolate->api_state(); 487 handle = UnwindApiScopes(isolate, handle);
Ivan Posva 2012/10/15 18:26:01 This avoids Siva's concern: ASSERT(state != NUL
Tom Ball 2012/10/15 19:51:06 Integrated recommended change.
476 ASSERT(state != NULL); 488 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(handle));
477 state->UnwindScopes(isolate->top_exit_frame_info());
478 Exceptions::PropagateError(Error::Cast(obj)); 489 Exceptions::PropagateError(Error::Cast(obj));
479 UNREACHABLE(); 490 UNREACHABLE();
480
481 return Api::NewError("Cannot reach here. Internal error."); 491 return Api::NewError("Cannot reach here. Internal error.");
482 } 492 }
483 493
484 494
485 DART_EXPORT void _Dart_ReportErrorHandle(const char* file, 495 DART_EXPORT void _Dart_ReportErrorHandle(const char* file,
486 int line, 496 int line,
487 const char* handle, 497 const char* handle,
488 const char* message) { 498 const char* message) {
489 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n", 499 fprintf(stderr, "%s:%d: error handle: '%s':\n '%s'\n",
490 file, line, handle, message); 500 file, line, handle, message);
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after
1135 CHECK_ISOLATE_SCOPE(isolate); 1145 CHECK_ISOLATE_SCOPE(isolate);
1136 ApiState* state = isolate->api_state(); 1146 ApiState* state = isolate->api_state();
1137 ApiLocalScope* scope = state->top_scope(); 1147 ApiLocalScope* scope = state->top_scope();
1138 1148
1139 state->set_top_scope(scope->previous()); // Reset top scope to previous. 1149 state->set_top_scope(scope->previous()); // Reset top scope to previous.
1140 delete scope; // Free up the old scope which we have just exited. 1150 delete scope; // Free up the old scope which we have just exited.
1141 } 1151 }
1142 1152
1143 1153
1144 DART_EXPORT uint8_t* Dart_ScopeAllocate(intptr_t size) { 1154 DART_EXPORT uint8_t* Dart_ScopeAllocate(intptr_t size) {
1145 ApiZone* zone; 1155 Zone* zone;
1146 Isolate* isolate = Isolate::Current(); 1156 Isolate* isolate = Isolate::Current();
1147 if (isolate != NULL) { 1157 if (isolate != NULL) {
1148 ApiState* state = isolate->api_state(); 1158 ApiState* state = isolate->api_state();
1149 if (state == NULL) return NULL; 1159 if (state == NULL) return NULL;
1150 ApiLocalScope* scope = state->top_scope(); 1160 ApiLocalScope* scope = state->top_scope();
1151 zone = scope->zone(); 1161 zone = scope->zone();
1152 } else { 1162 } else {
1153 ApiNativeScope* scope = ApiNativeScope::Current(); 1163 ApiNativeScope* scope = ApiNativeScope::Current();
1154 if (scope == NULL) return NULL; 1164 if (scope == NULL) return NULL;
1155 zone = scope->zone(); 1165 zone = scope->zone();
(...skipping 2733 matching lines...) Expand 10 before | Expand all | Expand 10 after
3889 instance.SetNativeField(index, value); 3899 instance.SetNativeField(index, value);
3890 return Api::Success(isolate); 3900 return Api::Success(isolate);
3891 } 3901 }
3892 3902
3893 3903
3894 // --- Exceptions ---- 3904 // --- Exceptions ----
3895 3905
3896 3906
3897 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception) { 3907 DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception) {
3898 Isolate* isolate = Isolate::Current(); 3908 Isolate* isolate = Isolate::Current();
3899 DARTSCOPE(isolate); 3909 CHECK_ISOLATE(isolate);
3900 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception); 3910 {
3901 if (excp.IsNull()) { 3911 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception);
3902 RETURN_TYPE_ERROR(isolate, exception, Instance); 3912 if (excp.IsNull()) {
3913 RETURN_TYPE_ERROR(isolate, exception, Instance);
3914 }
3903 } 3915 }
3904 if (isolate->top_exit_frame_info() == 0) { 3916 if (isolate->top_exit_frame_info() == 0) {
3905 // There are no dart frames on the stack so it would be illegal to 3917 // There are no dart frames on the stack so it would be illegal to
3906 // throw an exception here. 3918 // throw an exception here.
3907 return Api::NewError("No Dart frames on stack, cannot throw exception"); 3919 return Api::NewError("No Dart frames on stack, cannot throw exception");
3908 } 3920 }
3909 // Unwind all the API scopes till the exit frame before throwing an 3921 // Unwind all the API scopes till the exit frame before throwing an
3910 // exception. 3922 // exception.
3911 ApiState* state = isolate->api_state(); 3923 ApiState* state = isolate->api_state();
3912 ASSERT(state != NULL); 3924 ASSERT(state != NULL);
3913 state->UnwindScopes(isolate->top_exit_frame_info()); 3925
3926 // Unwind all the API scopes till the exit frame before throwing an
3927 // exception.
3928 exception = UnwindApiScopes(isolate, exception);
3929 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception);
3914 Exceptions::Throw(excp); 3930 Exceptions::Throw(excp);
3915 return Api::NewError("Exception was not thrown, internal error"); 3931 return Api::NewError("Exception was not thrown, internal error");
3916 } 3932 }
3917 3933
3918 3934
3935 // Unwind all the API scopes till the exit frame before rethrowing an exception.
3936 DART_EXPORT void UnwindApiScopesForRethrow(Isolate* isolate,
3937 Dart_Handle* exception,
3938 Dart_Handle* stacktrace) {
siva 2012/10/15 17:50:35 Ditto, why not make this a static method.
Tom Ball 2012/10/15 19:51:06 Inlined code and removed function.
3939 NoGCScope no_gc;
3940 ApiState* state = isolate->api_state();
3941 ASSERT(state != NULL);
3942 RawObject* raw_excp = Api::UnwrapHandle(*exception);
3943 RawObject* raw_stk = Api::UnwrapHandle(*stacktrace);
3944 state->UnwindScopes(isolate->top_exit_frame_info());
3945 *exception = Api::NewHandle(isolate, raw_excp);
Ivan Posva 2012/10/15 18:26:01 No Dart_Handles as they can leak out.
Tom Ball 2012/10/15 19:51:06 Removed, followed pattern you recommended for Prop
3946 *stacktrace = Api::NewHandle(isolate, raw_stk);
3947 }
3948
3949
3919 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception, 3950 DART_EXPORT Dart_Handle Dart_ReThrowException(Dart_Handle exception,
3920 Dart_Handle stacktrace) { 3951 Dart_Handle stacktrace) {
3921 Isolate* isolate = Isolate::Current(); 3952 Isolate* isolate = Isolate::Current();
3922 CHECK_ISOLATE(isolate); 3953 CHECK_ISOLATE(isolate);
3923 DARTSCOPE(isolate); 3954 {
3924 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception); 3955 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception);
3925 if (excp.IsNull()) { 3956 if (excp.IsNull()) {
3926 RETURN_TYPE_ERROR(isolate, exception, Instance); 3957 RETURN_TYPE_ERROR(isolate, exception, Instance);
3927 } 3958 }
3928 const Instance& stk = Api::UnwrapInstanceHandle(isolate, stacktrace); 3959 const Instance& stk = Api::UnwrapInstanceHandle(isolate, stacktrace);
3929 if (stk.IsNull()) { 3960 if (stk.IsNull()) {
3930 RETURN_TYPE_ERROR(isolate, stacktrace, Instance); 3961 RETURN_TYPE_ERROR(isolate, stacktrace, Instance);
3962 }
3931 } 3963 }
3932 if (isolate->top_exit_frame_info() == 0) { 3964 if (isolate->top_exit_frame_info() == 0) {
3933 // There are no dart frames on the stack so it would be illegal to 3965 // There are no dart frames on the stack so it would be illegal to
3934 // throw an exception here. 3966 // throw an exception here.
3935 return Api::NewError("No Dart frames on stack, cannot throw exception"); 3967 return Api::NewError("No Dart frames on stack, cannot throw exception");
3936 } 3968 }
3937 // Unwind all the API scopes till the exit frame before throwing an 3969 // Unwind all the API scopes till the exit frame before throwing an
3938 // exception. 3970 // exception.
3939 ApiState* state = isolate->api_state(); 3971 ApiState* state = isolate->api_state();
3940 ASSERT(state != NULL); 3972 ASSERT(state != NULL);
3941 state->UnwindScopes(isolate->top_exit_frame_info()); 3973 UnwindApiScopesForRethrow(isolate, &exception, &stacktrace);
3974 const Instance& excp = Api::UnwrapInstanceHandle(isolate, exception);
3975 const Instance& stk = Api::UnwrapInstanceHandle(isolate, stacktrace);
3942 Exceptions::ReThrow(excp, stk); 3976 Exceptions::ReThrow(excp, stk);
3943 return Api::NewError("Exception was not re thrown, internal error"); 3977 return Api::NewError("Exception was not re thrown, internal error");
3944 } 3978 }
3945 3979
3946 3980
3947 // --- Native functions --- 3981 // --- Native functions ---
3948 3982
3949 3983
3950 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, 3984 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args,
3951 int index) { 3985 int index) {
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
4415 4449
4416 DART_EXPORT void Dart_GetPprofSymbolInfo(void** buffer, int* buffer_size) { 4450 DART_EXPORT void Dart_GetPprofSymbolInfo(void** buffer, int* buffer_size) {
4417 Isolate* isolate = Isolate::Current(); 4451 Isolate* isolate = Isolate::Current();
4418 DebugInfo* pprof_symbol_generator = Dart::pprof_symbol_generator(); 4452 DebugInfo* pprof_symbol_generator = Dart::pprof_symbol_generator();
4419 if (pprof_symbol_generator != NULL) { 4453 if (pprof_symbol_generator != NULL) {
4420 DebugInfo::ByteBuffer* debug_region = new DebugInfo::ByteBuffer(); 4454 DebugInfo::ByteBuffer* debug_region = new DebugInfo::ByteBuffer();
4421 ASSERT(debug_region != NULL); 4455 ASSERT(debug_region != NULL);
4422 pprof_symbol_generator->WriteToMemory(debug_region); 4456 pprof_symbol_generator->WriteToMemory(debug_region);
4423 *buffer_size = debug_region->size(); 4457 *buffer_size = debug_region->size();
4424 if (*buffer_size != 0) { 4458 if (*buffer_size != 0) {
4425 ApiZone* zone = Api::TopScope(isolate)->zone(); 4459 Zone* zone = Api::TopScope(isolate)->zone();
4426 *buffer = reinterpret_cast<void*>(zone->AllocUnsafe(*buffer_size)); 4460 *buffer = reinterpret_cast<void*>(zone->AllocUnsafe(*buffer_size));
4427 memmove(*buffer, debug_region->data(), *buffer_size); 4461 memmove(*buffer, debug_region->data(), *buffer_size);
4428 } else { 4462 } else {
4429 *buffer = NULL; 4463 *buffer = NULL;
4430 } 4464 }
4431 delete debug_region; 4465 delete debug_region;
4432 } else { 4466 } else {
4433 *buffer = NULL; 4467 *buffer = NULL;
4434 *buffer_size = 0; 4468 *buffer_size = 0;
4435 } 4469 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
4476 } 4510 }
4477 { 4511 {
4478 NoGCScope no_gc; 4512 NoGCScope no_gc;
4479 RawObject* raw_obj = obj.raw(); 4513 RawObject* raw_obj = obj.raw();
4480 isolate->heap()->SetPeer(raw_obj, peer); 4514 isolate->heap()->SetPeer(raw_obj, peer);
4481 } 4515 }
4482 return Api::Success(isolate); 4516 return Api::Success(isolate);
4483 } 4517 }
4484 4518
4485 } // namespace dart 4519 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/code_descriptors_test.cc ('k') | runtime/vm/dart_api_impl_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698