Skip to content

Latest commit

 

History

History
89 lines (57 loc) · 2.18 KB

README.md

File metadata and controls

89 lines (57 loc) · 2.18 KB

Use EMSCRIPTEN_FETCH_SYNCHRONOUS with ASYNCIFY enabled in Berry custom function to send HTTP(s) requests from inside WASM

Instructions for this devcontainer

Tested with berry_web commit 9ebc4e2d, Berry commit fa5d4330, Emscripten v3.1.68.

Preparation

  1. Open this repo in devcontainer, e.g. using Github Codespaces. Type or copy/paste following commands to devcontainer's terminal.

Building

  1. cd into the folder of this example:
cd browser-sync-xhr-asyncify
  1. 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
  1. Copy example's source into the folder berry_web/platform:
cp ../browser-sync-xhr/main.c berry_web/platform/berry.c
  1. cd into the folder of berry_web:
cd berry_web
  1. 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
  1. Compile the example using Emscripten:
make

This command will output docs/berry.js and docs/berry.wasm.

  1. cd back into the folder of this example:
cd ..
  1. Copy WASM and glue JS into current folder:
cp berry_web/docs/berry.{js,wasm} ./
  1. Add writeOutputText() function to berry.js:
echo "async function writeOutputText(text) { console.log(text); }" >> berry.js

Test with browser

  1. 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.

  1. As index.html, JS files, and a 925k-sized berry.wasm are loaded into browser, refer to browser developer console to see the results.

Finish

Perform your own experiments if desired.