var _a;

const fileName_1 = "(@effect/core) _src/io/Fiber/_internal/context.ts";
import * as tsplus_module_1 from "@effect/core/io/Metrics/operations/frequency";
import * as tsplus_module_2 from "@tsplus/stdlib/data/LazyValue";
import * as tsplus_module_3 from "@effect/core/io/Metrics/operations/counter";
import * as tsplus_module_4 from "@effect/core/io/Metrics/Boundaries";
import * as tsplus_module_5 from "@effect/core/io/Metrics/operations/histogram";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_7 from "@tsplus/stdlib/data/AtomicBoolean";
import * as tsplus_module_8 from "@tsplus/stdlib/data/AtomicReference";
import * as tsplus_module_9 from "@effect/core/io/Fiber/_internal/fiberState";
import * as tsplus_module_10 from "@tsplus/stdlib/collections/HashSet/empty";
import * as tsplus_module_11 from "@effect/core/io/TraceElement/definition";
import * as tsplus_module_12 from "@effect/core/io/Effect/operations/done";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_14 from "@tsplus/stdlib/data/Either/definition";
import * as tsplus_module_15 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_16 from "@effect/core/io/Effect/operations/asyncInterrupt";
import * as tsplus_module_17 from "@tsplus/stdlib/collections/Chunk/builder";
import * as tsplus_module_18 from "@tsplus/stdlib/collections/Chunk/empty";
import * as tsplus_module_19 from "@tsplus/stdlib/collections/ImmutableMap/size";
import * as tsplus_module_20 from "@effect/core/io/Effect/operations/unit";
import * as tsplus_module_21 from "@effect/core/io/FiberRefs/operations/make";
import * as tsplus_module_22 from "@effect/core/io/Effect/operations/updateFiberRefs";
import * as tsplus_module_23 from "@effect/core/io/Effect/operations/suspendSucceed";
import * as tsplus_module_24 from "@effect/core/io/FiberScope/definition";
import * as tsplus_module_25 from "@effect/core/io/Effect/operations/provideEnvironment";
import * as tsplus_module_26 from "@effect/core/io/Effect/operations/intoDeferred";
import * as tsplus_module_27 from "@effect/core/io/Effect/operations/zipRight";
import * as tsplus_module_28 from "@effect/core/io/Deferred/operations/make";
import * as tsplus_module_29 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_30 from "@effect/core/io/Effect/operations/environment";
import * as tsplus_module_31 from "@effect/core/io/InterruptStatus/definition";
import * as tsplus_module_32 from "@effect/core/io/RuntimeConfig/Flag";
import * as tsplus_module_33 from "@effect/core/io/RuntimeConfig/Flags/operations/isEnabled";
import * as tsplus_module_34 from "@effect/core/io/FiberRef/operations/currentLogLevel";
import * as tsplus_module_35 from "@effect/core/io/FiberRef/operations/currentLogSpan";
import * as tsplus_module_36 from "@effect/core/io/FiberRef/operations/currentLogAnnotations";
import * as tsplus_module_37 from "@effect/core/io/Cause/definition";
import * as tsplus_module_38 from "@tsplus/stdlib/collections/HashSet/forEach";
import * as tsplus_module_39 from "@tsplus/stdlib/data/Maybe/getOrElse";
import * as tsplus_module_40 from "@tsplus/stdlib/collections/ImmutableMap/remove";
import * as tsplus_module_41 from "@tsplus/stdlib/collections/ImmutableMap/set";
import * as tsplus_module_42 from "@tsplus/stdlib/data/Stack";
import * as tsplus_module_43 from "@effect/core/io/Exit/operations/succeed";
import * as tsplus_module_44 from "@effect/core/io/Effect/operations/failCauseNow";
import * as tsplus_module_45 from "@effect/core/io/Effect/operations/foldCauseEffect";
import * as tsplus_module_46 from "@effect/core/io/Fiber/status";
import * as tsplus_module_47 from "@tsplus/stdlib/collections/HashSet/add";
import * as tsplus_module_48 from "@effect/core/io/Fiber/_internal/cancelerState";
import * as tsplus_module_49 from "@effect/core/io/Effect/operations/failCause";
import * as tsplus_module_50 from "@tsplus/stdlib/collections/HashSet/size";
import * as tsplus_module_51 from "@effect/core/io/FiberRef/operations/initital";
import * as tsplus_module_52 from "@tsplus/stdlib/collections/ImmutableMap/get";
import * as tsplus_module_53 from "@tsplus/stdlib/data/Maybe/map";
import * as tsplus_module_54 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_55 from "@tsplus/stdlib/collections/ImmutableMap/from";
import * as tsplus_module_56 from "@tsplus/stdlib/collections/List/empty";
import * as tsplus_module_57 from "@tsplus/stdlib/collections/List/definition";
import * as tsplus_module_58 from "@tsplus/stdlib/collections/List/prepend";
import * as tsplus_module_59 from "@tsplus/stdlib/collections/List/filter";
import * as tsplus_module_60 from "@tsplus/stdlib/collections/List/forEach";
import * as tsplus_module_61 from "@effect/core/io/FiberId/operations/threadName";
import * as tsplus_module_62 from "@effect/core/io/LogLevel/definition";
import * as tsplus_module_63 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_64 from "@effect/core/io/Cause/operations/contains";
import * as tsplus_module_65 from "@effect/core/io/Trace/definition";
import * as tsplus_module_66 from "@effect/core/io/Cause/errors";
import * as tsplus_module_67 from "@effect/core/io/Exit/operations/mapErrorCause";
import * as tsplus_module_68 from "@effect/core/io/Cause/operations/fold";
import * as tsplus_module_69 from "@effect/core/io/Exit/operations/fold";
import * as tsplus_module_70 from "@effect/core/io/FiberId/operations/make";
import * as tsplus_module_71 from "@effect/core/io/FiberRef/operations/fork";
import * as tsplus_module_72 from "@effect/core/io/FiberRef/operations/patch";
import * as tsplus_module_73 from "@effect/core/io/FiberRef/operations/forkScopeOverride";
import * as tsplus_module_74 from "@tsplus/stdlib/data/Maybe/orElse";
import * as tsplus_module_75 from "@effect/core/io/Supervisor/operations/none";
import * as tsplus_module_76 from "@effect/core/io/FiberRef/operations/currentEnvironment";
import * as tsplus_module_77 from "@effect/core/io/Exit/operations/flatten";
import * as tsplus_module_78 from "@effect/core/io/Effect/operations/interruption";
import * as tsplus_module_79 from "@tsplus/stdlib/collections/HashSet/from";
import * as tsplus_module_80 from "@effect/core/io/FiberId/operations/combineAll";
import * as tsplus_module_81 from "@effect/core/io/Effect/operations/async";
import * as tsplus_module_82 from "@effect/core/io/Cause/operations/isTraced";
import * as tsplus_module_83 from "@effect/core/io/Cause/operations/traced";
import * as tsplus_module_84 from "@effect/core/io/Cause/operations/stripFailures";
import * as tsplus_module_85 from "@effect/core/io/Exit/operations/failCause";
import * as tsplus_module_86 from "@effect/core/io/Effect/operations/ensuring";
import * as tsplus_module_87 from "@effect/core/io/Supervisor/operations/and";
import * as tsplus_module_88 from "@effect/core/io/RuntimeConfig/operations/make";
import * as tsplus_module_89 from "@effect/core/io/FiberId/operations/none";
import * as tsplus_module_90 from "@effect/core/io/Effect/definition";
import * as tsplus_module_91 from "@effect/core/io/Effect/operations/die";
import { instruction } from "@effect/core/io/Effect/definition/primitives";
import { _A, _E, FiberSym } from "@effect/core/io/Fiber/definition";
import { concreteFiberRefs } from "@effect/core/io/FiberRefs/operations/_internal/FiberRefsInternal";
import { joinFiberRefs } from "@effect/core/io/FiberRefs/operations/_internal/join";
import { scheduleTask } from "@effect/core/support/Scheduler";
import * as StackTraceBuilder from "@effect/core/support/StackTraceBuilder";
import { constVoid } from "@tsplus/stdlib/data/Function";
const fiberFailureCauses = /*#__PURE__*/tsplus_module_2.LazyValue.make(() => tsplus_module_1.frequency("effect_fiber_failure_causes"));
const fiberForkLocations = /*#__PURE__*/tsplus_module_2.LazyValue.make(() => tsplus_module_1.frequency("effect_fiber_fork_locations"));
const fibersStarted = /*#__PURE__*/tsplus_module_2.LazyValue.make(() => tsplus_module_3.counter("effect_fiber_started"));
const fiberSuccesses = /*#__PURE__*/tsplus_module_2.LazyValue.make(() => tsplus_module_3.counter("effect_fiber_successes"));
const fiberFailures = /*#__PURE__*/tsplus_module_2.LazyValue.make(() => tsplus_module_3.counter("effect_fiber_failures"));
const fiberLifetimes = /*#__PURE__*/tsplus_module_2.LazyValue.make(() => {
  const fiberLifetimeBoundaries = tsplus_module_4.exponential(1, 2, 100);
  return tsplus_module_5.histogram("effect_fiber_lifetimes", fiberLifetimeBoundaries);
});
export class InterruptExit {
  constructor(apply, trace) {
    this.apply = apply;
    this.trace = trace;
    this._tag = "InterruptExit";
  }

}
export class Finalizer {
  constructor(finalizer, handleInterrupts, trace) {
    this.finalizer = finalizer;
    this.handleInterrupts = handleInterrupts;
    this.trace = trace;
    this._tag = "Finalizer";
  }

  apply(a) {
    this.handleInterrupts();
    return tsplus_module_6.map_(this.finalizer, () => a, this.finalizer.trace);
  }

}
export class ApplyFrame {
  constructor(apply, trace) {
    this.apply = apply;
    this.trace = trace;
    this._tag = "ApplyFrame";
  }

}
export const catastrophicFailure = /*#__PURE__*/new tsplus_module_7.AtomicBoolean(false);
export const currentFiber = /*#__PURE__*/new tsplus_module_8.AtomicReference(null);
export class FiberContext {
  constructor(_id, childFibers, fiberRefLocals, runtimeConfig, interruptStatus) {
    this._id = _id;
    this.childFibers = childFibers;
    this._tag = "RuntimeFiber";
    this[_a] = FiberSym;
    this.state = new tsplus_module_8.AtomicReference(tsplus_module_9.initial());
    this.asyncEpoch = 0;
    this.stack = undefined;
    this.nextEffect = undefined; // ---------------------------------------------------------------------------
    // Runtime Fiber
    // ---------------------------------------------------------------------------

    this._location = this._id.location; // ---------------------------------------------------------------------------
    // Interruption
    // ---------------------------------------------------------------------------

    this.interruptExit = new InterruptExit(v => {
      if (this.unsafeIsInterruptible) {
        this.popInterruptStatus();
        return tsplus_module_15.succeedNow(v, fileName_1 + ":391:43");
      } else {
        return tsplus_module_13.succeed(() => {
          this.popInterruptStatus();
          return v;
        }, fileName_1 + ":394:23");
      }
    });
    this.fiberRefLocals = fiberRefLocals;
    this.runtimeConfig = runtimeConfig;
    this.interruptStatus = interruptStatus;

    if (this.trackMetrics) {
      fibersStarted.value.unsafeUpdate(1, tsplus_module_10.empty());
      fiberForkLocations.value.unsafeUpdate(tsplus_module_11.stringify(this._location), tsplus_module_10.empty());
    }
  } // ---------------------------------------------------------------------------
  // Base Fiber
  // ---------------------------------------------------------------------------


  get fiberId() {
    return this._id;
  }

  get _await() {
    return tsplus_module_16.asyncInterruptBlockingOn(k => {
      const cb = x => k(tsplus_module_12.done(() => x, fileName_1 + ":115:67"));

      const result = this.unsafeAddObserverMaybe(cb);
      return result == null ? tsplus_module_14.left(tsplus_module_13.succeed(() => this.unsafeRemoveObserver(cb), fileName_1 + ":119:37")) : tsplus_module_14.right(tsplus_module_15.succeedNow(result, fileName_1 + ":120:41"));
    }, () => this.fiberId, fileName_1 + ":114:43");
  }

  get _children() {
    return this._evalOnEffect(tsplus_module_13.succeed(() => {
      const chunkBuilder = tsplus_module_17.builder();

      for (const child of this.childFibers) {
        chunkBuilder.append(child);
      }

      return chunkBuilder.build();
    }, fileName_1 + ":126:21"), tsplus_module_13.succeed(() => tsplus_module_18.empty(), fileName_1 + ":133:21"));
  }

  get _inheritRefs() {
    return tsplus_module_23.suspendSucceed(() => {
      if (tsplus_module_19.size(this.fiberRefLocals) === 0) {
        return tsplus_module_20.unit;
      }

      const childFiberRefs = tsplus_module_21.make(this.fiberRefLocals);
      return tsplus_module_22.updateFiberRefs((_, parentFiberRefs) => joinFiberRefs(parentFiberRefs, childFiberRefs), fileName_1 + ":145:36");
    }, fileName_1 + ":138:33");
  }

  get _poll() {
    return tsplus_module_13.succeed(() => this.unsafePoll(), fileName_1 + ":150:26");
  }

  _interruptAs(fiberId) {
    return this.unsafeInterruptAs(fiberId);
  }

  get _scope() {
    return tsplus_module_24.unsafeMake(this);
  }

  get _status() {
    return tsplus_module_13.succeed(() => this.state.get.status, fileName_1 + ":168:26");
  }

  get _trace() {
    return tsplus_module_13.succeed(() => this.unsafeCaptureTrace([]), fileName_1 + ":172:26");
  }

  _evalOn(effect, orElse) {
    return tsplus_module_23.suspendSucceed(() => this.unsafeEvalOn(effect) ? tsplus_module_20.unit : tsplus_module_20.unit_(orElse, fileName_1 + ":180:60"), fileName_1 + ":179:33");
  }

  _evalOnEffect(effect, orElse) {
    return tsplus_module_29.flatMap_(tsplus_module_30.environment(fileName_1 + ":188:30"), environment => tsplus_module_29.flatMap_(tsplus_module_28.make(fileName_1 + ":189:20"), deferred => tsplus_module_27.zipRight_(this._evalOn(tsplus_module_26.intoDeferred_(tsplus_module_25.provideEnvironment_(effect, () => environment, fileName_1 + ":191:36"), () => deferred, fileName_1 + ":191:62"), tsplus_module_26.intoDeferred_(tsplus_module_25.provideEnvironment_(orElse, () => environment, fileName_1 + ":192:36"), () => deferred, fileName_1 + ":192:62")), () => deferred.await(fileName_1 + ":193:34"), fileName_1 + ":193:19"), fileName_1 + ":189:38"), fileName_1 + ":188:43");
  } // ---------------------------------------------------------------------------
  // Descriptor
  // ---------------------------------------------------------------------------


  unsafeGetDescriptor() {
    return {
      id: this.fiberId,
      status: this.state.get.status,
      interrupters: this.state.get.interruptors,
      interruptStatus: tsplus_module_31.fromBoolean(this.unsafeIsInterruptible)
    };
  } // ---------------------------------------------------------------------------
  // Metrics
  // ---------------------------------------------------------------------------


  get trackMetrics() {
    return tsplus_module_33.isEnabled_(this.runtimeConfig.value.flags, tsplus_module_32.trackRuntimeMetrics);
  }

  observeFailure(failure) {
    if (this.trackMetrics) {
      fiberFailureCauses.value.unsafeUpdate(failure, tsplus_module_10.empty());
    }
  } // ---------------------------------------------------------------------------
  // Logging
  // ---------------------------------------------------------------------------


  unsafeLog(message, trace) {
    const logLevel = this.unsafeGetRef(tsplus_module_34.currentLogLevel.value);
    const spans = this.unsafeGetRef(tsplus_module_35.currentLogSpan.value);
    const annotations = this.unsafeGetRef(tsplus_module_36.currentLogAnnotations.value);
    const contextMap = this.unsafeGetRefs(this.fiberRefLocals);
    tsplus_module_38.forEach_(this.runtimeConfig.value.loggers, logger => {
      logger.apply(tsplus_module_11.parse(trace), this.fiberId, logLevel, message, () => tsplus_module_37.empty, contextMap, spans, annotations);
    });
  }

  unsafeLogWith(message, cause, overrideLogLevel, overrideRef1 = null, overrideValue1 = null, trace) {
    const logLevel = tsplus_module_39.getOrElse_(overrideLogLevel, () => this.unsafeGetRef(tsplus_module_34.currentLogLevel.value));
    const spans = this.unsafeGetRef(tsplus_module_35.currentLogSpan.value);
    const annotations = this.unsafeGetRef(tsplus_module_36.currentLogAnnotations.value);
    let contextMap = this.unsafeGetRefs(this.fiberRefLocals);

    if (overrideRef1 != null) {
      if (overrideValue1 == null) {
        contextMap = tsplus_module_40.remove_(contextMap, overrideRef1);
      } else {
        contextMap = tsplus_module_41.set_(contextMap, overrideRef1, overrideValue1);
      }
    }

    tsplus_module_38.forEach_(this.runtimeConfig.value.loggers, logger => {
      logger.apply(tsplus_module_11.parse(trace), this.fiberId, logLevel, message, cause, contextMap, spans, annotations);
    });
  } // ---------------------------------------------------------------------------
  // Frame
  // ---------------------------------------------------------------------------


  get isStackEmpty() {
    return this.stack == null;
  }

  pushContinuation(k) {
    this.stack = new tsplus_module_42.Stack(k, this.stack);
  }

  popContinuation() {
    if (this.stack) {
      const current = this.stack.value;
      this.stack = this.stack.previous;
      return current;
    }

    return undefined;
  }

  unsafeNextEffect(previousSuccess) {
    if (!this.isStackEmpty) {
      const frame = this.popContinuation();
      return frame._tag === "Fold" ? frame.success(previousSuccess) : frame.apply(previousSuccess);
    }

    return this.unsafeTryDone(tsplus_module_43.succeed(previousSuccess));
  }
  /**
   * Unwinds the stack, leaving the first error handler on the top of the stack
   * (assuming one is found), and returning whether or not some folds had to be
   * discarded (indicating a change in the error type).
   */


  unsafeUnwindStack() {
    let unwinding = true;
    let discardedFolds = false; // Unwind the stack, looking for an error handler

    while (unwinding && !this.isStackEmpty) {
      const frame = this.popContinuation();

      switch (frame._tag) {
        case "InterruptExit":
          {
            this.popInterruptStatus();
            break;
          }

        case "Finalizer":
          {
            // We found a finalizer, we have to immediately disable interruption
            // so the runloop will continue and not abort due to interruption
            this.unsafeDisableInterrupting();
            this.pushContinuation(new ApplyFrame(cause => tsplus_module_45.foldCauseEffect_(frame.finalizer, finalizerCause => {
              this.popInterruptStatus();
              this.unsafeAddSuppressed(finalizerCause);
              return tsplus_module_44.failCauseNow(cause, fileName_1 + ":352:45");
            }, () => {
              this.popInterruptStatus();
              return tsplus_module_44.failCauseNow(cause, fileName_1 + ":356:45");
            }, fileName_1 + ":348:46")));
            unwinding = false;
            break;
          }

        case "Fold":
          {
            if (this.unsafeShouldInterrupt) {
              discardedFolds = true;
            } else {
              // Push error handler back onto the stack and halt iteration
              this.pushContinuation(new ApplyFrame(frame.failure, frame.trace));
              unwinding = false;
            }

            break;
          }
      }
    }

    return discardedFolds;
  }

  pushInterruptStatus(flag) {
    this.interruptStatus = new tsplus_module_42.Stack(flag, this.interruptStatus);
  }

  popInterruptStatus() {
    if (this.interruptStatus) {
      const current = this.interruptStatus.value;
      this.interruptStatus = this.interruptStatus.previous;
      return current;
    }

    return undefined;
  }

  unsafeInterruptAs(fiberId) {
    const interruptedCause = tsplus_module_37.interrupt(fiberId);
    return tsplus_module_23.suspendSucceed(() => {
      const oldState = this.state.get;

      if (oldState._tag === "Executing" && oldState.status._tag === "Suspended" && oldState.status.interruptible && oldState.asyncCanceler._tag === "Registered") {
        const newState = tsplus_module_9.executing(tsplus_module_46.statusRunning(true), oldState.observers, oldState.suppressed, tsplus_module_47.add_(oldState.interruptors, fiberId), tsplus_module_48.empty, oldState.mailbox);
        this.state.set(newState);
        const interrupt = tsplus_module_49.failCause(() => interruptedCause, fileName_1 + ":438:43");
        const asyncCanceler = oldState.asyncCanceler.asyncCanceler;
        const effect = asyncCanceler === tsplus_module_20.unit ? interrupt : tsplus_module_27.zipRight_(asyncCanceler, () => interrupt, fileName_1 + ":440:90");
        this.unsafeRunLater(effect);
      } else if (oldState._tag === "Executing") {
        const newCause = tsplus_module_37.combineSeq(oldState.suppressed, interruptedCause);
        const newState = tsplus_module_9.executing(oldState.status, oldState.observers, newCause, tsplus_module_47.add_(oldState.interruptors, fiberId), oldState.asyncCanceler, oldState.mailbox);
        this.state.set(newState);
      }

      return this._await;
    }, fileName_1 + ":418:33");
  }

  unsafeSetInterrupting(value) {
    const oldState = this.state.get;

    if (oldState._tag === "Executing") {
      this.state.set(tsplus_module_9.executing(tsplus_module_46.withInterrupting(oldState.status, value), oldState.observers, oldState.suppressed, oldState.interruptors, oldState.asyncCanceler, oldState.mailbox));
    }
  }
  /**
   * Disables interruption for the fiber.
   */


  unsafeDisableInterrupting() {
    this.interruptStatus = new tsplus_module_42.Stack(false, this.interruptStatus);
  }

  unsafeRestoreInterrupt() {
    this.stack = new tsplus_module_42.Stack(this.interruptExit, this.stack);
  }

  get unsafeIsInterrupted() {
    return tsplus_module_50.size(this.state.get.interruptors) > 0;
  }

  get unsafeIsInterruptible() {
    return this.interruptStatus ? this.interruptStatus.value : true;
  }

  get unsafeIsInterrupting() {
    return tsplus_module_9.isInterrupting(this.state.get);
  }

  get unsafeShouldInterrupt() {
    return this.unsafeIsInterrupted && this.unsafeIsInterruptible && !this.unsafeIsInterrupting;
  } // ---------------------------------------------------------------------------
  // FiberRefs
  // ---------------------------------------------------------------------------


  unsafeGetRef(fiberRef) {
    return tsplus_module_39.getOrElse_(tsplus_module_53.map_(tsplus_module_52.get_(this.fiberRefLocals, fiberRef), stack => stack.head.get(1)), () => tsplus_module_51.initial(fiberRef));
  }

  unsafeGetRefs(fiberRefLocals) {
    const refs = [];

    for (const {
      tuple: [fiberRef, stack]
    } of fiberRefLocals) {
      refs.push(tsplus_module_54.make(fiberRef, stack.head.get(1)));
    }

    return tsplus_module_55.from(refs);
  }

  unsafeSetRef(fiberRef, value) {
    const oldStack = tsplus_module_39.getOrElse_(tsplus_module_52.get_(this.fiberRefLocals, fiberRef), () => tsplus_module_56.empty());
    const newStack = tsplus_module_57.isNil(oldStack) ? tsplus_module_57.cons(tsplus_module_54.make(this._id, value), tsplus_module_57.nil()) : tsplus_module_57.cons(tsplus_module_54.make(this._id, value), oldStack.tail);
    this.fiberRefLocals = tsplus_module_41.set_(this.fiberRefLocals, fiberRef, newStack);
  }

  unsafeDeleteRef(fiberRef) {
    this.fiberRefLocals = tsplus_module_40.remove_(this.fiberRefLocals, fiberRef);
  } // ---------------------------------------------------------------------------
  // Observers
  // ---------------------------------------------------------------------------


  unsafeAddObserverMaybe(k) {
    const oldState = this.state.get;

    switch (oldState._tag) {
      case "Executing":
        {
          this.state.set(tsplus_module_9.executing(oldState.status, tsplus_module_58.prepend_(oldState.observers, k), oldState.suppressed, oldState.interruptors, oldState.asyncCanceler, oldState.mailbox));
          return undefined;
        }

      case "Done":
        {
          return oldState.value;
        }
    }
  }

  unsafeRemoveObserver(k) {
    const oldState = this.state.get;

    if (oldState._tag === "Executing") {
      const observers = tsplus_module_59.filter_(oldState.observers, o => o !== k);
      this.state.set(tsplus_module_9.executing(oldState.status, observers, oldState.suppressed, oldState.interruptors, oldState.asyncCanceler, oldState.mailbox));
    }
  }

  unsafeNotifyObservers(v, observers) {
    if (tsplus_module_57.length(observers) > 0) {
      const result = tsplus_module_43.succeed(v);
      tsplus_module_60.forEach_(observers, k => k(result));
    }
  }

  unsafeReportUnhandled(exit, trace) {
    if (exit._tag === "Failure") {
      try {
        this.unsafeLogWith(() => `Fiber ${tsplus_module_61.threadName(this.fiberId)} did not handle an error`, () => exit.cause, tsplus_module_63.some(tsplus_module_62.Debug), null, null, trace);
      } catch (error) {
        if (this.runtimeConfig.value.fatal(error)) {
          this.runtimeConfig.value.reportFatal(error);
        } else {
          console.log(`An exception was thrown by a logger:\n${error}`);
        }
      }
    }
  }

  unsafeAddSuppressed(cause) {
    if (!tsplus_module_37.isEmpty(cause)) {
      const oldState = this.state.get;

      if (oldState._tag === "Executing") {
        const newState = tsplus_module_9.executing(oldState.status, oldState.observers, tsplus_module_37.combineSeq(oldState.suppressed, cause), oldState.interruptors, oldState.asyncCanceler, oldState.mailbox);
        this.state.set(newState);
      }
    }
  }

  unsafeClearSuppressed() {
    const oldState = this.state.get;

    switch (oldState._tag) {
      case "Executing":
        {
          const newState = tsplus_module_9.executing(oldState.status, oldState.observers, tsplus_module_37.empty, oldState.interruptors, oldState.asyncCanceler, oldState.mailbox);
          this.state.set(newState);
          const interruptorsCause = tsplus_module_9.interruptorsCause(oldState);
          return tsplus_module_64.contains_(oldState.suppressed, interruptorsCause) ? oldState.suppressed : tsplus_module_37.combineSeq(oldState.suppressed, interruptorsCause);
        }

      case "Done":
        {
          return tsplus_module_9.interruptorsCause(oldState);
        }
    }
  }

  unsafeAddChild(child) {
    return this.unsafeEvalOn(tsplus_module_13.succeed(() => this.childFibers.add(child), fileName_1 + ":665:44"));
  }

  unsafePoll() {
    const state = this.state.get;
    return state._tag === "Done" ? tsplus_module_63.some(state.value) : tsplus_module_63.none;
  } // ---------------------------------------------------------------------------
  // Tracing
  // ---------------------------------------------------------------------------


  unsafeCaptureTrace(prefix) {
    const builder = StackTraceBuilder.unsafeMake();
    prefix.forEach(_ => builder.append(_));

    if (this.stack != null) {
      const stack = this.stack;
      const frames = [stack.value];
      let previous = stack.previous;

      while (previous != null) {
        frames.unshift(previous.value);
        previous = previous.previous;
      }

      frames.forEach(frame => builder.append(tsplus_module_11.parse(frame.trace)));
    }

    return new tsplus_module_65.Trace(this.fiberId, builder.build());
  } // ---------------------------------------------------------------------------
  // Async
  // ---------------------------------------------------------------------------


  unsafeEnterAsync(epoch, blockingOn, trace) {
    const oldState = this.state.get;

    if (oldState._tag === "Executing" && oldState.status._tag === "Running" && oldState.asyncCanceler._tag === "Empty") {
      const newStatus = tsplus_module_46.statusSuspended(oldState.status.interrupting, this.unsafeIsInterruptible && !this.unsafeIsInterrupting, epoch, blockingOn, trace);
      const newState = tsplus_module_9.executing(newStatus, oldState.observers, oldState.suppressed, oldState.interruptors, tsplus_module_48.pending, oldState.mailbox);
      this.state.set(newState);
    } else {
      throw new tsplus_module_66.IllegalStateException(`Fiber ${tsplus_module_61.threadName(this.fiberId)} is not running`);
    }
  }

  unsafeExitAsync(epoch) {
    const oldState = this.state.get;

    if (oldState._tag === "Executing" && oldState.status._tag === "Suspended" && oldState.status.asyncs === epoch) {
      const newState = tsplus_module_9.executing(tsplus_module_46.statusRunning(oldState.status.interrupting), oldState.observers, oldState.suppressed, oldState.interruptors, tsplus_module_48.empty, oldState.mailbox);
      this.state.set(newState);
      return true;
    }

    return false;
  }

  unsafeCreateAsyncResume(epoch) {
    return effect => {
      if (this.unsafeExitAsync(epoch)) {
        this.unsafeRunLater(effect);
      }
    };
  }

  unsafeSetAsyncCanceler(epoch, asyncCanceler0) {
    const oldState = this.state.get;
    const asyncCanceler = asyncCanceler0 == null ? tsplus_module_20.unit : asyncCanceler0;

    if (oldState._tag === "Executing" && oldState.status._tag === "Suspended" && oldState.asyncCanceler._tag === "Pending" && epoch === oldState.status.asyncs) {
      this.state.set(tsplus_module_9.executing(oldState.status, oldState.observers, oldState.suppressed, oldState.interruptors, tsplus_module_48.registered(asyncCanceler), oldState.mailbox));
    } else if (oldState._tag === "Executing" && oldState.status._tag === "Suspended" && oldState.asyncCanceler._tag === "Registered" && epoch === oldState.status.asyncs) {
      throw new Error("Bug, inconsistent state in unsafeSetAsyncCanceler");
    }
  } // ---------------------------------------------------------------------------
  // Finalizer
  // ---------------------------------------------------------------------------


  unsafeAddFinalizer(finalizer) {
    this.pushContinuation(new Finalizer(finalizer, () => {
      this.unsafeDisableInterrupting();
      this.unsafeRestoreInterrupt();
    }));
  } // ---------------------------------------------------------------------------
  // Execution
  // ---------------------------------------------------------------------------


  unsafeEvalOn(effect) {
    const oldState = this.state.get;

    switch (oldState._tag) {
      case "Executing":
        {
          const newMailbox = oldState.mailbox == null ? effect : tsplus_module_27.zipRight_(oldState.mailbox, () => effect, fileName_1 + ":823:89");
          this.state.set(tsplus_module_9.executing(oldState.status, oldState.observers, oldState.suppressed, oldState.interruptors, oldState.asyncCanceler, newMailbox));
          return true;
        }

      case "Done":
        {
          return false;
        }
    }
  }

  unsafeTryDone(exit) {
    const oldState = this.state.get;

    switch (oldState._tag) {
      case "Executing":
        {
          if (oldState.mailbox != null) {
            // Not done because the mailbox isn't empty
            const newState = tsplus_module_9.executing(oldState.status, oldState.observers, oldState.suppressed, oldState.interruptors, oldState.asyncCanceler, undefined);
            this.state.set(newState);
            this.unsafeSetInterrupting(true);
            return tsplus_module_27.zipRight_(oldState.mailbox, () => tsplus_module_12.done(() => exit, fileName_1 + ":864:67"), fileName_1 + ":864:55");
          } else if (this.childFibers.size === 0) {
            // The mailbox is empty and the _children are shut down
            const interruptorsCause = tsplus_module_9.interruptorsCause(oldState);
            const newExit = interruptorsCause === tsplus_module_37.empty ? exit : tsplus_module_67.mapErrorCause_(exit, cause => tsplus_module_64.contains_(cause, interruptorsCause) ? cause : tsplus_module_37.combineSeq(cause, interruptorsCause)); //  We are truly "unsafeTryDone" because the scope has been closed

            this.state.set(tsplus_module_9.done(newExit));
            this.unsafeReportUnhandled(newExit);
            this.unsafeNotifyObservers(newExit, oldState.observers);
            const startTimeSeconds = this._id.startTimeSeconds;
            const endTimeSeconds = new Date().getTime() / 1000;
            const lifetime = endTimeSeconds - startTimeSeconds;

            if (this.trackMetrics) {
              fiberLifetimes.value.unsafeUpdate(lifetime, tsplus_module_10.empty());
            }

            tsplus_module_69.fold_(newExit, cause => {
              if (this.trackMetrics) {
                fiberFailures.value.unsafeUpdate(1, tsplus_module_10.empty());
              }

              return tsplus_module_68.fold_(cause, () => fiberFailureCauses.value.unsafeUpdate("<empty>", tsplus_module_10.empty()), (failure, _) => {
                this.observeFailure(typeof failure === "object" ? failure.constructor.name : "<anonymous>");
              }, (defect, _) => {
                this.observeFailure(typeof defect === "object" ? defect.constructor.name : "<anonymous>");
              }, () => {
                this.observeFailure("InterruptedException");
              }, constVoid, constVoid, constVoid);
            }, () => {
              if (this.trackMetrics) {
                fiberSuccesses.value.unsafeUpdate(1, tsplus_module_10.empty());
              }
            });
            return undefined;
          } else {
            // Not done because there are _children left to close
            this.unsafeSetInterrupting(true);
            let interruptChildren = tsplus_module_20.unit;

            for (const child of this.childFibers) {
              interruptChildren = tsplus_module_27.zipRight_(interruptChildren, () => child._interruptAs(this._id), fileName_1 + ":931:59");
            }

            this.childFibers.clear();
            return tsplus_module_27.zipRight_(interruptChildren, () => tsplus_module_12.done(() => exit, fileName_1 + ":936:68"), fileName_1 + ":936:56");
          }
        }

      case "Done":
        {
          // Already unsafeTryDone
          return undefined;
        }
    }
  }

  unsafeDrainMailbox() {
    const oldState = this.state.get;

    switch (oldState._tag) {
      case "Executing":
        {
          const newState = tsplus_module_9.executing(oldState.status, oldState.observers, oldState.suppressed, oldState.interruptors, oldState.asyncCanceler, undefined);
          this.state.set(newState);
          return oldState.mailbox;
        }

      case "Done":
        {
          return undefined;
        }
    }
  }

  unsafeOnDone(k) {
    const result = this.unsafeAddObserverMaybe(k);

    if (result != null) {
      k(tsplus_module_43.succeed(result));
    }
  }
  /**
   * Forks an `IO` with the specified failure handler.
   */


  unsafeFork(effect, trace, forkScope = tsplus_module_63.none) {
    const childId = tsplus_module_70.unsafeMake(trace);
    const childFiberRefLocalEntries = [];

    for (const {
      tuple: [fiberRef, stack]
    } of this.fiberRefLocals) {
      const value = tsplus_module_72.patch_(fiberRef, tsplus_module_71.fork_(fiberRef))(stack.head.get(1));
      childFiberRefLocalEntries.push(tsplus_module_54.make(fiberRef, tsplus_module_58.prepend_(stack, tsplus_module_54.make(childId, value))));
    }

    const childFiberRefLocals = tsplus_module_55.from(childFiberRefLocalEntries);
    const parentScope = tsplus_module_39.getOrElse_(tsplus_module_74.orElse_(forkScope, () => this.unsafeGetRef(tsplus_module_73.forkScopeOverride.value)), () => this._scope);
    const grandChildren = new Set();
    const childContext = new FiberContext(childId, grandChildren, childFiberRefLocals, this.runtimeConfig, new tsplus_module_42.Stack(this.interruptStatus ? this.interruptStatus.value : true));

    if (this.runtimeConfig.value.supervisor !== tsplus_module_75.none) {
      this.runtimeConfig.value.supervisor.unsafeOnStart(this.unsafeGetRef(tsplus_module_76.currentEnvironment.value), effect, tsplus_module_63.some(this), childContext);
      childContext.unsafeOnDone(exit => this.runtimeConfig.value.supervisor.unsafeOnEnd(tsplus_module_77.flatten(exit), childContext));
    }

    const childEffect = !parentScope.unsafeAdd(this.runtimeConfig, childContext, fileName_1 + ":1024:47") ? tsplus_module_78.interruptAs(() => parentScope.fiberId, fileName_1 + ":1025:27") : effect;
    childContext.nextEffect = childEffect;
    scheduleTask(() => childContext.runUntil(this.runtimeConfig.value.maxOp));
    return childContext;
  }

  complete(winner, loser, cont, ab, cb) {
    if (ab.compareAndSet(true, false)) {
      cb(cont(winner, loser));
    }
  }

  unsafeRace(race, trace) {
    const raceIndicator = new tsplus_module_7.AtomicBoolean(true);
    const left = this.unsafeFork(race.left(), trace);
    const right = this.unsafeFork(race.right(), trace);
    return tsplus_module_81.asyncBlockingOn(cb => {
      const leftRegister = left.unsafeAddObserverMaybe(() => this.complete(left, right, race.leftWins, raceIndicator, cb));

      if (leftRegister != null) {
        this.complete(left, right, race.leftWins, raceIndicator, cb);
      } else {
        const rightRegister = right.unsafeAddObserverMaybe(() => this.complete(right, left, race.rightWins, raceIndicator, cb));

        if (rightRegister != null) {
          this.complete(right, left, race.rightWins, raceIndicator, cb);
        }
      }
    }, tsplus_module_80.combineAll(tsplus_module_79.from([left.fiberId, right.fiberId])), fileName_1 + ":1056:34");
  }

  unsafeRunLater(instr) {
    this.nextEffect = instr;
    scheduleTask(() => this.runUntil(this.runtimeConfig.value.maxOp));
  }
  /**
   * The main evaluator loop for the fiber. For purely synchronous effects, this
   * will run either to completion, or for the specified maximum operation
   * count. For effects with asynchronous callbacks, the loop will proceed no
   * further than the first asynchronous boundary.
   */


  runUntil(maxOpCount) {
    try {
      const flags = this.runtimeConfig.value.flags;
      const logRuntime = tsplus_module_33.isEnabled_(flags, tsplus_module_32.logRuntime);
      let current = this.nextEffect;
      this.nextEffect = undefined;
      const superviseOps = tsplus_module_33.isEnabled_(flags, tsplus_module_32.superviseOperations) && this.runtimeConfig.value.supervisor !== tsplus_module_75.none;

      if (tsplus_module_33.isEnabled_(flags, tsplus_module_32.enableCurrentFiber)) {
        currentFiber.set(this);
      }

      if (this.runtimeConfig.value.supervisor !== tsplus_module_75.none) {
        this.runtimeConfig.value.supervisor.unsafeOnResume(this);
      }

      while (current != null) {
        try {
          let opCount = 0;

          do {
            // Check to see if the fiber should continue executing or not:
            if (!this.unsafeShouldInterrupt) {
              // Fiber does not need to be interrupted, but might need to yield:
              const message = this.unsafeDrainMailbox();

              if (message != null) {
                const oldEffect = current; // TODO: trace

                current = tsplus_module_29.flatMap_(message, () => oldEffect, fileName_1 + ":1118:54");
              } else if (opCount === maxOpCount) {
                this.unsafeRunLater(current);
                current = undefined;
              } else {
                if (logRuntime) {
                  this.unsafeLog(() => current.unsafeLog(), current.trace);
                }

                if (superviseOps) {
                  this.runtimeConfig.value.supervisor.unsafeOnEffect(this, current);
                } // Fiber is neither being interrupted nor needs to yield. Execute
                // the next instruction in the program:


                switch (current._tag) {
                  case "FlatMap":
                    {
                      this.pushContinuation(new ApplyFrame(current.k, current.trace));
                      current = current.effect;
                      break;
                    }

                  case "SucceedNow":
                    {
                      current = this.unsafeNextEffect(current.value);
                      break;
                    }

                  case "Succeed":
                    {
                      current = this.unsafeNextEffect(current.effect());
                      break;
                    }

                  case "SucceedWith":
                    {
                      current = this.unsafeNextEffect(current.effect(this.runtimeConfig, this._id));
                      break;
                    }

                  case "Fail":
                    {
                      const cause = current.cause();
                      const tracedCause = tsplus_module_82.isTraced(cause) ? cause : tsplus_module_83.traced_(cause, this.unsafeCaptureTrace([tsplus_module_11.parse(current.trace)]));
                      const discardedFolds = this.unsafeUnwindStack();
                      const strippedCause = discardedFolds ? // We threw away some error handlers while unwinding the
                      tsplus_module_84.stripFailures( // We threw away some error handlers while unwinding the
                      // stack because we got interrupted during this instruction.
                      // So it's not safe to return typed failures from cause0,
                      // because they might not be typed correctly. Instead, we
                      // strip the typed failures, and return the remainders and
                      // the interruption.
                      tracedCause) : tracedCause;
                      const suppressed = this.unsafeClearSuppressed();
                      const fullCause = tsplus_module_64.contains_(strippedCause, suppressed) ? strippedCause : tsplus_module_37.combineSeq(strippedCause, suppressed);

                      if (this.isStackEmpty) {
                        // Error not caught, stack is empty
                        this.unsafeSetInterrupting(true);
                        current = this.unsafeTryDone(tsplus_module_85.failCause(fullCause));
                      } else {
                        this.unsafeSetInterrupting(false); // Error caught, next continuation on the stack will deal
                        // with it, so we just have to compute it here:

                        current = this.unsafeNextEffect(fullCause);
                      }

                      break;
                    }

                  case "Fold":
                    {
                      const effect = current;
                      current = effect.effect;
                      this.pushContinuation(effect);
                      break;
                    }

                  case "Suspend":
                    {
                      current = current.make();
                      break;
                    }

                  case "SuspendWith":
                    {
                      current = current.make(this.runtimeConfig, this._id);
                      break;
                    }

                  case "InterruptStatus":
                    {
                      const boolFlag = current.flag().toBoolean;
                      const interruptStatus = this.interruptStatus ? this.interruptStatus.value : true;

                      if (interruptStatus !== boolFlag) {
                        this.interruptStatus = new tsplus_module_42.Stack(boolFlag, this.interruptStatus);
                        this.unsafeRestoreInterrupt();
                      }

                      current = current.effect;
                      break;
                    }

                  case "CheckInterrupt":
                    {
                      current = current.k(tsplus_module_31.fromBoolean(this.unsafeIsInterruptible));
                      break;
                    }

                  case "Async":
                    {
                      const effect = current;
                      const epoch = this.asyncEpoch;
                      this.asyncEpoch = epoch + 1; // Enter suspended state

                      this.unsafeEnterAsync(epoch, effect.blockingOn(), tsplus_module_11.parse(effect.trace));
                      const k = effect.register;
                      const either = k(this.unsafeCreateAsyncResume(epoch));

                      switch (either._tag) {
                        case "Left":
                          {
                            const canceler = either.left;
                            this.unsafeSetAsyncCanceler(epoch, canceler);

                            if (this.unsafeShouldInterrupt) {
                              if (this.unsafeExitAsync(epoch)) {
                                this.unsafeSetInterrupting(true);
                                current = tsplus_module_27.zipRight_(canceler, () => tsplus_module_49.failCause(() => this.unsafeClearSuppressed(), fileName_1 + ":1261:49"), fileName_1 + ":1260:48");
                              } else {
                                current = undefined;
                              }
                            } else {
                              current = undefined;
                            }

                            break;
                          }

                        case "Right":
                          {
                            if (!this.unsafeExitAsync(epoch)) {
                              current = undefined;
                            } else {
                              current = either.right;
                            }

                            break;
                          }
                      }

                      break;
                    }

                  case "Fork":
                    {
                      const effect = current;
                      current = this.unsafeNextEffect(this.unsafeFork(effect.effect, tsplus_module_11.parse(effect.trace), effect.scope()));
                      break;
                    }

                  case "Descriptor":
                    {
                      current = current.f(this.unsafeGetDescriptor());
                      break;
                    }

                  case "Yield":
                    {
                      this.unsafeRunLater(tsplus_module_20.unit);
                      current = undefined;
                      break;
                    }

                  case "Trace":
                    {
                      current = this.unsafeNextEffect(this.unsafeCaptureTrace([tsplus_module_11.parse(current.trace)]));
                      break;
                    }

                  case "FiberRefModify":
                    {
                      const {
                        tuple: [result, newValue]
                      } = current.f(this.unsafeGetRef(current.fiberRef));
                      this.unsafeSetRef(current.fiberRef, newValue);
                      current = this.unsafeNextEffect(result);
                      break;
                    }

                  case "FiberRefModifyAll":
                    {
                      const {
                        tuple: [result, newValue]
                      } = current.f(this._id, tsplus_module_21.make(this.fiberRefLocals));
                      void 0;
                      this.fiberRefLocals = newValue.fiberRefLocals;
                      current = this.unsafeNextEffect(result);
                      break;
                    }

                  case "FiberRefLocally":
                    {
                      const effect = current;
                      const fiberRef = effect.fiberRef;
                      const oldValue = this.unsafeGetRef(fiberRef);
                      this.unsafeSetRef(fiberRef, effect.localValue);
                      current = tsplus_module_86.ensuring_(effect.effect, () => tsplus_module_13.succeed(() => this.unsafeSetRef(fiberRef, oldValue), fileName_1 + ":1351:39"), fileName_1 + ":1350:45");
                      break;
                    }

                  case "FiberRefDelete":
                    {
                      this.unsafeDeleteRef(current.fiberRef);
                      current = this.unsafeNextEffect(undefined);
                      break;
                    }

                  case "FiberRefWith":
                    {
                      current = current.f(this.unsafeGetRef(current.fiberRef));
                      break;
                    }

                  case "RaceWith":
                    {
                      current = this.unsafeRace(current, tsplus_module_11.parse(current.trace));
                      break;
                    }

                  case "Supervise":
                    {
                      const effect = current;
                      const oldSupervisor = this.runtimeConfig.value.supervisor;
                      const newSupervisor = tsplus_module_87.and_(effect.supervisor(), oldSupervisor);
                      this.runtimeConfig = tsplus_module_88.make({ ...this.runtimeConfig.value,
                        supervisor: newSupervisor
                      });
                      this.unsafeAddFinalizer(tsplus_module_13.succeed(() => {
                        this.runtimeConfig = tsplus_module_88.make({ ...this.runtimeConfig.value,
                          supervisor: oldSupervisor
                        });
                      }, fileName_1 + ":1390:37"));
                      current = effect.effect;
                      break;
                    }

                  case "GetForkScope":
                    {
                      const effect = current;
                      current = effect.f(tsplus_module_39.getOrElse_(this.unsafeGetRef(tsplus_module_73.forkScopeOverride.value), () => this._scope));
                      break;
                    }

                  case "OverrideForkScope":
                    {
                      const oldForkScopeOverride = this.unsafeGetRef(tsplus_module_73.forkScopeOverride.value);
                      this.unsafeSetRef(tsplus_module_73.forkScopeOverride.value, current.forkScope);
                      this.unsafeAddFinalizer(tsplus_module_13.succeed(() => this.unsafeSetRef(tsplus_module_73.forkScopeOverride.value, oldForkScopeOverride), fileName_1 + ":1428:37"));
                      current = current.effect;
                      break;
                    }

                  case "Ensuring":
                    {
                      this.unsafeAddFinalizer( // @ts-expect-error
                      current.finalizer);
                      current = current.effect;
                      break;
                    }

                  case "Logged":
                    {
                      const effect = current;
                      this.unsafeLogWith(effect.message, effect.cause, effect.overrideLogLevel, effect.overrideRef1, effect.overrideValue1, effect.trace);
                      current = this.unsafeNextEffect(undefined);
                      break;
                    }

                  case "SetRuntimeConfig":
                    {
                      this.runtimeConfig = current.runtimeConfig;
                      current = tsplus_module_20.unit;
                      break;
                    }
                }
              }
            } else {
              // Fiber was interrupted
              const trace = current.trace;
              current = tsplus_module_49.failCause(() => this.unsafeClearSuppressed(), trace); // Prevent interruption of interruption

              this.unsafeSetInterrupting(true);
            }

            opCount = opCount + 1;
          } while (current != null);
        } catch (e) {
          if (e instanceof tsplus_module_66.InterruptedException) {
            const trace = current?.trace;
            current = tsplus_module_78.interruptAs(() => tsplus_module_89.none, trace); // Prevent interruption of interruption:

            this.unsafeSetInterrupting(true);
          } else if (e instanceof tsplus_module_90.Effect.Error) {
            switch (e.exit._tag) {
              case "Success":
                {
                  current = this.unsafeNextEffect(e.exit.value);
                  break;
                }

              case "Failure":
                {
                  const trace = current ? current.trace : undefined;
                  current = tsplus_module_49.failCause(() => e.exit.cause, trace);
                  break;
                }
            }
          } else if (this.runtimeConfig.value.fatal(e)) {
            catastrophicFailure.set(true); // Catastrophic error handler. Any error thrown inside the interpreter
            // is either a bug in the interpreter or a bug in the user's code. Let
            // the fiber die but attempt finalization & report errors.

            this.runtimeConfig.value.reportFatal(e);
            current = undefined;
          } else {
            this.unsafeSetInterrupting(true);
            current = tsplus_module_91.die(() => e, fileName_1 + ":1517:45");
          }
        }
      }
    } finally {
      if (tsplus_module_33.isEnabled_(this.runtimeConfig.value.flags, tsplus_module_32.enableCurrentFiber)) {
        currentFiber.set(null);
      }

      if (this.runtimeConfig.value.supervisor !== tsplus_module_75.none) {
        this.runtimeConfig.value.supervisor.unsafeOnSuspend(this);
      }
    }
  }

  run() {
    return this.runUntil(this.runtimeConfig.value.maxOp);
  }

}
_a = FiberSym;
//# sourceMappingURL=context.mjs.map