@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
46 lines (45 loc) • 2.31 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Evaluate
* @description Function Declaration
*/
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.functionDeclarationEvaluator = exports.mountFunctionDeclaration = void 0;
const error_code_1 = require("../declare/error-code");
const variable_1 = require("../declare/variable");
const error_1 = require("../util/error/error");
const function_expression_1 = require("./function-expression");
const mountFunctionDeclaration = (sandbox) => {
sandbox.mount("FunctionDeclaration", exports.functionDeclarationEvaluator);
};
exports.mountFunctionDeclaration = mountFunctionDeclaration;
const functionDeclarationEvaluator = function (node, scope, trace) {
return __awaiter(this, void 0, void 0, function* () {
const nextTrace = trace.stack(node);
const func = yield function_expression_1.functionExpressionEvaluator.bind(this)(node, scope, nextTrace);
if (!node.id) {
throw (0, error_1.error)(error_code_1.ERROR_CODE.UNKNOWN_ERROR, void 0, node, trace);
}
if (node.id.type !== "Identifier") {
throw (0, error_1.error)(error_code_1.ERROR_CODE.UNKNOWN_ERROR, void 0, node, trace);
}
if (typeof node.id.name !== "string") {
throw (0, error_1.error)(error_code_1.ERROR_CODE.UNKNOWN_ERROR, void 0, node, trace);
}
const rawName = node.id.name;
const registerer = scope.register(variable_1.VARIABLE_TYPE.CONSTANT);
registerer(rawName, func);
return func;
});
};
exports.functionDeclarationEvaluator = functionDeclarationEvaluator;