This backend generates private Wasm call glue, JavaScript wrappers, and TypeScript declarations from the validated binding model. It does not import Clang or change the public native API. Native code initializes descriptors and dispatches interface calls; JavaScript does not depend on struct offsets or vtable layout.
The standalone test project uses the shared native fixtures, not Radient. The fixture headers use real Diligent declarations and macros and are checked as both C and C++. Its tests/API.json selects supported APIs from that common reference. Wasm memory hooks and virtual-file fixtures stay in tests/fixtures/. Tests run through generated JavaScript into the compiled Wasm module in Node. The descriptor, array, ownership, output-parameter, constant, and default-argument suites also run in a headless browser. No renderer or GPU is required.
Install Emscripten and activate its environment. Use Node 18 or newer and the Clang 22 Python environment described in the binding tools README. The extraction library is Clang 22; Emscripten supplies the Wasm compiler and its standard headers. Tested with Emscripten 6.0.11 and Node 24.
From the FX source directory:
On Windows, use the environment's Scripts/python.exe and LLVM's bin/libclang.dll. Alternatively, pass Emscripten's cmake/Modules/Platform/Emscripten.cmake as CMAKE_TOOLCHAIN_FILE directly. BINDINGS_NODE_EXECUTABLE can select a particular Node executable. Browser tests discover Chrome, Chromium, or Edge at test execution time. Use ctest -LE Browser to run without a browser.
Bindings-GenerateJavaScriptTests generates the model, glue, wrappers, and declarations. BindingTestsWasm compiles the native test implementation and glue. CTest runs Python emitter tests, browser-harness tests, and separate Node suites for each supported marshalling shape. Shared conformance suites run unchanged in Node and the browser, including the output and ownership cases below. Build BindingTestsWasm before running CTest; test execution never generates or rebuilds the artifacts. Use ctest -N to list tests or ctest -R <pattern> to select suites. Nothing is built by default. Native model tests remain independent of this optional Emscripten project.
Fixture sources and test suites are discovered automatically:
CTest names contain the runner and filename, and retain the Bindings, JavaScript, and Browser labels. Adding or removing matching files refreshes the build and test lists on the next build through CMake's CONFIGURE_DEPENDS; there is no per-suite CMake registration to maintain.
Generated artifacts reside in the build directory's generated/ folder and are not checked in. Only the generator, shared runtime, fixture declarations, metadata, native test implementations, and tests belong in source control.
tests/run_browser_tests.py runs a shared suite in headless Chrome, Chromium, or Edge without an automation package. It serves a generated module and the suite from a temporary local HTTP server with cross-origin isolation headers, and exits with an error if loading fails, a test fails, or the browser does not report before the timeout. --shared-memory additionally requires the module to use shared Wasm memory.
To select a browser when running CTest, set the BINDINGS_TEST_BROWSER_EXECUTABLE environment variable; no reconfiguration or rebuild is needed:
In PowerShell, set $env:BINDINGS_TEST_BROWSER_EXECUTABLE before running CTest. When invoking the runner directly, --browser overrides the environment variable. If neither specifies a browser, the runner discovers one automatically.
A suite exports registerTests({test, assert, api, constants, wasm}). Node supplies its standard test runner and assertions; the browser adapter supplies the small assertion subset used by these tests. Browser cleanup registered with t.after() runs even after failure. The production example is Radient's blob suite.
For example, with the generated test module:
The backend supports scalar and string marshalling, nested and inherited descriptors, individual struct pointers, fixed and dynamic arrays, caller-filled outputs, interface ownership, and native default arguments. Standalone constants and TypeScript typedef aliases are exported. Unsupported declarations and policies are rejected rather than silently omitted. Callbacks, by-value struct results, and additional custom lifetime adapters remain outside this backend's support. The first production integration is Radient's owning data blobs.
Add target-independent native type information to the shared model. Put JavaScript conversion and lifetime behavior in this backend. Add a native fixture and an execution test for each new supported API shape; emitter-only string checks cannot establish correct marshalling.
An output buffer annotated with length_from needs no JavaScript count argument. The generated C++ helper queries the selected input interface directly, including descriptor fields such as pSkin->GetDesc().JointCount. Once all inputs have been marshalled and retained, the wrapper queries the count, allocates exactly that many native elements, and invokes the operation. Output structs use native construction and layout. The result is copied into a JavaScript array, and temporary storage is released on success, native failure, or snapshot-conversion failure.
Zero counts produce empty arrays. Oversized counts are rejected before allocating the output. Native statuses are preserved; as with explicitly sized outputs, the API contract determines whether the returned data is valid for a given status. The source count must remain stable from the query through the native operation.
A const T& result (declared with const T REF in a Diligent C-compatible header) becomes a plain JavaScript object. Supported fields are scalars, enums, UTF-8 strings, embedded value structs, and borrowed pointer/count arrays. Nested data is copied before the wrapper returns or releases temporary inputs. Later native mutation or owner destruction does not affect the result, and editing the result does not modify native data. The snapshot does not retain the containing native owner. Any interface fields are separately retained wrappers and must be disposed explicitly.
The result policy requires direction: out, nullable: false, ownership: borrowed, and lifetime: call, object, or static. Native storage must remain valid and stable until synchronous snapshot conversion finishes. String fields use direction: out (or inout for a descriptor used in both directions), borrowed ownership, their documented lifetime, and explicit nullability. Inputs still require call-scoped string storage. These are data-field policies; an output parameter that points directly to const data remains invalid.
Generated TypeScript uses StructSnapshot<T> to make all returned fields, including nested fields, required while preserving optional input descriptor fields. For example, a getter is declared as GetDesc(): StructSnapshot<TextureDesc>. Native pointers and struct offsets stay private. Reading a borrowed result needs no temporary native descriptor allocation.
Pointers other than supported strings and annotated pointer/count arrays are rejected during generation. By-value struct returns and mutable-reference results are not supported yet.
Null-terminated const Char* and const char* values map to JavaScript string. Pointers with a length policy remain byte spans instead. Mutable character buffers, wide strings, and owned native string outputs require separate adapters.
For inputs and direct results, nullable: true permits null, which is distinct from "". Omitted or undefined descriptor fields retain native defaults. Non-string inputs and embedded null characters are rejected before the native call. Unpaired JavaScript UTF-16 surrogates become U+FFFD, following TextEncoder. Malformed native UTF-8 is rejected; a leading U+FEFF is preserved as content.
String conversion uses the current Wasm heap after allocation and native calls, including memory growth. Native addresses and string views are not exposed.
An input const T* with a length policy naming an unsigned 32-bit or 64-bit integer sibling becomes an array input. This works for parameters and descriptor fields. Counts remain explicit and measure elements; omitted descriptor fields retain native defaults. Metadata requires direction: in, ownership: borrowed, and lifetime: call. Native code that retains the data must copy it during the call.
Only the requested prefix is copied; a typed array's byte offset is respected. Counts larger than the supplied array or supported native storage are rejected before calling the API. Missing struct elements and invalid fields are errors. An empty array with a zero count becomes a null pointer. Explicit null is allowed only with nullable: true; the native API determines its meaning for the given count. Pointer/count descriptor fields are processed after counts are initialized, so JavaScript property order does not change the result.
All temporary arrays and nested data are released after the call, including when a later element, argument, or property getter fails. Input conversion does not modify the JavaScript arrays. Native pointers are never exposed.
Arrays of interface pointers are not supported yet.
Embedded C arrays such as Float32 Data[16] use the extent from their native declaration; no pointer/count policy is required. Numeric, enum, boolean, and character elements use the same typed-array representations as pointer/count arrays. Struct elements use arrays of descriptor objects. Multidimensional arrays use nested JavaScript arrays, with typed arrays for numeric rows. For example, Float32 Rows[2][4] takes two Float32Array(4) rows. Array typedefs preserve these dimensions.
A supplied field must have exactly the declared number of elements at every dimension. Short, long, sparse, null, and incorrectly typed arrays are rejected. Omitting the field or passing undefined leaves its native initializer unchanged, including matrix identity defaults. Struct elements may omit individual members; their defaults come from the containing descriptor's initializer. Empty objects preserve those defaults, while missing array elements are errors.
Input values are written directly into the containing native descriptor. Fixed arrays and their struct elements require no separate native allocation. Nested strings and dynamic spans share the call's existing temporary-storage cleanup. Results are independent typed arrays or recursively copied struct arrays, with the same lifetime rules as other snapshots. Const arrays and const element fields are supported in output-only descriptors.
TypeScript preserves the element types and dimensions; exact lengths are checked at runtime. Fixed arrays of pointers, callbacks, and other unsupported element types still require adapters. This support applies to embedded fields: C array function parameters decay to pointers and use pointer/count metadata instead.
Borrowed const T* arrays become independent JavaScript snapshots. Supported forms are pointer/count fields in returned descriptors and direct pointer results whose length policy names an explicit integer input parameter. Counts measure elements and use unsigned 32-bit or 64-bit types. Metadata requires direction: out, ownership: borrowed, and lifetime: call, object, or static. Fields used in both input and output descriptors use direction: inout and lifetime: call.
The wrapper checks counts, alignment, and the complete Wasm memory range before reading elements or allocating the snapshot. These checks cannot verify a native allocation's extent: the native API must return accurate counts and keep all referenced data valid and stable until synchronous copying finishes.
Snapshots do not retain native owners or expose native memory. They remain valid after native mutation, memory growth, or owner destruction, require no disposal, and can be edited without changing native data. Conversion finishes before temporary inputs are released; failed conversion also releases those inputs. Reading borrowed arrays does not allocate temporary native arrays.
Caller-filled output buffers with capacity/written-count parameters, owned array results, and counts obtained through separate getter methods require additional adapters and are not supported by this step.
A const void* or pointer to const eight-bit elements can be bound as a byte span when its metadata specifies direction: in, ownership: borrowed, lifetime: call, and length naming an unsigned 32-bit or 64-bit integer sibling. This applies to function parameters and descriptor fields. JavaScript passes a Uint8Array; the existing integer conventions still apply to the explicit length.
The wrapper copies the requested prefix of the array into temporary Wasm storage, respecting the array's byte offset. The native call may copy these bytes but must not retain their pointer. Temporary storage is freed after the call, including when conversion of a later argument fails. Lengths exceeding the supplied array or Wasm32 address space are rejected before the native call.
null is accepted only for a nullable pointer and is forwarded without an allocation; the native API determines its meaning. An empty array becomes a null pointer with zero length. Omitted descriptor fields retain their native defaults. All supplied scalar fields are initialized before buffer lengths are read, so JavaScript property order does not affect marshalling.
Pass --adapters path/to/Adapters.json to the JavaScript generator to bind an interface's acquire/release pair. The adapter contract is shared with other language backends; the language-neutral model marks those methods as manual. For example:
The acquire method returns a signed 32-bit status and one borrowed const void* output (void* for writable access). Read/write access is inferred from that pointee's constness, including typedefs. The adapter's exact success status means that access has been acquired; any other status returns {status, value: null}. The no-argument size query returns Uint32 or Uint64 and runs after acquisition. The no-argument release method returns a status and consumes the scope, including when that status indicates an error. The size query may be inherited. Both acquire and release methods must be marked manual in the API model.
A successful call returns {status, value: scope}, including for an empty span. The scope owns an additional strong reference to the native object and is usable even if the original interface wrapper is disposed. The release method is omitted from the public wrapper; call scope.dispose() exactly as shown below. Disposal is idempotent and returns the native release status. Garbage collection does not release scopes.
Read scopes provide byteLength, readByte(index), copyTo(destination, sourceOffset = 0), and toUint8Array(). Write scopes also provide writeByte(index, value) and copyFrom(source, destinationOffset = 0). Copy methods use the complete supplied array view and check the requested range. toUint8Array() returns an independent copy; no method exposes a writable view into read-only storage. All byte operations fail after disposal. Operations obtain the current Wasm heap each time, so scopes remain usable after memory growth without requiring callers to refresh views.
The native API must keep the span valid and its size unchanged until release. The adapter does not add locking or emulate reader/writer exclusion: conflicts and resize restrictions remain the responsibility of the native implementation.
The same adapter configuration can restrict an API without changing its native headers or language-neutral model:
These choices must agree: a temporary byte input cannot be combined with a native mode that retains its pointer. Radient's blob configuration fixes COPY storage, excludes callbacks and their context, and adapts only the owning creation path.
Writable scalar and struct pointer/reference outputs use native temporary storage. Scalar outputs start at zero; structs use their native defaults. The caller omits the output argument. Status-returning calls return {status, value}; void calls return the output value directly. Struct results are snapshots copied before the temporary output is destroyed, including on conversion failure. Output storage requires direction: out, non-nullability, and borrowed call-scoped ownership.
An output pointer with length names the input capacity; optional written names an unsigned scalar output containing the number actually written. The wrapper allocates the buffer and returns an owned JS copy of the written prefix. Zero capacity is supported. An invalid written count throws before reading data. Multiple outputs are returned in an object keyed by their native parameter names, inside value for status-returning functions. Output arguments are omitted from the JS call. Struct arrays use native default construction and destruction.
Returned interface pointers explicitly declare ownership and lifetime. Borrowed results acquire a reference for their JS wrapper; owned results transfer the existing reference. Call dispose() on either wrapper. Pointer outputs follow the same rule. Multiple-output conversion failures release both already wrapped and not yet wrapped owned results. Nullable results preserve null.
Interface input fields and array elements accept live wrappers from this module. They are retained until the call and snapshot conversion finish. Borrowed interface outputs acquire independent wrapper references, including when nested inside descriptors or arrays. Dispose those wrappers when finished. The pointer policy describes borrowed element ownership; element_nullable allows null interface elements independently of the array's own nullable flag.
Borrowed const T* inputs accept a descriptor object, or null when the policy allows it. Fields use the same representation; omitted fields preserve native defaults. Native temporary storage survives through result conversion. Borrowed struct pointers returned directly or through fields become copied snapshots, including null for optional results. Use length only for arrays, not individual struct pointers.
Descriptors declared with DILIGENT_DERIVE expose inherited and own fields in one JS object. The native derived constructor supplies all defaults; generated native accessors perform base adjustment and field addressing. Pointer/count relationships may refer to inherited count fields. The model retains inheritance, while emitted TypeScript descriptor interfaces list the complete field set.
An array result may use length_method: GetWeightCount instead of length. The named method must belong to the same interface or a base, take no arguments, and return an unsigned 32-bit or 64-bit count. The wrapper queries the native owner and copies the array before returning; no count argument is exposed to JS. The pointer and count must describe the same stable data for the synchronous call.
Selected constants are named ES module exports and read-only members of the created API. Integer constants wider than 32 bits use BigInt, preserving invalid IDs exactly. Strings use JS strings. Aggregate constants are recursively frozen objects and ordinary arrays, so importing a constant never needs native storage. Selected typedefs are exported TypeScript aliases; they have no runtime wrapper or allocation. Alias use in native signatures continues to resolve to the same marshalling representation.
Defaulted declarations accept fully specified calls. Omitted trailing arguments (or trailing undefined) use generated native call forms, so C++ evaluates the defaults at each invocation. TypeScript marks these arguments optional. Interior undefined is not a placeholder for a default when later arguments are supplied.
Counts needed for JS array validation remain explicit. Pointer/reference inputs also remain explicit when their default temporary could escape through borrowed results or output parameters; this preserves snapshot lifetime guarantees.
Descriptor span counts may also name nested value fields (for example, VertexLayout.BufferCount) or a fixed integer extent. Native count getters run after the descriptor has been populated, preserving nested defaults and independence from JavaScript property order. Paths cannot traverse pointers.
A field_count_functions JavaScript adapter can name a native function that accepts a descriptor by const reference and returns its span element count. Use this for type-tagged byte payloads whose size is not a descriptor field. The header supplied to the generator declares the function; a matching field_policies entry supplies the borrowed span's direction and lifetime. The adapter name is a qualified C++ function identifier, not an expression.
ModuleOptions.files optionally supplies a map of absolute POSIX paths to Uint8Array contents. The wrapper snapshots the bytes before asynchronous module initialization and populates Emscripten's virtual filesystem before native code runs. Targets using this option must export the FS runtime method. Native code continues to use its ordinary filesystem APIs; no native callback adapter is required. Existing Emscripten preRun hooks run after the files are installed.