Web API of Wren (Wasm imports)
Every function a page or worker module can import, generated from api/*.widl. The same description produces the
host trampolines (src/page/api_generated.odin), the Odin guest SDK (sdk/odin), these pages and the
conformance tests (examples/apiconformance, src/tools/apiconformance).
The Odin SDK
sdk/odin is a collection of packages, one per area of the API: a module, or the modules of one package below.
A program names the collection when it is built and imports the packages it uses:
import "wren:core" // errors, host objects, promises, buffers, events: what every other package uses
import "wren:dom"odin build . -target:freestanding_wasm32 -no-entry-point -default-to-nil-allocator -collection:wren=<path to sdk/odin>wren:coreholds the modulesweb,async,events.wren:webcodecsholds the moduleswebcodecs,webcodecs_audio_decoder,webcodecs_codec,webcodecs_encoders,webcodecs_image,webcodecs_video_decoder.- any other module is the package of its name (module
domiswren:dom).
Names are the Web IDL names of this description, without an interface prefix that repeats the package's name:
Fetch_promise of module fetch is fetch.promise, console_log is console.log, URL_parse is url.parse
(the prefix compared without case); a prefix naming one interface among several of the package stays
(dom.Node_appendChild, dom.Document_getElementById), and so does one whose rest would be an Odin keyword or a
builtin the wrappers call. A function's page says its Odin name when it differs.
ABI
| IDL type | wasm parameters | host implementation receives | Odin guest |
|---|---|---|---|
i32 u32 i64 u64 f32 f64 | the same value | the same | the same |
bool | i32 (0 = false) | bool | bool |
string | address, length (bytes) | string, valid UTF-8 (else an error) | string |
usvstring | address, length | string, invalid sequences replaced by U+FFFD | string |
bytes | address, length | []u8 | []u8 |
out bytes | address, capacity | []u8 to fill | []u8 |
optional string / bytes | address, length = all ones when absent | Maybe(...) | Maybe(...) |
T[] (numbers, handles) | address, element count | []T (handles: []u64) | []T |
handle (Node, URL, ...) | i64 (0 = null) | Api_<Handle>%!(MISSING ARGUMENT)%!(MISSING CLOSE BRACE)andle, data} when the API checks it, else u64 | distinct u64 |
enum / flags | i32 or u32 | Api_<Enum> / bit_set (out of range = an error) | enum / bit_set |
struct | one wasm value per field, in order | Api_<Struct> | struct |
| callback | i32: index in the module's function table | u32 | procedure value |
Addresses and lengths are i32 for a module whose memory 0 is 32-bit and i64 for a memory64 module: the browser defines each import with the width of the importing module's memory 0, which is the memory every pointer refers to (other memories of a multi-memory module are not visible to the browser).
Results:
- a number, bool, enum or handle is the wasm result;
- a
string(orbytes) result takes two more parameters, a buffer (address, capacity) the browser fills with what fits; the wasm result (address-sized) is the full length, so a guest can retry with a bigger buffer; - a function that
raisesreturns an i32 status: 0, or a negativeError; its result, if any, is written to a return pointer (the last parameter; a string result keeps the buffer convention, a negative length being the error); the message of the last error isException_message(moduleweb); - a struct result is always written to a return pointer (C layout, natural alignment).
A pointer outside the memory is a trap (as an out-of-bounds load would be). An argument the browser cannot accept
(invalid UTF-8 in a string, a handle that is not live or of another kind, an enum out of range) is an error for a
function that raises (Type, Invalid_Handle, Invalid_Argument) and a trap for one that does not.
Availability: Window = page modules, Worker = dedicated worker modules; a worker module that imports a
function not exposed to workers is not instantiated.
Modules
| module | Odin package | functions | exposed | |
|---|---|---|---|---|
abort | wren:abort | 10 | Window | AbortController and AbortSignal (DOM Standard). A signal is an EventTarget ("abort"). Its reason is a (code, text) |
animations | wren:animations | 30 | Window | Web Animations (src/page/web_animations.odin): Element.animate, Animation, KeyframeEffect, document.timeline. The |
async | wren:core | 17 | Window, Worker | Promises, microtasks, buffers and host objects: the asynchronous core every other module builds on. |
bloburl | wren:bloburl | 2 | Window, Worker | The blob URL store (src/fetch/blob_urls.odin): what URL.createObjectURL makes, for everything that loads a URL outside the |
caches | wren:caches | 11 | Window, Worker | The Cache API (Service Workers §5: CacheStorage, Cache) on the browser's storage (src/page/cache_store.odin): named |
canvas | wren:canvas | 144 | Window, Worker | Canvas 2D (HTML §4.12.5): the 2D rendering context of <canvas> and OffscreenCanvas, drawn by a software rasterizer |
compression | wren:compression | 4 | Window, Worker | CompressionStream and DecompressionStream (Compression Standard; src/page/compression.odin, the streams themselves: |
console | wren:console | 3 | Window, Worker | The console of the page (or of the worker): messages appear in the browser's console. Invalid UTF-8 is shown as |
credentials | wren:credentials | 16 | Window | Web Authentication Level 3 (passkeys, security keys) and the Credential Management container it hangs off |
crypto | wren:crypto | 20 | Window, Worker | Web Crypto: random values, UUIDs and digests (SHA-1, SHA-256, SHA-384, SHA-512 through BCrypt; large inputs are |
document | wren:document | 5 | Window, Worker | The document's address, origin and cookies. Window_origin and Window_isSecureContext belong to |
dom | wren:dom | 398 | Window, Worker | The document tree (DOM Standard: Document, Node, Element). In a worker there is no document: the functions taking a |
encoding | wren:encoding | 8 | Window, Worker | TextEncoder and TextDecoder (Encoding Standard). Decoders keep their state between calls, so every WHATWG label |
events | wren:core | 44 | Window, Worker | Events (DOM Standard: EventTarget, Event; UI Events: MouseEvent, KeyboardEvent). Any node and any host object (a |
eventsource | wren:eventsource | 7 | Window | Server-sent events (HTML EventSource) and the MessageEvent members that are not plain payload. Messages arrive as |
fetch | wren:fetch | 11 | Window | The promise side of the Fetch API, on the same records as the callback API of module net (a Response is a Fetch: |
fontface | wren:fontface | 2 | Window | What document.fonts.add(new FontFace(...)) puts in force: a rule of its own for the document's fonts (src/page/fonts.odin), in |
forms | wren:forms | 186 | Window | Form controls (HTML forms): values, checkedness, validity, selection, select / option / form members, focus, and |
frames | wren:frames | 29 | Window | Nested browsing contexts for JavaScript (src/page/frames.odin, frames_scripts.odin, frames_access.odin; |
fs | wren:fs | 20 | Window, Worker | File handles (WHATWG File System, WICG File System Access; src/page/fs_store.odin, fs_api.odin; |
fullscreen | wren:fullscreen | 6 | Window | The Fullscreen API (src/page/fullscreen.odin): an element fills the screen. The page keeps one fullscreen element per |
history | wren:history | 14 | Window | Window, Location and History (HTML). Navigations requested by a module happen from a task of their own, never |
idb | wren:idb | 1 | Window, Worker | IndexedDB (the JavaScript engine's prelude, native/js/lazy/idb.js, is the only caller). One entry point: the |
images | wren:images | 6 | Window, Worker | Images: HTMLImageElement (<img> and <input type=image>) and decoding image files from buffers. naturalWidth and |
js | wren:js | 13 | Window, Worker | What the JavaScript engine (native/js, a C guest) needs from the browser beyond the web API itself: the console with |
dnd | wren:dnd | 32 | Window | HTML drag and drop for JavaScript (src/page/dnd.odin; native/js/runtime/web_dnd.c): the drag data store behind a |
entries | wren:entries | 12 | Window | The File and Directory Entries API for what the user dropped (src/page/dnd_entries.odin; |
scripts | wren:scripts | 25 | Window, Worker | Script loading, the event loop and JavaScript workers as the engine needs them (HTML "scripting"): document.write / |
storageaccess | wren:storageaccess | 2 | Window | The Storage Access API (src/page/storage_access.odin): a frame of another site than its tab's, whose storage and |
navigator | wren:navigator | 10 | Window, Worker | What the web APIs of JavaScript need beyond the modules Wasm pages use (native/js/runtime/web*.c): the browser's |
media | wren:media | 4 | Window | |
locks | wren:locks | 6 | Window, Worker | The Web Locks API (src/page/bindings_locks.odin): navigator.locks.request / query, held per origin and shared |
htmlmedia | wren:htmlmedia | 37 | Window | HTMLMediaElement (<video>, <audio>): the media element state machine of HTML §4.8.11 (src/page/media_element.odin) on the |
mse | wren:mse | 35 | Window, Worker | Media Source Extensions: MediaSource, SourceBuffer and SourceBufferList (src/page/media_source.odin). A SourceBuffer holds |
mediatracks | wren:mediatracks | 49 | Window | The track lists of a media element (src/page/media_tracks.odin, media_texttracks.odin): AudioTrackList, VideoTrackList |
navigation | wren:navigation | 12 | Window | The Navigation API (HTML): window.navigation over the tab's session history. The browser keeps the entries, their |
net | wren:net | 12 | Window, Worker | Fetch with a callback (Fetch Standard through the browser's loader: origin, CORS, cookies, referrer, CSP and mixed |
performance | wren:performance | 20 | Window, Worker | High Resolution Time and User Timing: performance.now() and timeOrigin on the page's own timeline, mark / measure, |
popover | wren:popover | 5 | Window | The Popover API (src/page/popover.odin): the popover attribute, showPopover / hidePopover / togglePopover, the |
ports | wren:ports | 10 | Window, Worker | MessagePorts between realms (src/page/ports.odin; native/js/runtime/web_message.c, workers.c): a port whose other |
scheduler | wren:scheduler | 4 | Window, Worker | Prioritized Task Scheduling (src/page/bindings_scheduler.odin): scheduler.postTask / scheduler.yield, TaskController |
serviceworker | wren:serviceworker | 21 | Window, Worker | Service workers (src/page/service_worker.odin; native/js/runtime/web_sw.c): registrations kept by the browser, each |
storage | wren:storage | 8 | Window | localStorage and sessionStorage (Web Storage). Security for an opaque origin or disabled storage, Quota for a write |
testhost | wren:testhost | 8 | Window | What a page of a test program tells that program: its tests, their checks, what it measures, what it wants done to it. |
timers | wren:timers | 11 | Window, Worker | Timers and the rendering-related scheduling of HTML: setTimeout / setInterval (with the HTML nesting clamp and |
url | wren:url | 30 | Window, Worker | URL and URLSearchParams (WHATWG URL Standard) as host objects. A URL's searchParams is linked to it both ways: |
web | wren:core | 2 | Window, Worker | What every other module shares: the error codes (Web IDL exceptions and DOMException names), the exception record |
webassembly | wren:webassembly | 52 | Window, Worker | The WebAssembly JavaScript API (src/page/webassembly.odin, the objects: native/js/odin/web_wasm.odin): a page's own |
webaudio | wren:webaudio | 54 | Window | The Web Audio API's render graph (src/webaudio, src/page/bindings_webaudio.odin), for native/js/odin/web_audio.js: nodes |
webcodecs | wren:webcodecs | 20 | Window, Worker | WebCodecs (src/page/bindings_webcodecs.odin, webcodecs_frame.odin): the pixel model, memory accounting and format |
webcodecs_audio_decoder | wren:webcodecs | 9 | Window, Worker | WebCodecs AudioDecoder (src/page/bindings_audio_decoder.odin, native/js/runtime/web_webcodecs_audio_decoder.c). |
webcodecs_codec | wren:webcodecs | 23 | Window, Worker | WebCodecs encoded chunks and AudioData (src/page/webcodecs_chunks.odin, native/js/runtime/web_webcodecs_codec.c). |
webcodecs_encoders | wren:webcodecs | 18 | Window, Worker | WebCodecs VideoEncoder / AudioEncoder (src/page/bindings_encoders.odin, native/js/runtime/web_webcodecs_encoders.c). |
webcodecs_image | wren:webcodecs | 16 | Window, Worker | WebCodecs ImageDecoder (src/page/bindings_image_decoder.odin, native/js/runtime/web_webcodecs_image.c). The decode |
webcodecs_video_decoder | wren:webcodecs | 9 | Window, Worker | WebCodecs VideoDecoder (src/page/bindings_video_decoder.odin, native/js/runtime/web_webcodecs_video_decoder.c). |
webgl | wren:webgl | 15 | Window, Worker | WebGL 1 and 2 for native/js/odin/web_webgl.js: a context is a host object over a device of its own (native/webgl: a thread in the GPU |
websocket | wren:websocket | 12 | Window | WebSocket and CloseEvent. Messages arrive as "message" events: text in Event_data, or a buffer in Event_object. |
workers | wren:workers | 11 | Window, Worker | Dedicated workers (HTML). A message event carries the text in Event_data, the number of transferred buffers in |
1601 imports in 1502 functions.