import {
  Bk,
  Fk,
  KI,
  Pd,
  Rk,
  _a,
  gk,
  iC,
  qs,
  za
} from "./chunk-F4HVMUHG.js";

// ../hoppscotch-js-sandbox/dist/module-asyncify-R6PWJ6ZV-2Fad--rr-CwGkAzIl.js
var y = class extends Bk {
  async evalCodeAsync(e, t = "eval.js", i) {
    let s = i === void 0 ? 1 : 0, a = _a(i), o = 0;
    try {
      o = await this.memory.newHeapCharPointer(e).consume((n) => this.ffi.QTS_Eval_MaybeAsync(this.ctx.value, n.value.ptr, n.value.strlen, t, s, a));
    } catch (n) {
      throw this.runtime.debugLog("QTS_Eval_MaybeAsync threw", n), n;
    }
    let r = this.ffi.QTS_ResolveException(this.ctx.value, o);
    return r ? (this.ffi.QTS_FreeValuePointer(this.ctx.value, o), this.fail(this.memory.heapValueHandle(r))) : this.success(this.memory.heapValueHandle(o));
  }
  newAsyncifiedFunction(e, t) {
    return this.newFunction(e, t);
  }
};
var C = class extends gk {
  constructor(e) {
    super(e);
  }
  newContext(e = {}) {
    let t = za(e.intrinsics), i = new KI(this.ffi.QTS_NewContext(this.rt.value, t), void 0, (a) => {
      this.contextMap.delete(a), this.callbacks.deleteContext(a), this.ffi.QTS_FreeContext(a);
    }), s = new y({ module: this.module, ctx: i, ffi: this.ffi, rt: this.rt, ownedLifetimes: [], runtime: this, callbacks: this.callbacks });
    return this.contextMap.set(i.value, s), s;
  }
  setModuleLoader(e, t) {
    super.setModuleLoader(e, t);
  }
  setMaxStackSize(e) {
    return super.setMaxStackSize(e);
  }
};
var M = class extends Pd {
  constructor(e, t) {
    super(e, t), this.ffi = t, this.module = e;
  }
  newRuntime(e = {}) {
    let t = new KI(this.ffi.QTS_NewRuntime(), void 0, (s) => {
      this.callbacks.deleteRuntime(s), this.ffi.QTS_FreeRuntime(s);
    }), i = new C({ module: this.module, ffi: this.ffi, rt: t, callbacks: this.callbacks });
    return Fk(i, e), e.moduleLoader && i.setModuleLoader(e.moduleLoader), i;
  }
  newContext(e = {}) {
    let t = this.newRuntime(), i = e.ownedLifetimes ? e.ownedLifetimes.concat([t]) : [t], s = t.newContext({ ...e, ownedLifetimes: i });
    return t.context = s, s;
  }
  evalCode() {
    throw new qs("QuickJSWASMModuleAsyncify.evalCode: use evalCodeAsync instead");
  }
  evalCodeAsync(e, t) {
    return iC.withScopeAsync(async (i) => {
      let s = i.manage(this.newContext());
      Rk(s.runtime, t);
      let a = await s.evalCodeAsync(e, "eval.js");
      if (t.memoryLimitBytes !== void 0 && s.runtime.setMemoryLimit(-1), a.error) throw s.dump(i.manage(a.error));
      return s.dump(i.manage(a.value));
    });
  }
};
export {
  M as QuickJSAsyncWASMModule
};
//# sourceMappingURL=module-asyncify-R6PWJ6ZV-2Fad--rr-CwGkAzIl-BTB3GFJ4.js.map