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

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

Issue 289553002: Add accessors for Maps to the embedding API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 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/include/dart_api.h ('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 #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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 list_class, 75 list_class,
76 TypeArguments::Handle(isolate), 76 TypeArguments::Handle(isolate),
77 &malformed_type_error)) { 77 &malformed_type_error)) {
78 ASSERT(malformed_type_error.IsNull()); // Type is a raw List. 78 ASSERT(malformed_type_error.IsNull()); // Type is a raw List.
79 return instance.raw(); 79 return instance.raw();
80 } 80 }
81 } 81 }
82 return Instance::null(); 82 return Instance::null();
83 } 83 }
84 84
85 static RawInstance* GetMapInstance(Isolate* isolate, const Object& obj) {
86 if (obj.IsInstance()) {
87 const Library& core_lib = Library::Handle(Library::CoreLibrary());
88 const Class& map_class =
89 Class::Handle(core_lib.LookupClass(Symbols::Map()));
90 ASSERT(!map_class.IsNull());
91 const Instance& instance = Instance::Cast(obj);
92 const Class& obj_class = Class::Handle(isolate, obj.clazz());
93 Error& malformed_type_error = Error::Handle(isolate);
94 if (obj_class.IsSubtypeOf(TypeArguments::Handle(isolate),
95 map_class,
96 TypeArguments::Handle(isolate),
97 &malformed_type_error)) {
98 ASSERT(malformed_type_error.IsNull()); // Type is a raw Map.
99 return instance.raw();
100 }
101 }
102 return Instance::null();
103 }
104
85 105
86 static bool GetNativeStringArgument(NativeArguments* arguments, 106 static bool GetNativeStringArgument(NativeArguments* arguments,
87 int arg_index, 107 int arg_index,
88 Dart_Handle* str, 108 Dart_Handle* str,
89 void** peer) { 109 void** peer) {
90 ASSERT(peer != NULL); 110 ASSERT(peer != NULL);
91 if (Api::StringGetPeerHelper(arguments, arg_index, peer)) { 111 if (Api::StringGetPeerHelper(arguments, arg_index, peer)) {
92 *str = NULL; 112 *str = NULL;
93 return true; 113 return true;
94 } 114 }
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 // If 'size' would be a significant fraction of new space, then use old. 244 // If 'size' would be a significant fraction of new space, then use old.
225 static const int kExtNewRatio = 16; 245 static const int kExtNewRatio = 16;
226 if (size > (heap->CapacityInWords(Heap::kNew) * kWordSize) / kExtNewRatio) { 246 if (size > (heap->CapacityInWords(Heap::kNew) * kWordSize) / kExtNewRatio) {
227 return Heap::kOld; 247 return Heap::kOld;
228 } else { 248 } else {
229 return Heap::kNew; 249 return Heap::kNew;
230 } 250 }
231 } 251 }
232 252
233 253
254 static RawObject* Send0Arg(const Instance& receiver,
255 const String& selector) {
256 const intptr_t kNumArgs = 1;
257 ArgumentsDescriptor args_desc(
258 Array::Handle(ArgumentsDescriptor::New(kNumArgs)));
259 const Function& function = Function::Handle(
260 Resolver::ResolveDynamic(receiver, selector, args_desc));
261 if (function.IsNull()) {
262 return ApiError::New(String::Handle(String::New("")));
263 }
264 const Array& args = Array::Handle(Array::New(kNumArgs));
265 args.SetAt(0, receiver);
266 return DartEntry::InvokeFunction(function, args);
267 }
268
269
270 static RawObject* Send1Arg(const Instance& receiver,
271 const String& selector,
272 const Instance& argument) {
273 const intptr_t kNumArgs = 2;
274 ArgumentsDescriptor args_desc(
275 Array::Handle(ArgumentsDescriptor::New(kNumArgs)));
276 const Function& function = Function::Handle(
277 Resolver::ResolveDynamic(receiver, selector, args_desc));
278 if (function.IsNull()) {
279 return ApiError::New(String::Handle(String::New("")));
280 }
281 const Array& args = Array::Handle(Array::New(kNumArgs));
282 args.SetAt(0, receiver);
283 args.SetAt(1, argument);
284 return DartEntry::InvokeFunction(function, args);
285 }
286
287
234 WeakReferenceSetBuilder* ApiState::NewWeakReferenceSetBuilder() { 288 WeakReferenceSetBuilder* ApiState::NewWeakReferenceSetBuilder() {
235 return new WeakReferenceSetBuilder(this); 289 return new WeakReferenceSetBuilder(this);
236 } 290 }
237 291
238 292
239 void ApiState::DelayWeakReferenceSet(WeakReferenceSet* reference_set) { 293 void ApiState::DelayWeakReferenceSet(WeakReferenceSet* reference_set) {
240 WeakReferenceSet::Push(reference_set, &delayed_weak_reference_sets_); 294 WeakReferenceSet::Push(reference_set, &delayed_weak_reference_sets_);
241 } 295 }
242 296
243 297
(...skipping 1559 matching lines...) Expand 10 before | Expand all | Expand 10 after
1803 return true; 1857 return true;
1804 } 1858 }
1805 1859
1806 Isolate* isolate = Isolate::Current(); 1860 Isolate* isolate = Isolate::Current();
1807 DARTSCOPE(isolate); 1861 DARTSCOPE(isolate);
1808 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object)); 1862 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1809 return GetListInstance(isolate, obj) != Instance::null(); 1863 return GetListInstance(isolate, obj) != Instance::null();
1810 } 1864 }
1811 1865
1812 1866
1867 DART_EXPORT bool Dart_IsMap(Dart_Handle object) {
1868 Isolate* isolate = Isolate::Current();
1869 DARTSCOPE(isolate);
1870 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(object));
1871 return GetMapInstance(isolate, obj) != Instance::null();
1872 }
1873
1874
1813 DART_EXPORT bool Dart_IsLibrary(Dart_Handle object) { 1875 DART_EXPORT bool Dart_IsLibrary(Dart_Handle object) {
1814 TRACE_API_CALL(CURRENT_FUNC); 1876 TRACE_API_CALL(CURRENT_FUNC);
1815 return Api::ClassId(object) == kLibraryCid; 1877 return Api::ClassId(object) == kLibraryCid;
1816 } 1878 }
1817 1879
1818 1880
1819 DART_EXPORT bool Dart_IsType(Dart_Handle handle) { 1881 DART_EXPORT bool Dart_IsType(Dart_Handle handle) {
1820 TRACE_API_CALL(CURRENT_FUNC); 1882 TRACE_API_CALL(CURRENT_FUNC);
1821 return Api::ClassId(handle) == kTypeCid; 1883 return Api::ClassId(handle) == kTypeCid;
1822 } 1884 }
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
2498 DARTSCOPE(isolate); 2560 DARTSCOPE(isolate);
2499 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list)); 2561 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(list));
2500 if (obj.IsArray()) { 2562 if (obj.IsArray()) {
2501 GET_LIST_ELEMENT(isolate, Array, obj, index); 2563 GET_LIST_ELEMENT(isolate, Array, obj, index);
2502 } else if (obj.IsGrowableObjectArray()) { 2564 } else if (obj.IsGrowableObjectArray()) {
2503 GET_LIST_ELEMENT(isolate, GrowableObjectArray, obj, index); 2565 GET_LIST_ELEMENT(isolate, GrowableObjectArray, obj, index);
2504 } else if (obj.IsError()) { 2566 } else if (obj.IsError()) {
2505 return list; 2567 return list;
2506 } else { 2568 } else {
2507 CHECK_CALLBACK_STATE(isolate); 2569 CHECK_CALLBACK_STATE(isolate);
2508
2509 // Check and handle a dart object that implements the List interface. 2570 // Check and handle a dart object that implements the List interface.
2510 const Instance& instance = 2571 const Instance& instance =
2511 Instance::Handle(isolate, GetListInstance(isolate, obj)); 2572 Instance::Handle(isolate, GetListInstance(isolate, obj));
2512 if (!instance.IsNull()) { 2573 if (!instance.IsNull()) {
2513 const int kNumArgs = 2; 2574 return Api::NewHandle(isolate, Send1Arg(
2514 ArgumentsDescriptor args_desc( 2575 instance,
2515 Array::Handle(ArgumentsDescriptor::New(kNumArgs))); 2576 Symbols::IndexToken(),
2516 const Function& function = Function::Handle( 2577 Instance::Handle(isolate, Integer::New(index))));
2517 isolate,
2518 Resolver::ResolveDynamic(instance, Symbols::IndexToken(), args_desc));
2519 if (!function.IsNull()) {
2520 const Array& args = Array::Handle(isolate, Array::New(kNumArgs));
2521 const Integer& indexobj = Integer::Handle(isolate, Integer::New(index));
2522 args.SetAt(0, instance);
2523 args.SetAt(1, indexobj);
2524 return Api::NewHandle(isolate, DartEntry::InvokeFunction(function,
2525 args));
2526 }
2527 } 2578 }
2528 return Api::NewError("Object does not implement the 'List' interface"); 2579 return Api::NewError("Object does not implement the 'List' interface");
2529 } 2580 }
2530 } 2581 }
2531 2582
2532 2583
2533 #define SET_LIST_ELEMENT(isolate, type, obj, index, value) \ 2584 #define SET_LIST_ELEMENT(isolate, type, obj, index, value) \
2534 const type& array = type::Cast(obj); \ 2585 const type& array = type::Cast(obj); \
2535 const Object& value_obj = Object::Handle(isolate, Api::UnwrapHandle(value)); \ 2586 const Object& value_obj = Object::Handle(isolate, Api::UnwrapHandle(value)); \
2536 if (!value_obj.IsNull() && !value_obj.IsInstance()) { \ 2587 if (!value_obj.IsNull() && !value_obj.IsInstance()) { \
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
2895 return Api::NewHandle(isolate, result.raw()); 2946 return Api::NewHandle(isolate, result.raw());
2896 } 2947 }
2897 } 2948 }
2898 return Api::Success(); 2949 return Api::Success();
2899 } 2950 }
2900 } 2951 }
2901 return Api::NewError("Object does not implement the 'List' interface"); 2952 return Api::NewError("Object does not implement the 'List' interface");
2902 } 2953 }
2903 2954
2904 2955
2956 // --- Maps ---
2957
2958 DART_EXPORT Dart_Handle Dart_MapGetAt(Dart_Handle map, Dart_Handle key) {
2959 Isolate* isolate = Isolate::Current();
2960 DARTSCOPE(isolate);
2961 CHECK_CALLBACK_STATE(isolate);
2962 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(map));
2963 const Instance& instance =
2964 Instance::Handle(isolate, GetMapInstance(isolate, obj));
2965 if (!instance.IsNull()) {
2966 const Object& key_obj = Object::Handle(Api::UnwrapHandle(key));
2967 if (!(key_obj.IsInstance() || key_obj.IsNull())) {
2968 return Api::NewError("Key is not an instance");
2969 }
2970 return Api::NewHandle(isolate, Send1Arg(
2971 instance,
2972 Symbols::IndexToken(),
2973 Instance::Cast(key_obj)));
2974 }
2975 return Api::NewError("Object does not implement the 'Map' interface");
2976 }
2977
2978
2979 DART_EXPORT Dart_Handle Dart_MapContainsKey(Dart_Handle map, Dart_Handle key) {
2980 Isolate* isolate = Isolate::Current();
2981 DARTSCOPE(isolate);
2982 CHECK_CALLBACK_STATE(isolate);
2983 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(map));
2984 const Instance& instance =
2985 Instance::Handle(isolate, GetMapInstance(isolate, obj));
2986 if (!instance.IsNull()) {
2987 const Object& key_obj = Object::Handle(Api::UnwrapHandle(key));
2988 if (!(key_obj.IsInstance() || key_obj.IsNull())) {
2989 return Api::NewError("Key is not an instance");
2990 }
2991 return Api::NewHandle(isolate, Send1Arg(
2992 instance,
2993 String::Handle(isolate, String::New("containsKey")),
2994 Instance::Cast(key_obj)));
2995 }
2996 return Api::NewError("Object does not implement the 'Map' interface");
2997 }
2998
2999
3000 DART_EXPORT Dart_Handle Dart_MapKeys(Dart_Handle map) {
3001 Isolate* isolate = Isolate::Current();
3002 DARTSCOPE(isolate);
3003 CHECK_CALLBACK_STATE(isolate);
3004 Object& obj = Object::Handle(isolate, Api::UnwrapHandle(map));
3005 Instance& instance =
3006 Instance::Handle(isolate, GetMapInstance(isolate, obj));
3007 if (!instance.IsNull()) {
3008 const Object& iterator = Object::Handle(Send0Arg(
3009 instance, String::Handle(String::New("get:keys"))));
3010 if (!iterator.IsInstance()) {
3011 return Api::NewHandle(isolate, iterator.raw());
3012 }
3013 return Api::NewHandle(isolate, Send0Arg(
3014 Instance::Cast(iterator),
3015 String::Handle(String::New("toList"))));
3016 }
3017 return Api::NewError("Object does not implement the 'Map' interface");
3018 }
3019
3020
2905 // --- Typed Data --- 3021 // --- Typed Data ---
2906 3022
2907 // Helper method to get the type of a TypedData object. 3023 // Helper method to get the type of a TypedData object.
2908 static Dart_TypedData_Type GetType(intptr_t class_id) { 3024 static Dart_TypedData_Type GetType(intptr_t class_id) {
2909 Dart_TypedData_Type type; 3025 Dart_TypedData_Type type;
2910 switch (class_id) { 3026 switch (class_id) {
2911 case kByteDataViewCid : 3027 case kByteDataViewCid :
2912 type = Dart_TypedData_kByteData; 3028 type = Dart_TypedData_kByteData;
2913 break; 3029 break;
2914 case kTypedDataInt8ArrayCid : 3030 case kTypedDataInt8ArrayCid :
(...skipping 2166 matching lines...) Expand 10 before | Expand all | Expand 10 after
5081 5197
5082 5198
5083 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( 5199 DART_EXPORT void Dart_RegisterRootServiceRequestCallback(
5084 const char* name, 5200 const char* name,
5085 Dart_ServiceRequestCallback callback, 5201 Dart_ServiceRequestCallback callback,
5086 void* user_data) { 5202 void* user_data) {
5087 Service::RegisterRootEmbedderCallback(name, callback, user_data); 5203 Service::RegisterRootEmbedderCallback(name, callback, user_data);
5088 } 5204 }
5089 5205
5090 } // namespace dart 5206 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/include/dart_api.h ('k') | runtime/vm/dart_api_impl_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698