napi: napi_is_arraybuffer returns false for SharedArrayBuffer#32629
Conversation
JSC gives a SharedArrayBuffer the same ArrayBuffer cell type as a plain ArrayBuffer, so the js_type_loose() == ArrayBuffer check reported true for both. Node's napi_is_arraybuffer maps to V8's IsArrayBuffer(), which is false for a SharedArrayBuffer, so gate on the buffer's shared flag to match. napi_get_arraybuffer_info is intentionally left as-is: Node still accepts a SharedArrayBuffer there (napi_ok), and the regression test pins that.
|
Updated 8:49 PM PT - Jun 22nd, 2026
❌ @robobun, your commit 700ae3f has 2 failures in
🧪 To try this PR locally: bunx bun-pr 32629That installs a local version of the PR into your bun-32629 --bun |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
Walkthrough
Changesnapi_is_arraybuffer fix and tests
Possibly related issues
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
LGTM — targeted Node-compat fix with a test that diffs against Node.
Extended reasoning...
Overview
This PR fixes napi_is_arraybuffer in src/runtime/napi/napi_body.rs to return false for SharedArrayBuffer, matching Node/V8 semantics. The previous implementation only checked the JSC cell type, which is identical for ArrayBuffer and SharedArrayBuffer; the new code resolves the value via as_array_buffer and additionally requires !ab.shared. A C++ test fixture (test_is_arraybuffer) and a checkSameOutput case in test/napi/napi.test.ts pin the behavior for ArrayBuffer, SharedArrayBuffer, and Uint8Array, including the intentionally-asymmetric napi_get_arraybuffer_info status.
Security risks
None. This is a pure type-classification predicate over a JS value; no parsing, no allocation, no auth/permissions, no untrusted input handling. The underlying JSC__JSValue__asArrayBuffer helper is read-only and already used by other napi entry points (e.g. napi_get_arraybuffer_info).
Level of scrutiny
Low. The runtime change is three lines of logic plus a comment, scoped to a single N-API predicate, and follows an existing pattern (the shared flag is already populated by JSC__JSValue__asArrayBuffer in bindings.cpp:3313). The typed_array_type == ArrayBuffer guard preserves the prior false for typed arrays / DataView, and as_array_buffer returning None for non-cells preserves false for primitives. No CODEOWNERS cover this path.
Other factors
The test follows the established test_is_buffer / test_is_typedarray pattern in the same file and uses checkSameOutput to diff against the ABI-matching Node, which is the right harness for a Node-compat fix. The bug-hunting system found no issues, and there are no prior reviewer comments to address.
Jarred-Sumner
left a comment
There was a problem hiding this comment.
Move this to .cpp and it will simplify the. code
The native helper writes via printf, which emits CRLF line endings on Windows. Splitting on a bare LF left trailing CR on each line and failed the toEqual check. checkSameOutput already verifies Bun and Node produce identical bytes; this only fixes the supplementary per-line assertion. From #32629.
Problem
napi_is_arraybufferreturnstruefor aSharedArrayBuffer. Node returnsfalse: in V8 (which N-API maps onto)napi_is_arraybufferisvalue->IsArrayBuffer(), and aSharedArrayBufferis a distinct type.Cause
In JSC both
ArrayBufferandSharedArrayBuffercarry the same cell type (JSType::ArrayBufferType), so the previous check insrc/runtime/napi/napi_body.rscould not tell them apart:The only distinguisher is the backing buffer's
isShared()flag, already surfaced to Rust asArrayBuffer.shared.Fix
Resolve the value as an array buffer and additionally require it to be non-shared, matching V8's
IsArrayBuffer():napi_get_arraybuffer_infois left unchanged on purpose. Node's behavior there is asymmetric: it still accepts aSharedArrayBufferand returnsnapi_ok, which Bun already matched. The regression test pins both behaviors.Verification
Added
test_is_arraybufferto the napi-app fixture and a case intest/napi/napi.test.tsthat diffs Bun's output against the ABI-matching Node forArrayBuffer,SharedArrayBuffer, and a typed array (napi_okis 0,napi_invalid_argis 1):Fails before the fix (Bun reports
napi_is_arraybuffer=truefor theSharedArrayBuffer), passes after.Fixes #32624