Bug 1738712 - Add JS::ArrayBufferCopyData to JSAPI r=sfink

Differential Revision: https://phabricator.services.mozilla.com/D130114
This commit is contained in:
Matthew Gaudet 2021-11-15 22:01:48 +00:00
Родитель 4752460a3f
Коммит 80e3001aba
4 изменённых файлов: 44 добавлений и 6 удалений

Просмотреть файл

@ -265,6 +265,20 @@ extern JS_PUBLIC_API void* StealArrayBufferContents(JSContext* cx,
*/
extern JS_PUBLIC_API void SetLargeArrayBuffersEnabled(bool enable);
/**
* Copy data from one array buffer to another.
*
* Both fromBuffer and toBuffer must be ArrayBufferObjectMaybeShared.
*
* The API for this is modelled on CopyDataBlockBytes from the spec:
* https://tc39.es/ecma262/#sec-copydatablockbytes
*/
extern JS_PUBLIC_API void ArrayBufferCopyData(JSContext* cx,
Handle<JSObject*> toBlock,
size_t toIndex,
Handle<JSObject*> fromBlock,
size_t fromIndex, size_t count);
} // namespace JS
#endif /* js_ArrayBuffer_h */

Просмотреть файл

@ -2041,3 +2041,26 @@ JS::ArrayBuffer JS::ArrayBuffer::unwrap(JSObject* maybeWrapped) {
auto* ab = maybeWrapped->maybeUnwrapIf<ArrayBufferObjectMaybeShared>();
return fromObject(ab);
}
void JS::ArrayBufferCopyData(JSContext* cx, Handle<JSObject*> toBlock,
size_t toIndex, Handle<JSObject*> fromBlock,
size_t fromIndex, size_t count) {
MOZ_ASSERT(toBlock->is<ArrayBufferObjectMaybeShared>());
MOZ_ASSERT(fromBlock->is<ArrayBufferObjectMaybeShared>());
// If both are array bufferrs, can use ArrayBufferCopyData
if (toBlock->is<ArrayBufferObject>() && fromBlock->is<ArrayBufferObject>()) {
Rooted<ArrayBufferObject*> toArray(cx, &toBlock->as<ArrayBufferObject>());
Rooted<ArrayBufferObject*> fromArray(cx,
&fromBlock->as<ArrayBufferObject>());
ArrayBufferObject::copyData(toArray, toIndex, fromArray, fromIndex, count);
return;
}
Rooted<ArrayBufferObjectMaybeShared*> toArray(
cx, &toBlock->as<ArrayBufferObjectMaybeShared>());
Rooted<ArrayBufferObjectMaybeShared*> fromArray(
cx, &toBlock->as<ArrayBufferObjectMaybeShared>());
SharedArrayBufferObject::copyData(toArray, toIndex, fromArray, fromIndex,
count);
}

Просмотреть файл

@ -21,6 +21,7 @@
#include "wasm/WasmMemory.h"
#include "wasm/WasmSignalHandlers.h"
#include "vm/ArrayBufferObject-inl.h"
#include "vm/JSObject-inl.h"
#include "vm/NativeObject-inl.h"
@ -373,8 +374,8 @@ void SharedArrayBufferObject::addSizeOfExcludingThis(
/* static */
void SharedArrayBufferObject::copyData(
Handle<SharedArrayBufferObject*> toBuffer, size_t toIndex,
Handle<SharedArrayBufferObject*> fromBuffer, size_t fromIndex,
Handle<ArrayBufferObjectMaybeShared*> toBuffer, size_t toIndex,
Handle<ArrayBufferObjectMaybeShared*> fromBuffer, size_t fromIndex,
size_t count) {
MOZ_ASSERT(toBuffer->byteLength() >= count);
MOZ_ASSERT(toBuffer->byteLength() >= toIndex + count);
@ -382,8 +383,8 @@ void SharedArrayBufferObject::copyData(
MOZ_ASSERT(fromBuffer->byteLength() >= fromIndex + count);
jit::AtomicOperations::memcpySafeWhenRacy(
toBuffer->dataPointerShared() + toIndex,
fromBuffer->dataPointerShared() + fromIndex, count);
toBuffer->dataPointerEither() + toIndex,
fromBuffer->dataPointerEither() + fromIndex, count);
}
SharedArrayBufferObject* SharedArrayBufferObject::createFromNewRawBuffer(

Просмотреть файл

@ -231,9 +231,9 @@ class SharedArrayBufferObject : public ArrayBufferObjectMaybeShared {
JS::ClassInfo* info,
JS::RuntimeSizes* runtimeSizes);
static void copyData(Handle<SharedArrayBufferObject*> toBuffer,
static void copyData(Handle<ArrayBufferObjectMaybeShared*> toBuffer,
size_t toIndex,
Handle<SharedArrayBufferObject*> fromBuffer,
Handle<ArrayBufferObjectMaybeShared*> fromBuffer,
size_t fromIndex, size_t count);
SharedArrayRawBuffer* rawBufferObject() const;