Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: runtime/vm/dart_api_impl.cc

Issue 12938010: Implement creation of ByteData objects using the Dart API. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/lib/typeddata.dart ('k') | runtime/vm/dart_api_impl_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 2056 matching lines...) Expand 10 before | Expand all | Expand 10 after
2067 const Class& cls, 2067 const Class& cls,
2068 const String& class_name, 2068 const String& class_name,
2069 const String& dotted_name, 2069 const String& dotted_name,
2070 int num_args); 2070 int num_args);
2071 2071
2072 2072
2073 static RawObject* ThrowArgumentError(const char* exception_message) { 2073 static RawObject* ThrowArgumentError(const char* exception_message) {
2074 Isolate* isolate = Isolate::Current(); 2074 Isolate* isolate = Isolate::Current();
2075 // Lookup the class ArgumentError in dart:core. 2075 // Lookup the class ArgumentError in dart:core.
2076 const String& lib_url = String::Handle(String::New("dart:core")); 2076 const String& lib_url = String::Handle(String::New("dart:core"));
2077 const String& class_name = 2077 const String& class_name = String::Handle(String::New("ArgumentError"));
2078 String::Handle(String::New("ArgumentError"));
2079 const Library& lib = 2078 const Library& lib =
2080 Library::Handle(isolate, Library::LookupLibrary(lib_url)); 2079 Library::Handle(isolate, Library::LookupLibrary(lib_url));
2081 if (lib.IsNull()) { 2080 if (lib.IsNull()) {
2082 const String& message = String::Handle( 2081 const String& message = String::Handle(
2083 String::NewFormatted("%s: library '%s' not found.", 2082 String::NewFormatted("%s: library '%s' not found.",
2084 CURRENT_FUNC, lib_url.ToCString())); 2083 CURRENT_FUNC, lib_url.ToCString()));
2085 return ApiError::New(message); 2084 return ApiError::New(message);
2086 } 2085 }
2087 const Class& cls = Class::Handle(isolate, 2086 const Class& cls = Class::Handle(isolate,
2088 lib.LookupClassAllowPrivate(class_name)); 2087 lib.LookupClassAllowPrivate(class_name));
2089 if (cls.IsNull()) { 2088 if (cls.IsNull()) {
2090 const String& message = String::Handle( 2089 const String& message = String::Handle(
2091 String::NewFormatted("%s: class '%s' not found in library '%s'.", 2090 String::NewFormatted("%s: class '%s' not found in library '%s'.",
2092 CURRENT_FUNC, class_name.ToCString(), 2091 CURRENT_FUNC, class_name.ToCString(),
2093 lib_url.ToCString())); 2092 lib_url.ToCString()));
2094 return ApiError::New(message); 2093 return ApiError::New(message);
2095 } 2094 }
2095 Object& result = Object::Handle(isolate);
2096 String& dot_name = String::Handle(String::New(".")); 2096 String& dot_name = String::Handle(String::New("."));
2097 Object& result = Object::Handle(isolate); 2097 String& constr_name = String::Handle(String::Concat(class_name, dot_name));
2098 result = ResolveConstructor(CURRENT_FUNC, cls, class_name, dot_name, 1); 2098 result = ResolveConstructor(CURRENT_FUNC, cls, class_name, constr_name, 1);
2099 if (result.IsError()) return result.raw(); 2099 if (result.IsError()) return result.raw();
2100 ASSERT(result.IsFunction()); 2100 ASSERT(result.IsFunction());
2101 Function& constructor = Function::Handle(isolate); 2101 Function& constructor = Function::Handle(isolate);
2102 constructor ^= result.raw(); 2102 constructor ^= result.raw();
2103 if (!constructor.IsConstructor()) { 2103 if (!constructor.IsConstructor()) {
2104 const String& message = String::Handle( 2104 const String& message = String::Handle(
2105 String::NewFormatted("%s: class '%s' is not a constructor.", 2105 String::NewFormatted("%s: class '%s' is not a constructor.",
2106 CURRENT_FUNC, class_name.ToCString())); 2106 CURRENT_FUNC, class_name.ToCString()));
2107 return ApiError::New(message); 2107 return ApiError::New(message);
2108 } 2108 }
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 kByteArrayCid :
2333 type = kByteData;
2334 break;
2335 case kTypedDataInt8ArrayCid : 2332 case kTypedDataInt8ArrayCid :
2336 case kExternalTypedDataInt8ArrayCid : 2333 case kExternalTypedDataInt8ArrayCid :
2337 type = kInt8; 2334 type = kInt8;
2338 break; 2335 break;
2339 case kTypedDataUint8ArrayCid : 2336 case kTypedDataUint8ArrayCid :
2340 case kExternalTypedDataUint8ArrayCid : 2337 case kExternalTypedDataUint8ArrayCid :
2341 type = kUint8; 2338 type = kUint8;
2342 break; 2339 break;
2343 case kTypedDataUint8ClampedArrayCid : 2340 case kTypedDataUint8ClampedArrayCid :
2344 case kExternalTypedDataUint8ClampedArrayCid : 2341 case kExternalTypedDataUint8ClampedArrayCid :
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
2379 default: 2376 default:
2380 type = kInvalid; 2377 type = kInvalid;
2381 break; 2378 break;
2382 } 2379 }
2383 return type; 2380 return type;
2384 } 2381 }
2385 2382
2386 2383
2387 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfTypedData(Dart_Handle object) { 2384 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfTypedData(Dart_Handle object) {
2388 intptr_t class_id = Api::ClassId(object); 2385 intptr_t class_id = Api::ClassId(object);
2389 if (!RawObject::IsTypedDataClassId(class_id)) { 2386 if (RawObject::IsTypedDataClassId(class_id)) {
2390 return kInvalid; 2387 return GetType(class_id);
2391 } 2388 }
2392 return GetType(class_id); 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;
2393 } 2399 }
2394 2400
2395 2401
2396 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfExternalTypedData( 2402 DART_EXPORT Dart_TypedData_Type Dart_GetTypeOfExternalTypedData(
2397 Dart_Handle object) { 2403 Dart_Handle object) {
2398 intptr_t class_id = Api::ClassId(object); 2404 intptr_t class_id = Api::ClassId(object);
2399 if (!RawObject::IsExternalTypedDataClassId(class_id)) { 2405 if (RawObject::IsExternalTypedDataClassId(class_id)) {
2400 return kInvalid; 2406 return GetType(class_id);
2401 } 2407 }
2402 return GetType(class_id); 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;
2418 }
2419
2420
2421 static RawObject* GetByteDataConstructor(Isolate* isolate,
2422 const String& constructor_name,
2423 intptr_t num_args) {
2424 const Library& lib =
2425 Library::Handle(isolate->object_store()->typeddata_library());
2426 ASSERT(!lib.IsNull());
2427 const Class& cls =
2428 Class::Handle(isolate, lib.LookupClassAllowPrivate(Symbols::ByteData()));
2429 ASSERT(!cls.IsNull());
2430 return ResolveConstructor(CURRENT_FUNC,
2431 cls,
2432 Symbols::ByteData(),
2433 constructor_name,
2434 num_args);
2435 }
2436
2437
2438 static Dart_Handle NewByteData(Isolate* isolate, intptr_t length) {
2439 CHECK_LENGTH(length, TypedData::MaxElements(kTypedDataInt8ArrayCid));
2440 Object& result = Object::Handle(isolate);
2441 result = GetByteDataConstructor(isolate, Symbols::ByteDataDot(), 1);
2442 ASSERT(!result.IsNull());
2443 ASSERT(result.IsFunction());
2444 const Function& factory = Function::Cast(result);
2445 ASSERT(!factory.IsConstructor());
2446
2447 // Create the argument list.
2448 const Array& args = Array::Handle(isolate, Array::New(2));
2449 // Factories get type arguments.
2450 args.SetAt(0, TypeArguments::Handle(isolate));
2451 args.SetAt(1, Smi::Handle(isolate, Smi::New(length)));
2452
2453 // Invoke the constructor and return the new object.
2454 result = DartEntry::InvokeFunction(factory, args);
2455 ASSERT(result.IsInstance() || result.IsNull() || result.IsError());
2456 return Api::NewHandle(isolate, result.raw());
2403 } 2457 }
2404 2458
2405 2459
2406 static Dart_Handle NewTypedData(Isolate* isolate, 2460 static Dart_Handle NewTypedData(Isolate* isolate,
2407 intptr_t cid, 2461 intptr_t cid,
2408 intptr_t length) { 2462 intptr_t length) {
2409 CHECK_LENGTH(length, TypedData::MaxElements(cid)); 2463 CHECK_LENGTH(length, TypedData::MaxElements(cid));
2410 return Api::NewHandle(isolate, TypedData::New(cid, length)); 2464 return Api::NewHandle(isolate, TypedData::New(cid, length));
2411 } 2465 }
2412 2466
2413 2467
2414 static Dart_Handle NewExternalTypedData( 2468 static Dart_Handle NewExternalTypedData(
2415 intptr_t cid, 2469 intptr_t cid,
2416 void* data, 2470 void* data,
2417 intptr_t length, 2471 intptr_t length,
2418 void* peer, 2472 void* peer,
2419 Dart_WeakPersistentHandleFinalizer callback) { 2473 Dart_WeakPersistentHandleFinalizer callback) {
2420 CHECK_LENGTH(length, ExternalTypedData::MaxElements(cid)); 2474 CHECK_LENGTH(length, ExternalTypedData::MaxElements(cid));
2421 const ExternalTypedData& obj = ExternalTypedData::Handle( 2475 const ExternalTypedData& obj = ExternalTypedData::Handle(
2422 ExternalTypedData::New(cid, 2476 ExternalTypedData::New(cid,
2423 reinterpret_cast<uint8_t*>(data), 2477 reinterpret_cast<uint8_t*>(data),
2424 length)); 2478 length));
2425 return reinterpret_cast<Dart_Handle>(obj.AddFinalizer(peer, callback)); 2479 return reinterpret_cast<Dart_Handle>(obj.AddFinalizer(peer, callback));
2426 } 2480 }
2427 2481
2428 2482
2483 static Dart_Handle NewExternalByteData(Isolate* isolate,
2484 void* data,
2485 intptr_t length,
2486 void* peer,
2487 Dart_WeakPersistentHandleFinalizer cb) {
2488 Dart_Handle ext_data = NewExternalTypedData(kExternalTypedDataUint8ArrayCid,
2489 data,
2490 length,
2491 peer,
2492 cb);
2493 if (::Dart_IsError(ext_data)) {
2494 return ext_data;
2495 }
2496 Object& result = Object::Handle(isolate);
2497 result = GetByteDataConstructor(isolate, Symbols::ByteDataDotview(), 3);
2498 ASSERT(!result.IsNull());
2499 ASSERT(result.IsFunction());
2500 const Function& factory = Function::Cast(result);
2501 ASSERT(!factory.IsConstructor());
2502
2503 // Create the argument list.
2504 const intptr_t num_args = 3;
2505 const Array& args = Array::Handle(isolate, Array::New(num_args + 1));
2506 // Factories get type arguments.
2507 args.SetAt(0, TypeArguments::Handle(isolate));
2508 const ExternalTypedData& array =
2509 Api::UnwrapExternalTypedDataHandle(isolate, ext_data);
2510 args.SetAt(1, array);
2511 Smi& smi = Smi::Handle(isolate);
2512 smi = Smi::New(0);
2513 args.SetAt(2, smi);
2514 smi = Smi::New(length);
2515 args.SetAt(3, smi);
2516
2517 // Invoke the constructor and return the new object.
2518 result = DartEntry::InvokeFunction(factory, args);
2519 ASSERT(result.IsNull() || result.IsInstance() || result.IsError());
2520 return Api::NewHandle(isolate, result.raw());
2521 }
2522
2523
2429 DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, 2524 DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type,
2430 intptr_t length) { 2525 intptr_t length) {
2431 Isolate* isolate = Isolate::Current(); 2526 Isolate* isolate = Isolate::Current();
2432 DARTSCOPE(isolate); 2527 DARTSCOPE(isolate);
2433 CHECK_CALLBACK_STATE(isolate); 2528 CHECK_CALLBACK_STATE(isolate);
2434 switch (type) { 2529 switch (type) {
2435 case kByteData : 2530 case kByteData :
2436 // TODO(asiva): Add a new ByteArray::New() method. 2531 return NewByteData(isolate, length);
2437 break;
2438 case kInt8 : 2532 case kInt8 :
2439 return NewTypedData(isolate, kTypedDataInt8ArrayCid, length); 2533 return NewTypedData(isolate, kTypedDataInt8ArrayCid, length);
2440 case kUint8 : 2534 case kUint8 :
2441 return NewTypedData(isolate, kTypedDataUint8ArrayCid, length); 2535 return NewTypedData(isolate, kTypedDataUint8ArrayCid, length);
2442 case kUint8Clamped : 2536 case kUint8Clamped :
2443 return NewTypedData(isolate, kTypedDataUint8ClampedArrayCid, length); 2537 return NewTypedData(isolate, kTypedDataUint8ClampedArrayCid, length);
2444 case kInt16 : 2538 case kInt16 :
2445 return NewTypedData(isolate, kTypedDataInt16ArrayCid, length); 2539 return NewTypedData(isolate, kTypedDataInt16ArrayCid, length);
2446 case kUint16 : 2540 case kUint16 :
2447 return NewTypedData(isolate, kTypedDataUint16ArrayCid, length); 2541 return NewTypedData(isolate, kTypedDataUint16ArrayCid, length);
(...skipping 25 matching lines...) Expand all
2473 void* peer, 2567 void* peer,
2474 Dart_WeakPersistentHandleFinalizer callback) { 2568 Dart_WeakPersistentHandleFinalizer callback) {
2475 Isolate* isolate = Isolate::Current(); 2569 Isolate* isolate = Isolate::Current();
2476 DARTSCOPE(isolate); 2570 DARTSCOPE(isolate);
2477 if (data == NULL && length != 0) { 2571 if (data == NULL && length != 0) {
2478 RETURN_NULL_ERROR(data); 2572 RETURN_NULL_ERROR(data);
2479 } 2573 }
2480 CHECK_CALLBACK_STATE(isolate); 2574 CHECK_CALLBACK_STATE(isolate);
2481 switch (type) { 2575 switch (type) {
2482 case kByteData : 2576 case kByteData :
2483 // TODO(asiva): Allocate external ByteData object. 2577 return NewExternalByteData(isolate, data, length, peer, callback);
2484 break;
2485 case kInt8 : 2578 case kInt8 :
2486 return NewExternalTypedData(kExternalTypedDataInt8ArrayCid, 2579 return NewExternalTypedData(kExternalTypedDataInt8ArrayCid,
2487 data, 2580 data,
2488 length, 2581 length,
2489 peer, 2582 peer,
2490 callback); 2583 callback);
2491 case kUint8 : 2584 case kUint8 :
2492 return NewExternalTypedData(kExternalTypedDataUint8ArrayCid, 2585 return NewExternalTypedData(kExternalTypedDataUint8ArrayCid,
2493 data, 2586 data,
2494 length, 2587 length,
(...skipping 960 matching lines...) Expand 10 before | Expand all | Expand 10 after
3455 return TypeToHandle(isolate, "Dart_TypeVariableUpperBound", bound); 3548 return TypeToHandle(isolate, "Dart_TypeVariableUpperBound", bound);
3456 } 3549 }
3457 3550
3458 3551
3459 // --- Constructors, Methods, and Fields --- 3552 // --- Constructors, Methods, and Fields ---
3460 3553
3461 3554
3462 static RawObject* ResolveConstructor(const char* current_func, 3555 static RawObject* ResolveConstructor(const char* current_func,
3463 const Class& cls, 3556 const Class& cls,
3464 const String& class_name, 3557 const String& class_name,
3465 const String& dotted_name, 3558 const String& constr_name,
3466 int num_args) { 3559 int num_args) {
3467 // The constructor must be present in the interface. 3560 // The constructor must be present in the interface.
3468 String& constr_name = String::Handle(String::Concat(class_name, dotted_name));
3469 const Function& constructor = 3561 const Function& constructor =
3470 Function::Handle(cls.LookupFunctionAllowPrivate(constr_name)); 3562 Function::Handle(cls.LookupFunctionAllowPrivate(constr_name));
3471 if (constructor.IsNull() || 3563 if (constructor.IsNull() ||
3472 (!constructor.IsConstructor() && !constructor.IsFactory())) { 3564 (!constructor.IsConstructor() && !constructor.IsFactory())) {
3473 const String& lookup_class_name = String::Handle(cls.Name()); 3565 const String& lookup_class_name = String::Handle(cls.Name());
3474 if (!class_name.Equals(lookup_class_name)) { 3566 if (!class_name.Equals(lookup_class_name)) {
3475 // When the class name used to build the constructor name is 3567 // When the class name used to build the constructor name is
3476 // different than the name of the class in which we are doing 3568 // different than the name of the class in which we are doing
3477 // the lookup, it can be confusing to the user to figure out 3569 // the lookup, it can be confusing to the user to figure out
3478 // what's going on. Be a little more explicit for these error 3570 // what's going on. Be a little more explicit for these error
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
3543 dot_name = String::Concat(Symbols::Dot(), String::Cast(name_obj)); 3635 dot_name = String::Concat(Symbols::Dot(), String::Cast(name_obj));
3544 } else { 3636 } else {
3545 RETURN_TYPE_ERROR(isolate, constructor_name, String); 3637 RETURN_TYPE_ERROR(isolate, constructor_name, String);
3546 } 3638 }
3547 Dart_Handle state = Api::CheckIsolateState(isolate); 3639 Dart_Handle state = Api::CheckIsolateState(isolate);
3548 if (::Dart_IsError(state)) { 3640 if (::Dart_IsError(state)) {
3549 return state; 3641 return state;
3550 } 3642 }
3551 3643
3552 // Resolve the constructor. 3644 // Resolve the constructor.
3553 result = ResolveConstructor( 3645 String& constr_name =
3554 "Dart_New", cls, base_constructor_name, dot_name, number_of_arguments); 3646 String::Handle(String::Concat(base_constructor_name, dot_name));
3647 result = ResolveConstructor("Dart_New",
3648 cls,
3649 base_constructor_name,
3650 constr_name,
3651 number_of_arguments);
3555 if (result.IsError()) { 3652 if (result.IsError()) {
3556 return Api::NewHandle(isolate, result.raw()); 3653 return Api::NewHandle(isolate, result.raw());
3557 } 3654 }
3558 ASSERT(result.IsFunction()); 3655 ASSERT(result.IsFunction());
3559 Function& constructor = Function::Handle(isolate); 3656 Function& constructor = Function::Handle(isolate);
3560 constructor ^= result.raw(); 3657 constructor ^= result.raw();
3561 3658
3562 Instance& new_object = Instance::Handle(isolate); 3659 Instance& new_object = Instance::Handle(isolate);
3563 if (constructor.IsRedirectingFactory()) { 3660 if (constructor.IsRedirectingFactory()) {
3564 Type& type = Type::Handle(constructor.RedirectionType()); 3661 Type& type = Type::Handle(constructor.RedirectionType());
(...skipping 1119 matching lines...) Expand 10 before | Expand all | Expand 10 after
4684 } 4781 }
4685 { 4782 {
4686 NoGCScope no_gc; 4783 NoGCScope no_gc;
4687 RawObject* raw_obj = obj.raw(); 4784 RawObject* raw_obj = obj.raw();
4688 isolate->heap()->SetPeer(raw_obj, peer); 4785 isolate->heap()->SetPeer(raw_obj, peer);
4689 } 4786 }
4690 return Api::Success(isolate); 4787 return Api::Success(isolate);
4691 } 4788 }
4692 4789
4693 } // namespace dart 4790 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/lib/typeddata.dart ('k') | runtime/vm/dart_api_impl_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698