Skip to content

Commit 7cb4e7f

Browse files
ffi: support SharedArrayBuffer in getRawPointer
Signed-off-by: Junsoo Ha <junsoo2018@naver.com> PR-URL: #64864 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 427d2e1 commit 7cb4e7f

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

doc/api/ffi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ available storage. This function does not allocate memory on its own.
714714
added: v26.1.0
715715
-->
716716

717-
* `source` {Buffer|ArrayBuffer|ArrayBufferView}
717+
* `source` {Buffer|ArrayBuffer|SharedArrayBuffer|ArrayBufferView}
718718
* Returns: {bigint}
719719

720720
Returns the raw memory address of JavaScript-managed byte storage.

src/ffi/data.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,8 @@ void GetRawPointer(const FunctionCallbackInfo<Value>& args) {
739739
if (args.Length() < 1) {
740740
THROW_ERR_INVALID_ARG_TYPE(
741741
env,
742-
"The first argument must be a Buffer, ArrayBuffer, or ArrayBufferView");
742+
"The first argument must be a Buffer, ArrayBuffer, SharedArrayBuffer, "
743+
"or ArrayBufferView");
743744
return;
744745
}
745746

@@ -758,9 +759,10 @@ void GetRawPointer(const FunctionCallbackInfo<Value>& args) {
758759
store = args[0].As<ArrayBufferView>()->Buffer()->GetBackingStore();
759760
offset = args[0].As<ArrayBufferView>()->ByteOffset();
760761
} else {
761-
THROW_ERR_INVALID_ARG_TYPE(env,
762-
"The first argument must be a Buffer, "
763-
"ArrayBuffer, or ArrayBufferView");
762+
THROW_ERR_INVALID_ARG_TYPE(
763+
env,
764+
"The first argument must be a Buffer, "
765+
"ArrayBuffer, SharedArrayBuffer, or ArrayBufferView");
764766
return;
765767
}
766768

test/ffi/test-ffi-memory.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,25 @@ test('ffi getRawPointer returns raw addresses for byte sources', () => {
125125
const buffer = Buffer.from([1, 2, 3]);
126126
const arrayBuffer = new Uint8Array([4, 5, 6, 7]).buffer;
127127
const view = new Uint8Array(arrayBuffer, 2);
128+
const sharedArrayBuffer = new SharedArrayBuffer(4);
129+
const sharedView = new Uint8Array(sharedArrayBuffer, 2);
128130

129131
const bufferPointer = ffi.getRawPointer(buffer);
130132
const arrayBufferPointer = ffi.getRawPointer(arrayBuffer);
131133
const viewPointer = ffi.getRawPointer(view);
134+
const sharedArrayBufferPointer = ffi.getRawPointer(sharedArrayBuffer);
135+
const sharedViewPointer = ffi.getRawPointer(sharedView);
132136

133137
assert.strictEqual(typeof bufferPointer, 'bigint');
134138
assert.strictEqual(typeof arrayBufferPointer, 'bigint');
135139
assert.strictEqual(typeof viewPointer, 'bigint');
140+
assert.strictEqual(typeof sharedArrayBufferPointer, 'bigint');
141+
assert.strictEqual(typeof sharedViewPointer, 'bigint');
136142

137143
assert.strictEqual(bufferPointer, symbols.pointer_to_usize(buffer));
138144
assert.strictEqual(arrayBufferPointer, symbols.pointer_to_usize(arrayBuffer));
139145
assert.strictEqual(viewPointer, arrayBufferPointer + 2n);
146+
assert.strictEqual(sharedViewPointer, sharedArrayBufferPointer + 2n);
140147
});
141148

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

0 commit comments

Comments
 (0)