softReset method

  1. @override
void softReset()
override

Wipe tenant state while keeping the same native runtime and instance id. Used by JsEnginePool with EngineResetMode.soft.

Replaces the JSContext on the same JSRuntime: globalThis, global var / let / const / class bindings and builtin prototype changes are all dropped, while the native heap is kept.

Implementation

@override
void softReset() {
  if (_disposed) {
    throw StateError('QuickJsRuntime2 is disposed');
  }
  releaseHostCaches();
  localContext.clear();
  dartContext.clear();
  try {
    disposeChannelFunctions();
  } catch (_) {}
  final rt = _rt;
  if (rt == null) {
    _needsInitialization = false;
    init();
    return;
  }
  // Bounded drain: a self-rescheduling Promise chain must not hang reset.
  try {
    executePendingJobs();
  } catch (_) {}
  final referenceLeak = _freeContext(rt);
  _ctx = jsNewContext(rt);
  try {
    runGC();
  } catch (_) {}
  // [init] reinstalls sendMessage / console / setTimeout.
  init();
  if (referenceLeak != null) throw JSError(referenceLeak);
}