sval 0.6.8 → 0.6.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +10 -0
- package/README.md +24 -24
- package/dist/src/share/const.d.ts +2 -0
- package/dist/sval.js +1321 -1289
- package/dist/sval.min.js +1 -1
- package/dist/sval.umd.cjs +5 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# Sval · [](https://www.npmjs.com/package/sval) [](https://coveralls.io/github/Siubaak/sval) [](https://github.com/Siubaak/sval/actions/workflows/coverage.yml) [](https://makeapullrequest.com)
|
|
2
2
|
|
|
3
|
-
A JavaScript interpreter
|
|
3
|
+
A JavaScript interpreter written in JavaScript, based on the [Acorn](https://github.com/acornjs/acorn) parser.
|
|
4
4
|
|
|
5
|
-
- **
|
|
6
|
-
- **Both
|
|
5
|
+
- **Runs on ES5, supporting the latest ES features**
|
|
6
|
+
- **Both invasive and sandbox modes are available**
|
|
7
7
|
|
|
8
|
-
It
|
|
8
|
+
It is useful for evaluating code targeting higher ECMAScript editions, or for environments where `eval`, `setTimeout`, and `new Function` are disabled.
|
|
9
9
|
|
|
10
10
|
[Try Sval on the playground.](https://jsbin.com/kehahiqono/edit?js,console)
|
|
11
11
|
|
|
@@ -21,7 +21,7 @@ npm install sval
|
|
|
21
21
|
|
|
22
22
|
### Browser
|
|
23
23
|
|
|
24
|
-
Simply
|
|
24
|
+
Simply include it from [unpkg](https://unpkg.com/sval). Alternatively, download from [releases](https://github.com/Siubaak/sval/releases), get the minified file `dist/min/sval.min.js`, and include it in your HTML page. The global variable **Sval** will then be accessible directly.
|
|
25
25
|
|
|
26
26
|
```html
|
|
27
27
|
<script type="text/javascript" src="https://unpkg.com/sval"></script>
|
|
@@ -32,7 +32,7 @@ Simply source from [unpkg](https://unpkg.com/sval). Or, download from [releases]
|
|
|
32
32
|
```js
|
|
33
33
|
import Sval from 'sval'
|
|
34
34
|
|
|
35
|
-
// Create
|
|
35
|
+
// Create an interpreter
|
|
36
36
|
const interpreter = new Sval({
|
|
37
37
|
// ECMA Version of the code
|
|
38
38
|
// 3 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
|
|
@@ -54,42 +54,42 @@ interpreter.run(`
|
|
|
54
54
|
|
|
55
55
|
## Usage
|
|
56
56
|
|
|
57
|
-
Sval constructor
|
|
57
|
+
The Sval constructor accepts three options: **ecmaVer**, **sourceType**, and **sandBox**.
|
|
58
58
|
|
|
59
|
-
- **ecmaVer**
|
|
59
|
+
- **ecmaVer** specifies the ECMAScript edition of the code. Currently, 3, 5, 6(2015), 7(2016), 8(2017), 9(2018), 10(2019), 11(2020), 12(2021), 13(2022), 14(2023), 15(2024), and "latest" are supported, and the default value is "latest".
|
|
60
60
|
|
|
61
|
-
- **sourceType** is
|
|
61
|
+
- **sourceType** is either "script" or "module", which declares how Sval handles the code. A value of "script" means the code will be treated as a normal script, while "module" means the code will be treated as an ES module with global strict mode and support for import and export declarations. The default type is "script".
|
|
62
62
|
|
|
63
|
-
- **sandBox** is true for sandbox mode or false for
|
|
63
|
+
- **sandBox** is true for sandbox mode or false for invasive mode. Sandbox mode runs the code in an isolated environment and will not pollute your global scope. Invasive mode allows you to run code in the same global scope as your current environment. The default setting is true.
|
|
64
64
|
|
|
65
|
-
Sval instance has two main methods: **parse** and **run**.
|
|
65
|
+
A Sval instance has two main methods: **parse** and **run**.
|
|
66
66
|
|
|
67
|
-
- **parse**
|
|
67
|
+
- **parse** parses the code with the internal [Acorn](https://github.com/acornjs/acorn) or a custom parser to get the corresponding AST, as in `parse(code: string)` or `parse(code: string, parser: (code: string, options: SvalOptions) => estree.Node)`.
|
|
68
68
|
|
|
69
|
-
- **run**
|
|
69
|
+
- **run** evaluates the provided code, accepting either a string like `run(code: string)` or an AST conforming to the ESTree spec like `run(ast: estree.Node)`.
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
In addition, a Sval instance has one method, **import**, and one object, **exports**, for modularization.
|
|
72
72
|
|
|
73
|
-
- **import** is to import modules into
|
|
73
|
+
- **import** is used to import modules into the Sval instance scope. This method behaves differently depending on the `sourceType`.
|
|
74
74
|
|
|
75
|
-
For "script", this method
|
|
75
|
+
For "script", this method expects a name and a module as arguments like `import(name: string, mod: any)`, or an object containing the modules like `import({ [name: string]: any })`. The modules will be automatically declared as global variables in the Sval instance scope. This method is most commonly used in sandbox mode.
|
|
76
76
|
|
|
77
|
-
For "module", this method
|
|
77
|
+
For "module", this method expects a path and a module declaration as arguments like `import(path: string, mod: Module)`, or an object containing the module declarations like `import({ [path: string]: Module })`. The `Module` is either an ES module export object like `{ default?: any, [name: string]: any }` or a function returning one like `() => ({ default?: any, [name: string]: any })`. The `Module` can also be a promise or a function returning a promise for use with dynamic imports. The modules will not be automatically declared as global variables in the Sval instance scope, and the code should use import declarations to import them.
|
|
78
78
|
|
|
79
|
-
- **exports**
|
|
79
|
+
- **exports** holds what was exported from runs, merged across multiple runs if applicable. This object also behaves differently depending on the `sourceType`.
|
|
80
80
|
|
|
81
|
-
For "script", this object
|
|
81
|
+
For "script", this object is automatically declared as a global variable in the Sval instance scope, and the code can simply mount properties on it to export values.
|
|
82
82
|
|
|
83
|
-
For "module", this object
|
|
83
|
+
For "module", this object is not automatically declared as a global variable in the Sval instance scope, and the code must use export declarations to export values.
|
|
84
84
|
|
|
85
|
-
Here are examples for **import** and **exports
|
|
85
|
+
Here are examples for **import** and **exports**:
|
|
86
86
|
|
|
87
87
|
Example for "script":
|
|
88
88
|
|
|
89
89
|
```js
|
|
90
90
|
import Sval from 'sval'
|
|
91
91
|
|
|
92
|
-
// Create
|
|
92
|
+
// Create an interpreter for script
|
|
93
93
|
const scriptInterpreter = new Sval({ sourceType: 'script' })
|
|
94
94
|
|
|
95
95
|
// Add global modules in interpreter
|
|
@@ -110,7 +110,7 @@ Example for "module":
|
|
|
110
110
|
```js
|
|
111
111
|
import Sval from 'sval'
|
|
112
112
|
|
|
113
|
-
// Create
|
|
113
|
+
// Create an interpreter for module
|
|
114
114
|
const moduleInterpreter = new Sval({ sourceType: 'module' })
|
|
115
115
|
|
|
116
116
|
// Add ES modules in interpreter
|
|
@@ -141,7 +141,7 @@ console.log(moduleInterpreter.exports.mod) // Get 'AllKindsOfStuffs'
|
|
|
141
141
|
|
|
142
142
|
## Related
|
|
143
143
|
|
|
144
|
-
- [sval-rhino-gs](https://github.com/Patrick-ring-motive/sval-rhino-gs)
|
|
144
|
+
- [sval-rhino-gs](https://github.com/Patrick-ring-motive/sval-rhino-gs)
|
|
145
145
|
|
|
146
146
|
## License
|
|
147
147
|
|
|
@@ -18,5 +18,7 @@ export declare const NEWTARGET: string;
|
|
|
18
18
|
export declare const PRIVATE: string;
|
|
19
19
|
export declare const NOINIT: string;
|
|
20
20
|
export declare const DEADZONE: string;
|
|
21
|
+
export declare const OPTCHAIN: string;
|
|
21
22
|
export declare const IMPORT: string;
|
|
22
23
|
export declare const EXPORTS: string;
|
|
24
|
+
export declare const STRICT: string;
|