Browser Playground (WebAssembly)¶
Run Klong directly in your browser with zero server round-trips. Everything runs locally in WebAssembly on two complementary paths:
- Interactive Klong Evaluator (
klong_wasm.wasm) — the full Rustklong-coreandklong-rtinterpreter compiled towasm32-unknown-unknownwith persistent session state, in-memoryMemFs, columnar tables (.table,.aj), exact arithmetic (.exactdot,.exactround), heap tracking (.mem()), and the bundled standard library (nstat,math,csv,set,spline,util). - Static Profile Ahead-of-Time Compilation (
kg compile wasm) — Klong kernels in the static profile compiled to freestanding C99 and linked to standalone.wasmmodules withclang --target=wasm32andwasm-ld.
Loading the offline WebAssembly playground… (Requires a browser with WebAssembly ES modules enabled.)
Using the interactive session¶
- Type or select a preset expression in the editor and press Ctrl+Enter (or Cmd+Enter on macOS) or click Run.
- Definitions (
a::...,f::{...}) and.l(...)module loads persist across runs until you click Reset Session. - Switch to the Static Profile (
kg compile wasm) tab to execute precompiledmavg.wasm,fir.wasm, andpid.wasmmodules side by side with their generated freestanding C99 source.
Compiling your own Klong module to WebAssembly¶
Programs inside the static profile compile directly to standalone .wasm binaries and ES module loaders:
kg compile wasm -o dist/ examples/embedded/mavg.kg
kgpy compile wasm -o dist/ examples/embedded/fir.kg
Each invocation writes <name>.c, <name>.h, klong_rt.h, <name>.mjs, and (when a wasm32-capable clang + wasm-ld toolchain is available) <name>.wasm. Use --emit-only to generate the freestanding C99 and <name>.mjs loader without invoking clang.
From JavaScript (browser or Node.js), import the generated <name>.mjs module:
import { instantiate } from "./mavg.mjs";
const mavg = await instantiate(new URL("./mavg.wasm", import.meta.url));
console.log(mavg.step(4.0)); // 1
console.log(mavg.step(8.0)); // 3
console.log(mavg.step(12.0)); // 6
Programs with .extern declarations import their functions from the WebAssembly "env" module:
import { instantiate } from "./pid.mjs";
const pid = await instantiate(new URL("./pid.wasm", import.meta.url), {
env: {
read_adc: (channel) => 0.3,
write_pwm: (channel, duty) => 0,
},
});
console.log(pid.step(0.31));
Rebuilding the playground assets from source¶
See Compiling Klong to C and Native Runtime for details on the underlying static compiler and runtime.