@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
60 lines (59 loc) • 2.75 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Evaluate
* @description Callee Evaluator
*/
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.callExpressionEvaluator = exports.mountCallExpression = void 0;
const error_code_1 = require("../declare/error-code");
const native_to_sand_1 = require("../parse/native-to-sand");
const error_1 = require("../util/error/error");
const flag_1 = require("../variable/flag");
const sand_function_1 = require("../variable/sand-function/sand-function");
const mountCallExpression = (sandbox) => {
sandbox.mount("CallExpression", exports.callExpressionEvaluator);
};
exports.mountCallExpression = mountCallExpression;
const callExpressionEvaluator = function (node, scope, trace) {
return __awaiter(this, void 0, void 0, function* () {
const nextTrace = trace.stack(node);
const func = yield this.execute(node.callee, scope, nextTrace);
if (typeof func === "undefined") {
const calleeNode = node.callee;
const callee = calleeNode.property;
throw (0, error_1.error)(error_code_1.ERROR_CODE.CANNOT_CALL_MEMBER_FUNCTION_OF_UNDEFINED, callee.name, node, trace);
}
const args = [];
for (const arg of node.arguments) {
const argument = yield this.execute(arg, scope, nextTrace);
if (argument instanceof flag_1.Flag) {
return;
}
args.push(argument);
}
if (!(func instanceof sand_function_1.SandFunction)) {
if (this.usingAdditionalArgument) {
args.unshift(this.additionalArgument);
}
}
if (func instanceof sand_function_1.SandFunction) {
return yield func.execute(...args);
}
if (typeof func === "function") {
const result = yield Promise.resolve(func(...args));
const sandResult = (0, native_to_sand_1.parseNativeToSand)(result);
return sandResult;
}
});
};
exports.callExpressionEvaluator = callExpressionEvaluator;