UNPKG

@sudoo/marked

Version:

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

73 lines (72 loc) 4.05 kB
"use strict"; /** * @author WMXPY * @namespace Evaluate * @description Update 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.updateExpressionEvaluator = exports.mountUpdateExpression = void 0; const error_code_1 = require("../declare/error-code"); const update_1 = require("../operation/update"); const extract_object_1 = require("../operation/update-expression/extract-object"); const assert_1 = require("../util/error/assert"); const error_1 = require("../util/error/error"); const sand_list_1 = require("../variable/sand-list"); const mountUpdateExpression = (sandbox) => { sandbox.mount("UpdateExpression", exports.updateExpressionEvaluator); }; exports.mountUpdateExpression = mountUpdateExpression; const updateExpressionEvaluator = function (node, scope, trace) { return __awaiter(this, void 0, void 0, function* () { const nextTrace = trace.stack(node); const operation = (0, update_1.getUpdateOperation)(node.operator); if (!operation) { throw (0, error_1.error)(error_code_1.ERROR_CODE.UNARY_NOT_SUPPORT, node.operator, node, trace); } if (node.argument.type === "Identifier") { const identifierVariable = scope.rummage(node.argument.name); if (!identifierVariable) { throw (0, error_1.error)(error_code_1.ERROR_CODE.VARIABLE_IS_NOT_DEFINED, node.argument.name, node, trace); } const current = yield this.execute(node.argument, scope, nextTrace); const result = operation(current); identifierVariable.set(result); return node.prefix ? result : current; } else if (node.argument.type === "MemberExpression") { const argument = node.argument; const preExtractObject = yield this.execute(argument.object, scope, nextTrace); const member = argument.computed ? yield this.execute(argument.property, scope, nextTrace) : argument.property.name; const object = (0, extract_object_1.extractObjectForUpdateExpression)(preExtractObject); if (object === null) { throw (0, error_1.error)(error_code_1.ERROR_CODE.UNKNOWN_ERROR, String(preExtractObject), node, trace); } const memberVariable = object instanceof sand_list_1.SandList ? object.getRaw((0, assert_1.assert)(member).to.be.number(error_code_1.ERROR_CODE.ONLY_NUMBER_AVAILABLE_FOR_LIST).firstValue()) : object.getRaw((0, assert_1.assert)(member).to.be.string(error_code_1.ERROR_CODE.ONLY_STRING_AVAILABLE_FOR_MAP).firstValue()); const memberValue = memberVariable ? memberVariable.get() : undefined; if (typeof memberValue === "undefined" || typeof memberVariable === "undefined") { throw (0, error_1.error)(error_code_1.ERROR_CODE.MEMBER_EXPRESSION_VALUE_CANNOT_BE_UNDEFINED, memberValue, node, trace); } const result = operation(memberValue); memberVariable.set(result); return node.prefix ? result : memberValue; } throw (0, error_1.error)(error_code_1.ERROR_CODE.INTERNAL_ERROR, void 0, node, trace); }); }; exports.updateExpressionEvaluator = updateExpressionEvaluator;