// Compilation targets and their capability flags. // // Each `--target` names a concrete host environment and breaks down into // a fixed combination of `TargetFeatures`: // // Target | component | jsStrings | wasi | zenaJsHost | zenaComponentHost // ----------------|-----------|-----------|-------|------------|------------------ // core | false | false | false | false | false // js | false | true | false | false | false // zena-js | false | true | false | true | false // component | true | false | true | false | false // zena-component | true | false | true | false | true // // - `core`: core WebAssembly module with WTF-8 byte-backed strings and no // implicit host imports beyond `@external` declarations the program uses. // - `js`: core WebAssembly module for any JavaScript engine supporting // `wasm:js-string` builtins and imported string constants, with zero // `@zena-lang/runtime` imports. // - `zena-js`: extends `js` with `@zena-lang/runtime` host imports (console, // stack traces, host `URL`, number formatting/parsing, `zena:js`, // `zena:fetch`, `zena:time`, `zena:random`). // - `component`: WebAssembly Component Model output targeting WASI 0.3 and // application WIT worlds, runnable on any WASI 0.3 host. // - `zena-component`: extends `component` with `zena:host` WIT interfaces // provided by `zena-runtime` (stack traces, `zena:process`, `zena:wasm`, // `zena:extensions`, `zena:http` server). Default target of `zena build`. /** * The capability flags a compilation target enables. */ export type TargetFeatures = { /** Emit a WebAssembly Component rather than a core module. */ component: boolean, /** Use WTF-16 `externref` strings, `wasm:js-string` builtins, and imported string constants. */ jsStrings: boolean, /** Expose WASI 0.3 standard library modules (`cli`, `fs`, `fetch`, `time`, `random`). */ wasi: boolean, /** Enable `@zena-lang/runtime` JavaScript host imports (`console`, stack traces, host `URL`, etc.). */ zenaJsHost: boolean, /** Enable `zena:host` component interfaces (`stack-trace`, `process`, `wasm`, `extensions`, `http-server`, `secrets`, `terminal`). */ zenaComponentHost: boolean, }; /** The targets a program can be built for, in `--target`'s own spelling. */ export function targetNames(): FixedArray { return ["core", "js", "zena-js", "component", "zena-component"]; } /** The target used when `--target` is omitted. */ export let defaultTarget = "zena-component"; /** Whether `target` is a target name the compiler knows. */ export function isKnownTarget(target: String): boolean { for (let t in targetNames()) { if (t == target) { return true; } } return false; } /** `--target`'s accepted spellings, for an error message. */ export function knownTargetsDescription(): String { var out = ""; for (let t in targetNames()) { if (out != "") { out = out + ", "; } out = `${out}'${t}'`; } return out; } /** * The capability flags enabled by `target`. Throws on an unknown target. */ export function targetFeatures(target: String): TargetFeatures { if (target == "core") { return { component: false, jsStrings: false, wasi: false, zenaJsHost: false, zenaComponentHost: false, }; } if (target == "js") { return { component: false, jsStrings: true, wasi: false, zenaJsHost: false, zenaComponentHost: false, }; } if (target == "zena-js") { return { component: false, jsStrings: true, wasi: false, zenaJsHost: true, zenaComponentHost: false, }; } if (target == "component") { return { component: true, jsStrings: false, wasi: true, zenaJsHost: false, zenaComponentHost: false, }; } if (target == "zena-component") { return { component: true, jsStrings: false, wasi: true, zenaJsHost: false, zenaComponentHost: true, }; } throw new Error(`Unknown target '${target}' (expected ${knownTargetsDescription()})`); } /** * Whether the target's output is a component rather than a core module. * A component is the only form that can reach WASI p2 or p3. */ export function targetEmitsComponent(target: String): boolean { return targetFeatures(target).component; } /** * Whether the target uses WebAssembly JS String builtins (`wasm:js-string`) * and imported string constants (`js` and `zena-js`). */ export function targetUsesJsStrings(target: String): boolean { return targetFeatures(target).jsStrings; } /** Whether the target provides WASI 0.3 interfaces (`component` and `zena-component`). */ export function targetUsesWasi(target: String): boolean { return targetFeatures(target).wasi; } /** Whether the target links `@zena-lang/runtime` host imports (`zena-js`). */ export function targetUsesZenaJsHost(target: String): boolean { return targetFeatures(target).zenaJsHost; } /** Whether the target links `zena:host` WIT interfaces (`zena-component`). */ export function targetUsesZenaComponentHost(target: String): boolean { return targetFeatures(target).zenaComponentHost; } /** * The module string literals are imported from on a target that uses JS * strings, unless `--imported-string-constants` names another. It is the * `importedStringConstants` the module must be compiled with. */ export let defaultImportedStringConstants = "_";