Targets · TypeScript · Native modules

Native modules

The hand-written, per-platform code behind NATIVE functions.

Contents

How a Program becomes TypeScript, in parts. Start with the introduction, then follow each part into the rendered output and the runtime it links against.

Hand-written bodies

Most of a program is written in Program and rendered to TypeScript. A NATIVE function is the exception: its body is hand-written. Because JavaScript is valid TypeScript, a native with no ts NATIVE row reuses its js body verbatim, wrapped in a typed signature; a ts row is added only when the body needs types the plain JavaScript cannot express.

export function ToString(input: Code.Language.Json.Value): string {
  return JSON.stringify(Code.Language.Json.Value._unionToRaw(input));  // js body, typed
}

The registry

Each native module's top-level functions and classes are gathered into a registry so generated code can reach them by name, with their .d.ts declarations honoured by the compiler.

Drivers, and the runtime, are native too

The drivers are native code — Ui.Dom.Driver, Animate.Frame.Driver, Network.Web.Driver — the one place the real platform (document, fetch, requestAnimationFrame) is touched. Reactive and Context are themselves native modules, bundled first, so the result is one typed artifact — runtime, native bodies, and program — that a page or Node can load.