Gathering detailed insights and metrics for l7eval5
Gathering detailed insights and metrics for l7eval5
npm install l7eval5
Typescript
Module System
Node Version
NPM Version
91.4
Supply Chain
98.5
Quality
75
Maintenance
100
Vulnerability
100
License
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
4,219,860
Last Day
1,876
Last Week
9,771
Last Month
41,898
Last Year
660,585
Minified
Minified + Gzipped
Latest Version
0.0.3
Package Id
l7eval5@0.0.3
Unpacked Size
1.03 MB
Size
240.94 kB
File Count
40
NPM Version
8.1.0
Node Version
14.16.1
Published on
Nov 09, 2021
Cumulative downloads
Total Downloads
Last Day
11.6%
1,876
Compared to previous day
Last Week
1.4%
9,771
Compared to previous week
Last Month
4%
41,898
Compared to previous month
Last Year
-64.2%
660,585
Compared to previous year
中文 | English
基于 TypeScript 编写的 JavaScript 解释器,支持完整 ES5 语法
支持浏览器、node.js、小程序等 JavaScript 运行环境
eval
Function
的 JavaScript 运行环境:如 微信小程序 demo we-script taro-scriptES5
npm install --save eval5
1import { Interpreter } from "eval5"; 2 3const interpreter = new Interpreter(window, { 4 timeout: 1000, 5}); 6 7let result; 8 9try { 10 result = interpreter.evaluate("1+1"); 11 console.log(result); 12 13 interpreter.evaluate("var a=100"); 14 interpreter.evaluate("var b=200"); 15 result = interpreter.evaluate("a+b"); 16 17 console.log(result); 18} catch (e) { 19 console.log(e); 20}
1interface Options { 2 // 默认为:0,不限制 3 timeout?: number; 4 // 根作用域,只读 5 rootContext?: {} | null; 6 globalContextInFunction?: any; 7}
示例
import { Interpreter } from "eval5";
const ctx = {};
const interpreter = new Interpreter(ctx, {
rootContext: window,
timeout: 1000,
});
interpreter.evaluate(`
a = 100;
console.log(a); // 100
`);
window.a;//undefined
version
当前版本
global
默认值: {}
设置默认的全局作用域
1Interpreter.global = window; 2const interpreter = new Interpreter(); 3interpreter.evaluate('alert("hello eval5")');
globalContextInFunction
默认值: undefined
eval5
不支持 use strict
严格模式, 在非严格下的函数中this
默认指向的是全局作用域,但在eval5
中是undefined
, 可通过globalContextInFunction
来设置默认指向。
1import { Interpreter } from "Interpreter"; 2 3const ctx = {}; 4const interpreter = new Interpreter(ctx); 5interpreter.evaluate(` 6this; // ctx 7function func(){ 8 return this; // undefined 9} 10func(); 11`);
1import { Interpreter } from "Interpreter"; 2 3Interpreter.globalContextInFunction = window; 4const ctx = {}; 5const interpreter = new Interpreter({}); 6interpreter.evaluate(` 7this; // ctx 8function func(){ 9 return this; // window 10} 11func(); 12`);
原因,示例代码:
注意: alert异常
import { Interpreter } from "Interpreter";
Interpreter.globalContextInFunction = {};
const ctx = {alert: alert};
const interpreter = new Interpreter(ctx);
interpreter.evaluate(`
// throw Illegal invocation
alert('Hello eval5'); // 同 alert.call({}, 'Hello eval5')
`);
constructor(context = Interpreter.global, options?: Options )
构造函数
evaluate(code: string): any
执行给定的字符串代码,并返回最后一个表达式的值
1import { Interpreter } from "Interpreter"; 2 3const interpreter = new Interpreter(window); 4 5const result = interpreter.evaluate(` 6var a = 100; 7var b = 200; 8 9a+b; 10 11`); 12 13console.log(result); // 300
appendCode(code: string): any
evaluate
的别名
getExecutionTime(): number
获取上一次调用evaluate
的执行时长
setExecTimeout(timeout: number = 0): void
设置执行时长
getOptions(): Readonly<Options>
获取解释器参数
执行给定的字符串代码,并返回最后一个表达式的值
注: 该函数每次执行都会创建一个新的解释器
1import { evaluate } from "eval5"; 2 3evaluate( 4 ` 5var a = 100; 6var b = 100; 7console.log(a+b); 8`, 9 { console: console } 10); // 200 11 12evaluate(` 13 a; 14`); // a is not defined
该函数会将Interpreter.global
Interpreter.globalContextInFunction
当作默认值并创建新的解释器
1import { Function } from "eval5"; 2 3const func = new Function("a", "b", "return a+b;"); 4console.log(func(100, 200)); // 300
查看 vm
MIT
No vulnerabilities found.
No security vulnerabilities found.