Skip to content

Latest commit

 

History

History
97 lines (64 loc) · 2.41 KB

README.md

File metadata and controls

97 lines (64 loc) · 2.41 KB

Use ofunc/lua's lmodhttpclient library to send HTTP(s) requests from inside WASM

Instructions for this devcontainer

Preparation

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

As of erdian718/lua (aka ofunc/lua) received no updates since Jul 2019, it can not be compiled by recent versions of Golang compiler due to enforcement of module mode (say GO111MODULE=on). So let's build that Lua from sources, applying necessary patches:

  1. Clone erdian718/lua's repo to get sources:
git clone --depth=1 https://github.com/erdian718/lua.git 
  1. cd into the folder of erdian718/lua's sources:
cd lua
  1. Obtain erdian718/lmodhttpclient's sources by cloning its repo next to built-in Lua modules:
git clone --depth=1 https://github.com/erdian718/lmodhttpclient.git
  1. Init Golang module:
go mod init github.com/ofunc/lua
  1. Patch sources to be compatible with module mod by running a Sed command:
grep -rl '"ofunc/lua' * | xargs sed -i.bak 's|"ofunc/lua|"github.com/ofunc/lua|g'
  1. Additionally patch lmodjs's sources by running a Sed command:
sed -i.bak 's@if x := v.Get(LuaKey); x != undefined && x != null {@if x := v.Get(LuaKey); !(x.IsUndefined() || x.IsNull()) {@' lmodjs/mod.go

Building

  1. Ensure that your shell terminal is still in lua folder after completing all preparation steps.

  2. Install lmodhttpclient's dependency library publicsuffix:

go get golang.org/x/net/publicsuffix
  1. Make a folder for WASM module, copy there the sources of this example, and cd into it:
mkdir main
cd main
cp ../../browser-lmodhttpclient/main.go ./
cp ../../browser-lmodhttpclient/index.html ./
  1. Compile the example:
GOOS=js GOARCH=wasm go build -o main.wasm main.go
  1. Copy the glue JS from Golang distribution to example's folder:
cp "$(go env GOROOT)/misc/wasm/wasm_exec.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 and a 10M-sized main.wasm are loaded into browser, refer to browser developer console to see the results.

Finish

Perform your own experiments if desired.