From 374195ea30461ebbc1b211f1b82e32108ceaf6b6 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 7 Feb 2025 23:22:03 -0800 Subject: [PATCH] node: fix test-buffer-backing-arraybuffer.js (#17161) --- src/bun.js/bindings/InternalForTesting.cpp | 19 ++++++++++ src/bun.js/bindings/InternalForTesting.h | 10 ++++++ src/js/internal-for-testing.ts | 8 +++++ .../test-buffer-backing-arraybuffer.js | 36 +++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 src/bun.js/bindings/InternalForTesting.cpp create mode 100644 src/bun.js/bindings/InternalForTesting.h create mode 100644 test/js/node/test/parallel/test-buffer-backing-arraybuffer.js diff --git a/src/bun.js/bindings/InternalForTesting.cpp b/src/bun.js/bindings/InternalForTesting.cpp new file mode 100644 index 00000000000000..903793f53225f0 --- /dev/null +++ b/src/bun.js/bindings/InternalForTesting.cpp @@ -0,0 +1,19 @@ +#include "root.h" + +#include "ZigGlobalObject.h" +#include "JavaScriptCore/JSCJSValue.h" +#include "JavaScriptCore/JSCast.h" +#include "JavaScriptCore/JSArrayBufferView.h" + +namespace Bun { + +using namespace JSC; + +JSC_DEFINE_HOST_FUNCTION(jsFunction_arrayBufferViewHasBuffer, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) +{ + auto value = callFrame->argument(0); + auto view = jsCast(value); + return JSValue::encode(jsBoolean(view->hasArrayBuffer())); +} + +} diff --git a/src/bun.js/bindings/InternalForTesting.h b/src/bun.js/bindings/InternalForTesting.h new file mode 100644 index 00000000000000..4269a2fa82bfdf --- /dev/null +++ b/src/bun.js/bindings/InternalForTesting.h @@ -0,0 +1,10 @@ +#include "root.h" + +#include "ZigGlobalObject.h" +#include "JavaScriptCore/JSCJSValue.h" + +namespace Bun { + +JSC_DEFINE_HOST_FUNCTION(jsFunction_arrayBufferViewHasBuffer, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)); + +} diff --git a/src/js/internal-for-testing.ts b/src/js/internal-for-testing.ts index cffb35bec0144c..2b5ffd8e4ca934 100644 --- a/src/js/internal-for-testing.ts +++ b/src/js/internal-for-testing.ts @@ -1,3 +1,5 @@ +// Hardcoded module "bun:internal-for-testing" + // If you want to test an internal API, add a binding into this file. // // Then at test time: import ... from "bun:internal-for-testing" @@ -164,3 +166,9 @@ export const fsStreamInternals = { return str[require("internal/fs/streams").kWriteStreamFastPath]; }, }; + +export const arrayBufferViewHasBuffer = $newCppFunction( + "InternalForTesting.cpp", + "jsFunction_arrayBufferViewHasBuffer", + 1, +); diff --git a/test/js/node/test/parallel/test-buffer-backing-arraybuffer.js b/test/js/node/test/parallel/test-buffer-backing-arraybuffer.js new file mode 100644 index 00000000000000..3e90ee398b4290 --- /dev/null +++ b/test/js/node/test/parallel/test-buffer-backing-arraybuffer.js @@ -0,0 +1,36 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +// const { internalBinding } = require('internal/test/binding'); +// const { arrayBufferViewHasBuffer } = internalBinding('util'); +const { arrayBufferViewHasBuffer } = require('bun:internal-for-testing'); + +const tests = [ + { length: 0 }, + { length: 48 }, + { length: 96 }, + { length: 1024 }, +]; + +for (const { length, expectOnHeap } of tests) { + const arrays = [ + new Uint8Array(length), + new Uint16Array(length / 2), + new Uint32Array(length / 4), + new Float32Array(length / 4), + new Float64Array(length / 8), + Buffer.alloc(length), + Buffer.allocUnsafeSlow(length), + // Buffer.allocUnsafe() is missing because it may use pooled allocations. + ]; + + for (const array of arrays) { + const isOnHeap = arrayBufferViewHasBuffer(array); + const expectOnHeap = false; // in JSC this will always be false until `.buffer` is first accessed. + assert.strictEqual(isOnHeap, expectOnHeap, `mismatch: ${isOnHeap} vs ${expectOnHeap} ` + `for ${array.constructor.name}, length = ${length}`); + + // Consistency check: Accessing .buffer should create it. + array.buffer; // eslint-disable-line no-unused-expressions + assert(arrayBufferViewHasBuffer(array)); + } +}