项目作者: bplok20010

项目描述 :
A JavaScript interpreter written in TypeScript;基于TypeScript编写的JavaScript解释器
高级语言: JavaScript
项目地址: git://github.com/bplok20010/eval5.git


eval5

中文 | English

GitHub license
npm
npm bundle size

基于 TypeScript 编写的 JavaScript 解释器,支持完整 ES5 语法

支持浏览器、node.js、小程序等 JavaScript 运行环境

在线体验

更多示例

使用场景

  • 浏览器环境中需要使用沙盒环境执行 JavaScript 脚本
  • 控制执行时长
  • 不支持eval Function的 JavaScript 运行环境:如 微信小程序 demo we-script taro-script
  • 研究/学习用

支持 ECMAScript 版本

ES5

安装

  1. npm install --save eval5

使用

Edit eval5

  1. import { Interpreter } from "eval5";
  2. const interpreter = new Interpreter(window, {
  3. timeout: 1000,
  4. });
  5. let result;
  6. try {
  7. result = interpreter.evaluate("1+1");
  8. console.log(result);
  9. interpreter.evaluate("var a=100");
  10. interpreter.evaluate("var b=200");
  11. result = interpreter.evaluate("a+b");
  12. console.log(result);
  13. } catch (e) {
  14. console.log(e);
  15. }

参数

  1. interface Options {
  2. // 默认为:0,不限制
  3. timeout?: number;
  4. // 根作用域,只读
  5. rootContext?: {} | null;
  6. globalContextInFunction?: any;
  7. }

示例

  1. import { Interpreter } from "eval5";
  2. const ctx = {};
  3. const interpreter = new Interpreter(ctx, {
  4. rootContext: window,
  5. timeout: 1000,
  6. });
  7. interpreter.evaluate(`
  8. a = 100;
  9. console.log(a); // 100
  10. `);
  11. window.a;//undefined

Interpreter

version

当前版本

global

默认值: {}

设置默认的全局作用域

  1. Interpreter.global = window;
  2. const interpreter = new Interpreter();
  3. interpreter.evaluate('alert("hello eval5")');

globalContextInFunction

默认值: undefined

eval5 不支持 use strict 严格模式, 在非严格下的函数中this默认指向的是全局作用域,但在eval5中是undefined, 可通过globalContextInFunction来设置默认指向。

  1. import { Interpreter } from "Interpreter";
  2. const ctx = {};
  3. const interpreter = new Interpreter(ctx);
  4. interpreter.evaluate(`
  5. this; // ctx
  6. function func(){
  7. return this; // undefined
  8. }
  9. func();
  10. `);
  1. import { Interpreter } from "Interpreter";
  2. Interpreter.globalContextInFunction = window;
  3. const ctx = {};
  4. const interpreter = new Interpreter({});
  5. interpreter.evaluate(`
  6. this; // ctx
  7. function func(){
  8. return this; // window
  9. }
  10. func();
  11. `);

原因,示例代码:

注意: alert异常

  1. import { Interpreter } from "Interpreter";
  2. Interpreter.globalContextInFunction = {};
  3. const ctx = {alert: alert};
  4. const interpreter = new Interpreter(ctx);
  5. interpreter.evaluate(`
  6. // throw Illegal invocation
  7. alert('Hello eval5'); // 同 alert.call({}, 'Hello eval5')
  8. `);

constructor(context = Interpreter.global, options?: Options )

构造函数

Interpreter 的实例方法

evaluate(code: string): any

执行给定的字符串代码,并返回最后一个表达式的值

  1. import { Interpreter } from "Interpreter";
  2. const interpreter = new Interpreter(window);
  3. const result = interpreter.evaluate(`
  4. var a = 100;
  5. var b = 200;
  6. a+b;
  7. `);
  8. console.log(result); // 300

appendCode(code: string): any

evaluate的别名

getExecutionTime(): number

获取上一次调用evaluate的执行时长

setExecTimeout(timeout: number = 0): void

设置执行时长

getOptions(): Readonly<Options>

获取解释器参数


evaluate(code: string, ctx?: {}, options?: Options)

执行给定的字符串代码,并返回最后一个表达式的值

注: 该函数每次执行都会创建一个新的解释器

  1. import { evaluate } from "eval5";
  2. evaluate(
  3. `
  4. var a = 100;
  5. var b = 100;
  6. console.log(a+b);
  7. `,
  8. { console: console }
  9. ); // 200
  10. evaluate(`
  11. a;
  12. `); // a is not defined

Function

该函数会将Interpreter.global Interpreter.globalContextInFunction当作默认值并创建新的解释器

  1. import { Function } from "eval5";
  2. const func = new Function("a", "b", "return a+b;");
  3. console.log(func(100, 200)); // 300

vm

查看 vm

  • vm.createContext
  • vm.compileFunction
  • vm.runInContext
  • vm.runInNewContext
  • vm.Script

License

MIT

相关