Skip to content

Commit

Permalink
node: fix test-buffer-backing-arraybuffer.js (#17161)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Feb 8, 2025
1 parent 0da7025 commit 374195e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/bun.js/bindings/InternalForTesting.cpp
Original file line number Diff line number Diff line change
@@ -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<WebCore::JSArrayBufferView*>(value);
return JSValue::encode(jsBoolean(view->hasArrayBuffer()));
}

}
10 changes: 10 additions & 0 deletions src/bun.js/bindings/InternalForTesting.h
Original file line number Diff line number Diff line change
@@ -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));

}
8 changes: 8 additions & 0 deletions src/js/internal-for-testing.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -164,3 +166,9 @@ export const fsStreamInternals = {
return str[require("internal/fs/streams").kWriteStreamFastPath];
},
};

export const arrayBufferViewHasBuffer = $newCppFunction(
"InternalForTesting.cpp",
"jsFunction_arrayBufferViewHasBuffer",
1,
);
36 changes: 36 additions & 0 deletions test/js/node/test/parallel/test-buffer-backing-arraybuffer.js
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 374195e

Please sign in to comment.