tweag / asterius

DEPRECATED in favor of ghc wasm backend, see https://www.tweag.io/blog/2022-11-22-wasm-backend-merged-in-ghc
1.98k stars 55 forks source link

Crash by "table index is out of bounds" #861

Open y-taka-23 opened 2 years ago

y-taka-23 commented 2 years ago

Describe the bug

The repro codes below crash by a JSException "RuntimeError: table index is out of bounds" after 2379 times of increment.

To Reproduce

Main.hs

module Main where

main :: IO ()
main = error "built with --no-main"

increment :: Int -> Int
increment n = n + 1

foreign export javascript "increment" increment :: Int -> Int

index.mjs

import * as rts from './rts.mjs';
import module from './asterius-repro.wasm.mjs';
import req from './asterius-repro.req.mjs';

async function handleModule(m) {
  const asterius = await rts.newAsteriusInstance(Object.assign(req, { module: m }));

  const pre = document.getElementById("counter");
  let counter = 0;

  const render = async () => {
    pre.textContent = counter;
    counter = await asterius.exports.increment(counter);
    requestAnimationFrame(render);
  };

  requestAnimationFrame(render);
}

module.then(handleModule);

Build commands (in a container)

# ahc-cabal install --installdir . --install-method copy --overwrite-policy always
# ahc-dist input-exe asterius-repro --input-mjs index.mjs --no-main --browser --bundle

Environment