@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
116 lines (115 loc) • 4.99 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Evaluate
* @description For 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.forStatementEvaluator = exports.mountForStatement = 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 mountForStatement = (sandbox) => {
sandbox.mount("ForStatement", exports.forStatementEvaluator);
};
exports.mountForStatement = mountForStatement;
const forStatementEvaluator = function (node, scope, trace) {
return __awaiter(this, void 0, void 0, function* () {
const nextTrace = trace.stack(node);
const subScope = scope.child();
const limitCounter = new context_1.LimitCounter(this.getOption("maxForLoopLimit"));
if (node.init) {
yield this.execute(node.init, subScope, nextTrace);
}
const test = () => __awaiter(this, void 0, void 0, function* () {
if (node.test) {
const result = yield this.execute(node.test, subScope, nextTrace);
return Boolean(result);
}
return true;
});
const update = () => __awaiter(this, void 0, void 0, function* () {
if (node.update) {
yield this.execute(node.update, subScope, nextTrace);
}
});
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 (limitCounter.reset(); yield test(); limitCounter.add()) {
if (limitCounter.check()) {
this.break();
throw (0, error_1.error)(error_code_1.ERROR_CODE.MAXIMUM_FOR_LOOP_LIMIT_EXCEED, void 0, node, trace);
}
if (loopIsBreaking) {
this.recoverFromSkip();
break loop;
}
const result = yield this.execute(node.body, subScope, nextTrace);
if (loopIsContinuing) {
this.recoverFromSkip();
loopIsContinuing = false;
yield update();
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;
}
else if (result.isContinue()) {
if (typeof result.getValue() === "string") {
if (trace.hasLabel()
&& trace.ensureLabel() === result.getValue()) {
yield update();
continue loop;
}
const continuingLabel = result.getValue();
scope.executeLabelListener(continuingLabel, declare_1.SCOPE_LABEL_LISTENER_TYPE.CONTINUE);
break loop;
}
yield update();
continue loop;
}
else {
throw (0, error_1.error)(error_code_1.ERROR_CODE.INTERNAL_ERROR, void 0, node, trace);
}
}
yield update();
}
if (loopIsBreaking || loopIsContinuing) {
this.recoverFromSkip();
}
return;
});
};
exports.forStatementEvaluator = forStatementEvaluator;