Files
Nick Gerlemanandmeta-codesync[bot] ee001c475f Replace manual memory management with FinalizationRegistry (#1908)
Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1908

Fixes https://github.com/facebook/yoga/issues/1818
Fixes https://github.com/facebook/yoga/issues/1572

Yoga's JavaScript API currently requires users to manually call
`node.free()`, `node.freeRecursive()`, or `config.free()` to release
WASM memory. This is error-prone and unusual for a JavaScript library.

This diff uses `FinalizationRegistry` (available in all modern JS
engines) to automatically free WASM memory when JS objects are garbage
collected, removing this footgun entirely.

Changes:
- Added `FinalizationRegistry` instances for `Node` and `Config` in
  `wrapAssembly.ts`. When a `NodeImpl`/`ConfigImpl` is constructed, it
  is registered with the appropriate registry. The weak reference
  callback receives the WASM pointer and calls the appropriate C free
  function.
- The Node finalizer calls `YGNodeFinalize` (not `YGNodeFree`) to
  safely deallocate without disconnecting nodes from their
  owner/children, since an entire tree may be garbage collected
  together.
- Removed `free()` and `freeRecursive()` from the public `Node` type
  and `free()` from the public `Config` type.
- Removed `Yoga.Node.destroy()` and `Yoga.Config.destroy()` factory
  methods.
- Fixed `setDirtiedFunc` to use `WeakRef(this)` instead of capturing
  `this` directly in the closure stored in the dirtied func Map. The
  previous `() => dirtiedFunc(this)` pattern created a strong reference
  from the Map to the Node, preventing GC of detached nodes with a
  dirtied func set.
- Updated `gentest-javascript.js` to stop generating `try`/`finally`
  cleanup blocks and `let root` declarations in test prologues/epilogues.
- Removed all `free()`/`freeRecursive()`/`config.free()` calls from 25
  generated test files and 9 hand-written test files.
- Re-signed all generated test files with signedsource.
- Updated `README.md` to remove manual memory management documentation.

Reviewed By: cipolleschi

Differential Revision: D95014519

fbshipit-source-id: 1935b3a3ea51ce9c4e37644510b8f781e6329a7c
2026-03-06 02:49:18 -08:00

630 B

yoga-layout

This package provides prebuilt WebAssembly bindings for the Yoga layout engine.

See more at https://yogalayout.dev

Usage

import {Yoga, Align} from 'yoga-layout';

const node = Yoga.Node.create();
node.setAlignContent(Align.Center);

Requirements

yoga-layout requires a toolchain that supports ES Modules and top-level await.

If top-level-await is not supported, use the yoga-layout/load entry point instead. This requires to load yoga manually:

import {loadYoga, Align} from 'yoga-layout/load';

const node = (await loadYoga()).Node.create();
node.setAlignContent(Align.Center);