Something went wrong. Try again.
Offload functions to worker threads with shared memory primitives for Node.js.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172import { describe, it } from 'node:test';import assert from 'node:assert/strict';import { shared, int32, uint8, bool } from 'moroutine';
describe('SharedStruct', () => { it('load returns all field values', () => { const point = shared({ x: int32, y: int32 }); assert.deepEqual(point.load(), { x: 0, y: 0 }); });
it('store writes all field values', () => { const point = shared({ x: int32, y: int32 }); point.store({ x: 10, y: 20 }); assert.deepEqual(point.load(), { x: 10, y: 20 }); });
it('works with mixed types', () => { const state = shared({ x: int32, y: int32, health: uint8, alive: bool }); state.store({ x: 1, y: 2, health: 100, alive: true }); assert.deepEqual(state.load(), { x: 1, y: 2, health: 100, alive: true }); });
it('supports nested structs', () => { const rect = shared({ position: { x: int32, y: int32 }, size: { w: int32, h: int32 }, });
rect.store({ position: { x: 1, y: 2 }, size: { w: 100, h: 50 } }); assert.deepEqual(rect.load(), { position: { x: 1, y: 2 }, size: { w: 100, h: 50 } }); });
it('inner struct mutation is visible through outer struct', () => { const entity = shared({ pos: { x: int32, y: int32 }, alive: bool, });
entity.fields.pos.store({ x: 10, y: 20 }); assert.deepEqual(entity.load(), { pos: { x: 10, y: 20 }, alive: false }); });
it('exposes fields for direct access', () => { const point = shared({ x: int32, y: int32 }); point.fields.x.store(42); assert.equal(point.fields.y.load(), 0); assert.deepEqual(point.load(), { x: 42, y: 0 }); });
it('nested fields accessible via fields property', () => { const rect = shared({ pos: { x: int32, y: int32 }, w: int32, h: int32, });
rect.fields.pos.fields.x.store(99); rect.fields.w.store(200); assert.deepEqual(rect.load(), { pos: { x: 99, y: 0 }, w: 200, h: 0 }); });
it('is itself Loadable (can be nested)', () => { const outer = shared({ inner: { v: int32 }, flag: bool, });
outer.store({ inner: { v: 42 }, flag: true }); assert.deepEqual(outer.load(), { inner: { v: 42 }, flag: true }); });});