Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/wasm-release.yml
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: |

Copy link
Copy Markdown
Contributor

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.

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
9 changes: 9 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ tests:
test:
@go test -race -covermode=atomic -coverprofile coverage.txt ./...

# Build the browser WebAssembly binding (dist/numscript.wasm + wasm_exec.js).
# wasm_exec.js is version-locked to the building toolchain, so always copy it
# from GOROOT rather than pinning a checked-in copy.
wasm:
@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
@ls -lh dist/numscript.wasm

release-local:
@goreleaser release --nightly --skip=publish --clean

Expand Down
68 changes: 68 additions & 0 deletions cmd/wasm/main.go
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 {}
}
Loading
Loading