UNPKG

hakojs

Version:

A secure, embeddable JavaScript engine that runs untrusted code inside WebAssembly sandboxes with fine-grained permissions and resource limits

160 lines (157 loc) 4.43 kB
var __dispose = Symbol.dispose || /* @__PURE__ */ Symbol.for("Symbol.dispose"); var __asyncDispose = Symbol.dispose || /* @__PURE__ */ Symbol.for("Symbol.dispose"); var __using = (stack, value, async) => { if (value != null) { if (typeof value !== "object" && typeof value !== "function") throw TypeError('Object expected to be assigned to "using" declaration'); var dispose; if (async) dispose = value[__asyncDispose]; if (dispose === undefined) dispose = value[__dispose]; if (typeof dispose !== "function") throw TypeError("Object not disposable"); stack.push([async, dispose, value]); } else if (async) { stack.push([async]); } return value; }; var __callDispose = (stack, error, hasError) => { var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) { return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _; }, fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e), next = (it) => { while (it = stack.pop()) { try { var result = it[1] && it[1].call(it[2]); if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next())); } catch (e) { fail(e); } } if (hasError) throw error; }; return next(); }; // src/helpers/iterator-helper.ts import { DisposableResult } from "../mem/lifetime"; import { VMValue } from "../vm/value"; class VMIterator { handle; context; owner; _next; _isDone = false; constructor(handle, context) { this.handle = handle; this.context = context; this.owner = context.runtime; } [Symbol.iterator]() { return this; } next(value) { if (!this.alive || this._isDone) { return { done: true, value: undefined }; } if (this._next === undefined) { this._next = this.handle.getProperty("next"); } const nextMethod = this._next; return this.callIteratorMethod(nextMethod, value); } return(value) { if (!this.alive) { return { done: true, value: undefined }; } const returnMethod = this.handle.getProperty("return"); if (returnMethod.isUndefined() && value === undefined) { this.dispose(); return { done: true, value: undefined }; } const result = this.callIteratorMethod(returnMethod, value); returnMethod.dispose(); this.dispose(); return result; } throw(e) { if (!this.alive) { return { done: true, value: undefined }; } if (!(e instanceof Error) && !(e instanceof VMValue)) { throw new TypeError("throw() argument must be an Error or VMValue. How did it come to this?"); } const errorHandle = e instanceof VMValue ? e : this.context.newError(e); const throwMethod = this.handle.getProperty("throw"); const result = this.callIteratorMethod(throwMethod, errorHandle); if (errorHandle.alive) { errorHandle.dispose(); } throwMethod.dispose(); this.dispose(); return result; } get alive() { return this.handle.alive; } dispose() { this._isDone = true; this.handle.dispose(); if (this._next?.alive) { this._next.dispose(); } } [Symbol.dispose]() { this.dispose(); } callIteratorMethod(method, input) { let __stack = []; try { const callResult = input ? this.context.callFunction(method, this.handle, input) : this.context.callFunction(method, this.handle); if (callResult.error) { this.dispose(); return { value: callResult }; } const done = __using(__stack, callResult.value.getProperty("done").consume((v) => v.toNativeValue()), 0); if (done.value) { callResult.value.dispose(); this.dispose(); return { done: done.value, value: undefined }; } const value = callResult.value.getProperty("value"); callResult.dispose(); return { value: DisposableResult.success(value), done: done.value }; } catch (_catch) { var _err = _catch, _hasErr = 1; } finally { __callDispose(__stack, _err, _hasErr); } } } export { VMIterator }; //# debugId=75B024B54F23161C64756E2164756E21 //# sourceMappingURL=iterator-helper.js.map