Chromium Code Reviews| 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 2385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2396 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfExternalTypedData( | 2396 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfExternalTypedData( |
| 2397 Dart_Handle object) { | 2397 Dart_Handle object) { |
| 2398 intptr_t class_id = Api::ClassId(object); | 2398 intptr_t class_id = Api::ClassId(object); |
| 2399 if (!RawObject::IsExternalTypedDataClassId(class_id)) { | 2399 if (!RawObject::IsExternalTypedDataClassId(class_id)) { |
| 2400 return kInvalid; | 2400 return kInvalid; |
| 2401 } | 2401 } |
| 2402 return GetType(class_id); | 2402 return GetType(class_id); |
| 2403 } | 2403 } |
| 2404 | 2404 |
| 2405 | 2405 |
| 2406 static RawObject* GetByteDataConstructor(Isolate* isolate, | |
| 2407 const String& constructor_name, | |
| 2408 intptr_t num_args) { | |
| 2409 const String& cls_name = | |
| 2410 String::Handle(isolate, OneByteString::New("ByteData")); | |
|
Mads Ager (google)
2013/03/22 08:29:09
Does it make sense to have symbols for these strin
siva
2013/03/22 18:03:03
Yes, added "ByteData" and "view" to the symbols li
| |
| 2411 const Library& lib = | |
| 2412 Library::Handle(isolate->object_store()->typeddata_library()); | |
| 2413 ASSERT(!lib.IsNull()); | |
| 2414 const Class& cls = | |
| 2415 Class::Handle(isolate, lib.LookupClassAllowPrivate(cls_name)); | |
| 2416 ASSERT(!cls.IsNull()); | |
| 2417 return ResolveConstructor(CURRENT_FUNC, | |
| 2418 cls, cls_name, | |
| 2419 constructor_name, num_args); | |
| 2420 } | |
| 2421 | |
| 2422 | |
| 2423 static Dart_Handle NewByteData(Isolate* isolate, intptr_t length) { | |
| 2424 CHECK_LENGTH(length, TypedData::MaxElements(kTypedDataInt8ArrayCid)); | |
| 2425 Object& result = Object::Handle(isolate); | |
| 2426 result = GetByteDataConstructor(isolate, Symbols::Dot(), 1); | |
| 2427 ASSERT(!result.IsNull()); | |
| 2428 ASSERT(result.IsFunction()); | |
| 2429 const Function& factory = Function::Cast(result); | |
| 2430 ASSERT(!factory.IsConstructor()); | |
| 2431 | |
| 2432 // Create the argument list. | |
| 2433 const Array& args = Array::Handle(isolate, Array::New(2)); | |
| 2434 // Factories get type arguments. | |
| 2435 args.SetAt(0, TypeArguments::Handle(isolate)); | |
| 2436 args.SetAt(1, Smi::Handle(isolate, Smi::New(length))); | |
| 2437 | |
| 2438 // Invoke the constructor and return the new object. | |
| 2439 result = DartEntry::InvokeFunction(factory, args); | |
| 2440 ASSERT(result.IsInstance() || result.IsNull() || result.IsError()); | |
| 2441 return Api::NewHandle(isolate, result.raw()); | |
| 2442 } | |
| 2443 | |
| 2444 | |
| 2406 static Dart_Handle NewTypedData(Isolate* isolate, | 2445 static Dart_Handle NewTypedData(Isolate* isolate, |
| 2407 intptr_t cid, | 2446 intptr_t cid, |
| 2408 intptr_t length) { | 2447 intptr_t length) { |
| 2409 CHECK_LENGTH(length, TypedData::MaxElements(cid)); | 2448 CHECK_LENGTH(length, TypedData::MaxElements(cid)); |
| 2410 return Api::NewHandle(isolate, TypedData::New(cid, length)); | 2449 return Api::NewHandle(isolate, TypedData::New(cid, length)); |
| 2411 } | 2450 } |
| 2412 | 2451 |
| 2413 | 2452 |
| 2414 static Dart_Handle NewExternalTypedData( | 2453 static Dart_Handle NewExternalTypedData( |
| 2415 intptr_t cid, | 2454 intptr_t cid, |
| 2416 void* data, | 2455 void* data, |
| 2417 intptr_t length, | 2456 intptr_t length, |
| 2418 void* peer, | 2457 void* peer, |
| 2419 Dart_WeakPersistentHandleFinalizer callback) { | 2458 Dart_WeakPersistentHandleFinalizer callback) { |
| 2420 CHECK_LENGTH(length, ExternalTypedData::MaxElements(cid)); | 2459 CHECK_LENGTH(length, ExternalTypedData::MaxElements(cid)); |
| 2421 const ExternalTypedData& obj = ExternalTypedData::Handle( | 2460 const ExternalTypedData& obj = ExternalTypedData::Handle( |
| 2422 ExternalTypedData::New(cid, | 2461 ExternalTypedData::New(cid, |
| 2423 reinterpret_cast<uint8_t*>(data), | 2462 reinterpret_cast<uint8_t*>(data), |
| 2424 length)); | 2463 length)); |
| 2425 return reinterpret_cast<Dart_Handle>(obj.AddFinalizer(peer, callback)); | 2464 return reinterpret_cast<Dart_Handle>(obj.AddFinalizer(peer, callback)); |
| 2426 } | 2465 } |
| 2427 | 2466 |
| 2428 | 2467 |
| 2468 static Dart_Handle NewExternalByteData(Isolate* isolate, | |
| 2469 void* data, | |
| 2470 intptr_t length, | |
| 2471 void* peer, | |
| 2472 Dart_WeakPersistentHandleFinalizer cb) { | |
| 2473 Dart_Handle ext_data = NewExternalTypedData(kExternalTypedDataUint8ArrayCid, | |
| 2474 data, | |
| 2475 length, | |
| 2476 peer, | |
| 2477 cb); | |
| 2478 if (::Dart_IsError(ext_data)) { | |
| 2479 return ext_data; | |
| 2480 } | |
| 2481 Object& result = Object::Handle(isolate); | |
| 2482 const String& dot_name = String::Handle(String::New(".view")); | |
|
Mads Ager (google)
2013/03/22 08:29:09
Ditto for this one? Alternatively, should this be
siva
2013/03/22 18:03:03
Done.
| |
| 2483 result = GetByteDataConstructor(isolate, dot_name, 3); | |
| 2484 ASSERT(!result.IsNull()); | |
| 2485 ASSERT(result.IsFunction()); | |
| 2486 const Function& factory = Function::Cast(result); | |
| 2487 ASSERT(!factory.IsConstructor()); | |
| 2488 | |
| 2489 // Create the argument list. | |
| 2490 const intptr_t num_args = 3; | |
| 2491 const Array& args = Array::Handle(isolate, Array::New(num_args + 1)); | |
| 2492 // Factories get type arguments. | |
| 2493 args.SetAt(0, TypeArguments::Handle(isolate)); | |
| 2494 const ExternalTypedData& array = | |
| 2495 Api::UnwrapExternalTypedDataHandle(isolate, ext_data); | |
| 2496 args.SetAt(1, array); | |
| 2497 Smi& smi = Smi::Handle(isolate); | |
| 2498 smi = Smi::New(0); | |
| 2499 args.SetAt(2, smi); | |
| 2500 smi = Smi::New(length); | |
| 2501 args.SetAt(3, smi); | |
| 2502 | |
| 2503 // Invoke the constructor and return the new object. | |
| 2504 result = DartEntry::InvokeFunction(factory, args); | |
| 2505 ASSERT(result.IsNull() || result.IsInstance() || result.IsError()); | |
| 2506 return Api::NewHandle(isolate, result.raw()); | |
| 2507 } | |
| 2508 | |
| 2509 | |
| 2429 DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, | 2510 DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, |
| 2430 intptr_t length) { | 2511 intptr_t length) { |
| 2431 Isolate* isolate = Isolate::Current(); | 2512 Isolate* isolate = Isolate::Current(); |
| 2432 DARTSCOPE(isolate); | 2513 DARTSCOPE(isolate); |
| 2433 CHECK_CALLBACK_STATE(isolate); | 2514 CHECK_CALLBACK_STATE(isolate); |
| 2434 switch (type) { | 2515 switch (type) { |
| 2435 case kByteData : | 2516 case kByteData : |
| 2436 // TODO(asiva): Add a new ByteArray::New() method. | 2517 return NewByteData(isolate, length); |
| 2437 break; | |
| 2438 case kInt8 : | 2518 case kInt8 : |
| 2439 return NewTypedData(isolate, kTypedDataInt8ArrayCid, length); | 2519 return NewTypedData(isolate, kTypedDataInt8ArrayCid, length); |
| 2440 case kUint8 : | 2520 case kUint8 : |
| 2441 return NewTypedData(isolate, kTypedDataUint8ArrayCid, length); | 2521 return NewTypedData(isolate, kTypedDataUint8ArrayCid, length); |
| 2442 case kUint8Clamped : | 2522 case kUint8Clamped : |
| 2443 return NewTypedData(isolate, kTypedDataUint8ClampedArrayCid, length); | 2523 return NewTypedData(isolate, kTypedDataUint8ClampedArrayCid, length); |
| 2444 case kInt16 : | 2524 case kInt16 : |
| 2445 return NewTypedData(isolate, kTypedDataInt16ArrayCid, length); | 2525 return NewTypedData(isolate, kTypedDataInt16ArrayCid, length); |
| 2446 case kUint16 : | 2526 case kUint16 : |
| 2447 return NewTypedData(isolate, kTypedDataUint16ArrayCid, length); | 2527 return NewTypedData(isolate, kTypedDataUint16ArrayCid, length); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 2473 void* peer, | 2553 void* peer, |
| 2474 Dart_WeakPersistentHandleFinalizer callback) { | 2554 Dart_WeakPersistentHandleFinalizer callback) { |
| 2475 Isolate* isolate = Isolate::Current(); | 2555 Isolate* isolate = Isolate::Current(); |
| 2476 DARTSCOPE(isolate); | 2556 DARTSCOPE(isolate); |
| 2477 if (data == NULL && length != 0) { | 2557 if (data == NULL && length != 0) { |
| 2478 RETURN_NULL_ERROR(data); | 2558 RETURN_NULL_ERROR(data); |
| 2479 } | 2559 } |
| 2480 CHECK_CALLBACK_STATE(isolate); | 2560 CHECK_CALLBACK_STATE(isolate); |
| 2481 switch (type) { | 2561 switch (type) { |
| 2482 case kByteData : | 2562 case kByteData : |
| 2483 // TODO(asiva): Allocate external ByteData object. | 2563 return NewExternalByteData(isolate, data, length, peer, callback); |
| 2484 break; | |
| 2485 case kInt8 : | 2564 case kInt8 : |
| 2486 return NewExternalTypedData(kExternalTypedDataInt8ArrayCid, | 2565 return NewExternalTypedData(kExternalTypedDataInt8ArrayCid, |
| 2487 data, | 2566 data, |
| 2488 length, | 2567 length, |
| 2489 peer, | 2568 peer, |
| 2490 callback); | 2569 callback); |
| 2491 case kUint8 : | 2570 case kUint8 : |
| 2492 return NewExternalTypedData(kExternalTypedDataUint8ArrayCid, | 2571 return NewExternalTypedData(kExternalTypedDataUint8ArrayCid, |
| 2493 data, | 2572 data, |
| 2494 length, | 2573 length, |
| (...skipping 2179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4674 } | 4753 } |
| 4675 { | 4754 { |
| 4676 NoGCScope no_gc; | 4755 NoGCScope no_gc; |
| 4677 RawObject* raw_obj = obj.raw(); | 4756 RawObject* raw_obj = obj.raw(); |
| 4678 isolate->heap()->SetPeer(raw_obj, peer); | 4757 isolate->heap()->SetPeer(raw_obj, peer); |
| 4679 } | 4758 } |
| 4680 return Api::Success(isolate); | 4759 return Api::Success(isolate); |
| 4681 } | 4760 } |
| 4682 | 4761 |
| 4683 } // namespace dart | 4762 } // namespace dart |
| OLD | NEW |