Sval
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
.
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.
Usage
// Sval optionsconst 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 interpreterconst interpreter = options // Add global modules in interpreterinterpreter// Or interpreter.import({ importWhatYouNeed: 'AllKindsOfStuffs' }) // Parse and run the codeinterpreter interpreter // Get exports from runsconsole // Get 'Hello World'console // 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 likeimport({ [name: string]: any })
. The modules will be automatically declared as global variables. This method is more likely to be used in sandbox mode. -
parse is to parse the code with internal Acorn or custom parser, to get the corresponding AST, like
parse(code: string)
orparse(code: string, parser: (code: string, options: SvalOptions) => estree.Node
-
run is to evaluate the code inputed, expecting a string as argument like
run(code: string)
, or an AST followed ESTree Spec as argument likerun(ast: estree.Node)
. If you want to export something, there is a internal globalexports
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.