Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
4ae7f58
Fix WASM binding generator bugs and replace embind vectors with typed…
vijaiaeroastro Dec 5, 2026
4880148
Fix invalid C generated by the CDynamic example for prerelease and bu…
vijaiaeroastro Dec 5, 2026
68a7caf
Add KitchenSink example that uses every IDL construct
vijaiaeroastro Dec 5, 2026
a4ae4d5
Document KitchenSink and add WebAssembly to the binding feature matrix
vijaiaeroastro Dec 5, 2026
ccebfba
Point the README build badges to GitHub Actions
vijaiaeroastro Dec 5, 2026
01bf87a
Add callback support to the WASM binding
vijaiaeroastro Dec 5, 2026
266d3af
Fail clearly when a WASM binding is requested without a Cpp binding
vijaiaeroastro Dec 5, 2026
42463ff
Run the KitchenSink example in the examples CI script
vijaiaeroastro Dec 5, 2026
4d3bcd0
Generate a build script for the WASM example
vijaiaeroastro Dec 5, 2026
927b3f9
Emit TypeScript declarations and a more useful WASM example
vijaiaeroastro Dec 5, 2026
384ec57
Document the WebAssembly binding conventions in the README
vijaiaeroastro Dec 5, 2026
a649aa4
Do not generate ClassTypeId in the base class stub
vijaiaeroastro Dec 5, 2026
caa23f7
Fix example stub quirks found while testing the WASM binding
vijaiaeroastro Dec 5, 2026
3138197
WASM: callbacks with class and array out-parameters, memory helpers, …
vijaiaeroastro Dec 5, 2026
dc6b59d
Move the WebAssembly binding notes from the README to Documentation/W…
vijaiaeroastro Dec 5, 2026
cd31a69
WASM: fixes from the second review, ClearCallbacks
vijaiaeroastro Dec 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ debug

# Jetbrains
.idea/

# WASM example build output
Examples/**/Examples/WASM/*Binding.js
Examples/**/Examples/WASM/*Binding.wasm
Examples/**/Examples/WASM/*Binding.d.ts
Examples/**/Examples/WASM/build_*
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ You can also contribute by reporting bugs in the [Issue tracker](../../issues),
4. What did you expect to see?
5. What did you see instead?

If the problem is in a language binding, try to reproduce it with [Examples/KitchenSink](Examples/KitchenSink). It uses every construct of the IDL, so a small change to its XML is usually enough to show the problem.

2. When contributing to this repository, please first discuss the change you wish to make via issue with the [maintainers](#maintainers) of this repository. This way, we can ensure that there is no overlap between contributions or internal development work.

3. Adhere to the [code of conduct](CODE_OF_CONDUCT.md).

## Submitting a pull request
When ready to contribute, fork this repository and submit a pull request that references the issue it resolves. Be sure to include a clear and detailed description of the changes you've made so that we can verify them and eventually merge.

When you change a binding or implementation generator, generate [Examples/KitchenSink](Examples/KitchenSink) and run the test of your binding. If a construct is missing from it, add the construct to the XML so that every binding gets tested against it.

ACT follows the [git-flow](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow) branching model. New developments are integrated into the [develop](../../tree/develop)-branch. ACT's maintainers will create releases from the develop-branch when appropriate.

__NOTE__ _Before your code can be accepted into the project you must also sign the Contributor License Agreement (CLA). Please contact the maintainers via automatic-component-toolkit.contributor.agreements@autodesk.com for a copy of the CLA._
Expand Down
97 changes: 97 additions & 0 deletions Documentation/WASM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# WebAssembly bindings

ACT generates an [Emscripten](https://emscripten.org) binding (embind) that makes a component usable from JavaScript in the browser and in Node.js. The binding wraps the generated C++ binding, so the component is compiled to WebAssembly together with its C++ implementation. WASM is a binding language only; ACT does not generate implementation stubs for it.

## Generating

Add a `WASM` binding to the component description. A `Cpp` binding is required as well, ACT stops with an error if it is missing:

```xml
<bindings>
<binding language="Cpp" indentation="tabs" />
<binding language="WASM" indentation="tabs" />
</bindings>
```

ACT writes `Bindings/WASM/<basename>_bindings.cpp`. Unless ACT is called with `-suppressexamples`, it also writes to `Examples/WASM`:

* `<Name>_Example.mjs`, a small Node.js program that creates an object, calls it and shows error handling and `.delete()`.
* `buildExample.py`, a script that builds the component with `em++` and runs the `.mjs` files with Node.js. It writes `<name>Binding.js`, `<name>Binding.wasm` and TypeScript declarations (`<name>Binding.d.ts`) next to itself.

## Requirements

* Emscripten. The generated code is tested with 4.0.21 (the version of the CI image) and 7.0.1.
* Node.js to run the example. A browser needs nothing besides a web server that serves the `.js` and `.wasm` files.
* The component needs a C++ implementation that is compiled together with the binding. `buildExample.py` globs all sources in `Implementations/Cpp/Stub` and `Implementations/Cpp/Interfaces`.

`buildExample.py` links with `ENVIRONMENT=web,worker,node`, so the same module runs in a browser and in Node.js. It accepts `--env`, `--opt` and `--no-run`, and documents the required flags in its header.

## Using a component

```js
import initModule from './libprimesBinding.js';

const module = await initModule();
const wrapper = new module.CWrapper();
const { Major, Minor, Micro } = wrapper.GetVersion();
console.log(`version ${Major}.${Minor}.${Micro}`);

const calculator = wrapper.CreateFactorizationCalculator();
calculator.SetValue(735n);
calculator.Calculate();
console.log(calculator.GetPrimeFactors());
calculator.delete();
wrapper.delete();
```

* Method names are the names of the IDL. A method with `out` parameters returns an object keyed by parameter name, with the `return` value as `return`.
* Objects returned by the library are handles to C++ objects. Call `.delete()` on them, and on struct wrappers, when you are done.
* C++ exceptions surface as JavaScript errors. The error message of the library is in `e.message`.
* 64 bit integers and `pointer` values are `BigInt`.
* Enums are exposed as embind enums, structs as classes with `get_<member>` and `set_<member>` functions.
* Classes of imported components compile, but they are not bound at runtime: two bindings in one module collide on the base class and the wrapper. Methods that take or return such a class are skipped (see Not supported).

## Arrays

Arrays are exchanged as typed arrays and copied, so they stay valid when the WebAssembly memory grows.

* **Basic arrays** are `Uint8Array`, `Int8Array`, `Uint16Array`, `Int16Array`, `Uint32Array`, `Int32Array`, `BigUint64Array`, `BigInt64Array`, `Float32Array` or `Float64Array`, depending on the element type. Input parameters also accept a plain JavaScript array, except for 64 bit element types.
* **Structs whose members all have the same numeric type** (for example a position or a triangle) are exchanged as one interleaved typed array with one entry per member and struct. The length of an input array must be a multiple of the number of members.
* **All other structs** are exchanged as an object with one typed array per member, named like the member. Array members are flattened in C order.

## Callbacks

A method of the IDL that takes a callback takes a JavaScript function instead, and the user data parameter is hidden. Pass `null` to remove the callback.

```js
const sum = processor.TransformAll((value) => value * 10, Uint32Array.from([1, 2, 3]));
```

* The function receives the input parameters of the callback type in IDL order. Numbers are `Number`, 64 bit integers and pointers are `BigInt`, strings are strings, structs are struct wrappers (call `.delete()`), basic arrays are typed arrays.
* The result of the function is the `out` and `return` parameters: one result directly, several results as an object keyed by parameter name.
* A class argument arrives as an object of its most derived type. The JavaScript object owns a reference, call `.delete()` on it.
* An array `out` parameter is returned as a typed array or a plain array. The library may call the function once to ask for the size and a second time to fetch the data, as it does with a C callback, so a function can run more than once per operation.
* An exception thrown by the function reaches the caller of the method that triggered the callback.
* Callbacks are synchronous.
* A callback that is stored by the library (for example a progress callback) stays valid until the same method is called again on the same library object with the same string arguments (for example the same name), or until the module ends. Two consumers that register under different names or on different objects do not replace each other. A registration that the library rejects keeps the previous callback.
* An object that is handed to a callback holds its own reference. If the library owns the object through its own shared pointer, the JavaScript object may not outlive the callback, so use it inside the callback.
* Every class that has a method with callbacks also has `ClearCallbacks()`. It releases the JavaScript functions that were registered through that object, so their closures can be garbage collected. Call it when you are done with the object, before you call `.delete()` on it. The module does not purge them by itself, because it cannot tell when the library destroys the object. If the library still holds the callback and calls it afterwards, nothing runs and the call returns quietly with default results. Registering again after `ClearCallbacks()` works.
* Callback types without a user data parameter share one function per callback type, the last function that was set wins.

Some libraries pass the address of a buffer to a callback as an integer, for example a write callback that gets an address and a size. The module has two functions for this, they are only generated for components that have callbacks:

```js
module.ReadMemory(address, length); // returns a Uint8Array copy of the bytes; address and length are a Number or a BigInt
module.WriteMemory(address, bytes); // copies the bytes to the address
```

## Not supported

* Methods and callback types that use an enum or a struct of an imported component. The generated file lists them as skipped, for example `// .function("Describe", ...) // Skipped: enum or struct parameter of an imported component`.
* Methods with a class parameter of an imported component, and callback types with such a parameter.
* Methods with an array of `bool`, `pointer` or `enum` (`enumarray`), and methods that return an array (`returnType` array). The generated file lists them as skipped.
* Methods that need a symbol lookup method (`SymbolLookupMethod`, `SymbolAddressMethod`).

## Testing

[Examples/KitchenSink](../Examples/KitchenSink) uses every construct of the IDL, and its WASM test (`KitchenSink_Test.mjs`) covers all of the above. When you change the generator, generate `KitchenSink.xml` and run `Examples/KitchenSink/build.sh`. The test also runs in a browser: serve the `.js`, `.wasm` and `.mjs` files and import the test from a page.
Loading
Loading