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
2 changes: 1 addition & 1 deletion doc/api/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ available storage. This function does not allocate memory on its own.
added: v26.1.0
-->

* `source` {Buffer|ArrayBuffer|ArrayBufferView}
* `source` {Buffer|ArrayBuffer|SharedArrayBuffer|ArrayBufferView}
* Returns: {bigint}

Returns the raw memory address of JavaScript-managed byte storage.
Expand Down
10 changes: 6 additions & 4 deletions src/ffi/data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ void GetRawPointer(const FunctionCallbackInfo<Value>& args) {
if (args.Length() < 1) {
THROW_ERR_INVALID_ARG_TYPE(
env,
"The first argument must be a Buffer, ArrayBuffer, or ArrayBufferView");
"The first argument must be a Buffer, ArrayBuffer, SharedArrayBuffer, "
"or ArrayBufferView");
return;
}

Expand All @@ -758,9 +759,10 @@ void GetRawPointer(const FunctionCallbackInfo<Value>& args) {
store = args[0].As<ArrayBufferView>()->Buffer()->GetBackingStore();
offset = args[0].As<ArrayBufferView>()->ByteOffset();
} else {
THROW_ERR_INVALID_ARG_TYPE(env,
"The first argument must be a Buffer, "
"ArrayBuffer, or ArrayBufferView");
THROW_ERR_INVALID_ARG_TYPE(
env,
"The first argument must be a Buffer, "
"ArrayBuffer, SharedArrayBuffer, or ArrayBufferView");
return;
}

Expand Down
7 changes: 7 additions & 0 deletions test/ffi/test-ffi-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,25 @@ test('ffi getRawPointer returns raw addresses for byte sources', () => {
const buffer = Buffer.from([1, 2, 3]);
const arrayBuffer = new Uint8Array([4, 5, 6, 7]).buffer;
const view = new Uint8Array(arrayBuffer, 2);
const sharedArrayBuffer = new SharedArrayBuffer(4);
const sharedView = new Uint8Array(sharedArrayBuffer, 2);

const bufferPointer = ffi.getRawPointer(buffer);
const arrayBufferPointer = ffi.getRawPointer(arrayBuffer);
const viewPointer = ffi.getRawPointer(view);
const sharedArrayBufferPointer = ffi.getRawPointer(sharedArrayBuffer);
const sharedViewPointer = ffi.getRawPointer(sharedView);

assert.strictEqual(typeof bufferPointer, 'bigint');
assert.strictEqual(typeof arrayBufferPointer, 'bigint');
assert.strictEqual(typeof viewPointer, 'bigint');
assert.strictEqual(typeof sharedArrayBufferPointer, 'bigint');
assert.strictEqual(typeof sharedViewPointer, 'bigint');

assert.strictEqual(bufferPointer, symbols.pointer_to_usize(buffer));
assert.strictEqual(arrayBufferPointer, symbols.pointer_to_usize(arrayBuffer));
assert.strictEqual(viewPointer, arrayBufferPointer + 2n);
assert.strictEqual(sharedViewPointer, sharedArrayBufferPointer + 2n);
});

test('ffi exportString and exportBuffer copy data into native memory', () => {
Expand Down
Loading