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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 RawExternalTwoByteString* raw_string = | 286 RawExternalTwoByteString* raw_string = |
287 reinterpret_cast<RawExternalTwoByteString*>(raw_obj)->ptr(); | 287 reinterpret_cast<RawExternalTwoByteString*>(raw_obj)->ptr(); |
288 ExternalStringData<uint16_t>* data = raw_string->external_data_; | 288 ExternalStringData<uint16_t>* data = raw_string->external_data_; |
289 *peer = data->peer(); | 289 *peer = data->peer(); |
290 return true; | 290 return true; |
291 } | 291 } |
292 return false; | 292 return false; |
293 } | 293 } |
294 | 294 |
295 | 295 |
| 296 bool Api::GetNativeReceiver(Dart_NativeArguments args, intptr_t* value) { |
| 297 NoGCScope no_gc_scope; |
| 298 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
| 299 RawObject* raw_obj = arguments->NativeArg0(); |
| 300 if (raw_obj->IsHeapObject()) { |
| 301 intptr_t cid = raw_obj->GetClassId(); |
| 302 if (cid > kNumPredefinedCids) { |
| 303 ASSERT(Instance::Cast(Object::Handle(raw_obj)).IsValidNativeIndex(0)); |
| 304 RawTypedData* native_fields = *reinterpret_cast<RawTypedData**>( |
| 305 RawObject::ToAddr(raw_obj) + sizeof(RawObject)); |
| 306 if (native_fields == TypedData::null()) { |
| 307 *value = 0; |
| 308 } else { |
| 309 *value = *bit_cast<intptr_t*, uint8_t*>(native_fields->ptr()->data_); |
| 310 } |
| 311 return true; |
| 312 } |
| 313 } |
| 314 return false; |
| 315 } |
| 316 |
| 317 |
296 bool Api::GetNativeBooleanArgument(Dart_NativeArguments args, | 318 bool Api::GetNativeBooleanArgument(Dart_NativeArguments args, |
297 int arg_index, | 319 int arg_index, |
298 bool* value) { | 320 bool* value) { |
299 NoGCScope no_gc_scope; | 321 NoGCScope no_gc_scope; |
300 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 322 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
301 RawObject* raw_obj = arguments->NativeArgAt(arg_index); | 323 RawObject* raw_obj = arguments->NativeArgAt(arg_index); |
302 if (raw_obj->IsHeapObject()) { | 324 if (raw_obj->IsHeapObject()) { |
303 intptr_t cid = raw_obj->GetClassId(); | 325 intptr_t cid = raw_obj->GetClassId(); |
304 if (cid == kBoolCid) { | 326 if (cid == kBoolCid) { |
305 *value = (raw_obj == Object::bool_true().raw()); | 327 *value = (raw_obj == Object::bool_true().raw()); |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 Isolate* isolate = Isolate::Current(); | 571 Isolate* isolate = Isolate::Current(); |
550 DARTSCOPE(isolate); | 572 DARTSCOPE(isolate); |
551 ApiState* state = isolate->api_state(); | 573 ApiState* state = isolate->api_state(); |
552 ASSERT(state != NULL); | 574 ASSERT(state != NULL); |
553 const Object& old_ref = Object::Handle(isolate, Api::UnwrapHandle(object)); | 575 const Object& old_ref = Object::Handle(isolate, Api::UnwrapHandle(object)); |
554 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle(); | 576 PersistentHandle* new_ref = state->persistent_handles().AllocateHandle(); |
555 new_ref->set_raw(old_ref); | 577 new_ref->set_raw(old_ref); |
556 return reinterpret_cast<Dart_PersistentHandle>(new_ref); | 578 return reinterpret_cast<Dart_PersistentHandle>(new_ref); |
557 } | 579 } |
558 | 580 |
| 581 |
| 582 DART_EXPORT void Dart_SetPersistentHandle(Dart_PersistentHandle obj1, |
| 583 Dart_Handle obj2) { |
| 584 Isolate* isolate = Isolate::Current(); |
| 585 DARTSCOPE(isolate); |
| 586 ApiState* state = isolate->api_state(); |
| 587 ASSERT(state != NULL); |
| 588 ASSERT(state->IsValidPersistentHandle(obj1)); |
| 589 const Object& obj2_ref = Object::Handle(isolate, Api::UnwrapHandle(obj2)); |
| 590 PersistentHandle* obj1_ref = Api::UnwrapAsPersistentHandle(obj1); |
| 591 obj1_ref->set_raw(obj2_ref); |
| 592 } |
| 593 |
| 594 |
559 static Dart_WeakPersistentHandle AllocateFinalizableHandle( | 595 static Dart_WeakPersistentHandle AllocateFinalizableHandle( |
560 Isolate* isolate, | 596 Isolate* isolate, |
561 FinalizablePersistentHandles* handles, | 597 FinalizablePersistentHandles* handles, |
562 Dart_Handle object, | 598 Dart_Handle object, |
563 void* peer, | 599 void* peer, |
564 Dart_WeakPersistentHandleFinalizer callback) { | 600 Dart_WeakPersistentHandleFinalizer callback) { |
565 const Object& ref = Object::Handle(isolate, Api::UnwrapHandle(object)); | 601 const Object& ref = Object::Handle(isolate, Api::UnwrapHandle(object)); |
566 FinalizablePersistentHandle* finalizable_ref = handles->AllocateHandle(); | 602 FinalizablePersistentHandle* finalizable_ref = handles->AllocateHandle(); |
567 finalizable_ref->set_raw(ref); | 603 finalizable_ref->set_raw(ref); |
568 finalizable_ref->set_peer(peer); | 604 finalizable_ref->set_peer(peer); |
(...skipping 3207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3776 *value = instance.GetNativeField(isolate, fld_index); | 3812 *value = instance.GetNativeField(isolate, fld_index); |
3777 return Api::Success(); | 3813 return Api::Success(); |
3778 } | 3814 } |
3779 | 3815 |
3780 | 3816 |
3781 DART_EXPORT Dart_Handle Dart_GetNativeReceiver(Dart_NativeArguments args, | 3817 DART_EXPORT Dart_Handle Dart_GetNativeReceiver(Dart_NativeArguments args, |
3782 intptr_t* value) { | 3818 intptr_t* value) { |
3783 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 3819 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
3784 Isolate* isolate = arguments->isolate(); | 3820 Isolate* isolate = arguments->isolate(); |
3785 CHECK_ISOLATE(isolate); | 3821 CHECK_ISOLATE(isolate); |
3786 ReusableObjectHandleScope reused_obj_handle(isolate); | |
3787 Object& obj = reused_obj_handle.Handle(); | |
3788 obj = arguments->NativeArg0(); | |
3789 intptr_t cid = obj.GetClassId(); | |
3790 if (cid <= kNumPredefinedCids) { | |
3791 if (cid == kNullCid) { | |
3792 return Api::NewError("%s expects receiver argument to be non-null.", | |
3793 CURRENT_FUNC); | |
3794 } | |
3795 return Api::NewError("%s expects receiver argument to be of" | |
3796 " type Instance.", CURRENT_FUNC); | |
3797 } | |
3798 const Instance& instance = Instance::Cast(obj); | |
3799 ASSERT(instance.IsValidNativeIndex(0)); | |
3800 if (value == NULL) { | 3822 if (value == NULL) { |
3801 RETURN_NULL_ERROR(value); | 3823 RETURN_NULL_ERROR(value); |
3802 } | 3824 } |
3803 *value = instance.GetNativeField(isolate, 0); | 3825 if (Api::GetNativeReceiver(args, value)) { |
3804 return Api::Success(); | 3826 return Api::Success(); |
| 3827 } |
| 3828 return Api::NewError("%s expects receiver argument to be non-null and of" |
| 3829 " type Instance.", CURRENT_FUNC); |
3805 } | 3830 } |
3806 | 3831 |
3807 | 3832 |
3808 DART_EXPORT Dart_Handle Dart_GetNativeStringArgument(Dart_NativeArguments args, | 3833 DART_EXPORT Dart_Handle Dart_GetNativeStringArgument(Dart_NativeArguments args, |
3809 int arg_index, | 3834 int arg_index, |
3810 void** peer) { | 3835 void** peer) { |
3811 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 3836 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
3812 Isolate* isolate = arguments->isolate(); | 3837 Isolate* isolate = arguments->isolate(); |
3813 CHECK_ISOLATE(isolate); | 3838 CHECK_ISOLATE(isolate); |
3814 if (Api::StringGetPeerHelper(args, arg_index, peer)) { | 3839 if (Api::StringGetPeerHelper(args, arg_index, peer)) { |
(...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4464 } | 4489 } |
4465 { | 4490 { |
4466 NoGCScope no_gc; | 4491 NoGCScope no_gc; |
4467 RawObject* raw_obj = obj.raw(); | 4492 RawObject* raw_obj = obj.raw(); |
4468 isolate->heap()->SetPeer(raw_obj, peer); | 4493 isolate->heap()->SetPeer(raw_obj, peer); |
4469 } | 4494 } |
4470 return Api::Success(); | 4495 return Api::Success(); |
4471 } | 4496 } |
4472 | 4497 |
4473 } // namespace dart | 4498 } // namespace dart |
OLD | NEW |