eval5是完全基于JavaScript编写的JavaScript解释器,支持ECMA5语法。常用于不支持 eval/Function 的JavaScript运行环境,例如:微信小程序
更新内容:
- 新增 Interprete.ecmaVersion属性,设置要解析的ECMA版本,默认为:5,不建议修改!如果设置的数值为6以上的版本可能导致解释出错或得到非预期结果,例如,设Interprete.ecmaVersion为6,虽然能解析,eval5会忽略let声明,当作var用:
...
for(let i = 0; i < 10; i++) {
btn[i].onclick = function(){
console.log(i); //始终输出 10
}
}
...
- evaluate、vm.compileFunction及vm.runInContext的options新增ecmaVersion参数,默认:Interprete.ecmaVersion
- 超时机制timeout调整,constructor、evaluate、vm.compileFunction及vm.runInContext的options都支持timeout设置,运行被解析的代码执行的最大毫秒数,默认为:0,不限制。
import {Interpreter} from 'eval5';
const interpreter = new Interpreter(window, { timeout: 500 });
try {
interpreter.evaluate('for(;;);');
} catch(e) {
// Script execution timed out after 500ms
}