Skip to content

runtime: expose Atomics operations for SharedArrayBuffer views #2876

@andrewtdiz

Description

@andrewtdiz

Summary

Node exposes the global Atomics object for operations on integer typed-array views over SharedArrayBuffer. Perry has ArrayBuffer, SharedArrayBuffer, and DataView constructor support, but source search found no Atomics global or runtime operations.

Expected Node behavior

Local Node v25.9.0 probe:

console.log(typeof SharedArrayBuffer); // "function"
console.log(typeof Atomics);           // "object"

const sab = new SharedArrayBuffer(4);
const ia = new Int32Array(sab);
console.log(Atomics.add(ia, 0, 5)); // 0
console.log(ia[0]);                 // 5
console.log(Atomics.load(ia, 0));   // 5

The standard surface includes methods such as load, store, add, sub, and, or, xor, exchange, compareExchange, wait, notify, and related validation behavior.

Current Perry behavior in source

docs/typescript-parity-gaps.md still calls out SharedArrayBuffer / Atomics as a missing thread/shared-memory area.

Current source has substantial ArrayBuffer / SharedArrayBuffer / DataView support:

  • crates/perry-runtime/src/object/global_this.rs lists ArrayBuffer, SharedArrayBuffer, and DataView constructors.
  • crates/perry-codegen/src/lower_call/builtin.rs lowers new ArrayBuffer(...), new SharedArrayBuffer(...), and new DataView(...).
  • crates/perry-runtime/src/buffer/from.rs implements the backing storage helpers.

But source search found no Atomics entry in global_this.rs, no HIR/codegen lowering, and no runtime methods for atomic operations. That leaves SharedArrayBuffer without the Node-compatible synchronization API that normally accompanies it.

Suggested tests

Start with deterministic single-thread semantics:

expect(typeof Atomics).toBe("object");
const sab = new SharedArrayBuffer(8);
const i32 = new Int32Array(sab);
expect(Atomics.load(i32, 0)).toBe(0);
expect(Atomics.store(i32, 0, 7)).toBe(7);
expect(Atomics.add(i32, 0, 5)).toBe(7);
expect(Atomics.load(i32, 0)).toBe(12);
expect(Atomics.compareExchange(i32, 0, 12, 99)).toBe(12);
expect(Atomics.load(i32, 0)).toBe(99);

Also cover TypeError/RangeError validation for non-shared views, unsupported element types, and out-of-bounds indexes.

Scope / non-goals

This is scoped to exposing the global Atomics object and core operations for integer typed-array views over SharedArrayBuffer. Cross-thread wait/notify scheduling may need a separate implementation phase, but the object and deterministic operations should still have Node-compatible behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions