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 in JavaScript

  • 1.4.0
  • Source
  • npm
  • Socket score

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

eval5

GitHub license npm npm bundle size

A JavaScript interpreter written in JavaScript.

Try it out

You may not need it unless

  • Need to execute code in the browser with a sandbox environment
  • Controlling execution time
  • JavaScript runtime environment that does not support eval and Function. for example: WeChat Mini Program demo
  • Be interested or Be curious

Support

ECMA5

Install

npm install --save eval5

Usage

import { Interpreter } from "eval5";

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

let result;

try {
	result = interpreter.evaluate("1+1");
	console.log(result);

	interpreter.evaluate("var a=100");
	interpreter.evaluate("var b=200");
	result = interpreter.evaluate("a+b");

	console.log(result);
} catch (e) {
	console.log(e);
}

Options

interface Options {
	timeout?: number;
	rootContext?: {} | null;
	globalContextInFunction?: any;
}

Interpreter

version

current version

global

default: {}

global context

Interpreter.global = window;
const interpreter = new Interpreter();

globalContextInFunction

default: undefined

eval5 does not support use strict mode, but the default value of this in function calls is undefined, you can set this property as the default.

import { Interpreter } from "Interpreter";

const ctx = {};
const interpreter = new Interpreter(ctx);
interpreter.evaluate(`
this; // ctx
function func(){
    return this; // undefined
}
func();
`);
import { Interpreter } from "Interpreter";

Interpreter.globalContextInFunction = window;
const ctx = {};
const interpreter = new Interpreter({});
interpreter.evaluate(`
this; // ctx
function func(){
    return this; // window
}
func();
`);

Note: Illegal invocation

e.g.

import { Interpreter } from "Interpreter";

Interpreter.globalContextInFunction = {};

const ctx = {alert: alert};

const interpreter = new Interpreter(ctx);

interpreter.evaluate(`
// alert.call({}, 'Hello eval5')
// Illegal invocation
alert('Hello eval5');
`);

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

Instance methods

evaluate(code: string): any

executes string code and returns the value of the last expression

import { Interpreter } from "Interpreter";

const interpreter = new Interpreter(window);

const result = interpreter.evaluate(`
var a = 100;
var b = 200;

a+b;

`);

console.log(result); // 300

appendCode(code: string): any

alias of evaluate

getExecutionTime(): number

get the last execution time

setExecTimeout(): number

set the timeout for each execution

getOptions(): Readonly<Options>

get interpreter options

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

executes string code and returns the value of the last expression

note: a new interpreter is created with every execution

import { evaluate } from "eval5";

evaluate(
	`
var a = 100;
var b = 100;
console.log(a+b);
`,
	{ console: console }
); // 200

evaluate(`
    a;
`); // a is not defined

Function

use Interpreter.global as the default context, Interpreter.globalContextInFunction also

import { Function } from "eval5";

const func = new Function("a", "b", "return a+b;");
console.log(func(100, 200)); // 300

vm

see vm

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

License

MIT

Keywords

FAQs

Package last updated on 15 Mar 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