UNPKG

@sudoo/marked

Version:

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

81 lines (80 loc) 4.61 kB
"use strict"; /** * @author WMXPY * @namespace Evaluate * @description Assignment 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.assignmentExpressionEvaluator = exports.mountAssignmentExpression = void 0; const error_code_1 = require("../declare/error-code"); const assignment_1 = require("../operation/assignment"); 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 sand_map_1 = require("../variable/sand-map"); const mountAssignmentExpression = (sandbox) => { sandbox.mount("AssignmentExpression", exports.assignmentExpressionEvaluator); }; exports.mountAssignmentExpression = mountAssignmentExpression; const assignmentExpressionEvaluator = function (node, scope, trace) { return __awaiter(this, void 0, void 0, function* () { const nextTrace = trace.stack(node); const variable = yield (() => __awaiter(this, void 0, void 0, function* () { if (node.left.type === "Identifier") { const name = node.left.name; return (0, assert_1.assert)(scope.validateEditable(name).rummage(name)).to.be.exist(error_code_1.ERROR_CODE.VARIABLE_IS_NOT_DEFINED).firstValue(); } else if (node.left.type === "MemberExpression") { const preExtractObject = yield this.execute(node.left.object, scope, nextTrace); const member = node.left.computed ? yield this.execute(node.left.property, scope, nextTrace) : node.left.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()); if (memberVariable) { return memberVariable; } else { if (object instanceof sand_map_1.SandMap) { const securedMember = (0, assert_1.assert)(member).to.be.string(error_code_1.ERROR_CODE.ONLY_STRING_AVAILABLE_FOR_MAP).firstValue(); object.set(securedMember, undefined); const fetted = object.getRaw(securedMember); if (!fetted) { throw (0, error_1.error)(error_code_1.ERROR_CODE.UNKNOWN_ERROR, fetted.toString(), node, trace); } return fetted; } else { throw (0, error_1.error)(error_code_1.ERROR_CODE.MEMBER_EXPRESSION_VALUE_CANNOT_BE_UNDEFINED, memberVariable, node, trace); } } } else { throw (0, error_1.error)(error_code_1.ERROR_CODE.INTERNAL_ERROR, void 0, node, trace); } }))(); const operation = (0, assignment_1.getAssignmentOperation)(node.operator); if (!operation) { throw (0, error_1.error)(error_code_1.ERROR_CODE.ASSIGNMENT_NOT_SUPPORT, node.operator, node, trace); } const assignee = yield this.execute(node.right, scope, nextTrace); operation(variable, assignee); return assignee; }); }; exports.assignmentExpressionEvaluator = assignmentExpressionEvaluator;