Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eval5

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eval5

A JavaScript interpreter, written completely in JavaScript

  • 1.0.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
841
increased by41.34%
Maintainers
1
Weekly downloads
 
Created
Source

eval5

基于 JavaScript 编写的 JavaScript 解释器;A JavaScript interpreter, written completely in JavaScript;

用于解决在不支持evalFunction的执行环境下执行 JavaScript 代码。例如:各种小程序。

Usage

npm install --save eval5

import { evaluate, Function, vm, Interpreter } from 'eval5';

// 设置默认作用域
Interpreter.global = window;

//或 evaluate("1+1", Object.create(window));
evaluate("1+1", window); // 2

const func = new Function('a','b', 'return a+b;');

console.log(func(1,1)); // 2

const interpreter = new Interpreter(ctx, {
    timeout: 1000
});

let result;

try {
    result = interpreter.evaluate('1+1');
    console.log(result) //2
} catch(e) {
    //..
}

Interpreter

static global

设置默认作用域对象

例如:

Interpreter.global = window;

constructor(ctx: {}, options?: { timeout?: number})

构造函数

var interpreter = new Interpreter(window);

evaluate(code: string, ctx?: {}): any

返回脚本中执行的最后一个表达式结果

var interpreter = new Interpreter(window);
interpreter.evaluate('alert(1+1)')

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

执行给定的字符串脚本,返回脚本中执行的最后一个表达式结果

evaluate('console.log(1+1)', {console: console})

Function

同 js 原生的 Function

const func = new Function('a','b', 'return a+b;');
console.log(func(1,2))

vm

参考 node.js vm

支持 api 列表:

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

License

ISC

Support

  • ECMA5

evaljs closure-interpreter

Keywords

FAQs

Package last updated on 23 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc