-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecutor.js
More file actions
34 lines (26 loc) · 751 Bytes
/
Executor.js
File metadata and controls
34 lines (26 loc) · 751 Bytes
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
const vm = require('vm')
const PathSymbol = Symbol('@path')
const EnvironmentHandler = require('./proxies/Environment')
const Contextable = require('./proxies/Contextable')
class Executor {
constructor () {
this[PathSymbol] = []
}
async exec (code, state) {
if (!state) throw new Error('state not given to executor')
const cd = key => {
if (key === '..') {
this[PathSymbol].pop()
} else {
this[PathSymbol].push(key)
}
}
const globals = { Object, Date, cd }
const environment = new Proxy(
new Proxy(state, Contextable(this[PathSymbol])),
EnvironmentHandler(globals)
)
return vm.runInContext(code, vm.createContext(environment))
}
}
module.exports = Executor