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 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "vm/bigint_operations.h" | 8 #include "vm/bigint_operations.h" |
9 #include "vm/class_finalizer.h" | 9 #include "vm/class_finalizer.h" |
10 #include "vm/compiler.h" | 10 #include "vm/compiler.h" |
(...skipping 2311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2322 } | 2322 } |
2323 | 2323 |
2324 | 2324 |
2325 // --- Typed Data --- | 2325 // --- Typed Data --- |
2326 | 2326 |
2327 | 2327 |
2328 // Helper method to get the type of a TypedData object. | 2328 // Helper method to get the type of a TypedData object. |
2329 static Dart_TypedData_Type GetType(intptr_t class_id) { | 2329 static Dart_TypedData_Type GetType(intptr_t class_id) { |
2330 Dart_TypedData_Type type; | 2330 Dart_TypedData_Type type; |
2331 switch (class_id) { | 2331 switch (class_id) { |
| 2332 case kByteDataViewCid : |
| 2333 type = kByteData; |
| 2334 break; |
2332 case kTypedDataInt8ArrayCid : | 2335 case kTypedDataInt8ArrayCid : |
| 2336 case kTypedDataInt8ArrayViewCid : |
2333 case kExternalTypedDataInt8ArrayCid : | 2337 case kExternalTypedDataInt8ArrayCid : |
2334 type = kInt8; | 2338 type = kInt8; |
2335 break; | 2339 break; |
2336 case kTypedDataUint8ArrayCid : | 2340 case kTypedDataUint8ArrayCid : |
| 2341 case kTypedDataUint8ArrayViewCid : |
2337 case kExternalTypedDataUint8ArrayCid : | 2342 case kExternalTypedDataUint8ArrayCid : |
2338 type = kUint8; | 2343 type = kUint8; |
2339 break; | 2344 break; |
2340 case kTypedDataUint8ClampedArrayCid : | 2345 case kTypedDataUint8ClampedArrayCid : |
| 2346 case kTypedDataUint8ClampedArrayViewCid : |
2341 case kExternalTypedDataUint8ClampedArrayCid : | 2347 case kExternalTypedDataUint8ClampedArrayCid : |
2342 type = kUint8Clamped; | 2348 type = kUint8Clamped; |
2343 break; | 2349 break; |
2344 case kTypedDataInt16ArrayCid : | 2350 case kTypedDataInt16ArrayCid : |
| 2351 case kTypedDataInt16ArrayViewCid : |
2345 case kExternalTypedDataInt16ArrayCid : | 2352 case kExternalTypedDataInt16ArrayCid : |
2346 type = kInt16; | 2353 type = kInt16; |
2347 break; | 2354 break; |
2348 case kTypedDataUint16ArrayCid : | 2355 case kTypedDataUint16ArrayCid : |
| 2356 case kTypedDataUint16ArrayViewCid : |
2349 case kExternalTypedDataUint16ArrayCid : | 2357 case kExternalTypedDataUint16ArrayCid : |
2350 type = kUint16; | 2358 type = kUint16; |
2351 break; | 2359 break; |
2352 case kTypedDataInt32ArrayCid : | 2360 case kTypedDataInt32ArrayCid : |
| 2361 case kTypedDataInt32ArrayViewCid : |
2353 case kExternalTypedDataInt32ArrayCid : | 2362 case kExternalTypedDataInt32ArrayCid : |
2354 type = kInt32; | 2363 type = kInt32; |
2355 break; | 2364 break; |
2356 case kTypedDataUint32ArrayCid : | 2365 case kTypedDataUint32ArrayCid : |
| 2366 case kTypedDataUint32ArrayViewCid : |
2357 case kExternalTypedDataUint32ArrayCid : | 2367 case kExternalTypedDataUint32ArrayCid : |
2358 type = kUint32; | 2368 type = kUint32; |
2359 break; | 2369 break; |
2360 case kTypedDataInt64ArrayCid : | 2370 case kTypedDataInt64ArrayCid : |
| 2371 case kTypedDataInt64ArrayViewCid : |
2361 case kExternalTypedDataInt64ArrayCid : | 2372 case kExternalTypedDataInt64ArrayCid : |
2362 type = kInt64; | 2373 type = kInt64; |
2363 break; | 2374 break; |
2364 case kTypedDataUint64ArrayCid : | 2375 case kTypedDataUint64ArrayCid : |
| 2376 case kTypedDataUint64ArrayViewCid : |
2365 case kExternalTypedDataUint64ArrayCid : | 2377 case kExternalTypedDataUint64ArrayCid : |
2366 type = kUint64; | 2378 type = kUint64; |
2367 break; | 2379 break; |
2368 case kTypedDataFloat32ArrayCid : | 2380 case kTypedDataFloat32ArrayCid : |
| 2381 case kTypedDataFloat32ArrayViewCid : |
2369 case kExternalTypedDataFloat32ArrayCid : | 2382 case kExternalTypedDataFloat32ArrayCid : |
2370 type = kFloat32; | 2383 type = kFloat32; |
2371 break; | 2384 break; |
2372 case kTypedDataFloat64ArrayCid : | 2385 case kTypedDataFloat64ArrayCid : |
| 2386 case kTypedDataFloat64ArrayViewCid : |
2373 case kExternalTypedDataFloat64ArrayCid : | 2387 case kExternalTypedDataFloat64ArrayCid : |
2374 type = kFloat64; | 2388 type = kFloat64; |
2375 break; | 2389 break; |
2376 default: | 2390 default: |
2377 type = kInvalid; | 2391 type = kInvalid; |
2378 break; | 2392 break; |
2379 } | 2393 } |
2380 return type; | 2394 return type; |
2381 } | 2395 } |
2382 | 2396 |
2383 | 2397 |
2384 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfTypedData(Dart_Handle object) { | 2398 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfTypedData(Dart_Handle object) { |
2385 intptr_t class_id = Api::ClassId(object); | 2399 intptr_t class_id = Api::ClassId(object); |
2386 if (RawObject::IsTypedDataClassId(class_id)) { | 2400 if (RawObject::IsTypedDataClassId(class_id) || |
| 2401 RawObject::IsTypedDataViewClassId(class_id)) { |
2387 return GetType(class_id); | 2402 return GetType(class_id); |
2388 } | 2403 } |
2389 Isolate* isolate = Isolate::Current(); | |
2390 const Library& lib = | |
2391 Library::Handle(isolate->object_store()->typeddata_library()); | |
2392 const Class& cls = | |
2393 Class::Handle(isolate, | |
2394 lib.LookupClassAllowPrivate(Symbols::_ByteDataView())); | |
2395 if (isolate->class_table()->At(class_id) == cls.raw()) { | |
2396 return kByteData; | |
2397 } | |
2398 return kInvalid; | 2404 return kInvalid; |
2399 } | 2405 } |
2400 | 2406 |
2401 | 2407 |
2402 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfExternalTypedData( | 2408 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfExternalTypedData( |
2403 Dart_Handle object) { | 2409 Dart_Handle object) { |
2404 intptr_t class_id = Api::ClassId(object); | 2410 intptr_t class_id = Api::ClassId(object); |
2405 if (RawObject::IsExternalTypedDataClassId(class_id)) { | 2411 if (RawObject::IsExternalTypedDataClassId(class_id) || |
| 2412 RawObject::IsTypedDataViewClassId(class_id)) { |
2406 return GetType(class_id); | 2413 return GetType(class_id); |
2407 } | 2414 } |
2408 Isolate* isolate = Isolate::Current(); | |
2409 const Library& lib = | |
2410 Library::Handle(isolate->object_store()->typeddata_library()); | |
2411 const Class& cls = | |
2412 Class::Handle(isolate, | |
2413 lib.LookupClassAllowPrivate(Symbols::_ByteDataView())); | |
2414 if (isolate->class_table()->At(class_id) == cls.raw()) { | |
2415 return kByteData; | |
2416 } | |
2417 return kInvalid; | 2415 return kInvalid; |
2418 } | 2416 } |
2419 | 2417 |
2420 | 2418 |
2421 static RawObject* GetByteDataConstructor(Isolate* isolate, | 2419 static RawObject* GetByteDataConstructor(Isolate* isolate, |
2422 const String& constructor_name, | 2420 const String& constructor_name, |
2423 intptr_t num_args) { | 2421 intptr_t num_args) { |
2424 const Library& lib = | 2422 const Library& lib = |
2425 Library::Handle(isolate->object_store()->typeddata_library()); | 2423 Library::Handle(isolate->object_store()->typeddata_library()); |
2426 ASSERT(!lib.IsNull()); | 2424 ASSERT(!lib.IsNull()); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2668 | 2666 |
2669 | 2667 |
2670 DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object, | 2668 DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object, |
2671 Dart_TypedData_Type* type, | 2669 Dart_TypedData_Type* type, |
2672 void** data, | 2670 void** data, |
2673 intptr_t* len) { | 2671 intptr_t* len) { |
2674 Isolate* isolate = Isolate::Current(); | 2672 Isolate* isolate = Isolate::Current(); |
2675 DARTSCOPE(isolate); | 2673 DARTSCOPE(isolate); |
2676 intptr_t class_id = Api::ClassId(object); | 2674 intptr_t class_id = Api::ClassId(object); |
2677 if (!RawObject::IsExternalTypedDataClassId(class_id) && | 2675 if (!RawObject::IsExternalTypedDataClassId(class_id) && |
| 2676 !RawObject::IsTypedDataViewClassId(class_id) && |
2678 !RawObject::IsTypedDataClassId(class_id)) { | 2677 !RawObject::IsTypedDataClassId(class_id)) { |
2679 RETURN_TYPE_ERROR(isolate, object, 'TypedData'); | 2678 RETURN_TYPE_ERROR(isolate, object, 'TypedData'); |
2680 } | 2679 } |
2681 if (type == NULL) { | 2680 if (type == NULL) { |
2682 RETURN_NULL_ERROR(type); | 2681 RETURN_NULL_ERROR(type); |
2683 } | 2682 } |
2684 if (data == NULL) { | 2683 if (data == NULL) { |
2685 RETURN_NULL_ERROR(data); | 2684 RETURN_NULL_ERROR(data); |
2686 } | 2685 } |
2687 if (len == NULL) { | 2686 if (len == NULL) { |
2688 RETURN_NULL_ERROR(len); | 2687 RETURN_NULL_ERROR(len); |
2689 } | 2688 } |
2690 // Get the type of typed data object. | 2689 // Get the type of typed data object. |
2691 *type = GetType(class_id); | 2690 *type = GetType(class_id); |
2692 // If it is an external typed data object just return the data field. | 2691 // If it is an external typed data object just return the data field. |
2693 if (RawObject::IsExternalTypedDataClassId(class_id)) { | 2692 if (RawObject::IsExternalTypedDataClassId(class_id)) { |
2694 const ExternalTypedData& obj = | 2693 const ExternalTypedData& obj = |
2695 Api::UnwrapExternalTypedDataHandle(isolate, object); | 2694 Api::UnwrapExternalTypedDataHandle(isolate, object); |
2696 ASSERT(!obj.IsNull()); | 2695 ASSERT(!obj.IsNull()); |
2697 *len = obj.Length(); | 2696 *len = obj.Length(); |
2698 *data = reinterpret_cast<void*>(obj.DataAddr(0)); | 2697 *data = obj.DataAddr(0); |
2699 } else { | 2698 } else if (RawObject::IsTypedDataClassId(class_id)) { |
2700 // Regular typed data object, set up some GC and API callback guards. | 2699 // Regular typed data object, set up some GC and API callback guards. |
2701 const TypedData& obj = Api::UnwrapTypedDataHandle(isolate, object); | 2700 const TypedData& obj = Api::UnwrapTypedDataHandle(isolate, object); |
2702 ASSERT(!obj.IsNull()); | 2701 ASSERT(!obj.IsNull()); |
2703 *len = obj.Length(); | 2702 *len = obj.Length(); |
2704 isolate->IncrementNoGCScopeDepth(); | 2703 isolate->IncrementNoGCScopeDepth(); |
2705 START_NO_CALLBACK_SCOPE(isolate); | 2704 START_NO_CALLBACK_SCOPE(isolate); |
2706 *data = reinterpret_cast<void*>(obj.DataAddr(0)); | 2705 *data = obj.DataAddr(0); |
| 2706 } else { |
| 2707 // typed data view object, set up some GC and API callback guards. |
| 2708 // TODO(asiva): Have to come up with a scheme to directly access |
| 2709 // the fields using offsets for a more efficient implementation. |
| 2710 Dart_Handle field_name = Dart_NewStringFromCString("length"); |
| 2711 Dart_Handle field_value = Dart_GetField(object, field_name); |
| 2712 ASSERT(Api::IsSmi(field_value)); |
| 2713 *len = Api::SmiValue(field_value); |
| 2714 field_name = Dart_NewStringFromCString("offsetInBytes"); |
| 2715 field_value = Dart_GetField(object, field_name); |
| 2716 ASSERT(Api::IsSmi(field_value)); |
| 2717 intptr_t offset_in_bytes = Api::SmiValue(field_value); |
| 2718 field_name = Dart_NewStringFromCString("_typeddata"); |
| 2719 field_value = Dart_GetField(object, field_name); |
| 2720 const TypedData& obj = Api::UnwrapTypedDataHandle(isolate, field_value); |
| 2721 isolate->IncrementNoGCScopeDepth(); |
| 2722 START_NO_CALLBACK_SCOPE(isolate); |
| 2723 *data = obj.DataAddr(offset_in_bytes); |
2707 } | 2724 } |
2708 return Api::Success(isolate); | 2725 return Api::Success(isolate); |
2709 } | 2726 } |
2710 | 2727 |
2711 | 2728 |
2712 DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object) { | 2729 DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object) { |
2713 Isolate* isolate = Isolate::Current(); | 2730 Isolate* isolate = Isolate::Current(); |
2714 DARTSCOPE(isolate); | 2731 DARTSCOPE(isolate); |
2715 intptr_t class_id = Api::ClassId(object); | 2732 intptr_t class_id = Api::ClassId(object); |
2716 if (!RawObject::IsExternalTypedDataClassId(class_id) && | 2733 if (!RawObject::IsExternalTypedDataClassId(class_id) && |
| 2734 !RawObject::IsTypedDataViewClassId(class_id) && |
2717 !RawObject::IsTypedDataClassId(class_id)) { | 2735 !RawObject::IsTypedDataClassId(class_id)) { |
2718 RETURN_TYPE_ERROR(isolate, object, 'TypedData'); | 2736 RETURN_TYPE_ERROR(isolate, object, 'TypedData'); |
2719 } | 2737 } |
2720 if (!RawObject::IsExternalTypedDataClassId(class_id)) { | 2738 if (!RawObject::IsExternalTypedDataClassId(class_id)) { |
2721 isolate->DecrementNoGCScopeDepth(); | 2739 isolate->DecrementNoGCScopeDepth(); |
2722 END_NO_CALLBACK_SCOPE(isolate); | 2740 END_NO_CALLBACK_SCOPE(isolate); |
2723 } | 2741 } |
2724 return Api::Success(isolate); | 2742 return Api::Success(isolate); |
2725 } | 2743 } |
2726 | 2744 |
(...skipping 2056 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4783 } | 4801 } |
4784 { | 4802 { |
4785 NoGCScope no_gc; | 4803 NoGCScope no_gc; |
4786 RawObject* raw_obj = obj.raw(); | 4804 RawObject* raw_obj = obj.raw(); |
4787 isolate->heap()->SetPeer(raw_obj, peer); | 4805 isolate->heap()->SetPeer(raw_obj, peer); |
4788 } | 4806 } |
4789 return Api::Success(isolate); | 4807 return Api::Success(isolate); |
4790 } | 4808 } |
4791 | 4809 |
4792 } // namespace dart | 4810 } // namespace dart |
OLD | NEW |