From e539ae7fa7f87cf599548ffe853512268e6bb38e Mon Sep 17 00:00:00 2001 From: Ben Sully Date: Wed, 16 Oct 2024 14:40:34 +0100 Subject: [PATCH] Shim in a different function to run the init generator to completion When I tried to use the existing module within a Webpack project I found that the generator was continuing with promises and trying to initialize WebAssembly using them, which obviously failed. This function seems to work a lot better for some reason, although I haven't digged into why, exactly. --- .github/workflows/wasmstan.yml | 2 +- components/js/prophet-wasmstan/run.js | 17 +++++++++++++++++ components/justfile | 8 ++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 components/js/prophet-wasmstan/run.js diff --git a/.github/workflows/wasmstan.yml b/.github/workflows/wasmstan.yml index b2913ffa..186ff932 100644 --- a/.github/workflows/wasmstan.yml +++ b/.github/workflows/wasmstan.yml @@ -20,7 +20,7 @@ jobs: targets: wasm32-unknown-unknown,wasm32-wasip1 - uses: taiki-e/install-action@v2 with: - tool: cargo-binstall,just,wasmtime + tool: cargo-binstall,just,ripgrep,wasmtime - name: Install deps run: just components/install-deps - uses: actions/setup-node@v4 diff --git a/components/js/prophet-wasmstan/run.js b/components/js/prophet-wasmstan/run.js new file mode 100644 index 00000000..181107d5 --- /dev/null +++ b/components/js/prophet-wasmstan/run.js @@ -0,0 +1,17 @@ +// Note: this function comes from https://stackoverflow.com/questions/30401486/ecma6-generators-yield-promise. +// It is used to convert a generator function into a promise. +// `jco transpile` generates a similar function but it didn't work for me. +// I'm not sure why, but I'll raise an issue on the `jco` repo. +// See the `justfile` for how this gets shimmed into the transpiled code; +// in short, we use `ripgrep` as in +// https://unix.stackexchange.com/questions/181180/replace-multiline-string-in-files +// (it was a Stack-Overflow heavy day...) +// The indentation is intentional so the function matches the original. + function run(g) { + return Promise.resolve(function step(v) { + const res = g.next(v); + if (res.done) return res.value; + return res.value.then(step); + }()); + } + return run(gen); diff --git a/components/justfile b/components/justfile index 3d7958f9..23fc3109 100644 --- a/components/justfile +++ b/components/justfile @@ -74,6 +74,14 @@ transpile: build --name prophet-wasmstan \ --out-dir js/prophet-wasmstan \ cpp/prophet-wasmstan/wit/prophet-wasmstan.wit + rg --replace="$(rg --invert-match --no-line-number '//' js/prophet-wasmstan/run.js)" \ + --multiline --multiline-dotall \ + --passthru \ + --no-line-number \ + ' let promise, resolve, reject;.+?return promise \|\| maybeSyncReturn;' \ + js/prophet-wasmstan/prophet-wasmstan.js \ + > js/prophet-wasmstan/prophet-wasmstan.fixed.js + mv js/prophet-wasmstan/prophet-wasmstan.fixed.js js/prophet-wasmstan/prophet-wasmstan.js test: transpile cd js/prophet-wasmstan && npm ci && npm run test:ci