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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 ref->set_raw(object); | 99 ref->set_raw(object); |
100 return reinterpret_cast<Dart_Handle>(ref); | 100 return reinterpret_cast<Dart_Handle>(ref); |
101 } | 101 } |
102 | 102 |
103 RawObject* Api::UnwrapHandle(Dart_Handle object) { | 103 RawObject* Api::UnwrapHandle(Dart_Handle object) { |
104 #ifdef DEBUG | 104 #ifdef DEBUG |
105 Isolate* isolate = Isolate::Current(); | 105 Isolate* isolate = Isolate::Current(); |
106 ASSERT(isolate != NULL); | 106 ASSERT(isolate != NULL); |
107 ApiState* state = isolate->api_state(); | 107 ApiState* state = isolate->api_state(); |
108 ASSERT(state != NULL); | 108 ASSERT(state != NULL); |
109 ASSERT(state->IsValidWeakPersistentHandle(object) || | 109 ASSERT(state->IsValidPrologueWeakPersistentHandle(object) || |
| 110 state->IsValidWeakPersistentHandle(object) || |
110 state->IsValidPersistentHandle(object) || | 111 state->IsValidPersistentHandle(object) || |
111 state->IsValidLocalHandle(object)); | 112 state->IsValidLocalHandle(object)); |
112 ASSERT(WeakPersistentHandle::raw_offset() == 0 && | 113 ASSERT(FinalizablePersistentHandle::raw_offset() == 0 && |
113 PersistentHandle::raw_offset() == 0 && | 114 PersistentHandle::raw_offset() == 0 && |
114 LocalHandle::raw_offset() == 0); | 115 LocalHandle::raw_offset() == 0); |
115 #endif | 116 #endif |
116 return *(reinterpret_cast<RawObject**>(object)); | 117 return *(reinterpret_cast<RawObject**>(object)); |
117 } | 118 } |
118 | 119 |
119 #define DEFINE_UNWRAP(Type) \ | 120 #define DEFINE_UNWRAP(Type) \ |
120 const Type& Api::Unwrap##Type##Handle(Dart_Handle dart_handle) { \ | 121 const Type& Api::Unwrap##Type##Handle(Dart_Handle dart_handle) { \ |
121 const Object& tmp = Object::Handle(Api::UnwrapHandle(dart_handle)); \ | 122 const Object& tmp = Object::Handle(Api::UnwrapHandle(dart_handle)); \ |
122 Type& typed_handle = Type::Handle(); \ | 123 Type& typed_handle = Type::Handle(); \ |
(...skipping 13 matching lines...) Expand all Loading... |
136 } | 137 } |
137 | 138 |
138 | 139 |
139 PersistentHandle* Api::UnwrapAsPersistentHandle(const ApiState& state, | 140 PersistentHandle* Api::UnwrapAsPersistentHandle(const ApiState& state, |
140 Dart_Handle object) { | 141 Dart_Handle object) { |
141 ASSERT(state.IsValidPersistentHandle(object)); | 142 ASSERT(state.IsValidPersistentHandle(object)); |
142 return reinterpret_cast<PersistentHandle*>(object); | 143 return reinterpret_cast<PersistentHandle*>(object); |
143 } | 144 } |
144 | 145 |
145 | 146 |
146 WeakPersistentHandle* Api::UnwrapAsWeakPersistentHandle(const ApiState& state, | 147 FinalizablePersistentHandle* Api::UnwrapAsWeakPersistentHandle( |
147 Dart_Handle object) { | 148 const ApiState& state, |
| 149 Dart_Handle object) { |
148 ASSERT(state.IsValidWeakPersistentHandle(object)); | 150 ASSERT(state.IsValidWeakPersistentHandle(object)); |
149 return reinterpret_cast<WeakPersistentHandle*>(object); | 151 return reinterpret_cast<FinalizablePersistentHandle*>(object); |
150 } | 152 } |
151 | 153 |
152 | 154 |
| 155 FinalizablePersistentHandle* Api::UnwrapAsPrologueWeakPersistentHandle( |
| 156 const ApiState& state, |
| 157 Dart_Handle object) { |
| 158 ASSERT(state.IsValidPrologueWeakPersistentHandle(object)); |
| 159 return reinterpret_cast<FinalizablePersistentHandle*>(object); |
| 160 } |
| 161 |
| 162 |
153 Dart_Isolate Api::CastIsolate(Isolate* isolate) { | 163 Dart_Isolate Api::CastIsolate(Isolate* isolate) { |
154 return reinterpret_cast<Dart_Isolate>(isolate); | 164 return reinterpret_cast<Dart_Isolate>(isolate); |
155 } | 165 } |
156 | 166 |
157 | 167 |
158 Dart_Handle Api::Success() { | 168 Dart_Handle Api::Success() { |
159 Isolate* isolate = Isolate::Current(); | 169 Isolate* isolate = Isolate::Current(); |
160 ASSERT(isolate != NULL); | 170 ASSERT(isolate != NULL); |
161 ApiState* state = isolate->api_state(); | 171 ApiState* state = isolate->api_state(); |
162 ASSERT(state != NULL); | 172 ASSERT(state != NULL); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 CHECK_ISOLATE(isolate); | 412 CHECK_ISOLATE(isolate); |
403 DARTSCOPE_NOCHECKS(isolate); | 413 DARTSCOPE_NOCHECKS(isolate); |
404 ApiState* state = isolate->api_state(); | 414 ApiState* state = isolate->api_state(); |
405 ASSERT(state != NULL); | 415 ASSERT(state != NULL); |
406 const Object& old_ref = Object::Handle(Api::UnwrapHandle(object)); | 416 const Object& old_ref = Object::Handle(Api::UnwrapHandle(object)); |
407 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle(); | 417 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle(); |
408 new_ref->set_raw(old_ref); | 418 new_ref->set_raw(old_ref); |
409 return reinterpret_cast<Dart_Handle>(new_ref); | 419 return reinterpret_cast<Dart_Handle>(new_ref); |
410 } | 420 } |
411 | 421 |
| 422 static Dart_Handle AllocateFinalizableHandle( |
| 423 FinalizablePersistentHandles* handles, |
| 424 Dart_Handle object, |
| 425 void* peer, |
| 426 Dart_WeakPersistentHandleFinalizer callback) { |
| 427 const Object& ref = Object::Handle(Api::UnwrapHandle(object)); |
| 428 FinalizablePersistentHandle* finalizable_ref = handles->AllocateHandle(); |
| 429 finalizable_ref->set_raw(ref); |
| 430 finalizable_ref->set_peer(peer); |
| 431 finalizable_ref->set_callback(callback); |
| 432 return reinterpret_cast<Dart_Handle>(finalizable_ref); |
| 433 } |
| 434 |
412 | 435 |
413 DART_EXPORT Dart_Handle Dart_NewWeakPersistentHandle( | 436 DART_EXPORT Dart_Handle Dart_NewWeakPersistentHandle( |
414 Dart_Handle object, | 437 Dart_Handle object, |
415 void* peer, | 438 void* peer, |
416 Dart_WeakPersistentHandleFinalizer callback) { | 439 Dart_WeakPersistentHandleFinalizer callback) { |
417 Isolate* isolate = Isolate::Current(); | 440 Isolate* isolate = Isolate::Current(); |
418 CHECK_ISOLATE(isolate); | 441 CHECK_ISOLATE(isolate); |
419 DARTSCOPE_NOCHECKS(isolate); | 442 DARTSCOPE_NOCHECKS(isolate); |
420 ApiState* state = isolate->api_state(); | 443 ApiState* state = isolate->api_state(); |
421 ASSERT(state != NULL); | 444 ASSERT(state != NULL); |
422 const Object& ref = Object::Handle(Api::UnwrapHandle(object)); | 445 return AllocateFinalizableHandle(&state->weak_persistent_handles(), |
423 WeakPersistentHandle* weak_ref = | 446 object, |
424 state->weak_persistent_handles().AllocateHandle(); | 447 peer, |
425 weak_ref->set_raw(ref); | 448 callback); |
426 weak_ref->set_peer(peer); | |
427 weak_ref->set_callback(callback); | |
428 return reinterpret_cast<Dart_Handle>(weak_ref); | |
429 } | 449 } |
430 | 450 |
431 | 451 |
| 452 DART_EXPORT Dart_Handle Dart_NewPrologueWeakPersistentHandle( |
| 453 Dart_Handle object, |
| 454 void* peer, |
| 455 Dart_WeakPersistentHandleFinalizer callback) { |
| 456 Isolate* isolate = Isolate::Current(); |
| 457 CHECK_ISOLATE(isolate); |
| 458 DARTSCOPE_NOCHECKS(isolate); |
| 459 ApiState* state = isolate->api_state(); |
| 460 ASSERT(state != NULL); |
| 461 return AllocateFinalizableHandle(&state->prologue_weak_persistent_handles(), |
| 462 object, |
| 463 peer, |
| 464 callback); |
| 465 } |
| 466 |
| 467 |
432 DART_EXPORT void Dart_DeletePersistentHandle(Dart_Handle object) { | 468 DART_EXPORT void Dart_DeletePersistentHandle(Dart_Handle object) { |
433 Isolate* isolate = Isolate::Current(); | 469 Isolate* isolate = Isolate::Current(); |
434 CHECK_ISOLATE(isolate); | 470 CHECK_ISOLATE(isolate); |
435 ApiState* state = isolate->api_state(); | 471 ApiState* state = isolate->api_state(); |
436 ASSERT(state != NULL); | 472 ASSERT(state != NULL); |
| 473 if (state->IsValidPrologueWeakPersistentHandle(object)) { |
| 474 FinalizablePersistentHandle* prologue_weak_ref = |
| 475 Api::UnwrapAsPrologueWeakPersistentHandle(*state, object); |
| 476 state->prologue_weak_persistent_handles().FreeHandle(prologue_weak_ref); |
| 477 return; |
| 478 } |
437 if (state->IsValidWeakPersistentHandle(object)) { | 479 if (state->IsValidWeakPersistentHandle(object)) { |
438 WeakPersistentHandle* weak_ref = | 480 FinalizablePersistentHandle* weak_ref = |
439 Api::UnwrapAsWeakPersistentHandle(*state, object); | 481 Api::UnwrapAsWeakPersistentHandle(*state, object); |
440 state->weak_persistent_handles().FreeHandle(weak_ref); | 482 state->weak_persistent_handles().FreeHandle(weak_ref); |
441 } else { | 483 return; |
442 PersistentHandle* ref = Api::UnwrapAsPersistentHandle(*state, object); | 484 } |
443 ASSERT(!state->IsProtectedHandle(ref)); | 485 PersistentHandle* ref = Api::UnwrapAsPersistentHandle(*state, object); |
444 if (!state->IsProtectedHandle(ref)) { | 486 ASSERT(!state->IsProtectedHandle(ref)); |
445 state->persistent_handles().FreeHandle(ref); | 487 if (!state->IsProtectedHandle(ref)) { |
446 } | 488 state->persistent_handles().FreeHandle(ref); |
447 } | 489 } |
448 } | 490 } |
449 | 491 |
450 | 492 |
451 DART_EXPORT bool Dart_IsWeakPersistentHandle(Dart_Handle object) { | 493 DART_EXPORT bool Dart_IsWeakPersistentHandle(Dart_Handle object) { |
452 Isolate* isolate = Isolate::Current(); | 494 Isolate* isolate = Isolate::Current(); |
453 CHECK_ISOLATE(isolate); | 495 CHECK_ISOLATE(isolate); |
454 ApiState* state = isolate->api_state(); | 496 ApiState* state = isolate->api_state(); |
455 ASSERT(state != NULL); | 497 ASSERT(state != NULL); |
456 return state->IsValidWeakPersistentHandle(object); | 498 return state->IsValidWeakPersistentHandle(object); |
457 } | 499 } |
458 | 500 |
459 | 501 |
| 502 DART_EXPORT bool Dart_IsPrologueWeakPersistentHandle(Dart_Handle object) { |
| 503 Isolate* isolate = Isolate::Current(); |
| 504 CHECK_ISOLATE(isolate); |
| 505 ApiState* state = isolate->api_state(); |
| 506 ASSERT(state != NULL); |
| 507 return state->IsValidPrologueWeakPersistentHandle(object); |
| 508 } |
| 509 |
| 510 |
460 DART_EXPORT Dart_Handle Dart_NewWeakReferenceSet(Dart_Handle* keys, | 511 DART_EXPORT Dart_Handle Dart_NewWeakReferenceSet(Dart_Handle* keys, |
461 intptr_t num_keys, | 512 intptr_t num_keys, |
462 Dart_Handle* values, | 513 Dart_Handle* values, |
463 intptr_t num_values) { | 514 intptr_t num_values) { |
464 Isolate* isolate = Isolate::Current(); | 515 Isolate* isolate = Isolate::Current(); |
465 CHECK_ISOLATE(isolate); | 516 CHECK_ISOLATE(isolate); |
466 ApiState* state = isolate->api_state(); | 517 ApiState* state = isolate->api_state(); |
467 ASSERT(state != NULL); | 518 ASSERT(state != NULL); |
468 if (keys == NULL) { | 519 if (keys == NULL) { |
469 return Api::NewError("%s expects argument 'keys' to be non-null.", | 520 return Api::NewError("%s expects argument 'keys' to be non-null.", |
(...skipping 2724 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3194 *buffer = NULL; | 3245 *buffer = NULL; |
3195 } | 3246 } |
3196 delete debug_region; | 3247 delete debug_region; |
3197 } else { | 3248 } else { |
3198 *buffer = NULL; | 3249 *buffer = NULL; |
3199 *buffer_size = 0; | 3250 *buffer_size = 0; |
3200 } | 3251 } |
3201 } | 3252 } |
3202 | 3253 |
3203 } // namespace dart | 3254 } // namespace dart |
OLD | NEW |