From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- js/src/vm/ArrayBufferObjectMaybeShared.cpp | 76 ++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 js/src/vm/ArrayBufferObjectMaybeShared.cpp (limited to 'js/src/vm/ArrayBufferObjectMaybeShared.cpp') diff --git a/js/src/vm/ArrayBufferObjectMaybeShared.cpp b/js/src/vm/ArrayBufferObjectMaybeShared.cpp new file mode 100644 index 0000000000..400a8baa3f --- /dev/null +++ b/js/src/vm/ArrayBufferObjectMaybeShared.cpp @@ -0,0 +1,76 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim: set ts=8 sts=2 et sw=2 tw=80: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/Assertions.h" // MOZ_ASSERT + +#include // uint8_t, uint32_t + +#include "jstypes.h" // JS_PUBLIC_API + +#include "js/ArrayBufferMaybeShared.h" +#include "vm/ArrayBufferObject.h" // js::ArrayBufferObject +#include "vm/JSObject.h" // JSObject +#include "vm/SharedArrayObject.h" // js::SharedArrayBufferObject +#include "vm/SharedMem.h" // SharedMem + +using namespace js; + +JS_PUBLIC_API bool JS::IsArrayBufferObjectMaybeShared(JSObject* obj) { + return obj->canUnwrapAs(); +} + +JS_PUBLIC_API JSObject* JS::UnwrapArrayBufferMaybeShared(JSObject* obj) { + return obj->maybeUnwrapIf(); +} + +JS_PUBLIC_API void JS::GetArrayBufferMaybeSharedLengthAndData( + JSObject* obj, size_t* length, bool* isSharedMemory, uint8_t** data) { + MOZ_ASSERT(obj->is()); + + if (obj->is()) { + auto* buffer = &obj->as(); + *length = buffer->byteLength(); + *data = buffer->dataPointerShared().unwrap(); + *isSharedMemory = true; + } else { + auto* buffer = &obj->as(); + *length = buffer->byteLength(); + *data = buffer->dataPointer(); + *isSharedMemory = false; + } +} + +JS_PUBLIC_API uint8_t* JS::GetArrayBufferMaybeSharedData( + JSObject* obj, bool* isSharedMemory, const JS::AutoRequireNoGC&) { + MOZ_ASSERT(obj->maybeUnwrapIf()); + + if (ArrayBufferObject* aobj = obj->maybeUnwrapIf()) { + *isSharedMemory = false; + return aobj->dataPointer(); + } else if (SharedArrayBufferObject* saobj = + obj->maybeUnwrapIf()) { + *isSharedMemory = true; + return saobj->dataPointerShared().unwrap(); + } + + return nullptr; +} + +JS_PUBLIC_API bool JS::IsLargeArrayBufferMaybeShared(JSObject* obj) { +#ifdef JS_64BIT + obj = UnwrapArrayBufferMaybeShared(obj); + MOZ_ASSERT(obj); + size_t len = obj->is() + ? obj->as().byteLength() + : obj->as().byteLength(); + return len > ArrayBufferObject::MaxByteLengthForSmallBuffer; +#else + // Large ArrayBuffers are not supported on 32-bit. + static_assert(ArrayBufferObject::MaxByteLength == + ArrayBufferObject::MaxByteLengthForSmallBuffer); + return false; +#endif +} -- cgit v1.2.3