Browser Glue Architecture
The browser-glue package provides TypeScript implementations of the tairitsu-browser:full WIT interfaces, enabling WebAssembly components to interact with browser APIs through the Component Model.
Architecture Overview
mermaid
Key Components
TypeScript Glue (src/*.ts)
Auto-generated TypeScript implementations of WIT interfaces:
| Domain | File | Interfaces | Functions |
|---|---|---|---|
| DOM | domGlue.ts | 34 | ~300 |
| HTML | htmlGlue.ts | 182 | ~1500 |
| CSS | cssGlue.ts | 44 | ~400 |
| Canvas | canvasGlue.ts | 20 | ~200 |
| Fetch | fetchGlue.ts | 25 | ~150 |
| Events | eventsGlue.ts | 15 | ~100 |
| ... | ... | ... | ... |
Type Declarations (dist/*.d.ts)
TypeScript declaration files for IDE support and type checking.
Interface Wrappers (dist/browser-glue/*.js)
Minimal adapter files for jco transpiled imports:
- console.js - Logging interface
- document.js - Document creation
- element.js - Element attributes
- node.js - DOM tree operations
- style.js - CSS style properties
- event-target.js - Event listeners
- non-element-parent-node.js - getElementById
- window.js - Window dimensions
jco Integration
Import Map Configuration
html
1
2
3
4
5
6
7
8
Transpile Process
- 1Build WASM component: cargo build --target wasm32-wasip2 --lib --release
- 2Transpile with jco: jco transpile component.wasm -o output/
- 3jco generates wrapper with imports from tairitsu-browser:full/*
- 4Import map resolves to ./browser-glue/* adapters
Handle System
Browser objects are represented as opaque u64 handles:
typescript
1
2
3
4
5
6
// TypeScript side
const element = document.createElement('div');
const handle = registerHandle(element); // Returns bigint
// Rust side receives u64
let handle: u64 = bindings::document::create_element("div", None);
Handle Table (handles.ts)
typescript
1
2
3
4
5
6
7
8
9
10
11
12
const _handles = new Map<bigint, object>();
let _nextHandle = 1n;
export function registerHandle(obj: object): bigint {
const handle = BigInt(_nextHandle++);
_handles.set(handle, obj);
return handle;
}
export function lookupHandle<T>(handle: bigint): T | null {
return _handles.get(handle) as T ?? null;
}
Timer System
The platformHelpers.ts module provides timer functions that bridge WIT callbacks to browser APIs:
| Function | JS API | Callback type | WIT export |
|---|---|---|---|
| setTimeout | window.setTimeout | One-shot (FnOnce) | timer-callbacks.on-timeout |
| setInterval | window.setInterval | Recurring (FnMut) | timer-callbacks.on-interval |
| requestAnimationFrame | window.requestAnimationFrame | Per-frame | animation-callbacks.on-frame |
Interval callbacks fire repeatedly until cancelled via clearInterval. All timer state is stored on globalThis.__tairitsuTimerState.
Build Process
bash
1
2
3
4
5
6
7
8
# Regenerate glue from WIT
# Build with declarations
&&
# Production build with minification
Package Layout
mermaid