setHostFunctions method

void setHostFunctions(
  1. Map<String, Function> functions
)

Register host functions callable from JS as globalThis[name](...).

Each value is a Dart closure (may return a Future → JS Promise). The closure runs on this (spawning) isolate, not the worker — args are marshalled in, the return value (or resolved Future) marshalled back; the worker/JS never sees Dart closure internals. Inject-once: must be called before the first evaluate; runtime mutation is rejected.

Implementation

void setHostFunctions(Map<String, Function> functions) {
  if (_hostFunctionsBound) {
    throw StateError(
      'host functions must be registered before evaluate (inject-once)',
    );
  }
  functions.forEach((k, v) {
    // Dispose any prior registration for this key so repeated/accumulating
    // pre-evaluate calls don't leak IsolateFunction handlers.
    _hostFunctions[k]?.destroy();
    _hostFunctions[k] = IsolateFunction(v);
  });
}