RefCapture method

X RefCapture(
  1. String key,
  2. dynamic fn(
    1. S ptr
    )
)

Copies the native struct o into a uniquely-keyed tracked slot and returns its Dart-side X wrapper via pointerToStruct.

Unique key of the form '<id>_<key>' is generated from the allocator's ID counter. The returned X holds a live reference into temp-managed memory.

fn can either:

  • return V directly (native: struct returned by value)
  • return void/null and mutate ptr in place (WASM: sret convention)

Implementation

X RefCapture(String key, dynamic Function(S ptr) fn) {
  key = uniqueSlotKey(key);

  final ptr = At(key);
  final result = fn(pointerToSource(ptr));

  final X value;

  if (result is V) {
    value = pointerToStruct(setRefFunc(ptr, result));
  } else {
    value = pointerToStruct(ptr);
  }

  value.$state.allocKey = key;
  value.$state.nextId; // trigger the ID

  return value;
}