Skip to main content

Module bindings

Module bindings 

Source
Expand description

Javascript bindings: one call interface shared by the N-API addon (extensions/css-parser-napi) and the WASM module (extensions/css-parser-wasm). The backends are separate crates of the workspace: this crate only provides the exchange buffer, the arena handles and the dispatch of the calls.

Javascript encodes a call ([method, ...arguments], see value) into the exchange buffer and calls call(); the result is encoded back into the exchange buffer. The arena columns are not exchanged: javascript reads and writes them through typed array views of the rust memory (see column() in the backends), refreshed when the arena generation changes. The callbacks (visitors, load, generateScopedName, walk filters, …) are called through the backend host_call(): the arguments and the result go through the exchange buffer too. The buffer is decoded before the work starts and encoded when it ends, so the reentrant calls (a visitor that calls the arena) share it safely.

Modules§

api
The functions called by javascript, by name. Port of the entry points of src/node.ts / src/web.ts (parseSyncInput, transformSync, render, parseResult of src/utils/sync.ts) and of the arena methods.
value
Values exchanged with javascript and their binary encoding (shared by the N-API and the WASM bindings).

Structs§

JsHost
the javascript host of the parser: the callbacks of the options (visitors, loaders, …)

Constants§

EXCHANGE
the exchange buffer

Functions§

arena_handle
the handle of an arena: an arena already known keeps its handle
arena_of
the arena of a handle
call
run the call encoded in the exchange buffer ([method, ...arguments]) and encode its result in place. a panic is returned as an error
call_js
call a javascript callback: [callback id, arena, ...arguments] -> result
free_arena
release a handle: the arena is dropped when no other reference remains
give_output
give a buffer to javascript without a copy: its whole capacity becomes the ArrayBuffer, the content is a view of its length (like the output buffer of the typescript writer)
set_host_call
install the backend callback function
take_external_memory
the change of the memory held by the arenas of javascript since the last call (bytes, negative when arenas were released). the N-API backend reports it to V8 (napi_adjust_external_memory): V8 collects the arenas that are no longer used sooner, its heap does not show their memory
take_output
take a buffer given to javascript
write_exchange
write bytes at the start of the exchange buffer. the buffer never shrinks: javascript keeps a view of its whole length and writes the next request anywhere in it