SOURCE / PINNED RELEASE
Made of little things.
DOOM
- Release
- f8a5db646fdc…
- Author-recorded commit
- d8e055796675…
- License
- LICENSE
- Author’s source reference
- nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-37af437f0e6
Archive hash verified: 66555ff880270ca9…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
import { runInNewContext } from 'node:vm';
import ts from 'typescript';
const wasm = await readFile(
new URL('../vendor/doom/doom.wasm', import.meta.url),
);
const source = (
await readFile(new URL('../src/engine.ts', import.meta.url), 'utf8')
).replace(/^import wasmData.*$/m, '');
const compiled = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
},
}).outputText;
function runtime() {
const module = { exports: {} };
runInNewContext(compiled, {
module,
exports: module.exports,
wasmData: `data:application/wasm;base64,${wasm.toString('base64')}`,
WebAssembly,
Uint8Array,
TextDecoder,
performance,
btoa,
atob,
setTimeout,
clearInterval,
window: { setInterval },
});
return module.exports;
}
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
test('bundled engine needs only its documented local imports', async () => {
const mod = await WebAssembly.compile(wasm);
assert.equal(WebAssembly.Module.imports(mod).length, 10);
const modules = [
...new Set(WebAssembly.Module.imports(mod).map((i) => i.module)),
].sort();
assert.deepEqual(modules, [
'console',
'gameSaving',
'loading',
'runtimeControl',
'ui',
]);
});
test('real DOOM starts, saves, reloads and stops its frame loop', async () => {
const { DoomEngine, KEY, toBase64, fromBase64 } = runtime();
const saves = new Map();
const errors = [];
const canvas = {
width: 0,
height: 0,
getContext: () => ({
createImageData: (w, h) => ({ data: new Uint8ClampedArray(w * h * 4) }),
putImageData: () => {},
}),
};
const game = new DoomEngine(
canvas,
saves,
() => {},
(error) => errors.push(error),
);
try {
await game.initialize();
assert.equal(canvas.width, 640);
assert.equal(canvas.height, 400);
game.resume();
await game.sequence([KEY.escape, KEY.enter, KEY.enter, KEY.enter]);
await game.sequence([
KEY.save,
49,
KEY.enter,
67,
72,
69,
67,
75,
KEY.enter,
]);
assert.ok(saves.get(0)?.length >= 30000, 'native checkpoint is returned');
assert.deepEqual(
[...fromBase64(toBase64(saves.get(0)))],
[...saves.get(0)],
);
await game.sequence([KEY.load, 49, KEY.enter]);
assert.ok(game.frames > 15, 'engine rendered gameplay');
game.pause();
const stopped = game.frames;
await sleep(90);
assert.equal(game.frames, stopped);
game.resume();
await sleep(90);
assert.ok(game.frames > stopped);
assert.deepEqual(errors, []);
} finally {
game.dispose();
}
});
