diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-10-05 07:35:21 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-05 15:35:21 +0200 |
commit | 1619932a651a189d590bd579f334faac3c6b4397 (patch) | |
tree | 8f3771655e44ee5bf661ece398895c22d557e977 /ext/ffi/00_ffi.js | |
parent | 5d98a544b421e2b0bc3f99318fe44d1fed6d95d9 (diff) |
chore(ext/ffi): migrate from op -> op2 for ffi (#20509)
Migrate to op2. Making a few decisions to get this across the line:
- Empty slices, no matter where the come from, are null pointers. The v8
bugs (https://bugs.chromium.org/p/v8/issues/detail?id=13489) and
(https://bugs.chromium.org/p/v8/issues/detail?id=13488) make passing
around zero-length slice pointers too dangerous as they might be
uninitialized or null data.
- Offsets and lengths are `#[number] isize` and `#[number] usize`
respectively -- 53 bits should be enough for anyone
- Pointers are bigints. This is a u64 in the fastcall world, and can
accept Integer/Int32/Number/BigInt v8 types in the slow world.
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r-- | ext/ffi/00_ffi.js | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js index 67cb13ab6..fe7344e17 100644 --- a/ext/ffi/00_ffi.js +++ b/ext/ffi/00_ffi.js @@ -5,6 +5,7 @@ const ops = core.ops; const primordials = globalThis.__bootstrap.primordials; const { ArrayBufferIsView, + ArrayBufferPrototype, ArrayBufferPrototypeGetByteLength, ArrayPrototypeMap, ArrayPrototypeJoin, @@ -221,7 +222,24 @@ class UnsafePointer { if (ObjectPrototypeIsPrototypeOf(UnsafeCallbackPrototype, value)) { return value.pointer; } - const pointer = ops.op_ffi_ptr_of(value); + let pointer; + if (ArrayBufferIsView(value)) { + if (value.length === 0) { + pointer = ops.op_ffi_ptr_of_exact(value); + } else { + pointer = ops.op_ffi_ptr_of(value); + } + } else if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, value)) { + if (value.length === 0) { + pointer = ops.op_ffi_ptr_of_exact(new Uint8Array(value)); + } else { + pointer = ops.op_ffi_ptr_of(new Uint8Array(value)); + } + } else { + throw new TypeError( + "Expected ArrayBuffer, ArrayBufferView or UnsafeCallbackPrototype", + ); + } if (pointer) { POINTER_TO_BUFFER_WEAK_MAP.set(pointer, value); } |