Skip to content

Commit

Permalink
Shim in a different function to run the init generator to completion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sd2k committed Oct 16, 2024
1 parent 89fa1e3 commit e539ae7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/wasmstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions components/js/prophet-wasmstan/run.js
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 8 additions & 0 deletions components/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e539ae7

Please sign in to comment.