// Copyright (c) 2017-2022 Cloudflare, Inc. // Licensed under the Apache 2.0 license found in the LICENSE file or at: // https://opensource.org/licenses/Apache-2.0 #include "buffersource.h" #include "jsg-test.h" // ======================================================================================== namespace workerd::jsg::test { namespace { V8System v8System; struct BufferSourceContext: public jsg::Object, public jsg::ContextGlobal { BufferSource takeBufferSource(BufferSource buf) { auto ptr = buf.asArrayPtr(); KJ_ASSERT(!buf.isDetached()); KJ_ASSERT(buf.size() == 8); KJ_ASSERT(ptr[0] == 0); ptr[0] = 1; KJ_ASSERT(ptr[0] == 1); return kj::mv(buf); } BufferSource takeUint8Array(Lock& js, BufferSource buf) { // A BufferSource that is initially attached can be detached, releasing the original // object, then recreated as a new instance of the same type of JS object. KJ_ASSERT(!buf.isDetached()); auto handle = buf.getHandle(js); KJ_ASSERT(handle->IsUint8Array()); KJ_ASSERT(handle.As()->ByteLength() > 0); // Detaching removes the BackingStore from the BufferSource, // rendering the BufferSource useless. auto backingStore = buf.detach(js); KJ_ASSERT(buf.isDetached()); KJ_ASSERT(handle.As()->ByteLength() == 0); // We can create a new view over the same shared backing store. auto newView = backingStore.getTypedView(); auto dataView = newView.createHandle(js); KJ_ASSERT(dataView->IsDataView()); // We can create a new BufferSource from the detached BackingStore. return BufferSource(js, kj::mv(backingStore)); } BufferSource makeBufferSource(jsg::Lock& js) { return BufferSource(js, BackingStore::from(js, kj::arr(1, 2, 3))); } BufferSource makeArrayBuffer(jsg::Lock& js) { return BufferSource(js, BackingStore::alloc(js, 3)); } bool doTest(jsg::Lock& js, jsg::BufferSource buf) { buf.asArrayPtr()[0] = 1; buf.asArrayPtr()[1] = 2; buf.asArrayPtr()[2] = 3; buf.asArrayPtr()[3] = 4; buf.asArrayPtr()[4] = 5; buf.asArrayPtr()[5] = 6; buf.asArrayPtr()[6] = 7; buf.asArrayPtr()[7] = 8; auto ptr = buf.asArrayPtr(); KJ_ASSERT(ptr.size() == 2); KJ_ASSERT(ptr[0] == 0x04030201); KJ_ASSERT(ptr[1] == 0x08070605); return true; } JSG_RESOURCE_TYPE(BufferSourceContext) { JSG_METHOD(takeBufferSource); JSG_METHOD(takeUint8Array); JSG_METHOD(makeBufferSource); JSG_METHOD(makeArrayBuffer); JSG_METHOD(doTest); } }; JSG_DECLARE_ISOLATE_TYPE(BufferSourceIsolate, BufferSourceContext); KJ_TEST("BufferSource works") { Evaluator e(v8System); // By default, a BufferSource handle is created as a DataView e.expectEval("makeBufferSource() instanceof Uint8Array", "boolean", "true"); // ... but can be other types also e.expectEval("makeArrayBuffer() instanceof ArrayBuffer", "boolean", "true"); e.expectEval( "const ab = new ArrayBuffer(9); takeBufferSource(new Uint8Array(ab, 1, 8)).byteLength === 8", "boolean", "true"); e.expectEval("const ab = new ArrayBuffer(8); takeBufferSource(ab) === ab", "boolean", "true"); e.expectEval("const ab = new Uint8Array(8); takeBufferSource(ab) === ab", "boolean", "true"); e.expectEval("const ab = new Uint16Array(4); takeBufferSource(ab) === ab", "boolean", "true"); e.expectEval("const ab = new Uint32Array(2); takeBufferSource(ab) === ab", "boolean", "true"); e.expectEval("const ab = new BigInt64Array(1); takeBufferSource(ab) === ab", "boolean", "true"); e.expectEval("const ab = new Float16Array(4); takeBufferSource(ab) === ab", "boolean", "true"); e.expectEval("const ab = new Float32Array(2); takeBufferSource(ab) === ab", "boolean", "true"); e.expectEval("const ab = new Float64Array(1); takeBufferSource(ab) === ab", "boolean", "true"); e.expectEval("const ab = new ArrayBuffer(4); " "const u8 = new Uint8Array(ab, 1, 1);" "const u2 = takeUint8Array(u8);" "u8.byteLength === 0 && u2.byteLength === 1 && u2 instanceof Uint8Array && " "u2.buffer.byteLength === 4 && u2.byteOffset === 1 && u8 !== u2", "boolean", "true"); e.expectEval("const buf = new Uint8Array(12); doTest(buf.subarray(4))", "boolean", "true"); } } // namespace } // namespace workerd::jsg::test