UNPKG

@sudoo/marked

Version:

JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous

115 lines (114 loc) 5.19 kB
"use strict"; /** * @author WMXPY * @namespace Evaluate * @description For In Statement */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.forInStatementEvaluator = exports.mountForInStatement = void 0; const error_code_1 = require("../declare/error-code"); const error_1 = require("../util/error/error"); const context_1 = require("../util/node/context"); const declare_1 = require("../variable/declare"); const flag_1 = require("../variable/flag"); const sand_map_1 = require("../variable/sand-map"); const mountForInStatement = (sandbox) => { sandbox.mount("ForInStatement", exports.forInStatementEvaluator); }; exports.mountForInStatement = mountForInStatement; const forInStatementEvaluator = function (node, scope, trace) { return __awaiter(this, void 0, void 0, function* () { const nextTrace = trace.stack(node); const map = yield this.execute(node.right, scope, nextTrace); const limitCounter = new context_1.LimitCounter(this.getOption("maxForLoopLimit")); if (!(map instanceof sand_map_1.SandMap)) { throw (0, error_1.error)(error_code_1.ERROR_CODE.FOR_IN_LOOP_ONLY_FOR_MAP, void 0, node, trace); } if (node.left.type !== "VariableDeclaration") { throw (0, error_1.error)(error_code_1.ERROR_CODE.FOR_IN_LOOP_ONLY_FOR_MAP, void 0, node, trace); } let loopIsBreaking = false; let loopIsContinuing = false; if (trace.hasLabel()) { scope.registerLabelListener(trace.ensureLabel(), (type) => { if (type === declare_1.SCOPE_LABEL_LISTENER_TYPE.BREAK) { this.skip(); loopIsBreaking = true; } if (type === declare_1.SCOPE_LABEL_LISTENER_TYPE.CONTINUE) { this.skip(); loopIsContinuing = true; } }); } loop: for (const key of map.keys()) { if (limitCounter.addAndCheck()) { this.break(); throw (0, error_1.error)(error_code_1.ERROR_CODE.MAXIMUM_FOR_IN_LOOP_LIMIT_EXCEED, limitCounter.amount().toString(), node, trace); } if (loopIsBreaking) { this.recoverFromSkip(); break loop; } const subScope = scope.child(); const declarations = node.left.declarations; const left = node.left; const registerFunc = (name) => { const registerer = subScope.register(left.kind); registerer(name, key); }; declarations.forEach((declaration) => { const id = declaration.id; registerFunc(id.name); }); const result = yield this.execute(node.body, subScope, nextTrace); if (loopIsContinuing) { this.recoverFromSkip(); loopIsContinuing = false; continue loop; } if (result instanceof flag_1.Flag) { if (result.isBreak()) { if (typeof result.getValue() === "string") { const breakingLabel = result.getValue(); scope.executeLabelListener(breakingLabel, declare_1.SCOPE_LABEL_LISTENER_TYPE.BREAK); break loop; } break loop; } else if (result.isReturn()) { return result.getValue(); } else if (result.isContinue()) { if (typeof result.getValue() === "string") { if (trace.hasLabel() && trace.ensureLabel() === result.getValue()) { continue loop; } const continuingLabel = result.getValue(); scope.executeLabelListener(continuingLabel, declare_1.SCOPE_LABEL_LISTENER_TYPE.CONTINUE); break loop; } continue loop; } else { throw (0, error_1.error)(error_code_1.ERROR_CODE.INTERNAL_ERROR, void 0, node, trace); } } } if (loopIsBreaking || loopIsContinuing) { this.recoverFromSkip(); } return; }); }; exports.forInStatementEvaluator = forInStatementEvaluator;