-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.ts
73 lines (58 loc) · 1.77 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { parse, Options } from 'acorn'
import { Program } from './evaluate/program'
import Scope from './scope'
import { hoist } from './share/helper'
import { getOwnNames, createSandBox, runGenerator } from './share/util'
export interface SvalOptions {
ecmaVer?: 3 | 5 | 6 | 7 | 8 | 2015 | 2016 | 2017
sandBox?: boolean
}
class Sval {
private options: Options = {}
private scope = new Scope(null, true)
exports: { [name: string]: any } = {}
constructor(options: SvalOptions = {}) {
let { ecmaVer, sandBox = true } = options
if ([3, 5, 6, 7, 2015, 2016].indexOf(ecmaVer) === -1) {
ecmaVer = 7
}
this.options.ecmaVersion = ecmaVer
if (sandBox) {
// Shallow clone to create a sandbox
const win = createSandBox()
this.scope.let('window', win)
this.scope.let('this', win)
} else {
this.scope.let('window', window)
this.scope.let('this', window)
}
}
// Compatible
addModules(modules: { [name: string]: any }) {
console.warn('Use import instead. addModules is deprecated and will be removed soon.')
this.import(modules)
}
import(nameOrModules: string | { [name: string]: any }, mod?: any) {
const win = this.scope.find('window').get()
if (typeof nameOrModules === 'string') {
nameOrModules = { nameOrModules: mod }
}
if (typeof nameOrModules !== 'object') return
const names = getOwnNames(nameOrModules)
for (const name of names) {
win[name] = nameOrModules[name]
}
}
run(input: string) {
this.scope.let('exports', this.exports = {})
const ast = parse(input, this.options)
runGenerator(hoist, ast, this.scope)
runGenerator(Program, ast, this.scope)
}
}
try {
(window as any).Sval = Sval
} catch (err) {
/* empty */
}
export default Sval