Chromium Code Reviews| 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 "vm/bootstrap_natives.h" | 5 #include "vm/bootstrap_natives.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 | 8 |
| 9 #include "vm/bigint_operations.h" | 9 #include "vm/bigint_operations.h" |
| 10 #include "vm/exceptions.h" | 10 #include "vm/exceptions.h" |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 const String& error = String::Handle(String::NewFormatted( \ | 278 const String& error = String::Handle(String::NewFormatted( \ |
| 279 "Expected a TypedData object but found %s", instance.ToCString())); \ | 279 "Expected a TypedData object but found %s", instance.ToCString())); \ |
| 280 const Array& args = Array::Handle(Array::New(1)); \ | 280 const Array& args = Array::Handle(Array::New(1)); \ |
| 281 args.SetAt(0, error); \ | 281 args.SetAt(0, error); \ |
| 282 Exceptions::ThrowByType(Exceptions::kArgument, args); \ | 282 Exceptions::ThrowByType(Exceptions::kArgument, args); \ |
| 283 } \ | 283 } \ |
| 284 return Object::null(); \ | 284 return Object::null(); \ |
| 285 } | 285 } |
| 286 | 286 |
| 287 | 287 |
| 288 #define TYPED_DATA_NATIVES(name, getter, setter, object, get_object_value) \ | 288 #define TYPED_DATA_NATIVES(getter, setter, object, get_object_value) \ |
| 289 TYPED_DATA_GETTER(getter, object) \ | 289 TYPED_DATA_GETTER(getter, object) \ |
| 290 TYPED_DATA_SETTER(setter, object, get_object_value) \ | 290 TYPED_DATA_SETTER(setter, object, get_object_value) \ |
| 291 | 291 |
| 292 | 292 |
| 293 #define TYPED_DATA_UINT64_NATIVES(name, getter, setter, object) \ | 293 #define TYPED_DATA_UINT64_NATIVES(getter, setter, object) \ |
| 294 TYPED_DATA_UINT64_GETTER(getter, object) \ | 294 TYPED_DATA_UINT64_GETTER(getter, object) \ |
| 295 TYPED_DATA_UINT64_SETTER(setter, object) \ | 295 TYPED_DATA_UINT64_SETTER(setter, object) \ |
| 296 | 296 |
| 297 | 297 |
| 298 TYPED_DATA_NATIVES(Int8Array, GetInt8, SetInt8, Smi, Value) | 298 TYPED_DATA_NATIVES(GetInt8, SetInt8, Smi, Value) |
| 299 TYPED_DATA_NATIVES(Uint8Array, GetUint8, SetUint8, Smi, Value) | 299 TYPED_DATA_NATIVES(GetUint8, SetUint8, Smi, Value) |
| 300 TYPED_DATA_NATIVES(Int16Array, GetInt16, SetInt16, Smi, Value) | 300 TYPED_DATA_NATIVES(GetInt16, SetInt16, Smi, Value) |
| 301 TYPED_DATA_NATIVES(Uint16Array, GetUint16, SetUint16, Smi, Value) | 301 TYPED_DATA_NATIVES(GetUint16, SetUint16, Smi, Value) |
| 302 TYPED_DATA_NATIVES(Int32Array, GetInt32, SetInt32, Integer, AsInt64Value) | 302 TYPED_DATA_NATIVES(GetInt32, SetInt32, Integer, AsInt64Value) |
| 303 TYPED_DATA_NATIVES(Uint32Array, GetUint32, SetUint32, Integer, AsInt64Value) | 303 TYPED_DATA_NATIVES(GetUint32, SetUint32, Integer, AsInt64Value) |
| 304 TYPED_DATA_NATIVES(Int64Array, GetInt64, SetInt64, Integer, AsInt64Value) | 304 TYPED_DATA_NATIVES(GetInt64, SetInt64, Integer, AsInt64Value) |
| 305 TYPED_DATA_UINT64_NATIVES(Uint64Array, GetUint64, SetUint64, Integer) | 305 TYPED_DATA_UINT64_NATIVES(GetUint64, SetUint64, Integer) |
| 306 TYPED_DATA_NATIVES(Float32Array, GetFloat32, SetFloat32, Double, value) | 306 TYPED_DATA_NATIVES(GetFloat32, SetFloat32, Double, value) |
| 307 TYPED_DATA_NATIVES(Float64Array, GetFloat64, SetFloat64, Double, value) | 307 TYPED_DATA_NATIVES(GetFloat64, SetFloat64, Double, value) |
| 308 TYPED_DATA_NATIVES(Float32x4Array, GetFloat32x4, SetFloat32x4, Float32x4, value) | 308 TYPED_DATA_NATIVES(GetFloat32x4, SetFloat32x4, Float32x4, value) |
| 309 | |
| 310 | |
| 311 DEFINE_NATIVE_ENTRY(ByteData_ToEndianInt16, 2) { | |
| 312 GET_NON_NULL_NATIVE_ARGUMENT(Smi, host_value, arguments->NativeArgAt(0)); | |
| 313 GET_NON_NULL_NATIVE_ARGUMENT(Bool, little_endian, arguments->NativeArgAt(1)); | |
| 314 int16_t value = host_value.Value(); | |
| 315 if (little_endian.value()) { | |
| 316 value = Utils::HostToLittleEndian16(value); | |
| 317 } else { | |
| 318 value = Utils::HostToBigEndian16(value); | |
| 319 } | |
| 320 return Smi::New(value); | |
| 321 } | |
| 322 | |
| 323 | |
| 324 DEFINE_NATIVE_ENTRY(ByteData_ToEndianUint16, 2) { | |
| 325 GET_NON_NULL_NATIVE_ARGUMENT(Smi, host_value, arguments->NativeArgAt(0)); | |
| 326 GET_NON_NULL_NATIVE_ARGUMENT(Bool, little_endian, arguments->NativeArgAt(1)); | |
| 327 if (little_endian.value()) { | |
| 328 return Smi::New(Utils::HostToLittleEndian16(host_value.Value())); | |
|
Mads Ager (google)
2013/04/18 08:10:44
Maybe extract host_value.Value() as you have done
siva
2013/04/18 17:17:47
Done.
| |
| 329 } | |
| 330 return Smi::New(Utils::HostToBigEndian16(host_value.Value())); | |
| 331 } | |
| 332 | |
| 333 | |
| 334 DEFINE_NATIVE_ENTRY(ByteData_ToEndianInt32, 2) { | |
| 335 GET_NON_NULL_NATIVE_ARGUMENT(Integer, host_value, arguments->NativeArgAt(0)); | |
| 336 GET_NON_NULL_NATIVE_ARGUMENT(Bool, little_endian, arguments->NativeArgAt(1)); | |
| 337 ASSERT(host_value.AsInt64Value() <= kMaxInt32); | |
| 338 int32_t value = host_value.AsInt64Value(); | |
| 339 if (little_endian.value()) { | |
| 340 value = Utils::HostToLittleEndian32(value); | |
| 341 } else { | |
| 342 value = Utils::HostToBigEndian32(value); | |
| 343 } | |
| 344 return Integer::New(value); | |
| 345 } | |
| 346 | |
| 347 | |
| 348 DEFINE_NATIVE_ENTRY(ByteData_ToEndianUint32, 2) { | |
| 349 GET_NON_NULL_NATIVE_ARGUMENT(Integer, host_value, arguments->NativeArgAt(0)); | |
| 350 GET_NON_NULL_NATIVE_ARGUMENT(Bool, little_endian, arguments->NativeArgAt(1)); | |
| 351 ASSERT(host_value.AsInt64Value() <= kMaxUint32); | |
| 352 uint32_t value = host_value.AsInt64Value(); | |
| 353 if (little_endian.value()) { | |
| 354 value = Utils::HostToLittleEndian32(value); | |
| 355 } else { | |
| 356 value = Utils::HostToBigEndian32(value); | |
| 357 } | |
| 358 return Integer::New(value); | |
| 359 } | |
| 360 | |
| 361 | |
| 362 DEFINE_NATIVE_ENTRY(ByteData_ToEndianInt64, 2) { | |
| 363 GET_NON_NULL_NATIVE_ARGUMENT(Integer, host_value, arguments->NativeArgAt(0)); | |
| 364 GET_NON_NULL_NATIVE_ARGUMENT(Bool, little_endian, arguments->NativeArgAt(1)); | |
| 365 int64_t value = host_value.AsInt64Value(); | |
| 366 if (little_endian.value()) { | |
| 367 value = Utils::HostToLittleEndian64(value); | |
| 368 } else { | |
| 369 value = Utils::HostToBigEndian64(value); | |
| 370 } | |
| 371 return Integer::New(value); | |
| 372 } | |
| 373 | |
| 374 | |
| 375 DEFINE_NATIVE_ENTRY(ByteData_ToEndianUint64, 2) { | |
| 376 GET_NON_NULL_NATIVE_ARGUMENT(Integer, host_value, arguments->NativeArgAt(0)); | |
| 377 GET_NON_NULL_NATIVE_ARGUMENT(Bool, little_endian, arguments->NativeArgAt(1)); | |
| 378 uint64_t value; | |
| 379 if (host_value.IsBigint()) { | |
| 380 const Bigint& bigint = Bigint::Cast(host_value); | |
| 381 ASSERT(BigintOperations::FitsIntoUint64(bigint)); | |
| 382 value = BigintOperations::AbsToUint64(bigint); | |
| 383 } else { | |
| 384 ASSERT(host_value.IsMint() || host_value.IsSmi()); | |
| 385 value = host_value.AsInt64Value(); | |
| 386 } | |
| 387 if (little_endian.value()) { | |
| 388 value = Utils::HostToLittleEndian64(value); | |
| 389 } else { | |
| 390 value = Utils::HostToBigEndian64(value); | |
| 391 } | |
| 392 if (value > static_cast<uint64_t>(Mint::kMaxValue)) { | |
| 393 return BigintOperations::NewFromUint64(value); | |
| 394 } else if (value > static_cast<uint64_t>(Smi::kMaxValue)) { | |
| 395 return Mint::New(value); | |
| 396 } | |
| 397 return Smi::New(value); | |
| 398 } | |
| 399 | |
| 400 | |
| 401 DEFINE_NATIVE_ENTRY(ByteData_ToEndianFloat32, 2) { | |
| 402 GET_NON_NULL_NATIVE_ARGUMENT(Double, host_value, arguments->NativeArgAt(0)); | |
| 403 GET_NON_NULL_NATIVE_ARGUMENT(Bool, little_endian, arguments->NativeArgAt(1)); | |
| 404 float value = host_value.value(); | |
| 405 if (little_endian.value()) { | |
| 406 value = bit_cast<float>( | |
| 407 Utils::HostToLittleEndian32(bit_cast<uint32_t>(value))); | |
| 408 } else { | |
| 409 value = bit_cast<float>( | |
| 410 Utils::HostToBigEndian32(bit_cast<uint32_t>(value))); | |
| 411 } | |
| 412 return Double::New(value); | |
| 413 } | |
| 414 | |
| 415 | |
| 416 DEFINE_NATIVE_ENTRY(ByteData_ToEndianFloat64, 2) { | |
| 417 GET_NON_NULL_NATIVE_ARGUMENT(Double, host_value, arguments->NativeArgAt(0)); | |
| 418 GET_NON_NULL_NATIVE_ARGUMENT(Bool, little_endian, arguments->NativeArgAt(1)); | |
| 419 double value = host_value.value(); | |
| 420 if (little_endian.value()) { | |
| 421 value = bit_cast<double>( | |
| 422 Utils::HostToLittleEndian64(bit_cast<uint64_t>(value))); | |
| 423 } else { | |
| 424 value = bit_cast<double>( | |
| 425 Utils::HostToBigEndian64(bit_cast<uint64_t>(value))); | |
| 426 } | |
| 427 return Double::New(value); | |
| 428 } | |
| 309 | 429 |
| 310 } // namespace dart | 430 } // namespace dart |
| OLD | NEW |