| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "builtin/DataViewObject.h" |
| 8 |
|
| 9 |
#include "mozilla/Casting.h" |
| 10 |
#include "mozilla/EndianUtils.h" |
| 11 |
#include "mozilla/IntegerTypeTraits.h" |
| 12 |
#include "mozilla/WrappingOperations.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <string.h> |
| 16 |
#include <type_traits> |
| 17 |
|
| 18 |
#include "jsnum.h" |
| 19 |
|
| 20 |
#include "jit/AtomicOperations.h" |
| 21 |
#include "jit/InlinableNatives.h" |
| 22 |
#include "js/Conversions.h" |
| 23 |
#include "js/experimental/TypedData.h" // JS_NewDataView |
| 24 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 25 |
#include "js/PropertySpec.h" |
| 26 |
#include "js/Wrapper.h" |
| 27 |
#include "util/DifferentialTesting.h" |
| 28 |
#include "vm/ArrayBufferObject.h" |
| 29 |
#include "vm/Compartment.h" |
| 30 |
#include "vm/Float16.h" |
| 31 |
#include "vm/GlobalObject.h" |
| 32 |
#include "vm/Interpreter.h" |
| 33 |
#include "vm/JSContext.h" |
| 34 |
#include "vm/JSObject.h" |
| 35 |
#include "vm/SharedMem.h" |
| 36 |
#include "vm/Uint8Clamped.h" |
| 37 |
#include "vm/WrapperObject.h" |
| 38 |
|
| 39 |
#include "vm/ArrayBufferObject-inl.h" |
| 40 |
#include "vm/NativeObject-inl.h" |
| 41 |
|
| 42 |
using namespace js; |
| 43 |
|
| 44 |
using JS::CanonicalizeNaN; |
| 45 |
using JS::ToInt32; |
| 46 |
using mozilla::WrapToSigned; |
| 47 |
|
| 48 |
static bool IsDataView(HandleValue v) { |
| 49 |
return v.isObject() && v.toObject().is<DataViewObject>(); |
| 50 |
} |
| 51 |
|
| 52 |
DataViewObject* DataViewObject::create( |
| 53 |
JSContext* cx, size_t byteOffset, size_t byteLength, |
| 54 |
Handle<ArrayBufferObjectMaybeShared*> arrayBuffer, HandleObject proto) { |
| 55 |
MOZ_ASSERT(!arrayBuffer->isResizable()); |
| 56 |
MOZ_ASSERT(!arrayBuffer->isDetached()); |
| 57 |
|
| 58 |
auto* obj = NewObjectWithClassProto<FixedLengthDataViewObject>(cx, proto); |
| 59 |
if (!obj || !obj->init(cx, arrayBuffer, byteOffset, byteLength, |
| 60 |
/* bytesPerElement = */ 1)) { |
| 61 |
return nullptr; |
| 62 |
} |
| 63 |
|
| 64 |
return obj; |
| 65 |
} |
| 66 |
|
| 67 |
ResizableDataViewObject* ResizableDataViewObject::create( |
| 68 |
JSContext* cx, size_t byteOffset, size_t byteLength, AutoLength autoLength, |
| 69 |
Handle<ArrayBufferObjectMaybeShared*> arrayBuffer, HandleObject proto) { |
| 70 |
MOZ_ASSERT(arrayBuffer->isResizable()); |
| 71 |
MOZ_ASSERT(!arrayBuffer->isDetached()); |
| 72 |
MOZ_ASSERT(autoLength == AutoLength::No || byteLength == 0, |
| 73 |
"byte length is zero for 'auto' length views"); |
| 74 |
|
| 75 |
auto* obj = NewObjectWithClassProto<ResizableDataViewObject>(cx, proto); |
| 76 |
if (!obj || !obj->initResizable(cx, arrayBuffer, byteOffset, byteLength, |
| 77 |
/* bytesPerElement = */ 1, autoLength)) { |
| 78 |
return nullptr; |
| 79 |
} |
| 80 |
|
| 81 |
return obj; |
| 82 |
} |
| 83 |
|
| 84 |
// ES2017 draft rev 931261ecef9b047b14daacf82884134da48dfe0f |
| 85 |
// 24.3.2.1 DataView (extracted part of the main algorithm) |
| 86 |
bool DataViewObject::getAndCheckConstructorArgs( |
| 87 |
JSContext* cx, HandleObject bufobj, const CallArgs& args, |
| 88 |
size_t* byteOffsetPtr, size_t* byteLengthPtr, AutoLength* autoLengthPtr) { |
| 89 |
// Step 3. |
| 90 |
if (!bufobj->is<ArrayBufferObjectMaybeShared>()) { |
| 91 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 92 |
JSMSG_NOT_EXPECTED_TYPE, "DataView", |
| 93 |
"ArrayBuffer", bufobj->getClass()->name); |
| 94 |
return false; |
| 95 |
} |
| 96 |
auto buffer = bufobj.as<ArrayBufferObjectMaybeShared>(); |
| 97 |
|
| 98 |
// Step 4. |
| 99 |
uint64_t offset; |
| 100 |
if (!ToIndex(cx, args.get(1), &offset)) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
// Step 5. |
| 105 |
if (buffer->isDetached()) { |
| 106 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 107 |
JSMSG_TYPED_ARRAY_DETACHED); |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
// Step 6. |
| 112 |
size_t bufferByteLength = buffer->byteLength(); |
| 113 |
|
| 114 |
// Step 7. |
| 115 |
if (offset > bufferByteLength) { |
| 116 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 117 |
JSMSG_OFFSET_OUT_OF_BUFFER); |
| 118 |
return false; |
| 119 |
} |
| 120 |
MOZ_ASSERT(offset <= ArrayBufferObject::ByteLengthLimit); |
| 121 |
|
| 122 |
uint64_t viewByteLength = 0; |
| 123 |
auto autoLength = AutoLength::No; |
| 124 |
if (!args.hasDefined(2)) { |
| 125 |
if (buffer->isResizable()) { |
| 126 |
autoLength = AutoLength::Yes; |
| 127 |
} else { |
| 128 |
// Step 8.a |
| 129 |
viewByteLength = bufferByteLength - offset; |
| 130 |
} |
| 131 |
} else { |
| 132 |
// Step 9.a. |
| 133 |
if (!ToIndex(cx, args.get(2), &viewByteLength)) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
MOZ_ASSERT(offset + viewByteLength >= offset, |
| 138 |
"can't overflow: both numbers are less than " |
| 139 |
"DOUBLE_INTEGRAL_PRECISION_LIMIT"); |
| 140 |
|
| 141 |
// Step 9.b. |
| 142 |
if (offset + viewByteLength > bufferByteLength) { |
| 143 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 144 |
JSMSG_INVALID_DATA_VIEW_LENGTH); |
| 145 |
return false; |
| 146 |
} |
| 147 |
} |
| 148 |
MOZ_ASSERT(viewByteLength <= ArrayBufferObject::ByteLengthLimit); |
| 149 |
|
| 150 |
*byteOffsetPtr = offset; |
| 151 |
*byteLengthPtr = viewByteLength; |
| 152 |
*autoLengthPtr = autoLength; |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
static bool CheckConstructorArgs(JSContext* cx, |
| 157 |
Handle<ArrayBufferObjectMaybeShared*> buffer, |
| 158 |
size_t byteOffset, size_t byteLength) { |
| 159 |
if (buffer->isDetached()) { |
| 160 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 161 |
JSMSG_TYPED_ARRAY_DETACHED); |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
if (buffer->isResizable()) { |
| 166 |
size_t bufferByteLength = buffer->byteLength(); |
| 167 |
if (byteOffset + byteLength > bufferByteLength) { |
| 168 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 169 |
JSMSG_OFFSET_OUT_OF_BUFFER); |
| 170 |
return false; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
bool DataViewObject::constructSameCompartment(JSContext* cx, |
| 178 |
HandleObject bufobj, |
| 179 |
const CallArgs& args) { |
| 180 |
MOZ_ASSERT(args.isConstructing()); |
| 181 |
cx->check(bufobj); |
| 182 |
|
| 183 |
size_t byteOffset = 0; |
| 184 |
size_t byteLength = 0; |
| 185 |
auto autoLength = AutoLength::No; |
| 186 |
if (!getAndCheckConstructorArgs(cx, bufobj, args, &byteOffset, &byteLength, |
| 187 |
&autoLength)) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
RootedObject proto(cx); |
| 192 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_DataView, &proto)) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
auto buffer = bufobj.as<ArrayBufferObjectMaybeShared>(); |
| 197 |
|
| 198 |
// GetPrototypeFromBuiltinConstructor may have detached or resized the buffer, |
| 199 |
// so we have to revalidate the arguments. |
| 200 |
if (!CheckConstructorArgs(cx, buffer, byteOffset, byteLength)) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
DataViewObject* obj; |
| 205 |
if (!buffer->isResizable()) { |
| 206 |
obj = DataViewObject::create(cx, byteOffset, byteLength, buffer, proto); |
| 207 |
} else { |
| 208 |
obj = ResizableDataViewObject::create(cx, byteOffset, byteLength, |
| 209 |
autoLength, buffer, proto); |
| 210 |
} |
| 211 |
if (!obj) { |
| 212 |
return false; |
| 213 |
} |
| 214 |
args.rval().setObject(*obj); |
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
// Create a DataView object in another compartment. |
| 219 |
// |
| 220 |
// ES6 supports creating a DataView in global A (using global A's DataView |
| 221 |
// constructor) backed by an ArrayBuffer created in global B. |
| 222 |
// |
| 223 |
// Our DataViewObject implementation doesn't support a DataView in |
| 224 |
// compartment A backed by an ArrayBuffer in compartment B. So in this case, |
| 225 |
// we create the DataView in B (!) and return a cross-compartment wrapper. |
| 226 |
// |
| 227 |
// Extra twist: the spec says the new DataView's [[Prototype]] must be |
| 228 |
// A's DataView.prototype. So even though we're creating the DataView in B, |
| 229 |
// its [[Prototype]] must be (a cross-compartment wrapper for) the |
| 230 |
// DataView.prototype in A. |
| 231 |
bool DataViewObject::constructWrapped(JSContext* cx, HandleObject bufobj, |
| 232 |
const CallArgs& args) { |
| 233 |
MOZ_ASSERT(args.isConstructing()); |
| 234 |
MOZ_ASSERT(bufobj->is<WrapperObject>()); |
| 235 |
|
| 236 |
RootedObject unwrapped(cx, CheckedUnwrapStatic(bufobj)); |
| 237 |
if (!unwrapped) { |
| 238 |
ReportAccessDenied(cx); |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
// NB: This entails the IsArrayBuffer check |
| 243 |
size_t byteOffset = 0; |
| 244 |
size_t byteLength = 0; |
| 245 |
auto autoLength = AutoLength::No; |
| 246 |
if (!getAndCheckConstructorArgs(cx, unwrapped, args, &byteOffset, &byteLength, |
| 247 |
&autoLength)) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
// Make sure to get the [[Prototype]] for the created view from this |
| 252 |
// compartment. |
| 253 |
RootedObject proto(cx); |
| 254 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_DataView, &proto)) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
auto unwrappedBuffer = unwrapped.as<ArrayBufferObjectMaybeShared>(); |
| 259 |
|
| 260 |
// GetPrototypeFromBuiltinConstructor may have detached or resized the buffer, |
| 261 |
// so we have to revalidate the arguments. |
| 262 |
if (!CheckConstructorArgs(cx, unwrappedBuffer, byteOffset, byteLength)) { |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
Rooted<GlobalObject*> global(cx, cx->realm()->maybeGlobal()); |
| 267 |
if (!proto) { |
| 268 |
proto = GlobalObject::getOrCreateDataViewPrototype(cx, global); |
| 269 |
if (!proto) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
RootedObject dv(cx); |
| 275 |
{ |
| 276 |
JSAutoRealm ar(cx, unwrapped); |
| 277 |
|
| 278 |
RootedObject wrappedProto(cx, proto); |
| 279 |
if (!cx->compartment()->wrap(cx, &wrappedProto)) { |
| 280 |
return false; |
| 281 |
} |
| 282 |
|
| 283 |
if (!unwrappedBuffer->isResizable()) { |
| 284 |
dv = DataViewObject::create(cx, byteOffset, byteLength, unwrappedBuffer, |
| 285 |
wrappedProto); |
| 286 |
} else { |
| 287 |
dv = ResizableDataViewObject::create(cx, byteOffset, byteLength, |
| 288 |
autoLength, unwrappedBuffer, |
| 289 |
wrappedProto); |
| 290 |
} |
| 291 |
if (!dv) { |
| 292 |
return false; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if (!cx->compartment()->wrap(cx, &dv)) { |
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
args.rval().setObject(*dv); |
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
bool DataViewObject::construct(JSContext* cx, unsigned argc, Value* vp) { |
| 305 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 306 |
|
| 307 |
if (!ThrowIfNotConstructing(cx, args, "DataView")) { |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
RootedObject bufobj(cx); |
| 312 |
if (!GetFirstArgumentAsObject(cx, args, "DataView constructor", &bufobj)) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
if (bufobj->is<WrapperObject>()) { |
| 317 |
return constructWrapped(cx, bufobj, args); |
| 318 |
} |
| 319 |
return constructSameCompartment(cx, bufobj, args); |
| 320 |
} |
| 321 |
|
| 322 |
template <typename NativeType> |
| 323 |
SharedMem<uint8_t*> DataViewObject::getDataPointer(uint64_t offset, |
| 324 |
size_t length, |
| 325 |
bool* isSharedMemory) { |
| 326 |
MOZ_ASSERT(length <= *byteLength()); |
| 327 |
MOZ_ASSERT(offsetIsInBounds<NativeType>(offset, length)); |
| 328 |
|
| 329 |
MOZ_ASSERT(offset < SIZE_MAX); |
| 330 |
*isSharedMemory = this->isSharedMemory(); |
| 331 |
return dataPointerEither().cast<uint8_t*>() + size_t(offset); |
| 332 |
} |
| 333 |
|
| 334 |
static void ReportOutOfBounds(JSContext* cx, DataViewObject* dataView) { |
| 335 |
if (dataView->hasDetachedBuffer()) { |
| 336 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 337 |
JSMSG_TYPED_ARRAY_DETACHED); |
| 338 |
} else { |
| 339 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 340 |
JSMSG_TYPED_ARRAY_RESIZED_BOUNDS); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
template <typename T> |
| 345 |
static inline std::enable_if_t<sizeof(T) != 1> SwapBytes(T* value, |
| 346 |
bool isLittleEndian) { |
| 347 |
if (isLittleEndian) { |
| 348 |
mozilla::NativeEndian::swapToLittleEndianInPlace(value, 1); |
| 349 |
} else { |
| 350 |
mozilla::NativeEndian::swapToBigEndianInPlace(value, 1); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
template <typename T> |
| 355 |
static inline std::enable_if_t<sizeof(T) == 1> SwapBytes(T* value, |
| 356 |
bool isLittleEndian) { |
| 357 |
// mozilla::NativeEndian doesn't support int8_t/uint8_t types. |
| 358 |
} |
| 359 |
|
| 360 |
static inline void Memcpy(uint8_t* dest, uint8_t* src, size_t nbytes) { |
| 361 |
memcpy(dest, src, nbytes); |
| 362 |
} |
| 363 |
|
| 364 |
static inline void Memcpy(uint8_t* dest, SharedMem<uint8_t*> src, |
| 365 |
size_t nbytes) { |
| 366 |
jit::AtomicOperations::memcpySafeWhenRacy(dest, src, nbytes); |
| 367 |
} |
| 368 |
|
| 369 |
static inline void Memcpy(SharedMem<uint8_t*> dest, uint8_t* src, |
| 370 |
size_t nbytes) { |
| 371 |
jit::AtomicOperations::memcpySafeWhenRacy(dest, src, nbytes); |
| 372 |
} |
| 373 |
|
| 374 |
template <typename DataType, typename BufferPtrType> |
| 375 |
struct DataViewIO { |
| 376 |
using ReadWriteType = |
| 377 |
typename mozilla::UnsignedStdintTypeForSize<sizeof(DataType)>::Type; |
| 378 |
|
| 379 |
static constexpr auto alignMask = |
| 380 |
std::min<size_t>(alignof(void*), sizeof(DataType)) - 1; |
| 381 |
|
| 382 |
static void fromBuffer(DataType* dest, BufferPtrType unalignedBuffer, |
| 383 |
bool isLittleEndian) { |
| 384 |
MOZ_ASSERT((reinterpret_cast<uintptr_t>(dest) & alignMask) == 0); |
| 385 |
Memcpy((uint8_t*)dest, unalignedBuffer, sizeof(ReadWriteType)); |
| 386 |
ReadWriteType* rwDest = reinterpret_cast<ReadWriteType*>(dest); |
| 387 |
SwapBytes(rwDest, isLittleEndian); |
| 388 |
} |
| 389 |
|
| 390 |
static void toBuffer(BufferPtrType unalignedBuffer, const DataType* src, |
| 391 |
bool isLittleEndian) { |
| 392 |
MOZ_ASSERT((reinterpret_cast<uintptr_t>(src) & alignMask) == 0); |
| 393 |
ReadWriteType temp = *reinterpret_cast<const ReadWriteType*>(src); |
| 394 |
SwapBytes(&temp, isLittleEndian); |
| 395 |
Memcpy(unalignedBuffer, (uint8_t*)&temp, sizeof(ReadWriteType)); |
| 396 |
} |
| 397 |
}; |
| 398 |
|
| 399 |
template <typename NativeType> |
| 400 |
NativeType DataViewObject::read(uint64_t offset, size_t length, |
| 401 |
bool isLittleEndian) { |
| 402 |
bool isSharedMemory; |
| 403 |
SharedMem<uint8_t*> data = |
| 404 |
getDataPointer<NativeType>(offset, length, &isSharedMemory); |
| 405 |
MOZ_ASSERT(data); |
| 406 |
|
| 407 |
NativeType val{}; |
| 408 |
if (isSharedMemory) { |
| 409 |
DataViewIO<NativeType, SharedMem<uint8_t*>>::fromBuffer(&val, data, |
| 410 |
isLittleEndian); |
| 411 |
} else { |
| 412 |
DataViewIO<NativeType, uint8_t*>::fromBuffer(&val, data.unwrapUnshared(), |
| 413 |
isLittleEndian); |
| 414 |
} |
| 415 |
|
| 416 |
return val; |
| 417 |
} |
| 418 |
|
| 419 |
template uint32_t DataViewObject::read(uint64_t offset, size_t length, |
| 420 |
bool isLittleEndian); |
| 421 |
|
| 422 |
// https://tc39.github.io/ecma262/#sec-getviewvalue |
| 423 |
// GetViewValue ( view, requestIndex, isLittleEndian, type ) |
| 424 |
template <typename NativeType> |
| 425 |
/* static */ |
| 426 |
bool DataViewObject::read(JSContext* cx, Handle<DataViewObject*> obj, |
| 427 |
const CallArgs& args, NativeType* val) { |
| 428 |
// Step 1. done by the caller |
| 429 |
// Step 2. unnecessary assert |
| 430 |
|
| 431 |
// Step 3. |
| 432 |
uint64_t getIndex; |
| 433 |
if (!ToIndex(cx, args.get(0), &getIndex)) { |
| 434 |
return false; |
| 435 |
} |
| 436 |
|
| 437 |
// Step 4. |
| 438 |
bool isLittleEndian = args.length() >= 2 && ToBoolean(args[1]); |
| 439 |
|
| 440 |
// Steps 5-6. |
| 441 |
auto viewSize = obj->byteLength(); |
| 442 |
if (MOZ_UNLIKELY(!viewSize)) { |
| 443 |
ReportOutOfBounds(cx, obj); |
| 444 |
return false; |
| 445 |
} |
| 446 |
|
| 447 |
// Steps 7-10. |
| 448 |
if (!offsetIsInBounds<NativeType>(getIndex, *viewSize)) { |
| 449 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 450 |
JSMSG_OFFSET_OUT_OF_DATAVIEW); |
| 451 |
return false; |
| 452 |
} |
| 453 |
|
| 454 |
// Steps 11-12. |
| 455 |
*val = obj->read<NativeType>(getIndex, *viewSize, isLittleEndian); |
| 456 |
return true; |
| 457 |
} |
| 458 |
|
| 459 |
template <typename T> |
| 460 |
static inline T WrappingConvert(int32_t value) { |
| 461 |
if (std::is_unsigned_v<T>) { |
| 462 |
return static_cast<T>(value); |
| 463 |
} |
| 464 |
|
| 465 |
return WrapToSigned(static_cast<typename std::make_unsigned_t<T>>(value)); |
| 466 |
} |
| 467 |
|
| 468 |
template <typename NativeType> |
| 469 |
static inline bool WebIDLCast(JSContext* cx, HandleValue value, |
| 470 |
NativeType* out) { |
| 471 |
int32_t i; |
| 472 |
if (!ToInt32(cx, value, &i)) { |
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
*out = WrappingConvert<NativeType>(i); |
| 477 |
return true; |
| 478 |
} |
| 479 |
|
| 480 |
template <> |
| 481 |
inline bool WebIDLCast<int64_t>(JSContext* cx, HandleValue value, |
| 482 |
int64_t* out) { |
| 483 |
BigInt* bi = ToBigInt(cx, value); |
| 484 |
if (!bi) { |
| 485 |
return false; |
| 486 |
} |
| 487 |
*out = BigInt::toInt64(bi); |
| 488 |
return true; |
| 489 |
} |
| 490 |
|
| 491 |
template <> |
| 492 |
inline bool WebIDLCast<uint64_t>(JSContext* cx, HandleValue value, |
| 493 |
uint64_t* out) { |
| 494 |
BigInt* bi = ToBigInt(cx, value); |
| 495 |
if (!bi) { |
| 496 |
return false; |
| 497 |
} |
| 498 |
*out = BigInt::toUint64(bi); |
| 499 |
return true; |
| 500 |
} |
| 501 |
|
| 502 |
template <> |
| 503 |
inline bool WebIDLCast<float16>(JSContext* cx, HandleValue value, |
| 504 |
float16* out) { |
| 505 |
double temp; |
| 506 |
if (!ToNumber(cx, value, &temp)) { |
| 507 |
return false; |
| 508 |
} |
| 509 |
*out = float16(temp); |
| 510 |
return true; |
| 511 |
} |
| 512 |
|
| 513 |
template <> |
| 514 |
inline bool WebIDLCast<float>(JSContext* cx, HandleValue value, float* out) { |
| 515 |
double temp; |
| 516 |
if (!ToNumber(cx, value, &temp)) { |
| 517 |
return false; |
| 518 |
} |
| 519 |
*out = static_cast<float>(temp); |
| 520 |
return true; |
| 521 |
} |
| 522 |
|
| 523 |
template <> |
| 524 |
inline bool WebIDLCast<double>(JSContext* cx, HandleValue value, double* out) { |
| 525 |
return ToNumber(cx, value, out); |
| 526 |
} |
| 527 |
|
| 528 |
// https://tc39.github.io/ecma262/#sec-setviewvalue |
| 529 |
// SetViewValue ( view, requestIndex, isLittleEndian, type, value ) |
| 530 |
template <typename NativeType> |
| 531 |
/* static */ |
| 532 |
bool DataViewObject::write(JSContext* cx, Handle<DataViewObject*> obj, |
| 533 |
const CallArgs& args) { |
| 534 |
// Step 1. done by the caller |
| 535 |
// Step 2. unnecessary assert |
| 536 |
|
| 537 |
// Step 3. |
| 538 |
uint64_t getIndex; |
| 539 |
if (!ToIndex(cx, args.get(0), &getIndex)) { |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
// Steps 4-5. Call ToBigInt(value) or ToNumber(value) depending on the type. |
| 544 |
NativeType value; |
| 545 |
if (!WebIDLCast(cx, args.get(1), &value)) { |
| 546 |
return false; |
| 547 |
} |
| 548 |
|
| 549 |
// See the comment in ElementSpecific::doubleToNative. |
| 550 |
if constexpr (!std::numeric_limits<NativeType>::is_integer) { |
| 551 |
if (js::SupportDifferentialTesting()) { |
| 552 |
value = JS::CanonicalizeNaN(static_cast<double>(value)); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
// Step 6. |
| 557 |
bool isLittleEndian = args.length() >= 3 && ToBoolean(args[2]); |
| 558 |
|
| 559 |
// Steps 7-8. |
| 560 |
auto viewSize = obj->byteLength(); |
| 561 |
if (MOZ_UNLIKELY(!viewSize)) { |
| 562 |
ReportOutOfBounds(cx, obj); |
| 563 |
return false; |
| 564 |
} |
| 565 |
|
| 566 |
// Steps 9-12. |
| 567 |
if (!offsetIsInBounds<NativeType>(getIndex, *viewSize)) { |
| 568 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 569 |
JSMSG_OFFSET_OUT_OF_DATAVIEW); |
| 570 |
return false; |
| 571 |
} |
| 572 |
|
| 573 |
// Steps 13-14. |
| 574 |
bool isSharedMemory; |
| 575 |
SharedMem<uint8_t*> data = |
| 576 |
obj->getDataPointer<NativeType>(getIndex, *viewSize, &isSharedMemory); |
| 577 |
MOZ_ASSERT(data); |
| 578 |
|
| 579 |
if (isSharedMemory) { |
| 580 |
DataViewIO<NativeType, SharedMem<uint8_t*>>::toBuffer(data, &value, |
| 581 |
isLittleEndian); |
| 582 |
} else { |
| 583 |
DataViewIO<NativeType, uint8_t*>::toBuffer(data.unwrapUnshared(), &value, |
| 584 |
isLittleEndian); |
| 585 |
} |
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
bool DataViewObject::getInt8Impl(JSContext* cx, const CallArgs& args) { |
| 590 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 591 |
|
| 592 |
Rooted<DataViewObject*> thisView( |
| 593 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 594 |
|
| 595 |
int8_t val; |
| 596 |
if (!read(cx, thisView, args, &val)) { |
| 597 |
return false; |
| 598 |
} |
| 599 |
args.rval().setInt32(val); |
| 600 |
return true; |
| 601 |
} |
| 602 |
|
| 603 |
bool DataViewObject::fun_getInt8(JSContext* cx, unsigned argc, Value* vp) { |
| 604 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 605 |
return CallNonGenericMethod<IsDataView, getInt8Impl>(cx, args); |
| 606 |
} |
| 607 |
|
| 608 |
bool DataViewObject::getUint8Impl(JSContext* cx, const CallArgs& args) { |
| 609 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 610 |
|
| 611 |
Rooted<DataViewObject*> thisView( |
| 612 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 613 |
|
| 614 |
uint8_t val; |
| 615 |
if (!read(cx, thisView, args, &val)) { |
| 616 |
return false; |
| 617 |
} |
| 618 |
args.rval().setInt32(val); |
| 619 |
return true; |
| 620 |
} |
| 621 |
|
| 622 |
bool DataViewObject::fun_getUint8(JSContext* cx, unsigned argc, Value* vp) { |
| 623 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 624 |
return CallNonGenericMethod<IsDataView, getUint8Impl>(cx, args); |
| 625 |
} |
| 626 |
|
| 627 |
bool DataViewObject::getInt16Impl(JSContext* cx, const CallArgs& args) { |
| 628 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 629 |
|
| 630 |
Rooted<DataViewObject*> thisView( |
| 631 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 632 |
|
| 633 |
int16_t val; |
| 634 |
if (!read(cx, thisView, args, &val)) { |
| 635 |
return false; |
| 636 |
} |
| 637 |
args.rval().setInt32(val); |
| 638 |
return true; |
| 639 |
} |
| 640 |
|
| 641 |
bool DataViewObject::fun_getInt16(JSContext* cx, unsigned argc, Value* vp) { |
| 642 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 643 |
return CallNonGenericMethod<IsDataView, getInt16Impl>(cx, args); |
| 644 |
} |
| 645 |
|
| 646 |
bool DataViewObject::getUint16Impl(JSContext* cx, const CallArgs& args) { |
| 647 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 648 |
|
| 649 |
Rooted<DataViewObject*> thisView( |
| 650 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 651 |
|
| 652 |
uint16_t val; |
| 653 |
if (!read(cx, thisView, args, &val)) { |
| 654 |
return false; |
| 655 |
} |
| 656 |
args.rval().setInt32(val); |
| 657 |
return true; |
| 658 |
} |
| 659 |
|
| 660 |
bool DataViewObject::fun_getUint16(JSContext* cx, unsigned argc, Value* vp) { |
| 661 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 662 |
return CallNonGenericMethod<IsDataView, getUint16Impl>(cx, args); |
| 663 |
} |
| 664 |
|
| 665 |
bool DataViewObject::getInt32Impl(JSContext* cx, const CallArgs& args) { |
| 666 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 667 |
|
| 668 |
Rooted<DataViewObject*> thisView( |
| 669 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 670 |
|
| 671 |
int32_t val; |
| 672 |
if (!read(cx, thisView, args, &val)) { |
| 673 |
return false; |
| 674 |
} |
| 675 |
args.rval().setInt32(val); |
| 676 |
return true; |
| 677 |
} |
| 678 |
|
| 679 |
bool DataViewObject::fun_getInt32(JSContext* cx, unsigned argc, Value* vp) { |
| 680 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 681 |
return CallNonGenericMethod<IsDataView, getInt32Impl>(cx, args); |
| 682 |
} |
| 683 |
|
| 684 |
bool DataViewObject::getUint32Impl(JSContext* cx, const CallArgs& args) { |
| 685 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 686 |
|
| 687 |
Rooted<DataViewObject*> thisView( |
| 688 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 689 |
|
| 690 |
uint32_t val; |
| 691 |
if (!read(cx, thisView, args, &val)) { |
| 692 |
return false; |
| 693 |
} |
| 694 |
args.rval().setNumber(val); |
| 695 |
return true; |
| 696 |
} |
| 697 |
|
| 698 |
bool DataViewObject::fun_getUint32(JSContext* cx, unsigned argc, Value* vp) { |
| 699 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 700 |
return CallNonGenericMethod<IsDataView, getUint32Impl>(cx, args); |
| 701 |
} |
| 702 |
|
| 703 |
// BigInt proposal 7.26 |
| 704 |
// DataView.prototype.getBigInt64 ( byteOffset [ , littleEndian ] ) |
| 705 |
bool DataViewObject::getBigInt64Impl(JSContext* cx, const CallArgs& args) { |
| 706 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 707 |
|
| 708 |
Rooted<DataViewObject*> thisView( |
| 709 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 710 |
|
| 711 |
int64_t val; |
| 712 |
if (!read(cx, thisView, args, &val)) { |
| 713 |
return false; |
| 714 |
} |
| 715 |
|
| 716 |
BigInt* bi = BigInt::createFromInt64(cx, val); |
| 717 |
if (!bi) { |
| 718 |
return false; |
| 719 |
} |
| 720 |
args.rval().setBigInt(bi); |
| 721 |
return true; |
| 722 |
} |
| 723 |
|
| 724 |
bool DataViewObject::fun_getBigInt64(JSContext* cx, unsigned argc, Value* vp) { |
| 725 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 726 |
return CallNonGenericMethod<IsDataView, getBigInt64Impl>(cx, args); |
| 727 |
} |
| 728 |
|
| 729 |
// BigInt proposal 7.27 |
| 730 |
// DataView.prototype.getBigUint64 ( byteOffset [ , littleEndian ] ) |
| 731 |
bool DataViewObject::getBigUint64Impl(JSContext* cx, const CallArgs& args) { |
| 732 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 733 |
|
| 734 |
Rooted<DataViewObject*> thisView( |
| 735 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 736 |
|
| 737 |
int64_t val; |
| 738 |
if (!read(cx, thisView, args, &val)) { |
| 739 |
return false; |
| 740 |
} |
| 741 |
|
| 742 |
BigInt* bi = BigInt::createFromUint64(cx, val); |
| 743 |
if (!bi) { |
| 744 |
return false; |
| 745 |
} |
| 746 |
args.rval().setBigInt(bi); |
| 747 |
return true; |
| 748 |
} |
| 749 |
|
| 750 |
bool DataViewObject::fun_getBigUint64(JSContext* cx, unsigned argc, Value* vp) { |
| 751 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 752 |
return CallNonGenericMethod<IsDataView, getBigUint64Impl>(cx, args); |
| 753 |
} |
| 754 |
|
| 755 |
bool DataViewObject::getFloat16Impl(JSContext* cx, const CallArgs& args) { |
| 756 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 757 |
|
| 758 |
Rooted<DataViewObject*> thisView( |
| 759 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 760 |
|
| 761 |
float16 val{}; |
| 762 |
if (!read(cx, thisView, args, &val)) { |
| 763 |
return false; |
| 764 |
} |
| 765 |
|
| 766 |
args.rval().setDouble(CanonicalizeNaN(static_cast<double>(val))); |
| 767 |
return true; |
| 768 |
} |
| 769 |
|
| 770 |
bool DataViewObject::fun_getFloat16(JSContext* cx, unsigned argc, Value* vp) { |
| 771 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 772 |
return CallNonGenericMethod<IsDataView, getFloat16Impl>(cx, args); |
| 773 |
} |
| 774 |
|
| 775 |
bool DataViewObject::getFloat32Impl(JSContext* cx, const CallArgs& args) { |
| 776 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 777 |
|
| 778 |
Rooted<DataViewObject*> thisView( |
| 779 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 780 |
|
| 781 |
float val; |
| 782 |
if (!read(cx, thisView, args, &val)) { |
| 783 |
return false; |
| 784 |
} |
| 785 |
|
| 786 |
args.rval().setDouble(CanonicalizeNaN(val)); |
| 787 |
return true; |
| 788 |
} |
| 789 |
|
| 790 |
bool DataViewObject::fun_getFloat32(JSContext* cx, unsigned argc, Value* vp) { |
| 791 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 792 |
return CallNonGenericMethod<IsDataView, getFloat32Impl>(cx, args); |
| 793 |
} |
| 794 |
|
| 795 |
bool DataViewObject::getFloat64Impl(JSContext* cx, const CallArgs& args) { |
| 796 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 797 |
|
| 798 |
Rooted<DataViewObject*> thisView( |
| 799 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 800 |
|
| 801 |
double val; |
| 802 |
if (!read(cx, thisView, args, &val)) { |
| 803 |
return false; |
| 804 |
} |
| 805 |
|
| 806 |
args.rval().setDouble(CanonicalizeNaN(val)); |
| 807 |
return true; |
| 808 |
} |
| 809 |
|
| 810 |
bool DataViewObject::fun_getFloat64(JSContext* cx, unsigned argc, Value* vp) { |
| 811 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 812 |
return CallNonGenericMethod<IsDataView, getFloat64Impl>(cx, args); |
| 813 |
} |
| 814 |
|
| 815 |
bool DataViewObject::setInt8Impl(JSContext* cx, const CallArgs& args) { |
| 816 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 817 |
|
| 818 |
Rooted<DataViewObject*> thisView( |
| 819 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 820 |
|
| 821 |
if (!write<int8_t>(cx, thisView, args)) { |
| 822 |
return false; |
| 823 |
} |
| 824 |
args.rval().setUndefined(); |
| 825 |
return true; |
| 826 |
} |
| 827 |
|
| 828 |
bool DataViewObject::fun_setInt8(JSContext* cx, unsigned argc, Value* vp) { |
| 829 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 830 |
return CallNonGenericMethod<IsDataView, setInt8Impl>(cx, args); |
| 831 |
} |
| 832 |
|
| 833 |
bool DataViewObject::setUint8Impl(JSContext* cx, const CallArgs& args) { |
| 834 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 835 |
|
| 836 |
Rooted<DataViewObject*> thisView( |
| 837 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 838 |
|
| 839 |
if (!write<uint8_t>(cx, thisView, args)) { |
| 840 |
return false; |
| 841 |
} |
| 842 |
args.rval().setUndefined(); |
| 843 |
return true; |
| 844 |
} |
| 845 |
|
| 846 |
bool DataViewObject::fun_setUint8(JSContext* cx, unsigned argc, Value* vp) { |
| 847 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 848 |
return CallNonGenericMethod<IsDataView, setUint8Impl>(cx, args); |
| 849 |
} |
| 850 |
|
| 851 |
bool DataViewObject::setInt16Impl(JSContext* cx, const CallArgs& args) { |
| 852 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 853 |
|
| 854 |
Rooted<DataViewObject*> thisView( |
| 855 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 856 |
|
| 857 |
if (!write<int16_t>(cx, thisView, args)) { |
| 858 |
return false; |
| 859 |
} |
| 860 |
args.rval().setUndefined(); |
| 861 |
return true; |
| 862 |
} |
| 863 |
|
| 864 |
bool DataViewObject::fun_setInt16(JSContext* cx, unsigned argc, Value* vp) { |
| 865 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 866 |
return CallNonGenericMethod<IsDataView, setInt16Impl>(cx, args); |
| 867 |
} |
| 868 |
|
| 869 |
bool DataViewObject::setUint16Impl(JSContext* cx, const CallArgs& args) { |
| 870 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 871 |
|
| 872 |
Rooted<DataViewObject*> thisView( |
| 873 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 874 |
|
| 875 |
if (!write<uint16_t>(cx, thisView, args)) { |
| 876 |
return false; |
| 877 |
} |
| 878 |
args.rval().setUndefined(); |
| 879 |
return true; |
| 880 |
} |
| 881 |
|
| 882 |
bool DataViewObject::fun_setUint16(JSContext* cx, unsigned argc, Value* vp) { |
| 883 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 884 |
return CallNonGenericMethod<IsDataView, setUint16Impl>(cx, args); |
| 885 |
} |
| 886 |
|
| 887 |
bool DataViewObject::setInt32Impl(JSContext* cx, const CallArgs& args) { |
| 888 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 889 |
|
| 890 |
Rooted<DataViewObject*> thisView( |
| 891 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 892 |
|
| 893 |
if (!write<int32_t>(cx, thisView, args)) { |
| 894 |
return false; |
| 895 |
} |
| 896 |
args.rval().setUndefined(); |
| 897 |
return true; |
| 898 |
} |
| 899 |
|
| 900 |
bool DataViewObject::fun_setInt32(JSContext* cx, unsigned argc, Value* vp) { |
| 901 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 902 |
return CallNonGenericMethod<IsDataView, setInt32Impl>(cx, args); |
| 903 |
} |
| 904 |
|
| 905 |
bool DataViewObject::setUint32Impl(JSContext* cx, const CallArgs& args) { |
| 906 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 907 |
|
| 908 |
Rooted<DataViewObject*> thisView( |
| 909 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 910 |
|
| 911 |
if (!write<uint32_t>(cx, thisView, args)) { |
| 912 |
return false; |
| 913 |
} |
| 914 |
args.rval().setUndefined(); |
| 915 |
return true; |
| 916 |
} |
| 917 |
|
| 918 |
bool DataViewObject::fun_setUint32(JSContext* cx, unsigned argc, Value* vp) { |
| 919 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 920 |
return CallNonGenericMethod<IsDataView, setUint32Impl>(cx, args); |
| 921 |
} |
| 922 |
|
| 923 |
// BigInt proposal 7.28 |
| 924 |
// DataView.prototype.setBigInt64 ( byteOffset, value [ , littleEndian ] ) |
| 925 |
bool DataViewObject::setBigInt64Impl(JSContext* cx, const CallArgs& args) { |
| 926 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 927 |
|
| 928 |
Rooted<DataViewObject*> thisView( |
| 929 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 930 |
|
| 931 |
if (!write<int64_t>(cx, thisView, args)) { |
| 932 |
return false; |
| 933 |
} |
| 934 |
args.rval().setUndefined(); |
| 935 |
return true; |
| 936 |
} |
| 937 |
|
| 938 |
bool DataViewObject::fun_setBigInt64(JSContext* cx, unsigned argc, Value* vp) { |
| 939 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 940 |
return CallNonGenericMethod<IsDataView, setBigInt64Impl>(cx, args); |
| 941 |
} |
| 942 |
|
| 943 |
// BigInt proposal 7.29 |
| 944 |
// DataView.prototype.setBigUint64 ( byteOffset, value [ , littleEndian ] ) |
| 945 |
bool DataViewObject::setBigUint64Impl(JSContext* cx, const CallArgs& args) { |
| 946 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 947 |
|
| 948 |
Rooted<DataViewObject*> thisView( |
| 949 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 950 |
|
| 951 |
if (!write<uint64_t>(cx, thisView, args)) { |
| 952 |
return false; |
| 953 |
} |
| 954 |
args.rval().setUndefined(); |
| 955 |
return true; |
| 956 |
} |
| 957 |
|
| 958 |
bool DataViewObject::fun_setBigUint64(JSContext* cx, unsigned argc, Value* vp) { |
| 959 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 960 |
return CallNonGenericMethod<IsDataView, setBigUint64Impl>(cx, args); |
| 961 |
} |
| 962 |
|
| 963 |
bool DataViewObject::setFloat16Impl(JSContext* cx, const CallArgs& args) { |
| 964 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 965 |
|
| 966 |
Rooted<DataViewObject*> thisView( |
| 967 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 968 |
|
| 969 |
if (!write<float16>(cx, thisView, args)) { |
| 970 |
return false; |
| 971 |
} |
| 972 |
args.rval().setUndefined(); |
| 973 |
return true; |
| 974 |
} |
| 975 |
|
| 976 |
bool DataViewObject::fun_setFloat16(JSContext* cx, unsigned argc, Value* vp) { |
| 977 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 978 |
return CallNonGenericMethod<IsDataView, setFloat16Impl>(cx, args); |
| 979 |
} |
| 980 |
|
| 981 |
bool DataViewObject::setFloat32Impl(JSContext* cx, const CallArgs& args) { |
| 982 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 983 |
|
| 984 |
Rooted<DataViewObject*> thisView( |
| 985 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 986 |
|
| 987 |
if (!write<float>(cx, thisView, args)) { |
| 988 |
return false; |
| 989 |
} |
| 990 |
args.rval().setUndefined(); |
| 991 |
return true; |
| 992 |
} |
| 993 |
|
| 994 |
bool DataViewObject::fun_setFloat32(JSContext* cx, unsigned argc, Value* vp) { |
| 995 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 996 |
return CallNonGenericMethod<IsDataView, setFloat32Impl>(cx, args); |
| 997 |
} |
| 998 |
|
| 999 |
bool DataViewObject::setFloat64Impl(JSContext* cx, const CallArgs& args) { |
| 1000 |
MOZ_ASSERT(IsDataView(args.thisv())); |
| 1001 |
|
| 1002 |
Rooted<DataViewObject*> thisView( |
| 1003 |
cx, &args.thisv().toObject().as<DataViewObject>()); |
| 1004 |
|
| 1005 |
if (!write<double>(cx, thisView, args)) { |
| 1006 |
return false; |
| 1007 |
} |
| 1008 |
args.rval().setUndefined(); |
| 1009 |
return true; |
| 1010 |
} |
| 1011 |
|
| 1012 |
bool DataViewObject::fun_setFloat64(JSContext* cx, unsigned argc, Value* vp) { |
| 1013 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1014 |
return CallNonGenericMethod<IsDataView, setFloat64Impl>(cx, args); |
| 1015 |
} |
| 1016 |
|
| 1017 |
bool DataViewObject::bufferGetterImpl(JSContext* cx, const CallArgs& args) { |
| 1018 |
auto* thisView = &args.thisv().toObject().as<DataViewObject>(); |
| 1019 |
args.rval().set(thisView->bufferValue()); |
| 1020 |
return true; |
| 1021 |
} |
| 1022 |
|
| 1023 |
bool DataViewObject::bufferGetter(JSContext* cx, unsigned argc, Value* vp) { |
| 1024 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1025 |
return CallNonGenericMethod<IsDataView, bufferGetterImpl>(cx, args); |
| 1026 |
} |
| 1027 |
|
| 1028 |
bool DataViewObject::byteLengthGetterImpl(JSContext* cx, const CallArgs& args) { |
| 1029 |
auto* thisView = &args.thisv().toObject().as<DataViewObject>(); |
| 1030 |
|
| 1031 |
// Step 6. |
| 1032 |
auto byteLength = thisView->byteLength(); |
| 1033 |
if (MOZ_UNLIKELY(!byteLength)) { |
| 1034 |
ReportOutOfBounds(cx, thisView); |
| 1035 |
return false; |
| 1036 |
} |
| 1037 |
|
| 1038 |
// Step 7. |
| 1039 |
args.rval().set(NumberValue(*byteLength)); |
| 1040 |
return true; |
| 1041 |
} |
| 1042 |
|
| 1043 |
bool DataViewObject::byteLengthGetter(JSContext* cx, unsigned argc, Value* vp) { |
| 1044 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1045 |
return CallNonGenericMethod<IsDataView, byteLengthGetterImpl>(cx, args); |
| 1046 |
} |
| 1047 |
|
| 1048 |
bool DataViewObject::byteOffsetGetterImpl(JSContext* cx, const CallArgs& args) { |
| 1049 |
auto* thisView = &args.thisv().toObject().as<DataViewObject>(); |
| 1050 |
|
| 1051 |
// Step 6. |
| 1052 |
auto byteOffset = thisView->byteOffset(); |
| 1053 |
if (MOZ_UNLIKELY(!byteOffset)) { |
| 1054 |
ReportOutOfBounds(cx, thisView); |
| 1055 |
return false; |
| 1056 |
} |
| 1057 |
|
| 1058 |
// Step 7. |
| 1059 |
args.rval().set(NumberValue(*byteOffset)); |
| 1060 |
return true; |
| 1061 |
} |
| 1062 |
|
| 1063 |
bool DataViewObject::byteOffsetGetter(JSContext* cx, unsigned argc, Value* vp) { |
| 1064 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1065 |
return CallNonGenericMethod<IsDataView, byteOffsetGetterImpl>(cx, args); |
| 1066 |
} |
| 1067 |
|
| 1068 |
static const JSClassOps DataViewObjectClassOps = { |
| 1069 |
nullptr, // addProperty |
| 1070 |
nullptr, // delProperty |
| 1071 |
nullptr, // enumerate |
| 1072 |
nullptr, // newEnumerate |
| 1073 |
nullptr, // resolve |
| 1074 |
nullptr, // mayResolve |
| 1075 |
nullptr, // finalize |
| 1076 |
nullptr, // call |
| 1077 |
nullptr, // construct |
| 1078 |
ArrayBufferViewObject::trace, // trace |
| 1079 |
}; |
| 1080 |
|
| 1081 |
static JSObject* CreateDataViewPrototype(JSContext* cx, JSProtoKey key) { |
| 1082 |
return GlobalObject::createBlankPrototype(cx, cx->global(), |
| 1083 |
&DataViewObject::protoClass_); |
| 1084 |
} |
| 1085 |
|
| 1086 |
const ClassSpec DataViewObject::classSpec_ = { |
| 1087 |
GenericCreateConstructor<DataViewObject::construct, 1, |
| 1088 |
gc::AllocKind::FUNCTION>, |
| 1089 |
CreateDataViewPrototype, |
| 1090 |
nullptr, |
| 1091 |
nullptr, |
| 1092 |
DataViewObject::methods, |
| 1093 |
DataViewObject::properties, |
| 1094 |
}; |
| 1095 |
|
| 1096 |
const JSClass FixedLengthDataViewObject::class_ = { |
| 1097 |
"DataView", |
| 1098 |
JSCLASS_HAS_RESERVED_SLOTS(FixedLengthDataViewObject::RESERVED_SLOTS) | |
| 1099 |
JSCLASS_HAS_CACHED_PROTO(JSProto_DataView), |
| 1100 |
&DataViewObjectClassOps, |
| 1101 |
&DataViewObject::classSpec_, |
| 1102 |
}; |
| 1103 |
|
| 1104 |
const JSClass ResizableDataViewObject::class_ = { |
| 1105 |
"DataView", |
| 1106 |
JSCLASS_HAS_RESERVED_SLOTS(ResizableDataViewObject::RESERVED_SLOTS) | |
| 1107 |
JSCLASS_HAS_CACHED_PROTO(JSProto_DataView), |
| 1108 |
&DataViewObjectClassOps, |
| 1109 |
&DataViewObject::classSpec_, |
| 1110 |
}; |
| 1111 |
|
| 1112 |
const JSClass* const JS::DataView::FixedLengthClassPtr = |
| 1113 |
&FixedLengthDataViewObject::class_; |
| 1114 |
const JSClass* const JS::DataView::ResizableClassPtr = |
| 1115 |
&ResizableDataViewObject::class_; |
| 1116 |
|
| 1117 |
const JSClass DataViewObject::protoClass_ = { |
| 1118 |
"DataView.prototype", |
| 1119 |
JSCLASS_HAS_CACHED_PROTO(JSProto_DataView), |
| 1120 |
JS_NULL_CLASS_OPS, |
| 1121 |
&DataViewObject::classSpec_, |
| 1122 |
}; |
| 1123 |
|
| 1124 |
const JSFunctionSpec DataViewObject::methods[] = { |
| 1125 |
JS_INLINABLE_FN("getInt8", DataViewObject::fun_getInt8, 1, 0, |
| 1126 |
DataViewGetInt8), |
| 1127 |
JS_INLINABLE_FN("getUint8", DataViewObject::fun_getUint8, 1, 0, |
| 1128 |
DataViewGetUint8), |
| 1129 |
JS_INLINABLE_FN("getInt16", DataViewObject::fun_getInt16, 1, 0, |
| 1130 |
DataViewGetInt16), |
| 1131 |
JS_INLINABLE_FN("getUint16", DataViewObject::fun_getUint16, 1, 0, |
| 1132 |
DataViewGetUint16), |
| 1133 |
JS_INLINABLE_FN("getInt32", DataViewObject::fun_getInt32, 1, 0, |
| 1134 |
DataViewGetInt32), |
| 1135 |
JS_INLINABLE_FN("getUint32", DataViewObject::fun_getUint32, 1, 0, |
| 1136 |
DataViewGetUint32), |
| 1137 |
JS_INLINABLE_FN("getFloat16", DataViewObject::fun_getFloat16, 1, 0, |
| 1138 |
DataViewGetFloat16), |
| 1139 |
JS_INLINABLE_FN("getFloat32", DataViewObject::fun_getFloat32, 1, 0, |
| 1140 |
DataViewGetFloat32), |
| 1141 |
JS_INLINABLE_FN("getFloat64", DataViewObject::fun_getFloat64, 1, 0, |
| 1142 |
DataViewGetFloat64), |
| 1143 |
JS_INLINABLE_FN("getBigInt64", DataViewObject::fun_getBigInt64, 1, 0, |
| 1144 |
DataViewGetBigInt64), |
| 1145 |
JS_INLINABLE_FN("getBigUint64", DataViewObject::fun_getBigUint64, 1, 0, |
| 1146 |
DataViewGetBigUint64), |
| 1147 |
JS_INLINABLE_FN("setInt8", DataViewObject::fun_setInt8, 2, 0, |
| 1148 |
DataViewSetInt8), |
| 1149 |
JS_INLINABLE_FN("setUint8", DataViewObject::fun_setUint8, 2, 0, |
| 1150 |
DataViewSetUint8), |
| 1151 |
JS_INLINABLE_FN("setInt16", DataViewObject::fun_setInt16, 2, 0, |
| 1152 |
DataViewSetInt16), |
| 1153 |
JS_INLINABLE_FN("setUint16", DataViewObject::fun_setUint16, 2, 0, |
| 1154 |
DataViewSetUint16), |
| 1155 |
JS_INLINABLE_FN("setInt32", DataViewObject::fun_setInt32, 2, 0, |
| 1156 |
DataViewSetInt32), |
| 1157 |
JS_INLINABLE_FN("setUint32", DataViewObject::fun_setUint32, 2, 0, |
| 1158 |
DataViewSetUint32), |
| 1159 |
JS_INLINABLE_FN("setFloat16", DataViewObject::fun_setFloat16, 2, 0, |
| 1160 |
DataViewSetFloat16), |
| 1161 |
JS_INLINABLE_FN("setFloat32", DataViewObject::fun_setFloat32, 2, 0, |
| 1162 |
DataViewSetFloat32), |
| 1163 |
JS_INLINABLE_FN("setFloat64", DataViewObject::fun_setFloat64, 2, 0, |
| 1164 |
DataViewSetFloat64), |
| 1165 |
JS_INLINABLE_FN("setBigInt64", DataViewObject::fun_setBigInt64, 2, 0, |
| 1166 |
DataViewSetBigInt64), |
| 1167 |
JS_INLINABLE_FN("setBigUint64", DataViewObject::fun_setBigUint64, 2, 0, |
| 1168 |
DataViewSetBigUint64), |
| 1169 |
JS_FS_END, |
| 1170 |
}; |
| 1171 |
|
| 1172 |
const JSPropertySpec DataViewObject::properties[] = { |
| 1173 |
JS_PSG("buffer", DataViewObject::bufferGetter, 0), |
| 1174 |
JS_PSG("byteLength", DataViewObject::byteLengthGetter, 0), |
| 1175 |
JS_PSG("byteOffset", DataViewObject::byteOffsetGetter, 0), |
| 1176 |
JS_STRING_SYM_PS(toStringTag, "DataView", JSPROP_READONLY), |
| 1177 |
JS_PS_END, |
| 1178 |
}; |
| 1179 |
|
| 1180 |
JS_PUBLIC_API JSObject* JS_NewDataView(JSContext* cx, HandleObject buffer, |
| 1181 |
size_t byteOffset, size_t byteLength) { |
| 1182 |
JSProtoKey key = JSProto_DataView; |
| 1183 |
RootedObject constructor(cx, GlobalObject::getOrCreateConstructor(cx, key)); |
| 1184 |
if (!constructor) { |
| 1185 |
return nullptr; |
| 1186 |
} |
| 1187 |
|
| 1188 |
FixedConstructArgs<3> cargs(cx); |
| 1189 |
|
| 1190 |
cargs[0].setObject(*buffer); |
| 1191 |
cargs[1].setNumber(byteOffset); |
| 1192 |
cargs[2].setNumber(byteLength); |
| 1193 |
|
| 1194 |
RootedValue fun(cx, ObjectValue(*constructor)); |
| 1195 |
RootedObject obj(cx); |
| 1196 |
if (!Construct(cx, fun, cargs, fun, &obj)) { |
| 1197 |
return nullptr; |
| 1198 |
} |
| 1199 |
return obj; |
| 1200 |
} |
| 1201 |
|
| 1202 |
JSObject* js::NewDataView(JSContext* cx, HandleObject buffer, |
| 1203 |
size_t byteOffset) { |
| 1204 |
JSProtoKey key = JSProto_DataView; |
| 1205 |
RootedObject constructor(cx, GlobalObject::getOrCreateConstructor(cx, key)); |
| 1206 |
if (!constructor) { |
| 1207 |
return nullptr; |
| 1208 |
} |
| 1209 |
|
| 1210 |
FixedConstructArgs<2> cargs(cx); |
| 1211 |
|
| 1212 |
cargs[0].setObject(*buffer); |
| 1213 |
cargs[1].setNumber(byteOffset); |
| 1214 |
|
| 1215 |
RootedValue fun(cx, ObjectValue(*constructor)); |
| 1216 |
RootedObject obj(cx); |
| 1217 |
if (!Construct(cx, fun, cargs, fun, &obj)) { |
| 1218 |
return nullptr; |
| 1219 |
} |
| 1220 |
return obj; |
| 1221 |
} |