aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


client/tests/browser_loop.mjs (1514B)


      1 import assert from 'node:assert/strict';
      2 import fs from 'node:fs';
      3 import vm from 'node:vm';
      4 import test from 'node:test';
      5 
      6 function fixture(callback) {
      7   const frames = [];
      8   let library, calls = 0;
      9   vm.runInNewContext(fs.readFileSync(new URL('../runtime/browser_host.js', import.meta.url), 'utf8'), {
     10     addToLibrary: value => { library = value; },
     11     requestAnimationFrame: tick => frames.push(tick),
     12     getWasmTableEntry: address => { assert.equal(address, 7); return callback; },
     13     callUserCallback: callback => { ++calls; callback(); }
     14   });
     15   return {start: () => library.emscripten_request_animation_frame_loop(7, 19),
     16     frames, calls: () => calls};
     17 }
     18 
     19 test('frame callbacks use the runtime boundary and stop when the guest returns false', () => {
     20   const inputs = [];
     21   const game = fixture((timestamp, data) => { inputs.push([timestamp, data]); return inputs.length < 2; });
     22 
     23   const handle = game.start();
     24   game.frames.shift()(125);
     25   game.frames.shift()(150);
     26 
     27   assert.equal(handle, 1);
     28   assert.deepEqual(inputs, [[125, 19], [150, 19]]);
     29   assert.equal(game.calls(), 2);
     30   assert.equal(game.frames.length, 0);
     31 });
     32 
     33 test('an exception reaches the runtime boundary without scheduling another frame', () => {
     34   const failure = new WebAssembly.RuntimeError('guest failure');
     35   const game = fixture(() => { throw failure; });
     36   game.start();
     37 
     38   assert.throws(() => game.frames.shift()(125), error => error === failure);
     39   assert.equal(game.calls(), 1);
     40   assert.equal(game.frames.length, 0);
     41 });