Something went wrong. Try again.
Offload functions to worker threads with shared memory primitives for Node.js.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import { describe, it } from 'node:test';import assert from 'node:assert/strict';import { shared, string, int32 } from 'moroutine';
describe('string', () => { it('string(n) creates standalone empty string', () => { const s = string(32); assert.equal(s.load(), ''); });
it('shared(string(n)) creates string', () => { const s = shared(string(32)); assert.equal(s.load(), ''); });
it('store and load a string', () => { const s = string(32); s.store('hello'); assert.equal(s.load(), 'hello'); });
it('store overwrites previous value', () => { const s = string(32); s.store('hello'); s.store('world'); assert.equal(s.load(), 'world'); });
it('store empty string', () => { const s = string(32); s.store('hello'); s.store(''); assert.equal(s.load(), ''); });
it('store throws if encoded bytes exceed max', () => { const s = string(4); assert.throws(() => s.store('hello'), /exceeds/i); });
it('handles multibyte UTF-8', () => { const s = string(32); s.store('héllo'); assert.equal(s.load(), 'héllo'); });
it('string in struct schema', () => { const entity = shared({ name: string(16), hp: int32 }); entity.store({ name: 'goblin', hp: 50 }); assert.deepEqual(entity.load(), { name: 'goblin', hp: 50 }); });
it('string in struct shares one buffer', () => { const entity = shared({ name: string(16), hp: int32 }); const SHARED = Symbol.for('moroutine.shared'); const nameSer = (entity.fields.name as any)[SHARED](); const hpSer = (entity.fields.hp as any)[SHARED](); assert.equal(nameSer.buffer, hpSer.buffer); });});