Skip to content

Commit 6ccf218

Browse files
committed
feat(sandbox): add sandbox function
1 parent e9fae33 commit 6ccf218

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

compiler.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22

33
module.exports = {
44
compileCode,
5-
compileExpression
5+
compileExpression,
6+
sandbox
67
}
78

89
const expressionCache = new Map()
910
const codeCache = new Map()
1011

11-
function compileExpression (src, sandbox) {
12+
function compileExpression (src) {
1213
if (typeof src !== 'string') {
1314
throw new TypeError('first argument must be a string')
1415
}
15-
if (typeof sandbox !== 'object') {
16-
throw new TypeError('second argument must be an object')
17-
}
18-
sandbox = new Proxy(sandbox, {has})
1916
let expression = expressionCache.get(src)
2017
if (!expression) {
2118
expression = new Function('sandbox',
@@ -24,23 +21,26 @@ function compileExpression (src, sandbox) {
2421
}`)
2522
expressionCache.set(src, expression)
2623
}
27-
return expression.bind(sandbox, sandbox) // eslint-disable-line
24+
return expression // eslint-disable-line
2825
}
2926

30-
function compileCode (src, sandbox) {
27+
function compileCode (src) {
3128
if (typeof src !== 'string') {
3229
throw new TypeError('first argument must be a string')
3330
}
34-
if (typeof sandbox !== 'object') {
35-
throw new TypeError('second argument must be an object')
36-
}
37-
sandbox = new Proxy(sandbox, {has})
3831
let code = codeCache.get(src)
3932
if (!code) {
4033
code = new Function('sandbox', `with (sandbox) { ${src} }`)
4134
codeCache.set(src, code)
4235
}
43-
return code.bind(sandbox, sandbox) // eslint-disable-line
36+
return code // eslint-disable-line
37+
}
38+
39+
function sandbox (obj) {
40+
if (typeof obj !== 'object') {
41+
throw new TypeError('first argument must be an object')
42+
}
43+
return new Proxy(obj, {has})
4444
}
4545

4646
function has () {

0 commit comments

Comments
 (0)