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

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

Issue 9148051: Provide API support for weak handles and post-mortem finalization. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: a better strategy for protected handle checks Created 8 years, 11 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
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 ref->set_raw(object); 148 ref->set_raw(object);
149 return reinterpret_cast<Dart_Handle>(ref); 149 return reinterpret_cast<Dart_Handle>(ref);
150 } 150 }
151 151
152 RawObject* Api::UnwrapHandle(Dart_Handle object) { 152 RawObject* Api::UnwrapHandle(Dart_Handle object) {
153 #ifdef DEBUG 153 #ifdef DEBUG
154 Isolate* isolate = Isolate::Current(); 154 Isolate* isolate = Isolate::Current();
155 ASSERT(isolate != NULL); 155 ASSERT(isolate != NULL);
156 ApiState* state = isolate->api_state(); 156 ApiState* state = isolate->api_state();
157 ASSERT(state != NULL); 157 ASSERT(state != NULL);
158 ASSERT(state->IsValidPersistentHandle(object) || 158 ASSERT(state->IsValidWeakPersistentHandle(object) ||
159 state->IsValidPersistentHandle(object) ||
159 state->IsValidLocalHandle(object)); 160 state->IsValidLocalHandle(object));
160 ASSERT(PersistentHandle::raw_offset() == 0 && 161 ASSERT(WeakPersistentHandle::raw_offset() == 0 &&
162 PersistentHandle::raw_offset() == 0 &&
161 LocalHandle::raw_offset() == 0); 163 LocalHandle::raw_offset() == 0);
162 #endif 164 #endif
163 return *(reinterpret_cast<RawObject**>(object)); 165 return *(reinterpret_cast<RawObject**>(object));
164 } 166 }
165 167
166 #define DEFINE_UNWRAP(Type) \ 168 #define DEFINE_UNWRAP(Type) \
167 const Type& Api::Unwrap##Type##Handle(Dart_Handle dart_handle) { \ 169 const Type& Api::Unwrap##Type##Handle(Dart_Handle dart_handle) { \
168 const Object& tmp = Object::Handle(Api::UnwrapHandle(dart_handle)); \ 170 const Object& tmp = Object::Handle(Api::UnwrapHandle(dart_handle)); \
169 Type& typed_handle = Type::Handle(); \ 171 Type& typed_handle = Type::Handle(); \
170 if (tmp.Is##Type()) { \ 172 if (tmp.Is##Type()) { \
(...skipping 12 matching lines...) Expand all
183 } 185 }
184 186
185 187
186 PersistentHandle* Api::UnwrapAsPersistentHandle(const ApiState& state, 188 PersistentHandle* Api::UnwrapAsPersistentHandle(const ApiState& state,
187 Dart_Handle object) { 189 Dart_Handle object) {
188 ASSERT(state.IsValidPersistentHandle(object)); 190 ASSERT(state.IsValidPersistentHandle(object));
189 return reinterpret_cast<PersistentHandle*>(object); 191 return reinterpret_cast<PersistentHandle*>(object);
190 } 192 }
191 193
192 194
195 WeakPersistentHandle* Api::UnwrapAsWeakPersistentHandle(const ApiState& state,
196 Dart_Handle object) {
197 ASSERT(state.IsValidWeakPersistentHandle(object));
198 return reinterpret_cast<WeakPersistentHandle*>(object);
199 }
200
201
193 Dart_Isolate Api::CastIsolate(Isolate* isolate) { 202 Dart_Isolate Api::CastIsolate(Isolate* isolate) {
194 return reinterpret_cast<Dart_Isolate>(isolate); 203 return reinterpret_cast<Dart_Isolate>(isolate);
195 } 204 }
196 205
197 206
198 Dart_Message Api::CastMessage(uint8_t* message) { 207 Dart_Message Api::CastMessage(uint8_t* message) {
199 return reinterpret_cast<Dart_Message>(message); 208 return reinterpret_cast<Dart_Message>(message);
200 } 209 }
201 210
202 211
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 DART_EXPORT Dart_Handle Dart_IsSame(Dart_Handle obj1, Dart_Handle obj2, 459 DART_EXPORT Dart_Handle Dart_IsSame(Dart_Handle obj1, Dart_Handle obj2,
451 bool* value) { 460 bool* value) {
452 DARTSCOPE(Isolate::Current()); 461 DARTSCOPE(Isolate::Current());
453 const Object& expected = Object::Handle(Api::UnwrapHandle(obj1)); 462 const Object& expected = Object::Handle(Api::UnwrapHandle(obj1));
454 const Object& actual = Object::Handle(Api::UnwrapHandle(obj2)); 463 const Object& actual = Object::Handle(Api::UnwrapHandle(obj2));
455 *value = (expected.raw() == actual.raw()); 464 *value = (expected.raw() == actual.raw());
456 return Api::Success(); 465 return Api::Success();
457 } 466 }
458 467
459 468
460 static PersistentHandle* AllocatePersistentHandle(Dart_Handle object) { 469 DART_EXPORT Dart_Handle Dart_NewPersistentHandle(Dart_Handle object) {
461 Isolate* isolate = Isolate::Current(); 470 Isolate* isolate = Isolate::Current();
462 CHECK_ISOLATE(isolate); 471 CHECK_ISOLATE(isolate);
463 DARTSCOPE_NOCHECKS(isolate); 472 DARTSCOPE_NOCHECKS(isolate);
464 ApiState* state = isolate->api_state(); 473 ApiState* state = isolate->api_state();
465 ASSERT(state != NULL); 474 ASSERT(state != NULL);
466 const Object& old_ref = Object::Handle(Api::UnwrapHandle(object)); 475 const Object& old_ref = Object::Handle(Api::UnwrapHandle(object));
467 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle(); 476 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle();
468 new_ref->set_raw(old_ref); 477 new_ref->set_raw(old_ref);
469 return new_ref;
470 }
471
472
473 DART_EXPORT Dart_Handle Dart_NewPersistentHandle(Dart_Handle object) {
474 PersistentHandle* new_ref = AllocatePersistentHandle(object);
475 return reinterpret_cast<Dart_Handle>(new_ref); 478 return reinterpret_cast<Dart_Handle>(new_ref);
476 } 479 }
477 480
478 481
479 DART_EXPORT Dart_Handle Dart_NewWeakPersistentHandle( 482 DART_EXPORT Dart_Handle Dart_NewWeakPersistentHandle(
480 Dart_Handle object, 483 Dart_Handle object,
481 void* peer, 484 void* peer,
482 Dart_PeerFinalizer callback) { 485 Dart_PeerFinalizer callback) {
483 PersistentHandle* new_ref = AllocatePersistentHandle(object); 486 Isolate* isolate = Isolate::Current();
484 new_ref->set_kind(PersistentHandle::WeakReference); 487 CHECK_ISOLATE(isolate);
488 DARTSCOPE_NOCHECKS(isolate);
489 ApiState* state = isolate->api_state();
490 ASSERT(state != NULL);
491 const Object& old_ref = Object::Handle(Api::UnwrapHandle(object));
492 WeakPersistentHandle* new_ref =
493 state->weak_persistent_handles().AllocateHandle();
494 new_ref->set_raw(old_ref);
485 new_ref->set_peer(peer); 495 new_ref->set_peer(peer);
486 new_ref->set_callback(callback); 496 new_ref->set_callback(callback);
487 return reinterpret_cast<Dart_Handle>(new_ref); 497 return reinterpret_cast<Dart_Handle>(new_ref);
488 } 498 }
489 499
490 500
491 DART_EXPORT void Dart_DeletePersistentHandle(Dart_Handle object) { 501 DART_EXPORT void Dart_DeletePersistentHandle(Dart_Handle object) {
492 Isolate* isolate = Isolate::Current(); 502 Isolate* isolate = Isolate::Current();
493 CHECK_ISOLATE(isolate); 503 CHECK_ISOLATE(isolate);
494 ApiState* state = isolate->api_state(); 504 ApiState* state = isolate->api_state();
495 ASSERT(state != NULL); 505 ASSERT(state != NULL);
496 PersistentHandle* ref = Api::UnwrapAsPersistentHandle(*state, object); 506 if (state->IsValidWeakPersistentHandle(object)) {
497 ASSERT(!ref->IsProtected()); 507 WeakPersistentHandle* weak_ref =
498 if (!ref->IsProtected()) { 508 Api::UnwrapAsWeakPersistentHandle(*state, object);
509 state->weak_persistent_handles().FreeHandle(weak_ref);
510 } else {
511 PersistentHandle* ref = Api::UnwrapAsPersistentHandle(*state, object);
512 ASSERT(!state->IsProtectedHandle(ref));
499 state->persistent_handles().FreeHandle(ref); 513 state->persistent_handles().FreeHandle(ref);
Ivan Posva 2012/01/12 23:30:38 Why not checking for protected handles?
cshapiro 2012/01/13 01:30:03 A good idea. Done. This makes the diff a bit mor
500 } 514 }
501 } 515 }
502 516
503 517
504 DART_EXPORT bool Dart_IsWeakPersistentHandle(Dart_Handle object) { 518 DART_EXPORT bool Dart_IsWeakPersistentHandle(Dart_Handle object) {
505 Isolate* isolate = Isolate::Current(); 519 Isolate* isolate = Isolate::Current();
506 CHECK_ISOLATE(isolate); 520 CHECK_ISOLATE(isolate);
507 ApiState* state = isolate->api_state(); 521 ApiState* state = isolate->api_state();
508 ASSERT(state != NULL); 522 ASSERT(state != NULL);
509 if (state->IsValidPersistentHandle(object)) { 523 return state->IsValidWeakPersistentHandle(object);
510 PersistentHandle* ref = Api::UnwrapAsPersistentHandle(*state, object);
511 return ref->kind() == PersistentHandle::WeakReference;
512 }
513 return false;
514 } 524 }
515 525
516 526
517 // --- Initialization and Globals --- 527 // --- Initialization and Globals ---
518 528
519 529
520 DART_EXPORT bool Dart_Initialize(Dart_IsolateCreateCallback create, 530 DART_EXPORT bool Dart_Initialize(Dart_IsolateCreateCallback create,
521 Dart_IsolateInterruptCallback interrupt) { 531 Dart_IsolateInterruptCallback interrupt) {
522 return Dart::InitOnce(create, interrupt); 532 return Dart::InitOnce(create, interrupt);
523 } 533 }
(...skipping 1978 matching lines...) Expand 10 before | Expand all | Expand 10 after
2502 } 2512 }
2503 delete debug_region; 2513 delete debug_region;
2504 } else { 2514 } else {
2505 *buffer = NULL; 2515 *buffer = NULL;
2506 *buffer_size = 0; 2516 *buffer_size = 0;
2507 } 2517 }
2508 } 2518 }
2509 2519
2510 2520
2511 } // namespace dart 2521 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698