Repository navigation
feat: add WebAssembly binding for client-side analysis and execution #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ascandone
wants to merge
2
commits into
main
Choose a base branch
from
feat/wasm-binding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| name: WASM Release | ||
|
|
||
| # Builds the client-side Numscript WebAssembly artifact and attaches it to a | ||
| # GitHub release, so consumers (e.g. platform-ui's numscript-playground) can pull | ||
| # it at build time. The repo is public, so the release assets download | ||
| # anonymously — no token needed on the consumer side. | ||
| # | ||
| # Trigger: push a tag `wasm-vX.Y.Z` (separate from the Go CLI `vX.Y.Z` tags, so | ||
| # it never collides with GoReleaser). Uses the built-in GITHUB_TOKEN — no admin | ||
| # setup, no secret to provision. | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - 'wasm-v*.*.*' | ||
|
|
||
| permissions: | ||
| contents: write | ||
|
|
||
| jobs: | ||
| release: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 15 | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: actions/setup-go@v5 | ||
| with: | ||
| go-version: 1.27.3 | ||
| cache-dependency-path: go.sum | ||
|
|
||
| - name: Build wasm (+ matching toolchain glue) | ||
| run: | | ||
| mkdir -p dist | ||
| GOOS=js GOARCH=wasm go build -trimpath -ldflags="-s -w" -o dist/numscript.wasm ./cmd/wasm | ||
| cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" dist/wasm_exec.js | ||
| ( cd dist && shasum -a 256 numscript.wasm > numscript.wasm.sha256 ) || \ | ||
| ( cd dist && sha256sum numscript.wasm > numscript.wasm.sha256 ) | ||
| ls -lh dist | ||
|
|
||
| - name: Publish release assets | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| run: | | ||
| TAG="${GITHUB_REF_NAME}" | ||
| NOTES="Client-side Numscript WebAssembly build (Go $(go env GOVERSION)). | ||
|
|
||
| - \`numscript.wasm\` — the numscript library compiled to js/wasm. | ||
| - \`wasm_exec.js\` — matching Go toolchain glue (version-locked). | ||
| - \`numscript.wasm.sha256\` — checksum for the build-time download. | ||
|
|
||
| Consumed by platform-ui's numscript-playground via a pinned build-time download." | ||
| gh release create "$TAG" --prerelease --title "Numscript WASM $TAG" --notes "$NOTES" \ | ||
| dist/numscript.wasm dist/wasm_exec.js dist/numscript.wasm.sha256 \ | ||
| || gh release upload "$TAG" \ | ||
| dist/numscript.wasm dist/wasm_exec.js dist/numscript.wasm.sha256 --clobber | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //go:build js && wasm | ||
|
|
||
| // Command wasm is the browser-facing entry point of the Numscript WebAssembly | ||
| // binding. It is a thin syscall/js shim over internal/wasmbinding: it registers | ||
| // a single namespace object on globalThis.__numscript and then blocks forever so | ||
| // the Go runtime (and the registered callbacks) stay alive for the lifetime of | ||
| // the page. | ||
| // | ||
| // All the real work — parsing, static analysis, execution, JSON shaping, panic | ||
| // recovery — lives in internal/wasmbinding, which is pure Go and unit-tested on | ||
| // the host toolchain. Each registered function takes a single string argument | ||
| // and returns a JSON string the TS binding parses. | ||
| package main | ||
|
|
||
| import ( | ||
| "syscall/js" | ||
|
|
||
| "github.com/formancehq/numscript/internal/wasmbinding" | ||
| ) | ||
|
|
||
| func stringArg(args []js.Value) (string, bool) { | ||
| if len(args) < 1 || args[0].Type() != js.TypeString { | ||
| return "", false | ||
| } | ||
| return args[0].String(), true | ||
| } | ||
|
|
||
| func main() { | ||
| ns := js.Global().Get("Object").New() | ||
|
|
||
| // check(source) -> JSON { diagnostics: [...] } | ||
| ns.Set("check", js.FuncOf(func(_ js.Value, args []js.Value) any { | ||
| source, ok := stringArg(args) | ||
| if !ok { | ||
| return `{"diagnostics":[]}` | ||
| } | ||
| return string(wasmbinding.Check(source)) | ||
| })) | ||
|
|
||
| // run(inputJSON) -> JSON { ok, value } | { ok:false, error } | ||
| ns.Set("run", js.FuncOf(func(_ js.Value, args []js.Value) any { | ||
| input, ok := stringArg(args) | ||
| if !ok { | ||
| return `{"ok":false,"error":"run expects a JSON string argument"}` | ||
| } | ||
| return string(wasmbinding.Run([]byte(input))) | ||
| })) | ||
|
|
||
| // getNeededVariables(source) -> JSON { ok, variables } | { ok:false, error } | ||
| ns.Set("getNeededVariables", js.FuncOf(func(_ js.Value, args []js.Value) any { | ||
| source, ok := stringArg(args) | ||
| if !ok { | ||
| return `{"ok":false,"error":"getNeededVariables expects a string argument"}` | ||
| } | ||
| return string(wasmbinding.NeededVariables(source)) | ||
| })) | ||
|
|
||
| // version() -> the binding's ABI version string | ||
| ns.Set("version", js.FuncOf(func(_ js.Value, _ []js.Value) any { | ||
| return wasmbinding.Version | ||
| })) | ||
|
|
||
| js.Global().Set("__numscript", ns) | ||
|
|
||
| // Block forever: if main returned, the Go runtime would exit and every | ||
| // registered js.Func would become invalid. | ||
| select {} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 [minor] Release build duplicates the local recipe outside the standard environment
The workflow implements the wasm build directly with setup-go and shell commands rather than invoking the new Justfile recipe through Nix. This violates RFC-0015's shared local/CI recipe requirement: subsequent changes to just wasm will not affect released artifacts, and checksum generation exists only in CI.
Suggestion: Move artifact/checksum preparation into shared Just recipes and invoke them with nix develop --impure --command just from the workflow.