Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const {
ObjectFreeze,
ObjectGetOwnPropertyDescriptor,
ObjectKeys,
ObjectPrototypeToString,
ReflectConstruct,
SafeWeakMap,
SafeWeakRef,
Expand All @@ -18,6 +17,7 @@ const {
const { Buffer } = require('buffer');
const { emitExperimentalWarning } = require('internal/util');
const {
isArrayBuffer,
isDataView,
isArrayBufferView,
isSharedArrayBuffer,
Expand Down Expand Up @@ -313,7 +313,7 @@ function exportBuffer(source, data, len) {
function exportArrayBuffer(source, data, len) {
checkFFIPermission();

if (ObjectPrototypeToString(source) !== '[object ArrayBuffer]') {
if (!isArrayBuffer(source)) {
throw new ERR_INVALID_ARG_TYPE('arrayBuffer', 'ArrayBuffer', source);
}

Expand Down
9 changes: 9 additions & 0 deletions test/ffi/test-ffi-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ test('ffi exportString and exportBuffer copy data into native memory', () => {
ffi.exportArrayBuffer(arrayBuffer, arrayBufferPtr, 4);
assert.deepStrictEqual([...ffi.toBuffer(arrayBufferPtr, 4)], [8, 9, 10, 11]);

const taggedArrayBuffer = new Uint8Array([12, 13, 14, 15]).buffer;
Object.defineProperty(taggedArrayBuffer, Symbol.toStringTag, { value: 'Custom' });
ffi.exportArrayBuffer(taggedArrayBuffer, arrayBufferPtr, 4);
assert.deepStrictEqual([...ffi.toBuffer(arrayBufferPtr, 4)], [12, 13, 14, 15]);

const viewPtr = alloc(8);
const viewSource = new Uint16Array([0x0102, 0x0304, 0x0506]);
const middleBytes = new Uint8Array(viewSource.buffer, 2, 2);
Expand Down Expand Up @@ -318,6 +323,10 @@ test('ffi validates memory access arguments', () => {
assert.throws(() => ffi.exportBuffer(Buffer.from([1]), ptr, -1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportBuffer(Buffer.from([1, 2]), ptr, 1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportArrayBuffer('bad', ptr, 4), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => ffi.exportArrayBuffer({ [Symbol.toStringTag]: 'ArrayBuffer', byteLength: 1 }, ptr, 4), {
code: 'ERR_INVALID_ARG_TYPE',
message: /The "arrayBuffer" argument must be an instance of ArrayBuffer/,
});
assert.throws(() => ffi.exportArrayBuffer(new ArrayBuffer(1), ptr, -1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportArrayBuffer(new ArrayBuffer(2), ptr, 1), { code: 'ERR_OUT_OF_RANGE' });
assert.throws(() => ffi.exportArrayBufferView('bad', ptr, 4), { code: 'ERR_INVALID_ARG_TYPE' });
Expand Down
Loading