UNPKG

@sudoo/marked

Version:

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

55 lines (54 loc) 2.9 kB
"use strict"; /** * @author WMXPY * @namespace Evaluate * @description Function Expression */ 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.functionExpressionEvaluator = exports.mountFunctionExpression = void 0; const error_code_1 = require("../declare/error-code"); const params_register_1 = require("../operation/function-expression/params-register"); const error_1 = require("../util/error/error"); const flag_1 = require("../variable/flag"); const sand_class_1 = require("../variable/sand-class/sand-class"); const sand_class_instance_1 = require("../variable/sand-class/sand-class-instance"); const sand_function_1 = require("../variable/sand-function/sand-function"); const mountFunctionExpression = (sandbox) => { sandbox.mount("FunctionExpression", exports.functionExpressionEvaluator); }; exports.mountFunctionExpression = mountFunctionExpression; const functionExpressionEvaluator = function (node, scope, trace) { return __awaiter(this, void 0, void 0, function* () { const nextTrace = trace.stack(node); if (node.async) { throw (0, error_1.error)(error_code_1.ERROR_CODE.UNNECESSARY_ASYNC_EXPRESSION, void 0, node, trace); } const func = (thisValue, ...args) => __awaiter(this, void 0, void 0, function* () { const subScope = scope.child().initThis(); if (thisValue instanceof sand_class_1.SandClass) { subScope.replaceThis(thisValue.staticBody); } if (thisValue instanceof sand_class_instance_1.SandClassInstance) { subScope.replaceThis(thisValue.body); } const bindingRegisterFunctionExpressionParams = params_register_1.registerFunctionExpressionParams.bind(this); bindingRegisterFunctionExpressionParams(args, node.params, subScope); const result = yield this.execute(node.body, subScope, nextTrace); if (result instanceof flag_1.Flag) { return result.getValue(); } }); const sandFunction = sand_function_1.SandFunction.create(func); return sandFunction; }); }; exports.functionExpressionEvaluator = functionExpressionEvaluator;