npm

Join us for the upcoming webinar, "Enterprise JavaScript done right: the tools you love, the security you need."Sign up here »

sval

0.4.6 • Public • Published

Sval

npm travis-ci coveralls

A JavaScript interpreter writen in JavaScript, based on parser Acorn.

  • Running on ES5, supporting ES5~10 full features
  • Both invasived and sandbox modes available

It's useful to evaluate the code of higher ECMAScript editions, or for the environment with disabled eval, setTimeout and new Function.

Try Sval on the playground.

Installation

Node

Install Sval with npm.

npm install sval

Browser

Simply source from unpkg. Or, download from releases, get minimized file dist/min/sval.min.js, and source at your html page. You can access a global variable Sval directly.

<script type="text/javascript" src="https://unpkg.com/sval"></script>

Usage

import Sval from 'sval'
 
// Sval options
const options = {
  // ECMA Version of the code (5 | 6 | 7 | 8 | 9 | 10 | 2015 | 2016 | 2017 | 2018 | 2019)
  ecmaVer: 9,
  // Whether the code runs in a sandbox
  sandBox: true,
}
 
// Create a interpreter
const interpreter = new Sval(options)
 
// Add global modules in interpreter
interpreter.import('importWhatYouNeed', 'AllKindsOfStuffs')
// Or interpreter.import({ importWhatYouNeed: 'AllKindsOfStuffs' })
 
// Parse and run the code
interpreter.run(`
  const msg = 'Hello World'
  exports.msg = msg // Export any you want
`)
 
interpreter.run(`
  exports.mod = importWhatYouNeed // Export again and merge
`)
 
// Get exports from runs
console.log(interpreter.exports.msg) // Get 'Hello World'
console.log(interpreter.exports.mod) // Get 'AllKindsOfStuffs'

Sval constructor has options with two fields, ecmaVer and sandBox.

  • ecmaVer is the ECMAScript edition of the code. Currently, 5, 6(2015), 7(2016), 8(2017), 9(2018) and 10(2019) are supported, and the default edition is 9.

  • sandBox is true for sandbox mode or false for invasived mode. Sandbox mode will run code in an isolated sandbox and won't pollute your global scope. Invasived mode allows you run code in the same global scope of your current environment. The default setting is true.

Sval instance has two methods, import and run.

  • import is to import modules into your Sval instance scope, expecting a name and a module as arguments like import(name: string, mod: any), or an object which contains the modules as argument like import({ [name: string]: any }). The modules will be automatically declared as global variables. This method is more likely to be used in sandbox mode.

  • run is to evaluate the code inputed, expecting a string as argument like run(code: string). If you want to export something, there is a internal global exports object for mounting what you want to export.

Sval instance also has a field, exports, to get what you exported from runs, merged if several runs have exports.

Note

WithStatement and LabeledStatement aren't implemented and recommended. Please avoid to use them.

Reference

License

Sval is licensed under the MIT.

install

npm i sval

Downloadsweekly downloads

47

version

0.4.6

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability