@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
53 lines (52 loc) • 2.76 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Evaluate
* @description Export Named 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.exportNamedDeclarationEvaluator = exports.mountExportNamedDeclaration = void 0;
const error_code_1 = require("../declare/error-code");
const declare_variable_stack_1 = require("../operation/variable-declaration/declare-variable-stack");
const error_1 = require("../util/error/error");
const mountExportNamedDeclaration = (sandbox) => {
sandbox.mount("ExportNamedDeclaration", exports.exportNamedDeclarationEvaluator);
};
exports.mountExportNamedDeclaration = mountExportNamedDeclaration;
const exportNamedDeclarationEvaluator = function (node, scope, trace) {
return __awaiter(this, void 0, void 0, function* () {
const nextTrace = trace.stack(node);
if (node.declaration) { // Node has a declaration
if (node.declaration.type === "VariableDeclaration") {
const bindDeclareVariableStack = declare_variable_stack_1.declareVariableStack.bind(this);
const declareResults = yield bindDeclareVariableStack(node.declaration, scope, trace, nextTrace);
for (const result of declareResults) {
scope.expose(result.id, result.value, trace);
}
}
else {
throw (0, error_1.error)(error_code_1.ERROR_CODE.BESIDES_DECLARATION_NOT_SUPPORT, node.declaration.type, node, nextTrace);
}
}
else { // Node have no declaration
for (const specifier of node.specifiers) {
const exported = specifier.exported.type === "Literal"
? yield this.execute(specifier.exported, scope, nextTrace)
: specifier.exported.name;
const value = yield this.execute(specifier.local, scope, nextTrace);
scope.expose(exported, value, trace);
}
}
return;
});
};
exports.exportNamedDeclarationEvaluator = exportNamedDeclarationEvaluator;