sval 0.5.0 → 0.5.2
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/.github/workflows/coverage.yml +3 -3
- package/README.md +80 -28
- package/dist/sval.es6.js +979 -422
- package/dist/sval.js +1355 -694
- package/dist/sval.min.js +1 -1
- package/package.json +1 -2
- package/types/index.d.ts +6 -10
|
@@ -8,14 +8,14 @@ jobs:
|
|
|
8
8
|
runs-on: ubuntu-latest
|
|
9
9
|
steps:
|
|
10
10
|
- name: Check out repository code
|
|
11
|
-
uses: actions/checkout@
|
|
11
|
+
uses: actions/checkout@v4
|
|
12
12
|
- name: Setup node environment
|
|
13
|
-
uses: actions/setup-node@
|
|
13
|
+
uses: actions/setup-node@v4
|
|
14
14
|
- name: Install node modules
|
|
15
15
|
run: npm install
|
|
16
16
|
- name: Test code
|
|
17
17
|
run: npm test
|
|
18
18
|
- name: Send coverage info to Coveralls
|
|
19
|
-
uses: coverallsapp/github-action@
|
|
19
|
+
uses: coverallsapp/github-action@v2
|
|
20
20
|
with:
|
|
21
21
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# Sval · [](https://www.npmjs.com/package/sval) [](https://www.npmjs.com/package/sval) [](https://github.com/Siubaak/sval/actions/workflows/coverage.yml) [](https://coveralls.io/github/Siubaak/sval)
|
|
2
2
|
|
|
3
3
|
A JavaScript interpreter writen in JavaScript, based on parser [Acorn](https://github.com/acornjs/acorn).
|
|
4
4
|
|
|
5
|
-
- **Running on ES5, supporting
|
|
5
|
+
- **Running on ES5, supporting ES latest features**
|
|
6
6
|
- **Both invasived and sandbox modes available**
|
|
7
7
|
|
|
8
8
|
It's useful to evaluate the code of higher ECMAScript editions, or for the environment with disabled `eval`, `setTimeout` and `new Function`.
|
|
@@ -27,59 +27,111 @@ Simply source from [unpkg](https://unpkg.com/sval). Or, download from [releases]
|
|
|
27
27
|
<script type="text/javascript" src="https://unpkg.com/sval"></script>
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
##
|
|
30
|
+
## Get Started
|
|
31
31
|
|
|
32
32
|
```js
|
|
33
33
|
import Sval from 'sval'
|
|
34
34
|
|
|
35
|
-
//
|
|
36
|
-
const
|
|
35
|
+
// Create a interpreter
|
|
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
|
|
39
39
|
// or 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024
|
|
40
40
|
// or "latest"
|
|
41
41
|
ecmaVer: 'latest',
|
|
42
|
+
// Code source type
|
|
43
|
+
// "script" or "module"
|
|
44
|
+
sourceType: 'script',
|
|
42
45
|
// Whether the code runs in a sandbox
|
|
43
46
|
sandBox: true,
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Create a interpreter
|
|
47
|
-
const interpreter = new Sval(options)
|
|
48
|
-
|
|
49
|
-
// Add global modules in interpreter
|
|
50
|
-
interpreter.import('importWhatYouNeed', 'AllKindsOfStuffs')
|
|
51
|
-
// Or interpreter.import({ importWhatYouNeed: 'AllKindsOfStuffs' })
|
|
47
|
+
})
|
|
52
48
|
|
|
53
49
|
// Parse and run the code
|
|
54
50
|
interpreter.run(`
|
|
55
|
-
|
|
56
|
-
exports.msg = msg // Export any you want
|
|
51
|
+
console.log('Hello World')
|
|
57
52
|
`)
|
|
53
|
+
```
|
|
58
54
|
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
Sval constructor has three options: **ecmaVer**, **sourceType** and **sandBox**.
|
|
58
|
+
|
|
59
|
+
- **ecmaVer** is 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 edition is "latest".
|
|
60
|
+
|
|
61
|
+
- **sourceType** is ethier "script" or "module", which is to declare how Sval handle the code. The "script" means the code will be treated as a normal script, while the "module" means the code will be treated as an ES module with global strict mode and parsing of import and export declarations. The default type is "script".
|
|
62
|
+
|
|
63
|
+
- **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.
|
|
64
|
+
|
|
65
|
+
Sval instance has two main methods: **parse** and **run**.
|
|
66
|
+
|
|
67
|
+
- **parse** is to parse the code with internal [Acorn](https://github.com/acornjs/acorn) or custom parser, to get the corresponding AST, like `parse(code: string)` or `parse(code: string, parser: (code: string, options: SvalOptions) => estree.Node`.
|
|
68
|
+
|
|
69
|
+
- **run** is to evaluate the code inputed, expecting a string as argument like `run(code: string)`, or an AST followed ESTree Spec as argument like `run(ast: estree.Node)`.
|
|
70
|
+
|
|
71
|
+
Besides, Sval instance also has one method, **import**, and one object, **exports**, for modularization.
|
|
72
|
+
|
|
73
|
+
- **import** is to import modules into your Sval instance scope. This method has different behaviors for different `sourceType`.
|
|
74
|
+
|
|
75
|
+
For "script", this method 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 in Sval instance scope. This method is more likely to be used in sandbox mode.
|
|
76
|
+
|
|
77
|
+
For "module", this method expecting a path and a module declaration as arguments like `import(path: string, mod: Module)`, or an object which contains the module declarations as argument like `import({ [path: string]: Module })`. The `Module` is either an ES module exported object like `{ default?: any, [name: string]: any }` or a function returning an ES module exported object like `() => ({ default?: any, [name: string]: any })`. The `Module` can also be a promise or a function returning a promise if importing a module by dynamic import. The modules will not be automatically declared as global variables in Sval instance scope, and the code should use import declarations to import the module.
|
|
78
|
+
|
|
79
|
+
- **exports** is to get what you exported from runs, merged if several runs have exports. Also, this object has different behaviors for different `sourceType`.
|
|
80
|
+
|
|
81
|
+
For "script", this object will be automatically declared as global variables in Sval instance scope, and the code can just simple mount properties on it for export.
|
|
82
|
+
|
|
83
|
+
For "module", this object will not be automatically declared as global variables in Sval instance scope, and the code needs to use export declarations for export.
|
|
84
|
+
|
|
85
|
+
Here are examples for **import** and **exports** below:
|
|
86
|
+
|
|
87
|
+
Example for "script":
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
import Sval from 'sval'
|
|
91
|
+
|
|
92
|
+
// Create a interpreter for script
|
|
93
|
+
const scriptInterpreter = new Sval({ sourceType: 'script' })
|
|
94
|
+
|
|
95
|
+
// Add global modules in interpreter
|
|
96
|
+
scriptInterpreter.import('importWhatYouNeed', 'AllKindsOfStuffs')
|
|
97
|
+
// Or scriptInterpreter.import({ importWhatYouNeed: 'AllKindsOfStuffs' })
|
|
98
|
+
|
|
99
|
+
// Parse and run the code
|
|
100
|
+
scriptInterpreter.run(`
|
|
101
|
+
exports.mod = importWhatYouNeed
|
|
61
102
|
`)
|
|
62
103
|
|
|
63
104
|
// Get exports from runs
|
|
64
|
-
console.log(
|
|
65
|
-
console.log(interpreter.exports.mod) // Get 'AllKindsOfStuffs'
|
|
105
|
+
console.log(scriptInterpreter.exports.mod) // Get 'AllKindsOfStuffs'
|
|
66
106
|
```
|
|
67
107
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
- **ecmaVer** is 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 edition is "latest".
|
|
108
|
+
Example for "module":
|
|
71
109
|
|
|
72
|
-
|
|
110
|
+
```js
|
|
111
|
+
import Sval from 'sval'
|
|
73
112
|
|
|
74
|
-
|
|
113
|
+
// Create a interpreter for module
|
|
114
|
+
const moduleInterpreter = new Sval({ sourceType: 'module' })
|
|
75
115
|
|
|
76
|
-
|
|
116
|
+
// Add ES modules in interpreter
|
|
117
|
+
moduleInterpreter.import('./import-what-you-need', { default: 'AllKindsOfStuffs' })
|
|
118
|
+
// Or moduleInterpreter.import('./import-what-you-need', () => ({ default: 'AllKindsOfStuffs' }))
|
|
119
|
+
// Or moduleInterpreter.import({ './import-what-you-need': { default: 'AllKindsOfStuffs' } })
|
|
120
|
+
// Or moduleInterpreter.import({ './import-what-you-need': () => ({ default: 'AllKindsOfStuffs' }) })
|
|
77
121
|
|
|
78
|
-
|
|
122
|
+
// Add ES modules in interpreter for dynamic import
|
|
123
|
+
moduleInterpreter.import('./dynamic-import-what-you-need', Promise.resolve({ default: 'AllKindsOfStuffs' }))
|
|
79
124
|
|
|
80
|
-
|
|
125
|
+
// Parse and run the code
|
|
126
|
+
moduleInterpreter.run(`
|
|
127
|
+
import importWhatYouNeed from './import-what-you-need'
|
|
128
|
+
import('./dynamic-import-what-you-need').then(m => console.log(m.default)) // Get 'AllKindsOfStuffs'
|
|
129
|
+
export { importWhatYouNeed as mod }
|
|
130
|
+
`)
|
|
81
131
|
|
|
82
|
-
|
|
132
|
+
// Get exports from runs
|
|
133
|
+
console.log(moduleInterpreter.exports.mod) // Get 'AllKindsOfStuffs'
|
|
134
|
+
```
|
|
83
135
|
|
|
84
136
|
## Note
|
|
85
137
|
|