eval5 is a JavaScript interpreter written in TypeScript, designed to execute full ES5 syntax within various JavaScript environments including browsers, Node.js, and mini-programs like WeChat and Taro. Currently at version 1.4.8, the project maintains an active release cadence, primarily focusing on bug fixes and minor improvements. Its key differentiators include providing a sandboxed execution environment, allowing control over script execution time, and enabling JavaScript execution in environments where native `eval` or `Function` constructors are unavailable or restricted. It's particularly useful for scenarios requiring isolated script execution or educational purposes, strictly adhering to the ES5 specification without support for newer ECMAScript features.
npm install eval5Verified import paths — ran on the pinned version, not inferred.
Demonstrates how to create an `Interpreter` instance with a custom global context and options, then execute multiple JavaScript code snippets sequentially, maintaining state across calls and logging results.
Use `globalContextInFunction` option in the `Interpreter` constructor instead of `rootContext` to control the 'this' context within functions.
Set the `globalContextInFunction` option in the `Interpreter` constructor or globally via `Interpreter.globalContextInFunction` to the desired global object (e.g., `window` or `globalThis`) to control `this` behavior in functions.
Bind host functions explicitly when passing them to the interpreter's context. For example: `const ctx = { alert: alert.bind(window) };` or `const ctx = { console: { log: console.log.bind(console) } };`.Ensure that all JavaScript code provided to `eval5` is transpiled down to ES5 syntax using tools like Babel if it originates from a newer ECMAScript version.
If you need shared state across multiple code snippets, use an `Interpreter` instance. If using the standalone `evaluate` function, pass all necessary variables and functions into its `ctx` parameter for each invocation.
Bind the host function to its original object before passing it into the interpreter's context. Example: `new Interpreter({ alert: window.alert.bind(window) })`.Configure the `globalContextInFunction` option in the `Interpreter` constructor or globally via `Interpreter.globalContextInFunction` to set the desired `this` context for functions.
No dependency data recorded yet.