Use EMSCRIPTEN_FETCH_SYNCHRONOUS with ASYNCIFY enabled in Berry custom function to send HTTP(s) requests from inside WASM
Tested with berry_web commit 9ebc4e2d, Berry commit fa5d4330, Emscripten v3.1.68.
- Open this repo in devcontainer, e.g. using Github Codespaces. Type or copy/paste following commands to devcontainer's terminal.
cd
into the folder of this example:
cd browser-sync-xhr-asyncify
- Clone the
berry_web
repo. It contains Berry's port to Emscripten platform and has main Berry-lang repo as submodule:
git clone --depth=1 --recurse-submodules https://github.com/berry-lang/berry_web.git
- Copy example's source into the folder
berry_web/platform
:
cp ../browser-sync-xhr/main.c berry_web/platform/berry.c
cd
into the folder ofberry_web
:
cd berry_web
- Patch Makefile to produce WASM output and enable outbound HTTP requests:
sed -i.bak 's|-s WASM=0|-s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s FETCH=1 --proxy-to-worker|; s|docs/berry.js|docs/berry.html|' Makefile
- Compile the example using Emscripten:
make
This command will output docs/berry.js
and docs/berry.wasm
.
cd
back into the folder of this example:
cd ..
- Copy WASM and glue JS into current folder:
cp berry_web/docs/berry.{js,wasm} ./
- Add
writeOutputText()
function to berry.js:
echo "async function writeOutputText(text) { console.log(text); }" >> berry.js
- Run simple HTTP server to temporarily publish project to Web:
python3 -m http.server
Codespace will show you "Open in Browser" button. Just click that button or obtain web address from "Forwarded Ports" tab.
- As
index.html
, JS files, and a 925k-sizedberry.wasm
are loaded into browser, refer to browser developer console to see the results.
Perform your own experiments if desired.