OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 #include "include/dart_mirrors_api.h" | 6 #include "include/dart_mirrors_api.h" |
7 #include "include/dart_native_api.h" | 7 #include "include/dart_native_api.h" |
8 | 8 |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 #include "vm/bigint_operations.h" | 10 #include "vm/bigint_operations.h" |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 const ReusableObjectHandleScope& reuse, Dart_Handle dart_handle) { | 148 const ReusableObjectHandleScope& reuse, Dart_Handle dart_handle) { |
149 Object& ref = reuse.Handle(); | 149 Object& ref = reuse.Handle(); |
150 ref = Api::UnwrapHandle(dart_handle); | 150 ref = Api::UnwrapHandle(dart_handle); |
151 if (ref.IsInstance()) { | 151 if (ref.IsInstance()) { |
152 return Instance::Cast(ref); | 152 return Instance::Cast(ref); |
153 } | 153 } |
154 return Object::null_instance(); | 154 return Object::null_instance(); |
155 } | 155 } |
156 | 156 |
157 | 157 |
158 PersistentHandle* Api::UnwrapAsPersistentHandle(Dart_PersistentHandle object) { | |
159 ASSERT(Isolate::Current()->api_state()->IsValidPersistentHandle(object)); | |
160 return reinterpret_cast<PersistentHandle*>(object); | |
161 } | |
162 | |
163 | |
164 FinalizablePersistentHandle* Api::UnwrapAsWeakPersistentHandle( | |
165 Dart_WeakPersistentHandle object) { | |
166 ASSERT(Isolate::Current()->api_state()->IsValidWeakPersistentHandle(object) || | |
167 Isolate::Current()->api_state()-> | |
168 IsValidPrologueWeakPersistentHandle(object)); | |
169 return reinterpret_cast<FinalizablePersistentHandle*>(object); | |
170 } | |
171 | |
172 | |
173 FinalizablePersistentHandle* Api::UnwrapAsPrologueWeakPersistentHandle( | |
174 Dart_WeakPersistentHandle object) { | |
175 ASSERT(Isolate::Current()->api_state()->IsValidPrologueWeakPersistentHandle( | |
176 object)); | |
177 return reinterpret_cast<FinalizablePersistentHandle*>(object); | |
178 } | |
179 | |
180 | |
181 Dart_Handle Api::CheckIsolateState(Isolate* isolate) { | 158 Dart_Handle Api::CheckIsolateState(Isolate* isolate) { |
182 if (!isolate->AllowClassFinalization()) { | 159 if (!isolate->AllowClassFinalization()) { |
183 // Class finalization is blocked for the isolate. Do nothing. | 160 // Class finalization is blocked for the isolate. Do nothing. |
184 return Api::Success(); | 161 return Api::Success(); |
185 } | 162 } |
186 if (ClassFinalizer::ProcessPendingClasses()) { | 163 if (ClassFinalizer::ProcessPendingClasses()) { |
187 return Api::Success(); | 164 return Api::Success(); |
188 } | 165 } |
189 ASSERT(isolate->object_store()->sticky_error() != Object::null()); | 166 ASSERT(isolate->object_store()->sticky_error() != Object::null()); |
190 return Api::NewHandle(isolate, isolate->object_store()->sticky_error()); | 167 return Api::NewHandle(isolate, isolate->object_store()->sticky_error()); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 *value = false; | 328 *value = false; |
352 return true; | 329 return true; |
353 } | 330 } |
354 } | 331 } |
355 return false; | 332 return false; |
356 } | 333 } |
357 | 334 |
358 | 335 |
359 void Api::SetWeakHandleReturnValue(NativeArguments* args, | 336 void Api::SetWeakHandleReturnValue(NativeArguments* args, |
360 Dart_WeakPersistentHandle retval) { | 337 Dart_WeakPersistentHandle retval) { |
361 args->SetReturnUnsafe(Api::UnwrapAsWeakPersistentHandle(retval)->raw()); | 338 args->SetReturnUnsafe(FinalizablePersistentHandle::Cast(retval)->raw()); |
| 339 } |
| 340 |
| 341 |
| 342 PersistentHandle* PersistentHandle::Cast(Dart_PersistentHandle handle) { |
| 343 ASSERT(Isolate::Current()->api_state()->IsValidPersistentHandle(handle)); |
| 344 return reinterpret_cast<PersistentHandle*>(handle); |
| 345 } |
| 346 |
| 347 |
| 348 FinalizablePersistentHandle* FinalizablePersistentHandle::Cast( |
| 349 Dart_WeakPersistentHandle handle) { |
| 350 #if defined(DEBUG) |
| 351 ApiState* state = Isolate::Current()->api_state(); |
| 352 ASSERT(state->IsValidWeakPersistentHandle(handle) || |
| 353 state->IsValidPrologueWeakPersistentHandle(handle)); |
| 354 #endif |
| 355 uword addr = reinterpret_cast<uword>(handle); |
| 356 return reinterpret_cast<FinalizablePersistentHandle*>( |
| 357 addr & ~kWeakPersistentTagMask); |
362 } | 358 } |
363 | 359 |
364 | 360 |
365 // --- Handles --- | 361 // --- Handles --- |
366 | 362 |
367 DART_EXPORT bool Dart_IsError(Dart_Handle handle) { | 363 DART_EXPORT bool Dart_IsError(Dart_Handle handle) { |
368 TRACE_API_CALL(CURRENT_FUNC); | 364 TRACE_API_CALL(CURRENT_FUNC); |
369 return RawObject::IsErrorClassId(Api::ClassId(handle)); | 365 return RawObject::IsErrorClassId(Api::ClassId(handle)); |
370 } | 366 } |
371 | 367 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 return false; | 557 return false; |
562 } | 558 } |
563 | 559 |
564 | 560 |
565 DART_EXPORT Dart_Handle Dart_HandleFromPersistent( | 561 DART_EXPORT Dart_Handle Dart_HandleFromPersistent( |
566 Dart_PersistentHandle object) { | 562 Dart_PersistentHandle object) { |
567 Isolate* isolate = Isolate::Current(); | 563 Isolate* isolate = Isolate::Current(); |
568 CHECK_ISOLATE(isolate); | 564 CHECK_ISOLATE(isolate); |
569 ApiState* state = isolate->api_state(); | 565 ApiState* state = isolate->api_state(); |
570 ASSERT(state != NULL); | 566 ASSERT(state != NULL); |
571 ASSERT(state->IsValidPersistentHandle(object)); | 567 PersistentHandle* ref = PersistentHandle::Cast(object); |
572 return Api::NewHandle(isolate, | 568 return Api::NewHandle(isolate, ref->raw()); |
573 reinterpret_cast<PersistentHandle*>(object)->raw()); | |
574 } | 569 } |
575 | 570 |
576 | 571 |
577 DART_EXPORT Dart_Handle Dart_HandleFromWeakPersistent( | 572 DART_EXPORT Dart_Handle Dart_HandleFromWeakPersistent( |
578 Dart_WeakPersistentHandle object) { | 573 Dart_WeakPersistentHandle object) { |
579 Isolate* isolate = Isolate::Current(); | 574 Isolate* isolate = Isolate::Current(); |
580 CHECK_ISOLATE(isolate); | 575 CHECK_ISOLATE(isolate); |
581 ApiState* state = isolate->api_state(); | 576 ApiState* state = isolate->api_state(); |
582 ASSERT(state != NULL); | 577 ASSERT(state != NULL); |
583 ASSERT(state->IsValidWeakPersistentHandle(object) || | 578 FinalizablePersistentHandle* weak_ref = |
584 state->IsValidPrologueWeakPersistentHandle(object)); | 579 FinalizablePersistentHandle::Cast(object); |
585 return Api::NewHandle( | 580 return Api::NewHandle(isolate, weak_ref->raw()); |
586 isolate, reinterpret_cast<FinalizablePersistentHandle*>(object)->raw()); | |
587 } | 581 } |
588 | 582 |
589 | 583 |
590 DART_EXPORT Dart_PersistentHandle Dart_NewPersistentHandle(Dart_Handle object) { | 584 DART_EXPORT Dart_PersistentHandle Dart_NewPersistentHandle(Dart_Handle object) { |
591 Isolate* isolate = Isolate::Current(); | 585 Isolate* isolate = Isolate::Current(); |
592 DARTSCOPE(isolate); | 586 DARTSCOPE(isolate); |
593 ApiState* state = isolate->api_state(); | 587 ApiState* state = isolate->api_state(); |
594 ASSERT(state != NULL); | 588 ASSERT(state != NULL); |
595 const Object& old_ref = Object::Handle(isolate, Api::UnwrapHandle(object)); | 589 const Object& old_ref = Object::Handle(isolate, Api::UnwrapHandle(object)); |
596 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle(); | 590 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle(); |
597 new_ref->set_raw(old_ref); | 591 new_ref->set_raw(old_ref); |
598 return reinterpret_cast<Dart_PersistentHandle>(new_ref); | 592 return new_ref->apiHandle(); |
599 } | 593 } |
600 | 594 |
601 | 595 |
602 DART_EXPORT void Dart_SetPersistentHandle(Dart_PersistentHandle obj1, | 596 DART_EXPORT void Dart_SetPersistentHandle(Dart_PersistentHandle obj1, |
603 Dart_Handle obj2) { | 597 Dart_Handle obj2) { |
604 Isolate* isolate = Isolate::Current(); | 598 Isolate* isolate = Isolate::Current(); |
605 DARTSCOPE(isolate); | 599 DARTSCOPE(isolate); |
606 ApiState* state = isolate->api_state(); | 600 ApiState* state = isolate->api_state(); |
607 ASSERT(state != NULL); | 601 ASSERT(state != NULL); |
608 ASSERT(state->IsValidPersistentHandle(obj1)); | 602 ASSERT(state->IsValidPersistentHandle(obj1)); |
609 const Object& obj2_ref = Object::Handle(isolate, Api::UnwrapHandle(obj2)); | 603 const Object& obj2_ref = Object::Handle(isolate, Api::UnwrapHandle(obj2)); |
610 PersistentHandle* obj1_ref = Api::UnwrapAsPersistentHandle(obj1); | 604 PersistentHandle* obj1_ref = PersistentHandle::Cast(obj1); |
611 obj1_ref->set_raw(obj2_ref); | 605 obj1_ref->set_raw(obj2_ref); |
612 } | 606 } |
613 | 607 |
614 | 608 |
615 static Dart_WeakPersistentHandle AllocateFinalizableHandle( | 609 static Dart_WeakPersistentHandle AllocateFinalizableHandle( |
616 Isolate* isolate, | 610 Isolate* isolate, |
617 FinalizablePersistentHandles* handles, | |
618 Dart_Handle object, | 611 Dart_Handle object, |
| 612 bool is_prologue, |
619 void* peer, | 613 void* peer, |
620 Dart_WeakPersistentHandleFinalizer callback) { | 614 Dart_WeakPersistentHandleFinalizer callback) { |
| 615 ApiState* state = isolate->api_state(); |
| 616 ASSERT(state != NULL); |
621 ReusableObjectHandleScope reused_obj_handle(isolate); | 617 ReusableObjectHandleScope reused_obj_handle(isolate); |
622 Object& ref = reused_obj_handle.Handle(); | 618 Object& ref = reused_obj_handle.Handle(); |
623 ref = Api::UnwrapHandle(object); | 619 ref = Api::UnwrapHandle(object); |
624 FinalizablePersistentHandle* finalizable_ref = handles->AllocateHandle(); | 620 FinalizablePersistentHandle* finalizable_ref = is_prologue ? |
| 621 state->prologue_weak_persistent_handles().AllocateHandle() : |
| 622 state->weak_persistent_handles().AllocateHandle(); |
| 623 |
625 finalizable_ref->set_raw(ref); | 624 finalizable_ref->set_raw(ref); |
626 finalizable_ref->set_peer(peer); | 625 finalizable_ref->set_peer(peer); |
627 finalizable_ref->set_callback(callback); | 626 finalizable_ref->set_callback(callback); |
628 return reinterpret_cast<Dart_WeakPersistentHandle>(finalizable_ref); | 627 if (is_prologue) { |
| 628 return finalizable_ref->apiPrologueHandle(); |
| 629 } else { |
| 630 return finalizable_ref->apiHandle(); |
| 631 } |
629 } | 632 } |
630 | 633 |
631 | 634 |
632 DART_EXPORT Dart_WeakPersistentHandle Dart_NewWeakPersistentHandle( | 635 DART_EXPORT Dart_WeakPersistentHandle Dart_NewWeakPersistentHandle( |
633 Dart_Handle object, | 636 Dart_Handle object, |
634 void* peer, | 637 void* peer, |
635 Dart_WeakPersistentHandleFinalizer callback) { | 638 Dart_WeakPersistentHandleFinalizer callback) { |
636 Isolate* isolate = Isolate::Current(); | 639 Isolate* isolate = Isolate::Current(); |
637 CHECK_ISOLATE(isolate); | 640 CHECK_ISOLATE(isolate); |
638 ApiState* state = isolate->api_state(); | |
639 ASSERT(state != NULL); | |
640 return AllocateFinalizableHandle(isolate, | 641 return AllocateFinalizableHandle(isolate, |
641 &state->weak_persistent_handles(), | |
642 object, | 642 object, |
| 643 false, |
643 peer, | 644 peer, |
644 callback); | 645 callback); |
645 } | 646 } |
646 | 647 |
647 | 648 |
648 DART_EXPORT Dart_WeakPersistentHandle Dart_NewPrologueWeakPersistentHandle( | 649 DART_EXPORT Dart_WeakPersistentHandle Dart_NewPrologueWeakPersistentHandle( |
649 Dart_Handle object, | 650 Dart_Handle object, |
650 void* peer, | 651 void* peer, |
651 Dart_WeakPersistentHandleFinalizer callback) { | 652 Dart_WeakPersistentHandleFinalizer callback) { |
652 Isolate* isolate = Isolate::Current(); | 653 Isolate* isolate = Isolate::Current(); |
653 CHECK_ISOLATE(isolate); | 654 CHECK_ISOLATE(isolate); |
654 ApiState* state = isolate->api_state(); | |
655 ASSERT(state != NULL); | |
656 return AllocateFinalizableHandle(isolate, | 655 return AllocateFinalizableHandle(isolate, |
657 &state->prologue_weak_persistent_handles(), | |
658 object, | 656 object, |
| 657 true, |
659 peer, | 658 peer, |
660 callback); | 659 callback); |
661 } | 660 } |
662 | 661 |
663 | 662 |
664 DART_EXPORT void Dart_DeletePersistentHandle(Dart_PersistentHandle object) { | 663 DART_EXPORT void Dart_DeletePersistentHandle(Dart_PersistentHandle object) { |
665 Isolate* isolate = Isolate::Current(); | 664 Isolate* isolate = Isolate::Current(); |
666 CHECK_ISOLATE(isolate); | 665 CHECK_ISOLATE(isolate); |
667 ApiState* state = isolate->api_state(); | 666 ApiState* state = isolate->api_state(); |
668 ASSERT(state != NULL); | 667 ASSERT(state != NULL); |
669 PersistentHandle* ref = Api::UnwrapAsPersistentHandle(object); | 668 PersistentHandle* ref = PersistentHandle::Cast(object); |
670 ASSERT(!state->IsProtectedHandle(ref)); | 669 ASSERT(!state->IsProtectedHandle(ref)); |
671 if (!state->IsProtectedHandle(ref)) { | 670 if (!state->IsProtectedHandle(ref)) { |
672 state->persistent_handles().FreeHandle(ref); | 671 state->persistent_handles().FreeHandle(ref); |
673 } | 672 } |
674 } | 673 } |
675 | 674 |
676 | 675 |
677 DART_EXPORT void Dart_DeleteWeakPersistentHandle( | 676 DART_EXPORT void Dart_DeleteWeakPersistentHandle( |
678 Dart_WeakPersistentHandle object) { | 677 Dart_WeakPersistentHandle object) { |
679 Isolate* isolate = Isolate::Current(); | 678 Isolate* isolate = Isolate::Current(); |
680 CHECK_ISOLATE(isolate); | 679 CHECK_ISOLATE(isolate); |
681 ApiState* state = isolate->api_state(); | 680 ApiState* state = isolate->api_state(); |
682 ASSERT(state != NULL); | 681 ASSERT(state != NULL); |
683 if (state->IsValidPrologueWeakPersistentHandle(object)) { | 682 if (FinalizablePersistentHandle::IsPrologueWeakPersistentHandle(object)) { |
684 FinalizablePersistentHandle* prologue_weak_ref = | 683 ASSERT(state->IsValidPrologueWeakPersistentHandle(object)); |
685 Api::UnwrapAsPrologueWeakPersistentHandle(object); | 684 FinalizablePersistentHandle* weak_ref = |
686 state->prologue_weak_persistent_handles().FreeHandle(prologue_weak_ref); | 685 FinalizablePersistentHandle::Cast(object); |
687 return; | 686 state->prologue_weak_persistent_handles().FreeHandle(weak_ref); |
| 687 } else { |
| 688 ASSERT(!state->IsValidPrologueWeakPersistentHandle(object)); |
| 689 FinalizablePersistentHandle* weak_ref = |
| 690 FinalizablePersistentHandle::Cast(object); |
| 691 state->weak_persistent_handles().FreeHandle(weak_ref); |
688 } | 692 } |
689 FinalizablePersistentHandle* weak_ref = | |
690 Api::UnwrapAsWeakPersistentHandle(object); | |
691 state->weak_persistent_handles().FreeHandle(weak_ref); | |
692 return; | |
693 } | 693 } |
694 | 694 |
695 | 695 |
696 DART_EXPORT bool Dart_IsPrologueWeakPersistentHandle( | 696 DART_EXPORT bool Dart_IsPrologueWeakPersistentHandle( |
697 Dart_WeakPersistentHandle object) { | 697 Dart_WeakPersistentHandle object) { |
| 698 #if defined(DEBUG) |
698 Isolate* isolate = Isolate::Current(); | 699 Isolate* isolate = Isolate::Current(); |
699 CHECK_ISOLATE(isolate); | 700 CHECK_ISOLATE(isolate); |
700 ApiState* state = isolate->api_state(); | 701 ApiState* state = isolate->api_state(); |
701 ASSERT(state != NULL); | 702 ASSERT(state != NULL); |
702 return state->IsValidPrologueWeakPersistentHandle(object); | 703 ASSERT(state->IsValidPrologueWeakPersistentHandle(object) == |
| 704 FinalizablePersistentHandle::IsPrologueWeakPersistentHandle(object)); |
| 705 #endif |
| 706 return FinalizablePersistentHandle::IsPrologueWeakPersistentHandle(object); |
703 } | 707 } |
704 | 708 |
705 | 709 |
706 DART_EXPORT Dart_Handle Dart_NewWeakReferenceSet( | 710 DART_EXPORT Dart_Handle Dart_NewWeakReferenceSet( |
707 Dart_WeakPersistentHandle* keys, | 711 Dart_WeakPersistentHandle* keys, |
708 intptr_t num_keys, | 712 intptr_t num_keys, |
709 Dart_WeakPersistentHandle* values, | 713 Dart_WeakPersistentHandle* values, |
710 intptr_t num_values) { | 714 intptr_t num_values) { |
711 Isolate* isolate = Isolate::Current(); | 715 Isolate* isolate = Isolate::Current(); |
712 CHECK_ISOLATE(isolate); | 716 CHECK_ISOLATE(isolate); |
(...skipping 3894 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4607 | 4611 |
4608 | 4612 |
4609 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( | 4613 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( |
4610 const char* name, | 4614 const char* name, |
4611 Dart_ServiceRequestCallback callback, | 4615 Dart_ServiceRequestCallback callback, |
4612 void* user_data) { | 4616 void* user_data) { |
4613 Service::RegisterRootEmbedderCallback(name, callback, user_data); | 4617 Service::RegisterRootEmbedderCallback(name, callback, user_data); |
4614 } | 4618 } |
4615 | 4619 |
4616 } // namespace dart | 4620 } // namespace dart |
OLD | NEW |