Comparing version 1.2.0 to 1.3.0
@@ -10,6 +10,6 @@ "use strict"; | ||
var _default = function _default(code, ctx) { | ||
return (0, _vm.runInContext)(code, ctx); | ||
var _default = function _default(code, ctx, options) { | ||
return (0, _vm.runInContext)(code, ctx, options); | ||
}; | ||
exports.default = _default; |
@@ -16,3 +16,3 @@ "use strict"; | ||
var version = "1.2.0"; | ||
var version = "1.3.0"; | ||
@@ -154,3 +154,5 @@ function defineFunctionName(func, name) { | ||
this.lastExecNode = null; | ||
this.isRunning = false; | ||
this.options = { | ||
ecmaVersion: options.ecmaVersion || Interpreter.ecmaVersion, | ||
timeout: options.timeout || 0, | ||
@@ -288,3 +290,4 @@ initEnv: options.initEnv | ||
ranges: true, | ||
locations: true | ||
locations: true, | ||
ecmaVersion: this.options.ecmaVersion || Interpreter.ecmaVersion | ||
}); | ||
@@ -305,2 +308,3 @@ return this.evaluateNode(node, code); | ||
this.sourceList.push(source); | ||
this.isRunning = true; | ||
var bodyClosure = this.createClosure(node); // add declares to data | ||
@@ -321,2 +325,3 @@ | ||
this.isRunning = false; | ||
return this.getValue(); | ||
@@ -331,3 +336,7 @@ }; | ||
var message = msg[1].replace("%0", String(value)); | ||
message += this.getNodePosition(node || this.lastExecNode); | ||
if (node !== null) { | ||
message += this.getNodePosition(node || this.lastExecNode); | ||
} | ||
return message; | ||
@@ -357,2 +366,3 @@ }; | ||
_proto.checkTimeout = function checkTimeout() { | ||
if (!this.isRunning) return false; | ||
var timeout = this.options.timeout || 0; | ||
@@ -533,3 +543,3 @@ var now = Date.now(); | ||
if (timeout && timeout > 0 && _this2.checkTimeout()) { | ||
throw _this2.createInternalThrowError(_messages.Messages.ExecutionTimeOutError, timeout, node); | ||
throw _this2.createInternalThrowError(_messages.Messages.ExecutionTimeOutError, timeout, null); | ||
} | ||
@@ -1903,3 +1913,4 @@ | ||
Interpreter.eval = IEval; | ||
Interpreter.Function = IFunction; // alert.call(rootContext, 1); | ||
Interpreter.Function = IFunction; | ||
Interpreter.ecmaVersion = 5; // alert.call(rootContext, 1); | ||
// But alert({}, 1); // Illegal invocation | ||
@@ -1906,0 +1917,0 @@ |
@@ -13,3 +13,4 @@ "use strict"; | ||
// class Context {} | ||
// TODO: | ||
// add tests | ||
function createContext(ctx) { | ||
@@ -36,6 +37,6 @@ if (ctx === void 0) { | ||
var interpreter = new _main.Interpreter(ctx, { | ||
ecmaVersion: options.ecmaVersion, | ||
timeout: timeout | ||
}); | ||
interpreter.evaluate(wrapCode); | ||
return interpreter.getValue(); | ||
return interpreter.evaluate(wrapCode); | ||
} | ||
@@ -45,4 +46,3 @@ | ||
var interpreter = new _main.Interpreter(ctx, options); | ||
interpreter.evaluate(code); | ||
return interpreter.getValue(); | ||
return interpreter.evaluate(code); | ||
} | ||
@@ -49,0 +49,0 @@ |
import { runInContext } from "./vm"; | ||
export default (function (code, ctx) { | ||
return runInContext(code, ctx); | ||
export default (function (code, ctx, options) { | ||
return runInContext(code, ctx, options); | ||
}); |
import _construct from "@babel/runtime/helpers/construct"; | ||
import { parse } from "acorn"; | ||
import { Messages, InterruptThrowError, InterruptThrowReferenceError, InterruptThrowSyntaxError } from "./messages"; | ||
var version = "1.2.0"; | ||
var version = "1.3.0"; | ||
@@ -141,3 +141,5 @@ function defineFunctionName(func, name) { | ||
this.lastExecNode = null; | ||
this.isRunning = false; | ||
this.options = { | ||
ecmaVersion: options.ecmaVersion || Interpreter.ecmaVersion, | ||
timeout: options.timeout || 0, | ||
@@ -275,3 +277,4 @@ initEnv: options.initEnv | ||
ranges: true, | ||
locations: true | ||
locations: true, | ||
ecmaVersion: this.options.ecmaVersion || Interpreter.ecmaVersion | ||
}); | ||
@@ -292,2 +295,3 @@ return this.evaluateNode(node, code); | ||
this.sourceList.push(source); | ||
this.isRunning = true; | ||
var bodyClosure = this.createClosure(node); // add declares to data | ||
@@ -308,2 +312,3 @@ | ||
this.isRunning = false; | ||
return this.getValue(); | ||
@@ -318,3 +323,7 @@ }; | ||
var message = msg[1].replace("%0", String(value)); | ||
message += this.getNodePosition(node || this.lastExecNode); | ||
if (node !== null) { | ||
message += this.getNodePosition(node || this.lastExecNode); | ||
} | ||
return message; | ||
@@ -344,2 +353,3 @@ }; | ||
_proto.checkTimeout = function checkTimeout() { | ||
if (!this.isRunning) return false; | ||
var timeout = this.options.timeout || 0; | ||
@@ -520,3 +530,3 @@ var now = Date.now(); | ||
if (timeout && timeout > 0 && _this2.checkTimeout()) { | ||
throw _this2.createInternalThrowError(Messages.ExecutionTimeOutError, timeout, node); | ||
throw _this2.createInternalThrowError(Messages.ExecutionTimeOutError, timeout, null); | ||
} | ||
@@ -1888,3 +1898,4 @@ | ||
Interpreter.eval = IEval; | ||
Interpreter.Function = IFunction; // alert.call(rootContext, 1); | ||
Interpreter.Function = IFunction; | ||
Interpreter.ecmaVersion = 5; // alert.call(rootContext, 1); | ||
// But alert({}, 1); // Illegal invocation | ||
@@ -1891,0 +1902,0 @@ |
@@ -1,2 +0,3 @@ | ||
import { Interpreter } from "./interpreter/main"; // class Context {} | ||
import { Interpreter } from "./interpreter/main"; // TODO: | ||
// add tests | ||
@@ -23,6 +24,6 @@ export function createContext(ctx) { | ||
var interpreter = new Interpreter(ctx, { | ||
ecmaVersion: options.ecmaVersion, | ||
timeout: timeout | ||
}); | ||
interpreter.evaluate(wrapCode); | ||
return interpreter.getValue(); | ||
return interpreter.evaluate(wrapCode); | ||
} | ||
@@ -32,4 +33,3 @@ | ||
var interpreter = new Interpreter(ctx, options); | ||
interpreter.evaluate(code); | ||
return interpreter.getValue(); | ||
return interpreter.evaluate(code); | ||
} | ||
@@ -36,0 +36,0 @@ |
@@ -1,3 +0,3 @@ | ||
import { VMContext } from "./types"; | ||
declare const _default: (code: string, ctx?: VMContext | undefined) => any; | ||
import { VMContext, ScriptOptions } from "./types"; | ||
declare const _default: (code: string, ctx?: VMContext | undefined, options?: ScriptOptions | undefined) => any; | ||
export default _default; |
import { runInContext } from "./vm"; | ||
export default (code, ctx) => { | ||
return runInContext(code, ctx); | ||
export default (code, ctx, options) => { | ||
return runInContext(code, ctx, options); | ||
}; |
@@ -16,3 +16,5 @@ import { MessageItem } from "./messages"; | ||
declare type ReturnStringClosure = () => string; | ||
declare type ECMA_VERSION = 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020; | ||
interface Options { | ||
ecmaVersion?: ECMA_VERSION; | ||
timeout?: number; | ||
@@ -56,7 +58,9 @@ initEnv?: (inst: Interpreter) => void; | ||
protected lastExecNode: Node | null; | ||
protected isRunning: boolean; | ||
protected execStartTime: number; | ||
protected execEndTime: number; | ||
static readonly version = "1.2.0"; | ||
static readonly version = "1.3.0"; | ||
static readonly eval: symbol; | ||
static readonly Function: symbol; | ||
static ecmaVersion: ECMA_VERSION; | ||
static rootContext: undefined; | ||
@@ -74,3 +78,3 @@ static global: any; | ||
getExecutionTime(): number; | ||
createErrorMessage(msg: MessageItem, value: string | number, node?: Node): string; | ||
createErrorMessage(msg: MessageItem, value: string | number, node?: Node | null): string; | ||
createError<T>(message: string, error: { | ||
@@ -82,3 +86,3 @@ new (msg: string): T; | ||
}): T; | ||
createInternalThrowError<T extends MessageItem>(msg: T, value: string | number, node?: Node): Error; | ||
createInternalThrowError<T extends MessageItem>(msg: T, value: string | number, node?: Node | null): Error; | ||
setExecTimeout(timeout?: number): void; | ||
@@ -85,0 +89,0 @@ protected checkTimeout(): boolean; |
import { parse } from "acorn"; | ||
import { Messages, InterruptThrowError, InterruptThrowReferenceError, InterruptThrowSyntaxError, } from "./messages"; | ||
const version = "1.2.0"; | ||
const version = "1.3.0"; | ||
function defineFunctionName(func, name) { | ||
@@ -111,3 +111,5 @@ Object.defineProperty(func, "name", { | ||
this.lastExecNode = null; | ||
this.isRunning = false; | ||
this.options = { | ||
ecmaVersion: options.ecmaVersion || Interpreter.ecmaVersion, | ||
timeout: options.timeout || 0, | ||
@@ -226,2 +228,3 @@ initEnv: options.initEnv, | ||
locations: true, | ||
ecmaVersion: this.options.ecmaVersion || Interpreter.ecmaVersion, | ||
}); | ||
@@ -236,2 +239,3 @@ return this.evaluateNode(node, code); | ||
this.sourceList.push(source); | ||
this.isRunning = true; | ||
const bodyClosure = this.createClosure(node); | ||
@@ -253,2 +257,3 @@ // add declares to data | ||
} | ||
this.isRunning = false; | ||
return this.getValue(); | ||
@@ -261,3 +266,5 @@ } | ||
let message = msg[1].replace("%0", String(value)); | ||
message += this.getNodePosition(node || this.lastExecNode); | ||
if (node !== null) { | ||
message += this.getNodePosition(node || this.lastExecNode); | ||
} | ||
return message; | ||
@@ -278,2 +285,4 @@ } | ||
checkTimeout() { | ||
if (!this.isRunning) | ||
return false; | ||
const timeout = this.options.timeout || 0; | ||
@@ -408,3 +417,3 @@ const now = Date.now(); | ||
if (timeout && timeout > 0 && this.checkTimeout()) { | ||
throw this.createInternalThrowError(Messages.ExecutionTimeOutError, timeout, node); | ||
throw this.createInternalThrowError(Messages.ExecutionTimeOutError, timeout, null); | ||
} | ||
@@ -1481,2 +1490,3 @@ this.lastExecNode = node; | ||
Interpreter.Function = IFunction; | ||
Interpreter.ecmaVersion = 5; | ||
// alert.call(rootContext, 1); | ||
@@ -1483,0 +1493,0 @@ // But alert({}, 1); // Illegal invocation |
@@ -1,8 +0,13 @@ | ||
export declare type VMContext = {}; | ||
export declare type VMContext = { | ||
[x: string]: any; | ||
[x: number]: any; | ||
}; | ||
export interface CompileOptions { | ||
parsingContext?: VMContext; | ||
timeout?: number; | ||
ecmaVersion?: 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020; | ||
} | ||
export interface ScriptOptions { | ||
ecmaVersion?: 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020; | ||
timeout?: number; | ||
} |
import { Interpreter } from "./interpreter/main"; | ||
// class Context {} | ||
// TODO: | ||
// add tests | ||
export function createContext(ctx = Object.create(null)) { | ||
@@ -15,11 +16,10 @@ return ctx; | ||
const interpreter = new Interpreter(ctx, { | ||
ecmaVersion: options.ecmaVersion, | ||
timeout, | ||
}); | ||
interpreter.evaluate(wrapCode); | ||
return interpreter.getValue(); | ||
return interpreter.evaluate(wrapCode); | ||
} | ||
export function runInContext(code, ctx, options) { | ||
const interpreter = new Interpreter(ctx, options); | ||
interpreter.evaluate(code); | ||
return interpreter.getValue(); | ||
return interpreter.evaluate(code); | ||
} | ||
@@ -26,0 +26,0 @@ export const runInNewContext = runInContext; |
{ | ||
"name": "eval5", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "A JavaScript interpreter, written completely in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs/index.js", |
@@ -94,2 +94,27 @@ # eval5 | ||
### static `ecmaVersion` | ||
可选值: `3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020` | ||
默认: `5` | ||
> 注:eval5 只支持 es5 语法,如果将 ecmaVersion 设为高版本尽管能编译通过,但解释时可能会报错或得到错误结果。 | ||
例如,如果设`ecmaVersion=6`或更高,以下代码可以正常解析执行,但结果非预期: | ||
``` | ||
const a = []; | ||
for(let i = 0; i < 10; i++) { | ||
a.push(function(){ | ||
console.log(i); | ||
}) | ||
} | ||
... | ||
// output: 10 10 10... | ||
``` | ||
**原因在于解释器会忽略`const` `let`类型,都当作`var`处理。** | ||
### `constructor`(ctx: {}, options?: { timeout?: number}) | ||
@@ -120,2 +145,4 @@ | ||
获取`evaluate`的执行时间 | ||
## evaluate(code: string, ctx?: {}) | ||
@@ -122,0 +149,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1028062
12118
190