eval5
Version:
A JavaScript interpreter written in JavaScript
46 lines (45 loc) • 1.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Script = exports.runInNewContext = exports.runInContext = exports.compileFunction = exports.createContext = void 0;
var main_1 = require("./interpreter/main");
// TODO:
// add tests
function createContext(ctx) {
if (ctx === void 0) { ctx = Object.create(null); }
return ctx;
}
exports.createContext = createContext;
function compileFunction(code, params, options) {
if (params === void 0) { params = []; }
if (options === void 0) { options = {}; }
var ctx = options.parsingContext;
var timeout = options.timeout === undefined ? 0 : options.timeout;
var wrapCode = "\n (function anonymous(".concat(params.join(","), "){\n ").concat(code, "\n });\n ");
var interpreter = new main_1.Interpreter(ctx, {
ecmaVersion: options.ecmaVersion,
timeout: timeout,
rootContext: options.rootContext,
globalContextInFunction: options.globalContextInFunction,
});
return interpreter.evaluate(wrapCode);
}
exports.compileFunction = compileFunction;
function runInContext(code, ctx, options) {
var interpreter = new main_1.Interpreter(ctx, options);
return interpreter.evaluate(code);
}
exports.runInContext = runInContext;
exports.runInNewContext = runInContext;
var Script = /** @class */ (function () {
function Script(code) {
this._code = code;
}
Script.prototype.runInContext = function (ctx) {
return runInContext(this._code, ctx);
};
Script.prototype.runInNewContext = function (ctx) {
return runInContext(this._code, ctx);
};
return Script;
}());
exports.Script = Script;