Status: Draft
JavaScript is a first-class Chasm compilation target. It allows Chasm applications and libraries to run directly in browsers, Node.js, and JavaScript workers without requiring WebAssembly.
The JavaScript compiler consumes the same typed intermediate representation as the VM and native backends:
Source
↓
Lexer
↓
Parser
↓
AST
↓
Type Checker
↓
Typed IR
├── Bytecode VM
├── Native Compiler
├── JavaScript Compiler
└── WebAssembly Compiler
The core language semantics remain the same across backends. Host APIs and low-level capabilities may differ by target.
The output target and host platform are separate concepts:
chasm build --target javascript --platform browser
chasm build --target javascript --platform node
chasm build --target javascript --platform workerThe profiles provide different host capabilities:
browserexposes browser and DOM APIs.nodeexposes Node.js and npm integration.workerexposes the APIs available to the selected worker runtime.
A program that uses unavailable host capabilities should fail at compile time when possible.
Each Chasm module compiles to an ECMAScript module:
src/
app.chasm → dist/app.js
net/
http.chasm → dist/net/http.js
module.chasm → dist/net.js
Chasm's filesystem modules and explicit barrel exports map naturally to ESM imports and exports. Generated output should remain compatible with standard JavaScript runtimes, bundlers, and tree-shaking tools.
A JavaScript build should be able to emit:
module.js
module.js.map
module.d.ts
- JavaScript contains executable ESM output.
- Source maps preserve Chasm locations in browser and Node debuggers.
- TypeScript declarations allow JavaScript and TypeScript applications to consume exported Chasm APIs.
Readable stack traces and reliable source maps are part of the backend's correctness requirements.
On the JavaScript backend, imported JavaScript modules should compile to direct ESM imports whenever possible.
Conceptually:
extern "js" module npm::"date-fns" as dates
let result = dates#format(
date: now,
format: "yyyy-MM-dd",
)
The compiler delegates package resolution to the host toolchain rather than implementing an npm resolver.
A mixed project may retain the ordinary JavaScript ecosystem files:
project/
chasm.toml
package.json
pnpm-lock.yaml
src/
main.chasm
npm, pnpm, or another supported JavaScript package manager remains responsible for installation and lockfiles.
TypeScript declaration files can provide the starting point for generated Chasm foreign-module contracts. Complex TypeScript features may require generated approximations, manual binding overrides, or a dynamic JsValue escape hatch.
Browser APIs may be described by generated contracts derived from Web IDL or TypeScript's DOM declarations.
Conceptually:
let element = dom::document#querySelector(
selector: "#app",
)
Chasm should not build a separate DOM abstraction when direct, typed bindings can represent the platform accurately.
JavaScript objects have reference semantics; Chasm values do not. Generated JavaScript must preserve Chasm's observable value semantics.
let second = first
second.name = "changed"
This must not mutate first unless an explicit mutable borrow or foreign reference-semantic handle is involved.
The backend may use:
- Copy elision
- Uniqueness analysis
- Copy-on-write storage
- Immutable internal representations
- Explicit copying when required
A foreign JsValue retains JavaScript reference semantics and remains distinct from an ordinary Chasm value.
JavaScript cannot guarantee immediate physical reclamation of ordinary object memory. The JavaScript backend therefore preserves Chasm's lifetime and resource semantics while delegating physical object reclamation to the host garbage collector.
At region destruction, generated code:
- Runs deterministic Chasm resource finalizers.
- Unregisters region-owned browser or host resources.
- Releases references owned by the region.
- Allows the JavaScript garbage collector to reclaim ordinary object memory later.
Useful region-owned browser resources include:
- Event listeners
- Timers
- Observers
- Object URLs
- WebSocket connections
- WebGL or WebGPU resources
Chasm must not claim that a JavaScript build can force immediate physical memory reclamation.
Ordinary JavaScript objects do not have a stable native memory layout. Layout-sensitive Chasm features therefore require explicit target capabilities.
A hybrid representation is preferred:
- Ordinary structs compile to efficient JavaScript representations.
- Explicitly layout-sensitive data uses
ArrayBuffer,DataView, or typed arrays. - Raw pointers are unavailable for ordinary JavaScript objects.
- FFI-layout data may retain a declared layout when backed by linear memory.
The final capability and diagnostic model remains to be designed.
Generated JavaScript must match the VM and native backends:
I32uses explicit signed 32-bit operations.U32uses explicit unsigned normalization.F32usesMath.froundwhere required.F64uses JavaScript numbers.I64andU64useBigIntor a specified runtime representation.- Overflow, division, conversion, and comparison behavior remain backend-independent.
These rules require shared backend conformance tests.
The JavaScript backend should eventually map Chasm asynchronous functions to JavaScript promises and allow declared promise-returning JavaScript functions to participate in Chasm async code.
The exact async model remains undecided. It must account for:
- Promise rejection and Chasm errors
- Region lifetimes across suspension
- Cancellation
- Browser and Node event loops
- Async callbacks entering Chasm
Chasm emits standards-based ESM and delegates bundling, minification, package resolution, and development serving to mature JavaScript tools:
Chasm compiler
↓
ESM JavaScript + source maps + TypeScript declarations
↓
Vite, esbuild, Rollup, Node.js, or a browser
Chasm should not implement its own JavaScript bundler.
The compiler plan proposes an interpreter over typed IR as the initial semantic reference, with the bytecode VM checked against it later. A shared conformance suite should run equivalent programs through the reference, JavaScript, and native backends and compare observable behavior. Parity covers specified observations on supported capabilities, within stated resource limits.
The suite must cover:
- Evaluation order
- Integer and floating-point behavior
- Value copying and mutation
- Structural projection
- Borrow validation
- Region and resource cleanup
- Errors and panics
- Callable values
- Module initialization
- Establish the typed-IR interpreter as the semantic reference.
- Emit standards-based ESM from typed IR.
- Add source maps and browser debugging.
- Implement Chasm value semantics in generated JavaScript.
- Add direct JavaScript module bindings.
- Generate TypeScript declarations for Chasm exports.
- Consume straightforward TypeScript declarations for imports.
- Add DOM and npm workflows.
- Add async interoperability after Chasm's async semantics are defined.
- Add layout-sensitive JavaScript representations where required.