Offload functions to worker threads with shared memory primitives for Node.js.
1import { describe, it } from 'node:test';
2import assert from 'node:assert/strict';
3import { shared, int32, uint8, bool } from 'moroutine';
4
5describe('SharedStruct', () => {
6 it('load returns all field values', () => {
7 const point = shared({ x: int32, y: int32 });
8 assert.deepEqual(point.load(), { x: 0, y: 0 });
9 });
10
11 it('store writes all field values', () => {
12 const point = shared({ x: int32, y: int32 });
13 point.store({ x: 10, y: 20 });
14 assert.deepEqual(point.load(), { x: 10, y: 20 });
15 });
16
17 it('works with mixed types', () => {
18 const state = shared({ x: int32, y: int32, health: uint8, alive: bool });
19 state.store({ x: 1, y: 2, health: 100, alive: true });
20 assert.deepEqual(state.load(), { x: 1, y: 2, health: 100, alive: true });
21 });
22
23 it('supports nested structs', () => {
24 const rect = shared({
25 position: { x: int32, y: int32 },
26 size: { w: int32, h: int32 },
27 });
28
29 rect.store({ position: { x: 1, y: 2 }, size: { w: 100, h: 50 } });
30 assert.deepEqual(rect.load(), { position: { x: 1, y: 2 }, size: { w: 100, h: 50 } });
31 });
32
33 it('inner struct mutation is visible through outer struct', () => {
34 const entity = shared({
35 pos: { x: int32, y: int32 },
36 alive: bool,
37 });
38
39 entity.fields.pos.store({ x: 10, y: 20 });
40 assert.deepEqual(entity.load(), { pos: { x: 10, y: 20 }, alive: false });
41 });
42
43 it('exposes fields for direct access', () => {
44 const point = shared({ x: int32, y: int32 });
45 point.fields.x.store(42);
46 assert.equal(point.fields.y.load(), 0);
47 assert.deepEqual(point.load(), { x: 42, y: 0 });
48 });
49
50 it('nested fields accessible via fields property', () => {
51 const rect = shared({
52 pos: { x: int32, y: int32 },
53 w: int32,
54 h: int32,
55 });
56
57 rect.fields.pos.fields.x.store(99);
58 rect.fields.w.store(200);
59 assert.deepEqual(rect.load(), { pos: { x: 99, y: 0 }, w: 200, h: 0 });
60 });
61
62 it('is itself Loadable (can be nested)', () => {
63 const outer = shared({
64 inner: { v: int32 },
65 flag: bool,
66 });
67
68 outer.store({ inner: { v: 42 }, flag: true });
69 assert.deepEqual(outer.load(), { inner: { v: 42 }, flag: true });
70 });
71});