npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

evaljs

v2.0.1

Published

A JavaScript interpreter written in JavaScript

Downloads

154

Readme

eval.js

Build Status Dependency Status devDependency Status

A JavaScript interpreter written in JavaScript.

Why?

You might be working in a JavaScript environment where eval() isn't allowed (and you have a genuinely good reason why you want to use it). Maybe this'll slip under the radar. You could also extend this to make it execute ES6 code in an ES5 environment. PRs welcome!

How?

Most of the heavy lifting is done by acorn, a JavaScript parser written in JavaScript. eval.js converts the AST it generates into JavaScript function closures, which when run execute the whole program.

It's also possible to use eval.js with esprima.

Command line interface

This npm package comes with a REPL which allows you to experiment with it. It's easy to install and use:

marten@procyon:~/git/evaljs$ npm install -g evaljs
marten@procyon:~/git/evaljs$ evaljs
> 1 + 1
2
> new Error('Hello World!')
[Error: Hello World!]
> throw new Error('Hello World!')
Error: Hello World!
    at newWithArgs (/home/marten/git/evaljs/index.js:255:10)
    at /home/marten/git/evaljs/index.js:249:12
    at Array.0 (/home/marten/git/evaljs/index.js:581:11)
    at /home/marten/git/evaljs/index.js:466:31
    at REPLServer.repl.start.eval (/home/marten/git/evaljs/bin/evaljs:12:34)
    at repl.js:249:20
    at REPLServer.repl.start.eval (/home/marten/git/evaljs/bin/evaljs:14:7)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
> marten@procyon:~/git/evaljs$

API

  • evaljs.evaluate(code) A drop in alternative for window.eval().

  • new evaljs.Environment([scopesOrGlobalObject]) Generates a new JS Environment to 'run' code in. The argument can be one of the following:

    • a global object
    • nothing (in this case, '{}' is used as the global object)
    • a list of objects. The first will be the global object, others will be other scopes loaded into the interpreter. Kind of like wrapping the code in a with statement for each further object in the array. This is handy for emulating Node.js (for passing in require(), exports, and module.)

    A JS Environment has the following properties:

    • env.gen(node): Takes either the result of acorn's parse() method (an AST), or a JS string containing source code. This AST/code will be converted into a function that, when run, executes the AST/code passed in and returns the result.
    • env.DEBUG: When set to true, evaljs will write debug information to stdout.

Size?

16.3kB min+gzip

License?

ISC

Is it complete?

No labeled statements; no nice error handling (although there is a DEBUG option). There are probably bugs. That said, it can run itself and acorn, so its supported subset of JS is usable. PRs containing improvements welcome!

How slow is it?

Not sure. I only tested with small snippets so far in Node.js, for which the speed difference isn't notable. But it's probably slow.

Who?

eval.js is written by Marten de Vries. Maintained by Jason Huggins. Credits for the original idea go to closure-interpreter.