Wren

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:

Odin
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:core holds the modules web, async, events.
  • wren:webcodecs holds the modules webcodecs, webcodecs_audio_decoder, webcodecs_codec, webcodecs_encoders, webcodecs_image, webcodecs_video_decoder.
  • any other module is the package of its name (module dom is wren: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 typewasm parametershost implementation receivesOdin guest
i32 u32 i64 u64 f32 f64the same valuethe samethe same
booli32 (0 = false)boolbool
stringaddress, length (bytes)string, valid UTF-8 (else an error)string
usvstringaddress, lengthstring, invalid sequences replaced by U+FFFDstring
bytesaddress, length[]u8[]u8
out bytesaddress, capacity[]u8 to fill[]u8
optional string / bytesaddress, length = all ones when absentMaybe(...)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 u64distinct u64
enum / flagsi32 or u32Api_<Enum> / bit_set (out of range = an error)enum / bit_set
structone wasm value per field, in orderApi_<Struct>struct
callbacki32: index in the module's function tableu32procedure 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 (or bytes) 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 raises returns an i32 status: 0, or a negative Error; 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 is Exception_message (module web);
  • 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

moduleOdin packagefunctionsexposed
abortwren:abort10WindowAbortController and AbortSignal (DOM Standard). A signal is an EventTarget ("abort"). Its reason is a (code, text)
animationswren:animations30WindowWeb Animations (src/page/web_animations.odin): Element.animate, Animation, KeyframeEffect, document.timeline. The
asyncwren:core17Window, WorkerPromises, microtasks, buffers and host objects: the asynchronous core every other module builds on.
bloburlwren:bloburl2Window, WorkerThe blob URL store (src/fetch/blob_urls.odin): what URL.createObjectURL makes, for everything that loads a URL outside the
cacheswren:caches11Window, WorkerThe Cache API (Service Workers §5: CacheStorage, Cache) on the browser's storage (src/page/cache_store.odin): named
canvaswren:canvas144Window, WorkerCanvas 2D (HTML §4.12.5): the 2D rendering context of <canvas> and OffscreenCanvas, drawn by a software rasterizer
compressionwren:compression4Window, WorkerCompressionStream and DecompressionStream (Compression Standard; src/page/compression.odin, the streams themselves:
consolewren:console3Window, WorkerThe console of the page (or of the worker): messages appear in the browser's console. Invalid UTF-8 is shown as
credentialswren:credentials16WindowWeb Authentication Level 3 (passkeys, security keys) and the Credential Management container it hangs off
cryptowren:crypto20Window, WorkerWeb Crypto: random values, UUIDs and digests (SHA-1, SHA-256, SHA-384, SHA-512 through BCrypt; large inputs are
documentwren:document5Window, WorkerThe document's address, origin and cookies. Window_origin and Window_isSecureContext belong to
domwren:dom398Window, WorkerThe document tree (DOM Standard: Document, Node, Element). In a worker there is no document: the functions taking a
encodingwren:encoding8Window, WorkerTextEncoder and TextDecoder (Encoding Standard). Decoders keep their state between calls, so every WHATWG label
eventswren:core44Window, WorkerEvents (DOM Standard: EventTarget, Event; UI Events: MouseEvent, KeyboardEvent). Any node and any host object (a
eventsourcewren:eventsource7WindowServer-sent events (HTML EventSource) and the MessageEvent members that are not plain payload. Messages arrive as
fetchwren:fetch11WindowThe promise side of the Fetch API, on the same records as the callback API of module net (a Response is a Fetch:
fontfacewren:fontface2WindowWhat document.fonts.add(new FontFace(...)) puts in force: a rule of its own for the document's fonts (src/page/fonts.odin), in
formswren:forms186WindowForm controls (HTML forms): values, checkedness, validity, selection, select / option / form members, focus, and
frameswren:frames29WindowNested browsing contexts for JavaScript (src/page/frames.odin, frames_scripts.odin, frames_access.odin;
fswren:fs20Window, WorkerFile handles (WHATWG File System, WICG File System Access; src/page/fs_store.odin, fs_api.odin;
fullscreenwren:fullscreen6WindowThe Fullscreen API (src/page/fullscreen.odin): an element fills the screen. The page keeps one fullscreen element per
historywren:history14WindowWindow, Location and History (HTML). Navigations requested by a module happen from a task of their own, never
idbwren:idb1Window, WorkerIndexedDB (the JavaScript engine's prelude, native/js/lazy/idb.js, is the only caller). One entry point: the
imageswren:images6Window, WorkerImages: HTMLImageElement (<img> and <input type=image>) and decoding image files from buffers. naturalWidth and
jswren:js13Window, WorkerWhat the JavaScript engine (native/js, a C guest) needs from the browser beyond the web API itself: the console with
dndwren:dnd32WindowHTML drag and drop for JavaScript (src/page/dnd.odin; native/js/runtime/web_dnd.c): the drag data store behind a
entrieswren:entries12WindowThe File and Directory Entries API for what the user dropped (src/page/dnd_entries.odin;
scriptswren:scripts25Window, WorkerScript loading, the event loop and JavaScript workers as the engine needs them (HTML "scripting"): document.write /
storageaccesswren:storageaccess2WindowThe Storage Access API (src/page/storage_access.odin): a frame of another site than its tab's, whose storage and
navigatorwren:navigator10Window, WorkerWhat the web APIs of JavaScript need beyond the modules Wasm pages use (native/js/runtime/web*.c): the browser's
mediawren:media4Window
lockswren:locks6Window, WorkerThe Web Locks API (src/page/bindings_locks.odin): navigator.locks.request / query, held per origin and shared
htmlmediawren:htmlmedia37WindowHTMLMediaElement (<video>, <audio>): the media element state machine of HTML §4.8.11 (src/page/media_element.odin) on the
msewren:mse35Window, WorkerMedia Source Extensions: MediaSource, SourceBuffer and SourceBufferList (src/page/media_source.odin). A SourceBuffer holds
mediatrackswren:mediatracks49WindowThe track lists of a media element (src/page/media_tracks.odin, media_texttracks.odin): AudioTrackList, VideoTrackList
navigationwren:navigation12WindowThe Navigation API (HTML): window.navigation over the tab's session history. The browser keeps the entries, their
netwren:net12Window, WorkerFetch with a callback (Fetch Standard through the browser's loader: origin, CORS, cookies, referrer, CSP and mixed
performancewren:performance20Window, WorkerHigh Resolution Time and User Timing: performance.now() and timeOrigin on the page's own timeline, mark / measure,
popoverwren:popover5WindowThe Popover API (src/page/popover.odin): the popover attribute, showPopover / hidePopover / togglePopover, the
portswren:ports10Window, WorkerMessagePorts between realms (src/page/ports.odin; native/js/runtime/web_message.c, workers.c): a port whose other
schedulerwren:scheduler4Window, WorkerPrioritized Task Scheduling (src/page/bindings_scheduler.odin): scheduler.postTask / scheduler.yield, TaskController
serviceworkerwren:serviceworker21Window, WorkerService workers (src/page/service_worker.odin; native/js/runtime/web_sw.c): registrations kept by the browser, each
storagewren:storage8WindowlocalStorage and sessionStorage (Web Storage). Security for an opaque origin or disabled storage, Quota for a write
testhostwren:testhost8WindowWhat a page of a test program tells that program: its tests, their checks, what it measures, what it wants done to it.
timerswren:timers11Window, WorkerTimers and the rendering-related scheduling of HTML: setTimeout / setInterval (with the HTML nesting clamp and
urlwren:url30Window, WorkerURL and URLSearchParams (WHATWG URL Standard) as host objects. A URL's searchParams is linked to it both ways:
webwren:core2Window, WorkerWhat every other module shares: the error codes (Web IDL exceptions and DOMException names), the exception record
webassemblywren:webassembly52Window, WorkerThe WebAssembly JavaScript API (src/page/webassembly.odin, the objects: native/js/odin/web_wasm.odin): a page's own
webaudiowren:webaudio54WindowThe Web Audio API's render graph (src/webaudio, src/page/bindings_webaudio.odin), for native/js/odin/web_audio.js: nodes
webcodecswren:webcodecs20Window, WorkerWebCodecs (src/page/bindings_webcodecs.odin, webcodecs_frame.odin): the pixel model, memory accounting and format
webcodecs_audio_decoderwren:webcodecs9Window, WorkerWebCodecs AudioDecoder (src/page/bindings_audio_decoder.odin, native/js/runtime/web_webcodecs_audio_decoder.c).
webcodecs_codecwren:webcodecs23Window, WorkerWebCodecs encoded chunks and AudioData (src/page/webcodecs_chunks.odin, native/js/runtime/web_webcodecs_codec.c).
webcodecs_encoderswren:webcodecs18Window, WorkerWebCodecs VideoEncoder / AudioEncoder (src/page/bindings_encoders.odin, native/js/runtime/web_webcodecs_encoders.c).
webcodecs_imagewren:webcodecs16Window, WorkerWebCodecs ImageDecoder (src/page/bindings_image_decoder.odin, native/js/runtime/web_webcodecs_image.c). The decode
webcodecs_video_decoderwren:webcodecs9Window, WorkerWebCodecs VideoDecoder (src/page/bindings_video_decoder.odin, native/js/runtime/web_webcodecs_video_decoder.c).
webglwren:webgl15Window, WorkerWebGL 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
websocketwren:websocket12WindowWebSocket and CloseEvent. Messages arrive as "message" events: text in Event_data, or a buffer in Event_object.
workerswren:workers11Window, WorkerDedicated workers (HTML). A message event carries the text in Event_data, the number of transferred buffers in

1601 imports in 1502 functions.

Edit this page on GitHub