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 #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 Loading... | |
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 Loading... | |
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 Object& argument) { | |
koda
2014/05/30 20:40:23
Instance seems safer.
rmacnak
2014/05/30 21:49:14
Done.
| |
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 Loading... | |
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 Loading... | |
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 Integer::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 Loading... | |
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 | |
2959 | |
2960 | |
2961 DART_EXPORT Dart_Handle Dart_MapGetAt(Dart_Handle map, Dart_Handle key) { | |
2962 Isolate* isolate = Isolate::Current(); | |
2963 DARTSCOPE(isolate); | |
2964 CHECK_CALLBACK_STATE(isolate); | |
2965 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(map)); | |
2966 const Instance& instance = | |
2967 Instance::Handle(isolate, GetMapInstance(isolate, obj)); | |
2968 if (!instance.IsNull()) { | |
2969 return Api::NewHandle(isolate, Send1Arg( | |
2970 instance, | |
2971 Symbols::IndexToken(), | |
2972 Object::Handle(isolate, Api::UnwrapHandle(key)))); | |
2973 } | |
2974 return Api::NewError("Object does not implement the 'Map' interface"); | |
2975 } | |
2976 | |
2977 | |
2978 DART_EXPORT Dart_Handle Dart_MapContainsKey(Dart_Handle map, Dart_Handle key) { | |
2979 Isolate* isolate = Isolate::Current(); | |
2980 DARTSCOPE(isolate); | |
2981 CHECK_CALLBACK_STATE(isolate); | |
2982 const Object& obj = Object::Handle(isolate, Api::UnwrapHandle(map)); | |
2983 const Instance& instance = | |
2984 Instance::Handle(isolate, GetMapInstance(isolate, obj)); | |
2985 if (!instance.IsNull()) { | |
2986 return Api::NewHandle(isolate, Send1Arg( | |
2987 instance, | |
2988 String::Handle(isolate, String::New("containsKey")), | |
2989 Object::Handle(isolate, Api::UnwrapHandle(key)))); | |
2990 } | |
2991 return Api::NewError("Object does not implement the 'Map' interface"); | |
2992 } | |
2993 | |
2994 | |
2995 DART_EXPORT Dart_Handle Dart_MapKeys(Dart_Handle map) { | |
2996 Isolate* isolate = Isolate::Current(); | |
2997 DARTSCOPE(isolate); | |
2998 CHECK_CALLBACK_STATE(isolate); | |
2999 Object& obj = Object::Handle(isolate, Api::UnwrapHandle(map)); | |
3000 Instance& instance = | |
3001 Instance::Handle(isolate, GetMapInstance(isolate, obj)); | |
3002 if (!instance.IsNull()) { | |
3003 const Object& iterator = Object::Handle(Send0Arg( | |
3004 instance, String::Handle(String::New("get:keys")))); | |
3005 if (!iterator.IsInstance()) { | |
3006 return Api::NewHandle(isolate, iterator.raw()); | |
3007 } | |
3008 return Api::NewHandle(isolate, Send0Arg( | |
3009 Instance::Cast(iterator), | |
3010 String::Handle(String::New("toList")))); | |
3011 } | |
3012 return Api::NewError("Object does not implement the 'Map' interface"); | |
3013 } | |
3014 | |
3015 | |
2905 // --- Typed Data --- | 3016 // --- Typed Data --- |
2906 | 3017 |
2907 // Helper method to get the type of a TypedData object. | 3018 // Helper method to get the type of a TypedData object. |
2908 static Dart_TypedData_Type GetType(intptr_t class_id) { | 3019 static Dart_TypedData_Type GetType(intptr_t class_id) { |
2909 Dart_TypedData_Type type; | 3020 Dart_TypedData_Type type; |
2910 switch (class_id) { | 3021 switch (class_id) { |
2911 case kByteDataViewCid : | 3022 case kByteDataViewCid : |
2912 type = Dart_TypedData_kByteData; | 3023 type = Dart_TypedData_kByteData; |
2913 break; | 3024 break; |
2914 case kTypedDataInt8ArrayCid : | 3025 case kTypedDataInt8ArrayCid : |
(...skipping 2166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5081 | 5192 |
5082 | 5193 |
5083 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( | 5194 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( |
5084 const char* name, | 5195 const char* name, |
5085 Dart_ServiceRequestCallback callback, | 5196 Dart_ServiceRequestCallback callback, |
5086 void* user_data) { | 5197 void* user_data) { |
5087 Service::RegisterRootEmbedderCallback(name, callback, user_data); | 5198 Service::RegisterRootEmbedderCallback(name, callback, user_data); |
5088 } | 5199 } |
5089 | 5200 |
5090 } // namespace dart | 5201 } // namespace dart |
OLD | NEW |