WASM evaluation runtime
The WASM evaluation runtime is a standalone WebAssembly boundary for local Go Feature Flag evaluation. A host sends one flag, its evaluation context, and evaluation options as JSON. The runtime checks the input, uses the core evaluator, and returns a JSON variation result without a network call. See the flag evaluation and targeting and core evaluation module pages for the evaluation model, and the OpenFeature provider support page for a host integration.
Evaluation boundary
The cmd/wasm module exposes evaluate as the host-facing entrypoint. It turns a serialized request into the core evaluation context, evaluates the flag, and returns the same result shape used by the core evaluation system.
Evaluation request
The request combines the data needed for one local decision. The flag data carries its variations and rules. The evaluation context carries the targeting key and custom attributes. The flag context carries the default SDK value and any shared context data.
| Field | Role |
|---|---|
flagKey | Identifies the flag being evaluated. |
flag | Supplies the flag definition and its targeting rules. |
evalContext | Supplies the targeting key and custom attributes. |
flagContext | Supplies evaluation options such as the SDK default value. |
The runtime allows a missing or empty targeting key at this boundary. Core evaluation decides whether the flag needs one for its targeting or bucketing path.
References: cmd/…/evaluate_input.go• L7-12 cmd/…/main.go• L122-135 modules/…/context.go• L3-14
Evaluation flow
The flow shows how a host request becomes a structured variation result and returns to the host.
flowchart LR
H[Host] -->|JSON request| E[evaluate]
E --> G[Input and query guards]
G --> C[Core evaluation]
C --> R[VariationResult JSON]
R -->|pointer and length| H
The entrypoint reads the host buffer before it calls the evaluator. It checks input shape and query complexity first. A valid request is converted to the core context and passed to the core evaluator with the requested flag key and default value. The result includes the selected value and resolution details such as reason, error code, version, metadata, and whether evaluation failed.
References: cmd/…/main.go• L41-57 cmd/…/main.go• L89-119 modules/…/evaluation.go• L16-66 modules/…/variation_result.go• L14-31
Input safeguards
The runtime rejects input that could exhaust the fixed WebAssembly stack before recursive decoding or query parsing can run. These checks return a structured PARSE_ERROR, so a host can treat the result as a failed evaluation instead of losing the runtime instance.
Request guards
The request JSON has a maximum nesting depth of 128. The guard scans JSON syntax while ignoring brackets inside string values. This protects the request decode step, which must handle the complete flag, context, and flag-context object before evaluation starts.
References: cmd/…/main.go• L89-105 cmd/…/guards.go• L19-25 cmd/…/guards_test.go• L17-35 cmd/…/main_test.go• L13-46
Targeting query guards
The runtime checks every targeting query in the flag before core evaluation. This includes normal rules, the default rule, and scheduled rollout steps. It recognizes both supported query forms and applies limits that match their different parser shapes.
| Query form | Guarded condition | Limit |
|---|---|---|
| Nikunjy expression | Bracket or parenthesis nesting | 64 levels |
| JSONLogic document | JSON nesting | 256 levels |
| Either form | Items in one list or operand array | 1,000 items |
| Nikunjy expression | Conditions joined by and or or | 1,000 conditions |
| Nikunjy expression | Segments in one dotted attribute path | 128 segments |
The scanner examines the trimmed query that the evaluator parses. A query that exceeds a limit returns PARSE_ERROR before the core parser runs. JSONLogic variable paths do not use the Nikunjy dotted-path limit because core JSONLogic evaluation resolves them iteratively. The guard tests also cover large flat lists, split logical operators, scheduled steps, and fuzzed JSON shapes so that safe inputs remain accepted while unsafe inputs are rejected.
References: cmd/…/guards.go• L27-74 cmd/…/guards.go• L319-411 cmd/…/main.go• L97-105 cmd/…/guards_test.go• L223-493 cmd/…/guards_test.go• L495-664
Error recovery
The runtime converts ordinary Go panics during input handling or evaluation into a structured GENERAL error result. It also protects the final output-copy step with a pre-serialized error result. The evaluation tests verify that valid, invalid, and arbitrary input still produce JSON that a host can parse. A WebAssembly trap caused by stack overflow or a similar runtime fault cannot be recovered inside the call, so the host must discard that instance and create a new one.
References: cmd/…/main.go• L22-87 cmd/…/main_test.go• L48-66 cmd/…/guards_test.go• L666-697
Memory exchange
The runtime uses a small C-like memory interface so hosts in different languages can exchange JSON without sharing Go values. The host owns the input buffer it allocates. The runtime returns a pointer and byte length for its output.
Host call contract
For one evaluation, a host follows this sequence:
- Encode the request as UTF-8 JSON.
- Allocate an input buffer with
mallocand write the JSON bytes plus a trailing NUL byte. - Call
evaluatewith the input pointer and the JSON length. The length excludes the NUL byte. - Split the returned 64-bit value into an output pointer and output length. Read and parse the output before calling into the instance again.
- Free the input buffer with
free.
The output remains valid only until the next call into the same instance. The host must not run two evaluations at once on one instance. Parallel callers need separate instances or an instance pool.
References: cmd/…/README.md• L33-71 cmd/…/wasm.go• L17-40
Instance lifetime
The module uses a single-threaded execution model for each instance. A successful call leaves the output reachable until the next call. A trapped instance is permanently unsafe because its shadow-stack state may remain damaged. Hosts must drop the instance, its store, and its memory after a trap, and they must not call free on that instance.
References: cmd/…/README.md• L58-71 cmd/…/wasm.go• L17-24
Compilation targets
The runtime is built in two forms so the same evaluation boundary can run in browser-oriented hosts and in WASI runtimes. Both forms expose the evaluation ABI and use a one-megabyte shadow stack to support realistic recursive flag and query processing.
Browser WASM target
The .wasm target is intended for browser and JavaScript hosts. It is built from the standalone module with TinyGo and the custom WASM target definition.
References: cmd/…/README.md• L9-14 Makefile• L62-64 cmd/…/wasm-stack1m.json• L1-4
WASI target
The .wasi target is intended for WASI hosts such as Wasmtime and Chicory. It uses the same evaluation behavior as the browser target while matching the host runtime’s WASI interface.
References: cmd/…/README.md• L9-14 Makefile• L65-66 cmd/…/wasi-stack1m.json• L1-4
Stack validation and release
The build gives both targets a one-megabyte shadow stack instead of the default 64-kilobyte stack. A separate verification step checks the built binaries and fails when the linker setting is missing. The release flow builds, verifies, and publishes both artifacts for consumers such as in-process OpenFeature providers. The current WASM module release is 0.3.1 and uses core evaluator 0.8.2. The standalone module uses Go 1.25.14 and a maintained rules fork that adds prerelease version support and rejects invalid rule syntax during parsing.
References: Makefile• L7-17 Makefile• L62-70 .github/…/release_wasm.sh• L7-17 .github/…/release-wasm.yml• L36-54 cmd/…/CHANGELOG.md• L3-10 cmd/…/go.mod• L1-25
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the JSON request fields aligned with the host providers that serialize
EvaluateInput. - Keep the returned pointer-and-length contract aligned with every host runtime. The host must read output before its next module call.
- If core flag or query parsing becomes more recursive, review the guard limits and the one-megabyte stack validation together.
- If the exported ABI changes, update both compilation targets and the published .wasm and
.wasiartifacts as one release unit. - If targeting query trimming changes, keep guard scanning aligned with the exact query text that core evaluation parses.
- If the core evaluator or its rules dependencies change, confirm that the standalone
cmd/wasmmodule still resolves compatible versions before publishing either artifact. - If the module or core evaluator version changes, update the release metadata and any provider that pins the bundled WASI artifact.
- If the rules fork changes, confirm that rule version checks and syntax errors still match core evaluation behavior.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Input JSON exceeds 128 nesting levels | The request is rejected before evaluation with PARSE_ERROR. | The input-depth scan runs before JSON decoding. |
| A targeting query exceeds its nesting, list, condition, or path limit | The flag returns a structured PARSE_ERROR and the host can use its normal default-value behavior. | Query guards run across rules, default rules, and scheduled steps. |
| The host calls into an instance before reading the previous output | The previous output buffer may no longer be valid. | Read and parse output before any further call. |
| Two threads share one instance | Calls can race because an instance is not reentrant. | Use one instance per thread or an instance pool. |
| A call traps | Later calls on that instance can fault, including memory allocation. | Discard the trapped instance and create a fresh one. |
| A target build loses the linker stack setting | Realistic configurations can trap after release. | Run verify-wasm-stack for both binaries before publishing. |
| The standalone module resolves an incompatible core or rules dependency | The WASM artifact can differ from the main evaluator or reject rules in a different way. | Review go.mod and go.sum with each module release. |
Key files
| File | Why to read it |
|---|---|
| cmd/…/main.go | Defines the exported entrypoint, request flow, context conversion, and structured error behavior. |
| cmd/…/evaluate_input.go | Defines the JSON shape for one evaluation request. |
| cmd/…/guards.go | Defines the safety limits for request and targeting-query complexity. |
| cmd/…/wasm.go | Defines the host-to-module buffer exchange and output lifetime rules. |
| cmd/…/README.md | States the host ABI, instance rules, safeguards, and supported host types in one place. |
cmd/wasm/go.mod | Defines the standalone module’s Go version, core evaluator boundary, and maintained rules dependency. |
cmd/wasm/go.sum | Records checksums for the standalone module’s resolved dependencies. |
| cmd/…/CHANGELOG.md | Records the released WASM module version and its core evaluator dependency updates. |
| cmd/…/wasm-stack1m.json | Selects the browser-oriented target with the required shadow stack. |
| cmd/…/wasi-stack1m.json | Selects the WASI target with the required shadow stack. |
| openfeature/…/_wasi_version.txt | Pins the WASI engine version used by the Python provider. |
Makefile | Shows how both artifacts are built and how their stack sizes are checked. |