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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 TypeArguments::Handle(isolate), | 74 TypeArguments::Handle(isolate), |
75 &malformed_type_error)) { | 75 &malformed_type_error)) { |
76 ASSERT(malformed_type_error.IsNull()); // Type is a raw List. | 76 ASSERT(malformed_type_error.IsNull()); // Type is a raw List. |
77 return instance.raw(); | 77 return instance.raw(); |
78 } | 78 } |
79 } | 79 } |
80 return Instance::null(); | 80 return Instance::null(); |
81 } | 81 } |
82 | 82 |
83 | 83 |
| 84 static bool GetNativeStringArgument(NativeArguments* arguments, |
| 85 int arg_index, |
| 86 Dart_Handle* str, |
| 87 void** peer) { |
| 88 ASSERT(peer != NULL); |
| 89 if (Api::StringGetPeerHelper(arguments, arg_index, peer)) { |
| 90 *str = NULL; |
| 91 return true; |
| 92 } |
| 93 Isolate* isolate = arguments->isolate(); |
| 94 ASSERT(isolate == Isolate::Current()); |
| 95 *peer = NULL; |
| 96 REUSABLE_OBJECT_HANDLESCOPE(isolate); |
| 97 Object& obj = isolate->ObjectHandle(); |
| 98 obj = arguments->NativeArgAt(arg_index); |
| 99 if (RawObject::IsStringClassId(obj.GetClassId())) { |
| 100 ASSERT(isolate->api_state() && |
| 101 isolate->api_state()->top_scope() != NULL); |
| 102 *str = Api::NewHandle(isolate, obj.raw()); |
| 103 return true; |
| 104 } |
| 105 if (obj.IsNull()) { |
| 106 *str = Api::Null(); |
| 107 return true; |
| 108 } |
| 109 return false; |
| 110 } |
| 111 |
| 112 |
| 113 static bool GetNativeIntegerArgument(NativeArguments* arguments, |
| 114 int arg_index, |
| 115 int64_t* value) { |
| 116 ASSERT(value != NULL); |
| 117 if (Api::GetNativeIntegerArgument(arguments, arg_index, value)) { |
| 118 return true; |
| 119 } |
| 120 Isolate* isolate = arguments->isolate(); |
| 121 ASSERT(isolate == Isolate::Current()); |
| 122 REUSABLE_OBJECT_HANDLESCOPE(isolate); |
| 123 Object& obj = isolate->ObjectHandle(); |
| 124 obj = arguments->NativeArgAt(arg_index); |
| 125 intptr_t cid = obj.GetClassId(); |
| 126 if (cid == kBigintCid) { |
| 127 const Bigint& bigint = Bigint::Cast(obj); |
| 128 if (BigintOperations::FitsIntoInt64(bigint)) { |
| 129 *value = BigintOperations::ToInt64(bigint); |
| 130 return true; |
| 131 } |
| 132 } |
| 133 return false; |
| 134 } |
| 135 |
| 136 |
| 137 static bool GetNativeUnsignedIntegerArgument(NativeArguments* arguments, |
| 138 int arg_index, |
| 139 uint64_t* value) { |
| 140 ASSERT(value != NULL); |
| 141 int64_t arg_value = 0; |
| 142 if (Api::GetNativeIntegerArgument(arguments, arg_index, &arg_value)) { |
| 143 *value = static_cast<uint64_t>(arg_value); |
| 144 return true; |
| 145 } |
| 146 Isolate* isolate = arguments->isolate(); |
| 147 ASSERT(isolate == Isolate::Current()); |
| 148 REUSABLE_OBJECT_HANDLESCOPE(isolate); |
| 149 Object& obj = isolate->ObjectHandle(); |
| 150 obj = arguments->NativeArgAt(arg_index); |
| 151 intptr_t cid = obj.GetClassId(); |
| 152 if (cid == kBigintCid) { |
| 153 const Bigint& bigint = Bigint::Cast(obj); |
| 154 if (BigintOperations::FitsIntoUint64(bigint)) { |
| 155 *value = BigintOperations::ToUint64(bigint); |
| 156 return true; |
| 157 } |
| 158 } |
| 159 return false; |
| 160 } |
| 161 |
| 162 |
| 163 static bool GetNativeDoubleArgument(NativeArguments* arguments, |
| 164 int arg_index, |
| 165 double* value) { |
| 166 ASSERT(value != NULL); |
| 167 if (Api::GetNativeDoubleArgument(arguments, arg_index, value)) { |
| 168 return true; |
| 169 } |
| 170 Isolate* isolate = arguments->isolate(); |
| 171 ASSERT(isolate == Isolate::Current()); |
| 172 REUSABLE_OBJECT_HANDLESCOPE(isolate); |
| 173 Object& obj = isolate->ObjectHandle(); |
| 174 obj = arguments->NativeArgAt(arg_index); |
| 175 intptr_t cid = obj.GetClassId(); |
| 176 if (cid == kBigintCid) { |
| 177 *value = Bigint::Cast(obj).AsDoubleValue(); |
| 178 return true; |
| 179 } |
| 180 return false; |
| 181 } |
| 182 |
| 183 |
| 184 static Dart_Handle GetNativeFieldsOfArgument(NativeArguments* arguments, |
| 185 int arg_index, |
| 186 int num_fields, |
| 187 intptr_t* field_values, |
| 188 const char* current_func) { |
| 189 ASSERT(field_values != NULL); |
| 190 if (Api::GetNativeFieldsOfArgument(arguments, |
| 191 arg_index, |
| 192 num_fields, |
| 193 field_values)) { |
| 194 return Api::Success(); |
| 195 } |
| 196 Isolate* isolate = arguments->isolate(); |
| 197 ASSERT(isolate == Isolate::Current()); |
| 198 REUSABLE_OBJECT_HANDLESCOPE(isolate); |
| 199 Object& obj = isolate->ObjectHandle(); |
| 200 obj = arguments->NativeArgAt(arg_index); |
| 201 if (obj.IsNull()) { |
| 202 memset(field_values, 0, (num_fields * sizeof(field_values[0]))); |
| 203 return Api::Success(); |
| 204 } |
| 205 // We did not succeed in extracting the native fields report the |
| 206 // appropriate error. |
| 207 if (!obj.IsInstance()) { |
| 208 return Api::NewError("%s expects argument at index '%d' to be of" |
| 209 " type Instance.", current_func, arg_index); |
| 210 } |
| 211 const Instance& instance = Instance::Cast(obj); |
| 212 int field_count = instance.NumNativeFields(); |
| 213 ASSERT(num_fields != field_count); |
| 214 return Api::NewError( |
| 215 "%s: expected %d 'num_fields' but was passed in %d.", |
| 216 current_func, field_count, num_fields); |
| 217 } |
| 218 |
| 219 |
84 Heap::Space SpaceForExternal(Isolate* isolate, intptr_t size) { | 220 Heap::Space SpaceForExternal(Isolate* isolate, intptr_t size) { |
85 Heap* heap = isolate->heap(); | 221 Heap* heap = isolate->heap(); |
86 // If 'size' would be a significant fraction of new space, then use old. | 222 // If 'size' would be a significant fraction of new space, then use old. |
87 static const int kExtNewRatio = 16; | 223 static const int kExtNewRatio = 16; |
88 if (size > (heap->CapacityInWords(Heap::kNew) * kWordSize) / kExtNewRatio) { | 224 if (size > (heap->CapacityInWords(Heap::kNew) * kWordSize) / kExtNewRatio) { |
89 return Heap::kOld; | 225 return Heap::kOld; |
90 } else { | 226 } else { |
91 return Heap::kNew; | 227 return Heap::kNew; |
92 } | 228 } |
93 } | 229 } |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 } | 485 } |
350 if (cid == kNullCid) { | 486 if (cid == kNullCid) { |
351 *value = false; | 487 *value = false; |
352 return true; | 488 return true; |
353 } | 489 } |
354 } | 490 } |
355 return false; | 491 return false; |
356 } | 492 } |
357 | 493 |
358 | 494 |
| 495 bool Api::GetNativeIntegerArgument(NativeArguments* arguments, |
| 496 int arg_index, |
| 497 int64_t* value) { |
| 498 NoGCScope no_gc_scope; |
| 499 RawObject* raw_obj = arguments->NativeArgAt(arg_index); |
| 500 if (raw_obj->IsHeapObject()) { |
| 501 intptr_t cid = raw_obj->GetClassId(); |
| 502 if (cid == kMintCid) { |
| 503 *value = reinterpret_cast<RawMint*>(raw_obj)->ptr()->value_; |
| 504 return true; |
| 505 } |
| 506 return false; |
| 507 } |
| 508 *value = Smi::Value(reinterpret_cast<RawSmi*>(raw_obj)); |
| 509 return true; |
| 510 } |
| 511 |
| 512 |
| 513 bool Api::GetNativeDoubleArgument(NativeArguments* arguments, |
| 514 int arg_index, |
| 515 double* value) { |
| 516 NoGCScope no_gc_scope; |
| 517 RawObject* raw_obj = arguments->NativeArgAt(arg_index); |
| 518 if (raw_obj->IsHeapObject()) { |
| 519 intptr_t cid = raw_obj->GetClassId(); |
| 520 if (cid == kDoubleCid) { |
| 521 *value = reinterpret_cast<RawDouble*>(raw_obj)->ptr()->value_; |
| 522 return true; |
| 523 } |
| 524 if (cid == kMintCid) { |
| 525 *value = static_cast<double>( |
| 526 reinterpret_cast<RawMint*>(raw_obj)->ptr()->value_); |
| 527 return true; |
| 528 } |
| 529 return false; |
| 530 } |
| 531 *value = static_cast<double>(Smi::Value(reinterpret_cast<RawSmi*>(raw_obj))); |
| 532 return true; |
| 533 } |
| 534 |
| 535 |
| 536 bool Api::GetNativeFieldsOfArgument(NativeArguments* arguments, |
| 537 int arg_index, |
| 538 int num_fields, |
| 539 intptr_t* field_values) { |
| 540 NoGCScope no_gc_scope; |
| 541 RawObject* raw_obj = arguments->NativeArgAt(arg_index); |
| 542 if (raw_obj->IsHeapObject()) { |
| 543 intptr_t cid = raw_obj->GetClassId(); |
| 544 if (cid > kNumPredefinedCids) { |
| 545 RawTypedData* native_fields = *reinterpret_cast<RawTypedData**>( |
| 546 RawObject::ToAddr(raw_obj) + sizeof(RawObject)); |
| 547 if (native_fields == TypedData::null()) { |
| 548 memset(field_values, 0, (num_fields * sizeof(field_values[0]))); |
| 549 } else if (num_fields == Smi::Value(native_fields->ptr()->length_)) { |
| 550 intptr_t* native_values = |
| 551 bit_cast<intptr_t*, uint8_t*>(native_fields->ptr()->data_); |
| 552 memmove(field_values, |
| 553 native_values, |
| 554 (num_fields * sizeof(field_values[0]))); |
| 555 } |
| 556 return true; |
| 557 } |
| 558 } |
| 559 return false; |
| 560 } |
| 561 |
| 562 |
359 void Api::SetWeakHandleReturnValue(NativeArguments* args, | 563 void Api::SetWeakHandleReturnValue(NativeArguments* args, |
360 Dart_WeakPersistentHandle retval) { | 564 Dart_WeakPersistentHandle retval) { |
361 args->SetReturnUnsafe(FinalizablePersistentHandle::Cast(retval)->raw()); | 565 args->SetReturnUnsafe(FinalizablePersistentHandle::Cast(retval)->raw()); |
362 } | 566 } |
363 | 567 |
364 | 568 |
365 PersistentHandle* PersistentHandle::Cast(Dart_PersistentHandle handle) { | 569 PersistentHandle* PersistentHandle::Cast(Dart_PersistentHandle handle) { |
366 ASSERT(Isolate::Current()->api_state()->IsValidPersistentHandle(handle)); | 570 ASSERT(Isolate::Current()->api_state()->IsValidPersistentHandle(handle)); |
367 return reinterpret_cast<PersistentHandle*>(handle); | 571 return reinterpret_cast<PersistentHandle*>(handle); |
368 } | 572 } |
(...skipping 3638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4007 | 4211 |
4008 | 4212 |
4009 DART_EXPORT void* Dart_GetNativeIsolateData(Dart_NativeArguments args) { | 4213 DART_EXPORT void* Dart_GetNativeIsolateData(Dart_NativeArguments args) { |
4010 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 4214 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
4011 Isolate* isolate = arguments->isolate(); | 4215 Isolate* isolate = arguments->isolate(); |
4012 ASSERT(isolate == Isolate::Current()); | 4216 ASSERT(isolate == Isolate::Current()); |
4013 return isolate->init_callback_data(); | 4217 return isolate->init_callback_data(); |
4014 } | 4218 } |
4015 | 4219 |
4016 | 4220 |
| 4221 DART_EXPORT Dart_Handle Dart_GetNativeArguments( |
| 4222 Dart_NativeArguments args, |
| 4223 int num_arguments, |
| 4224 const Dart_NativeArgument_Descriptor* argument_descriptors, |
| 4225 Dart_NativeArgument_Value* arg_values) { |
| 4226 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
| 4227 ASSERT(arguments->isolate() == Isolate::Current()); |
| 4228 if (arg_values == NULL) { |
| 4229 RETURN_NULL_ERROR(arg_values); |
| 4230 } |
| 4231 for (int i = 0; i < num_arguments; i++) { |
| 4232 Dart_NativeArgument_Descriptor desc = argument_descriptors[i]; |
| 4233 Dart_NativeArgument_Type arg_type = static_cast<Dart_NativeArgument_Type>( |
| 4234 desc.type); |
| 4235 int arg_index = desc.index; |
| 4236 ASSERT(arg_index >= 0 && arg_index < arguments->NativeArgCount()); |
| 4237 Dart_NativeArgument_Value* native_value = &(arg_values[i]); |
| 4238 switch (arg_type) { |
| 4239 case Dart_NativeArgument_kBool: |
| 4240 if (!Api::GetNativeBooleanArgument(arguments, |
| 4241 arg_index, |
| 4242 &(native_value->as_bool))) { |
| 4243 return Api::NewError("%s: expects argument at index %d to be of" |
| 4244 " type Boolean.", CURRENT_FUNC, i); |
| 4245 } |
| 4246 break; |
| 4247 |
| 4248 case Dart_NativeArgument_kInt32: { |
| 4249 int64_t value = 0; |
| 4250 if (!GetNativeIntegerArgument(arguments, |
| 4251 arg_index, |
| 4252 &value)) { |
| 4253 return Api::NewError("%s: expects argument at index %d to be of" |
| 4254 " type Integer.", CURRENT_FUNC, i); |
| 4255 } |
| 4256 if (value < INT_MIN || value > INT_MAX) { |
| 4257 return Api::NewError("%s: argument value at index %d is out of range", |
| 4258 CURRENT_FUNC, i); |
| 4259 } |
| 4260 native_value->as_int32 = static_cast<int32_t>(value); |
| 4261 break; |
| 4262 } |
| 4263 |
| 4264 case Dart_NativeArgument_kUint32: { |
| 4265 int64_t value = 0; |
| 4266 if (!GetNativeIntegerArgument(arguments, |
| 4267 arg_index, |
| 4268 &value)) { |
| 4269 return Api::NewError("%s: expects argument at index %d to be of" |
| 4270 " type Integer.", CURRENT_FUNC, i); |
| 4271 } |
| 4272 if (value < 0 || value > UINT_MAX) { |
| 4273 return Api::NewError("%s: argument value at index %d is out of range", |
| 4274 CURRENT_FUNC, i); |
| 4275 } |
| 4276 native_value->as_uint32 = static_cast<uint32_t>(value); |
| 4277 break; |
| 4278 } |
| 4279 |
| 4280 case Dart_NativeArgument_kInt64: { |
| 4281 int64_t value = 0; |
| 4282 if (!GetNativeIntegerArgument(arguments, |
| 4283 arg_index, |
| 4284 &value)) { |
| 4285 return Api::NewError("%s: expects argument at index %d to be of" |
| 4286 " type Integer.", CURRENT_FUNC, i); |
| 4287 } |
| 4288 native_value->as_int64 = value; |
| 4289 break; |
| 4290 } |
| 4291 |
| 4292 case Dart_NativeArgument_kUint64: { |
| 4293 uint64_t value = 0; |
| 4294 if (!GetNativeUnsignedIntegerArgument(arguments, |
| 4295 arg_index, |
| 4296 &value)) { |
| 4297 return Api::NewError("%s: expects argument at index %d to be of" |
| 4298 " type Integer.", CURRENT_FUNC, i); |
| 4299 } |
| 4300 native_value->as_uint64 = value; |
| 4301 break; |
| 4302 } |
| 4303 |
| 4304 case Dart_NativeArgument_kDouble: |
| 4305 if (!GetNativeDoubleArgument(arguments, |
| 4306 arg_index, |
| 4307 &(native_value->as_double))) { |
| 4308 return Api::NewError("%s: expects argument at index %d to be of" |
| 4309 " type Double.", CURRENT_FUNC, i); |
| 4310 } |
| 4311 break; |
| 4312 |
| 4313 case Dart_NativeArgument_kString: |
| 4314 if (!GetNativeStringArgument(arguments, |
| 4315 arg_index, |
| 4316 &(native_value->as_string.dart_str), |
| 4317 &(native_value->as_string.peer))) { |
| 4318 return Api::NewError("%s: expects argument at index %d to be of" |
| 4319 " type String.", CURRENT_FUNC, i); |
| 4320 } |
| 4321 break; |
| 4322 |
| 4323 case Dart_NativeArgument_kNativeFields: { |
| 4324 Dart_Handle result = GetNativeFieldsOfArgument( |
| 4325 arguments, |
| 4326 arg_index, |
| 4327 native_value->as_native_fields.num_fields, |
| 4328 native_value->as_native_fields.values, |
| 4329 CURRENT_FUNC); |
| 4330 if (result != Api::Success()) { |
| 4331 return result; |
| 4332 } |
| 4333 break; |
| 4334 } |
| 4335 |
| 4336 case Dart_NativeArgument_kInstance: { |
| 4337 Isolate* isolate = arguments->isolate(); |
| 4338 ASSERT(isolate == Isolate::Current()); |
| 4339 ASSERT(isolate->api_state() && |
| 4340 isolate->api_state()->top_scope() != NULL); |
| 4341 native_value->as_instance = |
| 4342 Api::NewHandle(isolate, arguments->NativeArgAt(arg_index)); |
| 4343 break; |
| 4344 } |
| 4345 |
| 4346 default: |
| 4347 return Api::NewError("%s: invalid argument type %d.", |
| 4348 CURRENT_FUNC, arg_type); |
| 4349 } |
| 4350 } |
| 4351 return Api::Success(); |
| 4352 } |
| 4353 |
| 4354 |
4017 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, | 4355 DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, |
4018 int index) { | 4356 int index) { |
4019 TRACE_API_CALL(CURRENT_FUNC); | 4357 TRACE_API_CALL(CURRENT_FUNC); |
4020 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 4358 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
4021 if ((index < 0) || (index >= arguments->NativeArgCount())) { | 4359 if ((index < 0) || (index >= arguments->NativeArgCount())) { |
4022 return Api::NewError( | 4360 return Api::NewError( |
4023 "%s: argument 'index' out of range. Expected 0..%d but saw %d.", | 4361 "%s: argument 'index' out of range. Expected 0..%d but saw %d.", |
4024 CURRENT_FUNC, arguments->NativeArgCount() - 1, index); | 4362 CURRENT_FUNC, arguments->NativeArgCount() - 1, index); |
4025 } | 4363 } |
4026 return Api::NewHandle(arguments->isolate(), arguments->NativeArgAt(index)); | 4364 return Api::NewHandle(arguments->isolate(), arguments->NativeArgAt(index)); |
(...skipping 14 matching lines...) Expand all Loading... |
4041 intptr_t* field_values) { | 4379 intptr_t* field_values) { |
4042 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 4380 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
4043 if ((arg_index < 0) || (arg_index >= arguments->NativeArgCount())) { | 4381 if ((arg_index < 0) || (arg_index >= arguments->NativeArgCount())) { |
4044 return Api::NewError( | 4382 return Api::NewError( |
4045 "%s: argument 'arg_index' out of range. Expected 0..%d but saw %d.", | 4383 "%s: argument 'arg_index' out of range. Expected 0..%d but saw %d.", |
4046 CURRENT_FUNC, arguments->NativeArgCount() - 1, arg_index); | 4384 CURRENT_FUNC, arguments->NativeArgCount() - 1, arg_index); |
4047 } | 4385 } |
4048 if (field_values == NULL) { | 4386 if (field_values == NULL) { |
4049 RETURN_NULL_ERROR(field_values); | 4387 RETURN_NULL_ERROR(field_values); |
4050 } | 4388 } |
4051 Isolate* isolate = arguments->isolate(); | 4389 return GetNativeFieldsOfArgument(arguments, |
4052 ASSERT(isolate == Isolate::Current()); | 4390 arg_index, |
4053 REUSABLE_OBJECT_HANDLESCOPE(isolate); | 4391 num_fields, |
4054 Object& obj = isolate->ObjectHandle(); | 4392 field_values, |
4055 obj = arguments->NativeArgAt(arg_index); | 4393 CURRENT_FUNC); |
4056 if (obj.IsNull()) { | |
4057 for (intptr_t i = 0; i < num_fields; i++) { | |
4058 field_values[i] = 0; | |
4059 } | |
4060 return Api::Success(); | |
4061 } | |
4062 if (!obj.IsInstance()) { | |
4063 return Api::NewError("%s expects argument at index '%d' to be of" | |
4064 " type Instance.", CURRENT_FUNC, arg_index); | |
4065 } | |
4066 const Instance& instance = Instance::Cast(obj); | |
4067 uint16_t field_count = instance.NumNativeFields(); | |
4068 if (num_fields != field_count) { | |
4069 return Api::NewError( | |
4070 "%s: invalid 'field_values' array specified for returning field values", | |
4071 CURRENT_FUNC); | |
4072 } | |
4073 instance.GetNativeFields(num_fields, field_values); | |
4074 return Api::Success(); | |
4075 } | 4394 } |
4076 | 4395 |
4077 | 4396 |
4078 DART_EXPORT Dart_Handle Dart_GetNativeReceiver(Dart_NativeArguments args, | 4397 DART_EXPORT Dart_Handle Dart_GetNativeReceiver(Dart_NativeArguments args, |
4079 intptr_t* value) { | 4398 intptr_t* value) { |
4080 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 4399 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
4081 ASSERT(arguments->isolate() == Isolate::Current()); | 4400 ASSERT(arguments->isolate() == Isolate::Current()); |
4082 if (value == NULL) { | 4401 if (value == NULL) { |
4083 RETURN_NULL_ERROR(value); | 4402 RETURN_NULL_ERROR(value); |
4084 } | 4403 } |
4085 if (Api::GetNativeReceiver(arguments, value)) { | 4404 if (Api::GetNativeReceiver(arguments, value)) { |
4086 return Api::Success(); | 4405 return Api::Success(); |
4087 } | 4406 } |
4088 return Api::NewError("%s expects receiver argument to be non-null and of" | 4407 return Api::NewError("%s expects receiver argument to be non-null and of" |
4089 " type Instance.", CURRENT_FUNC); | 4408 " type Instance.", CURRENT_FUNC); |
4090 } | 4409 } |
4091 | 4410 |
4092 | 4411 |
4093 DART_EXPORT Dart_Handle Dart_GetNativeStringArgument(Dart_NativeArguments args, | 4412 DART_EXPORT Dart_Handle Dart_GetNativeStringArgument(Dart_NativeArguments args, |
4094 int arg_index, | 4413 int arg_index, |
4095 void** peer) { | 4414 void** peer) { |
4096 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 4415 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
4097 Isolate* isolate = arguments->isolate(); | 4416 Dart_Handle result = Api::Null(); |
4098 ASSERT(isolate == Isolate::Current()); | 4417 if (!GetNativeStringArgument(arguments, arg_index, &result, peer)) { |
4099 if (Api::StringGetPeerHelper(arguments, arg_index, peer)) { | 4418 return Api::NewError("%s expects argument at %d to be of" |
4100 return Api::Success(); | 4419 " type String.", CURRENT_FUNC, arg_index); |
4101 } | 4420 } |
4102 *peer = NULL; | 4421 return result; |
4103 REUSABLE_OBJECT_HANDLESCOPE(isolate); | |
4104 Object& obj = isolate->ObjectHandle(); | |
4105 obj = arguments->NativeArgAt(arg_index); | |
4106 if (RawObject::IsStringClassId(obj.GetClassId())) { | |
4107 return Api::NewHandle(isolate, obj.raw()); | |
4108 } | |
4109 if (obj.IsNull()) { | |
4110 return Api::Null(); | |
4111 } | |
4112 return Api::NewError("%s expects argument to be of" | |
4113 " type String.", CURRENT_FUNC); | |
4114 } | 4422 } |
4115 | 4423 |
4116 | 4424 |
4117 DART_EXPORT Dart_Handle Dart_GetNativeIntegerArgument(Dart_NativeArguments args, | 4425 DART_EXPORT Dart_Handle Dart_GetNativeIntegerArgument(Dart_NativeArguments args, |
4118 int index, | 4426 int index, |
4119 int64_t* value) { | 4427 int64_t* value) { |
4120 TRACE_API_CALL(CURRENT_FUNC); | 4428 TRACE_API_CALL(CURRENT_FUNC); |
4121 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 4429 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
4122 if ((index < 0) || (index >= arguments->NativeArgCount())) { | 4430 if ((index < 0) || (index >= arguments->NativeArgCount())) { |
4123 return Api::NewError( | 4431 return Api::NewError( |
4124 "%s: argument 'index' out of range. Expected 0..%d but saw %d.", | 4432 "%s: argument 'index' out of range. Expected 0..%d but saw %d.", |
4125 CURRENT_FUNC, arguments->NativeArgCount() - 1, index); | 4433 CURRENT_FUNC, arguments->NativeArgCount() - 1, index); |
4126 } | 4434 } |
4127 Isolate* isolate = arguments->isolate(); | 4435 if (!GetNativeIntegerArgument(arguments, index, value)) { |
4128 ASSERT(isolate == Isolate::Current()); | 4436 return Api::NewError("%s: expects argument at %d to be of" |
4129 REUSABLE_OBJECT_HANDLESCOPE(isolate); | 4437 " type Integer.", CURRENT_FUNC, index); |
4130 Object& obj = isolate->ObjectHandle(); | |
4131 obj = arguments->NativeArgAt(index); | |
4132 intptr_t cid = obj.GetClassId(); | |
4133 if (cid == kSmiCid) { | |
4134 *value = Smi::Cast(obj).Value(); | |
4135 return Api::Success(); | |
4136 } | 4438 } |
4137 if (cid == kMintCid) { | 4439 return Api::Success(); |
4138 *value = Mint::Cast(obj).value(); | |
4139 return Api::Success(); | |
4140 } | |
4141 if (cid == kBigintCid) { | |
4142 const Bigint& bigint = Bigint::Cast(obj); | |
4143 if (BigintOperations::FitsIntoInt64(bigint)) { | |
4144 *value = BigintOperations::ToInt64(bigint); | |
4145 return Api::Success(); | |
4146 } | |
4147 return Api::NewError( | |
4148 "%s: argument %d is a big integer that does not fit in 'value'.", | |
4149 CURRENT_FUNC, index); | |
4150 } | |
4151 return Api::NewError( | |
4152 "%s: argument %d is not an Integer argument.", | |
4153 CURRENT_FUNC, index); | |
4154 } | 4440 } |
4155 | 4441 |
4156 | 4442 |
4157 DART_EXPORT Dart_Handle Dart_GetNativeBooleanArgument(Dart_NativeArguments args, | 4443 DART_EXPORT Dart_Handle Dart_GetNativeBooleanArgument(Dart_NativeArguments args, |
4158 int index, | 4444 int index, |
4159 bool* value) { | 4445 bool* value) { |
4160 TRACE_API_CALL(CURRENT_FUNC); | 4446 TRACE_API_CALL(CURRENT_FUNC); |
4161 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 4447 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
4162 if ((index < 0) || (index >= arguments->NativeArgCount())) { | 4448 if ((index < 0) || (index >= arguments->NativeArgCount())) { |
4163 return Api::NewError( | 4449 return Api::NewError( |
4164 "%s: argument 'index' out of range. Expected 0..%d but saw %d.", | 4450 "%s: argument 'index' out of range. Expected 0..%d but saw %d.", |
4165 CURRENT_FUNC, arguments->NativeArgCount() - 1, index); | 4451 CURRENT_FUNC, arguments->NativeArgCount() - 1, index); |
4166 } | 4452 } |
4167 if (Api::GetNativeBooleanArgument(arguments, index, value)) { | 4453 if (!Api::GetNativeBooleanArgument(arguments, index, value)) { |
4168 return Api::Success(); | 4454 return Api::NewError("%s: expects argument at %d to be of type Boolean.", |
| 4455 CURRENT_FUNC, index); |
4169 } | 4456 } |
4170 return Api::NewError("%s: argument %d is not a Boolean argument.", | 4457 return Api::Success(); |
4171 CURRENT_FUNC, index); | |
4172 } | 4458 } |
4173 | 4459 |
4174 | 4460 |
4175 DART_EXPORT Dart_Handle Dart_GetNativeDoubleArgument(Dart_NativeArguments args, | 4461 DART_EXPORT Dart_Handle Dart_GetNativeDoubleArgument(Dart_NativeArguments args, |
4176 int index, | 4462 int index, |
4177 double* value) { | 4463 double* value) { |
4178 TRACE_API_CALL(CURRENT_FUNC); | 4464 TRACE_API_CALL(CURRENT_FUNC); |
4179 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 4465 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
4180 if ((index < 0) || (index >= arguments->NativeArgCount())) { | 4466 if ((index < 0) || (index >= arguments->NativeArgCount())) { |
4181 return Api::NewError( | 4467 return Api::NewError( |
4182 "%s: argument 'index' out of range. Expected 0..%d but saw %d.", | 4468 "%s: argument 'index' out of range. Expected 0..%d but saw %d.", |
4183 CURRENT_FUNC, arguments->NativeArgCount() - 1, index); | 4469 CURRENT_FUNC, arguments->NativeArgCount() - 1, index); |
4184 } | 4470 } |
4185 Isolate* isolate = arguments->isolate(); | 4471 if (!GetNativeDoubleArgument(arguments, index, value)) { |
4186 ASSERT(isolate == Isolate::Current()); | 4472 return Api::NewError("%s: expects argument at %d to be of" |
4187 REUSABLE_OBJECT_HANDLESCOPE(isolate); | 4473 " type Double.", CURRENT_FUNC, index); |
4188 Object& obj = isolate->ObjectHandle(); | |
4189 obj = arguments->NativeArgAt(index); | |
4190 intptr_t cid = obj.GetClassId(); | |
4191 if (cid == kDoubleCid) { | |
4192 *value = Double::Cast(obj).value(); | |
4193 return Api::Success(); | |
4194 } | 4474 } |
4195 if (cid == kSmiCid) { | 4475 return Api::Success(); |
4196 *value = Smi::Cast(obj).AsDoubleValue(); | |
4197 return Api::Success(); | |
4198 } | |
4199 if (cid == kMintCid) { | |
4200 *value = Mint::Cast(obj).AsDoubleValue(); | |
4201 return Api::Success(); | |
4202 } | |
4203 if (cid == kBigintCid) { | |
4204 *value = Bigint::Cast(obj).AsDoubleValue(); | |
4205 return Api::Success(); | |
4206 } | |
4207 return Api::NewError( | |
4208 "%s: argument %d is not a Double argument.", | |
4209 CURRENT_FUNC, index); | |
4210 } | 4476 } |
4211 | 4477 |
4212 | 4478 |
4213 DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, | 4479 DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, |
4214 Dart_Handle retval) { | 4480 Dart_Handle retval) { |
4215 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); | 4481 NativeArguments* arguments = reinterpret_cast<NativeArguments*>(args); |
4216 ASSERT(arguments->isolate() == Isolate::Current()); | 4482 ASSERT(arguments->isolate() == Isolate::Current()); |
4217 if ((retval != Api::Null()) && (!Api::IsInstance(retval))) { | 4483 if ((retval != Api::Null()) && (!Api::IsInstance(retval))) { |
4218 const Object& ret_obj = Object::Handle(Api::UnwrapHandle(retval)); | 4484 const Object& ret_obj = Object::Handle(Api::UnwrapHandle(retval)); |
4219 FATAL1("Return value check failed: saw '%s' expected a dart Instance.", | 4485 FATAL1("Return value check failed: saw '%s' expected a dart Instance.", |
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4785 | 5051 |
4786 | 5052 |
4787 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( | 5053 DART_EXPORT void Dart_RegisterRootServiceRequestCallback( |
4788 const char* name, | 5054 const char* name, |
4789 Dart_ServiceRequestCallback callback, | 5055 Dart_ServiceRequestCallback callback, |
4790 void* user_data) { | 5056 void* user_data) { |
4791 Service::RegisterRootEmbedderCallback(name, callback, user_data); | 5057 Service::RegisterRootEmbedderCallback(name, callback, user_data); |
4792 } | 5058 } |
4793 | 5059 |
4794 } // namespace dart | 5060 } // namespace dart |
OLD | NEW |