@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
94 lines (93 loc) • 4.29 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Evaluate
* @description While 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.whileStatementEvaluator = exports.mountWhileStatement = 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 mountWhileStatement = (sandbox) => {
sandbox.mount("WhileStatement", exports.whileStatementEvaluator);
};
exports.mountWhileStatement = mountWhileStatement;
const whileStatementEvaluator = function (node, scope, trace) {
return __awaiter(this, void 0, void 0, function* () {
const nextTrace = trace.stack(node);
const limitCounter = new context_1.LimitCounter(this.getOption("maxWhileLoopLimit"));
const test = () => __awaiter(this, void 0, void 0, function* () { return yield this.execute(node.test, scope, 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: while (yield test()) {
if (limitCounter.addAndCheck()) {
this.break();
throw (0, error_1.error)(error_code_1.ERROR_CODE.MAXIMUM_WHILE_LOOP_LIMITED_EXCEED, void 0, node, trace);
}
const subScope = scope.child();
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;
}
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.whileStatementEvaluator = whileStatementEvaluator;