Chromium Code Reviews| Index: runtime/vm/dart_api_impl.cc |
| =================================================================== |
| --- runtime/vm/dart_api_impl.cc (revision 20330) |
| +++ runtime/vm/dart_api_impl.cc (working copy) |
| @@ -2403,6 +2403,45 @@ |
| } |
| +static RawObject* GetByteDataConstructor(Isolate* isolate, |
| + const String& constructor_name, |
| + intptr_t num_args) { |
| + const String& cls_name = |
| + 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
|
| + const Library& lib = |
| + Library::Handle(isolate->object_store()->typeddata_library()); |
| + ASSERT(!lib.IsNull()); |
| + const Class& cls = |
| + Class::Handle(isolate, lib.LookupClassAllowPrivate(cls_name)); |
| + ASSERT(!cls.IsNull()); |
| + return ResolveConstructor(CURRENT_FUNC, |
| + cls, cls_name, |
| + constructor_name, num_args); |
| +} |
| + |
| + |
| +static Dart_Handle NewByteData(Isolate* isolate, intptr_t length) { |
| + CHECK_LENGTH(length, TypedData::MaxElements(kTypedDataInt8ArrayCid)); |
| + Object& result = Object::Handle(isolate); |
| + result = GetByteDataConstructor(isolate, Symbols::Dot(), 1); |
| + ASSERT(!result.IsNull()); |
| + ASSERT(result.IsFunction()); |
| + const Function& factory = Function::Cast(result); |
| + ASSERT(!factory.IsConstructor()); |
| + |
| + // Create the argument list. |
| + const Array& args = Array::Handle(isolate, Array::New(2)); |
| + // Factories get type arguments. |
| + args.SetAt(0, TypeArguments::Handle(isolate)); |
| + args.SetAt(1, Smi::Handle(isolate, Smi::New(length))); |
| + |
| + // Invoke the constructor and return the new object. |
| + result = DartEntry::InvokeFunction(factory, args); |
| + ASSERT(result.IsInstance() || result.IsNull() || result.IsError()); |
| + return Api::NewHandle(isolate, result.raw()); |
| +} |
| + |
| + |
| static Dart_Handle NewTypedData(Isolate* isolate, |
| intptr_t cid, |
| intptr_t length) { |
| @@ -2426,6 +2465,48 @@ |
| } |
| +static Dart_Handle NewExternalByteData(Isolate* isolate, |
| + void* data, |
| + intptr_t length, |
| + void* peer, |
| + Dart_WeakPersistentHandleFinalizer cb) { |
| + Dart_Handle ext_data = NewExternalTypedData(kExternalTypedDataUint8ArrayCid, |
| + data, |
| + length, |
| + peer, |
| + cb); |
| + if (::Dart_IsError(ext_data)) { |
| + return ext_data; |
| + } |
| + Object& result = Object::Handle(isolate); |
| + 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.
|
| + result = GetByteDataConstructor(isolate, dot_name, 3); |
| + ASSERT(!result.IsNull()); |
| + ASSERT(result.IsFunction()); |
| + const Function& factory = Function::Cast(result); |
| + ASSERT(!factory.IsConstructor()); |
| + |
| + // Create the argument list. |
| + const intptr_t num_args = 3; |
| + const Array& args = Array::Handle(isolate, Array::New(num_args + 1)); |
| + // Factories get type arguments. |
| + args.SetAt(0, TypeArguments::Handle(isolate)); |
| + const ExternalTypedData& array = |
| + Api::UnwrapExternalTypedDataHandle(isolate, ext_data); |
| + args.SetAt(1, array); |
| + Smi& smi = Smi::Handle(isolate); |
| + smi = Smi::New(0); |
| + args.SetAt(2, smi); |
| + smi = Smi::New(length); |
| + args.SetAt(3, smi); |
| + |
| + // Invoke the constructor and return the new object. |
| + result = DartEntry::InvokeFunction(factory, args); |
| + ASSERT(result.IsNull() || result.IsInstance() || result.IsError()); |
| + return Api::NewHandle(isolate, result.raw()); |
| +} |
| + |
| + |
| DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, |
| intptr_t length) { |
| Isolate* isolate = Isolate::Current(); |
| @@ -2433,8 +2514,7 @@ |
| CHECK_CALLBACK_STATE(isolate); |
| switch (type) { |
| case kByteData : |
| - // TODO(asiva): Add a new ByteArray::New() method. |
| - break; |
| + return NewByteData(isolate, length); |
| case kInt8 : |
| return NewTypedData(isolate, kTypedDataInt8ArrayCid, length); |
| case kUint8 : |
| @@ -2480,8 +2560,7 @@ |
| CHECK_CALLBACK_STATE(isolate); |
| switch (type) { |
| case kByteData : |
| - // TODO(asiva): Allocate external ByteData object. |
| - break; |
| + return NewExternalByteData(isolate, data, length, peer, callback); |
| case kInt8 : |
| return NewExternalTypedData(kExternalTypedDataInt8ArrayCid, |
| data, |