Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

1.9 KB
Raw Blame
// The native host API, installed by the Rust runtime as `globalThis.__native` before runtime JS runs.
// Every module owns one namespace (`sys`, `gpu`, `c2d`, `net`).

interface NativeSys {
  /** page time in ms (performance.now()) */
  now(): number;
  /** wake the page at page time `ms` (earliest pending timer); Infinity cancels */
  wakeAt(ms: number): void;
  /** ask for an animation frame (__rt.frame will be called) */
  requestFrame(): void;
  log(level: "log" | "info" | "warn" | "error" | "debug", message: string): void;
  /** load a new page (navigation / reload) */
  navigate(url: string): void;
  /** message to the host shell bridge */
  shell(name: string, ...args: string[]): void;
  closeWindow(): void;
  /** test hook: write a PNG of the next composited frame */
  shot(path: string): void;
  /** test hook: quit the app */
  exit(code?: number): void;
  /** text file shipped next to the bundle, or null */
  resource(name: string): string | null;
  /** fill a typed array with CSPRNG bytes (returns it) */
  randomBytes<T extends ArrayBufferView>(a: T): T;
}

interface NativeHost {
  sys: NativeSys;
  /** WebGPU over wgpu (webgpu/, webgpu/js/webgpu.ts); absent without a GPU device */
  gpu?: any;
  /** Canvas 2D + image decoding/encoding (runtime/crates/canvas2d, runtime/js/canvas2d.ts) */
  c2d?: any;
  /** fetch / WebSocket / storage / clipboard / audio (runtime/crates/net, runtime/js/net.ts) */
  net?: any;
}

declare var __native: NativeHost;

/** Boot parameters from the host. */
declare var __boot: { url: string; width: number; height: number; dpr: number; lang: string; title: string };

/** Entry points the host calls (defined by runtime/index.ts). */
interface RuntimeEntry {
  boot(): void;
  timers(now: number): void;
  frame(now: number): void;
  windowEvent(name: string, ...data: number[]): void;
  unload(): void;
  /** UI engine → DOM events (see runtime/dom/events) */
  [key: string]: any;
}
declare var __rt: RuntimeEntry;