2
2
3
3
module . exports = {
4
4
compileCode,
5
- compileExpression
5
+ compileExpression,
6
+ sandbox
6
7
}
7
8
8
9
const expressionCache = new Map ( )
9
10
const codeCache = new Map ( )
10
11
11
- function compileExpression ( src , sandbox ) {
12
+ function compileExpression ( src ) {
12
13
if ( typeof src !== 'string' ) {
13
14
throw new TypeError ( 'first argument must be a string' )
14
15
}
15
- if ( typeof sandbox !== 'object' ) {
16
- throw new TypeError ( 'second argument must be an object' )
17
- }
18
- sandbox = new Proxy ( sandbox , { has} )
19
16
let expression = expressionCache . get ( src )
20
17
if ( ! expression ) {
21
18
expression = new Function ( 'sandbox' ,
@@ -24,23 +21,26 @@ function compileExpression (src, sandbox) {
24
21
}` )
25
22
expressionCache . set ( src , expression )
26
23
}
27
- return expression . bind ( sandbox , sandbox ) // eslint-disable-line
24
+ return expression // eslint-disable-line
28
25
}
29
26
30
- function compileCode ( src , sandbox ) {
27
+ function compileCode ( src ) {
31
28
if ( typeof src !== 'string' ) {
32
29
throw new TypeError ( 'first argument must be a string' )
33
30
}
34
- if ( typeof sandbox !== 'object' ) {
35
- throw new TypeError ( 'second argument must be an object' )
36
- }
37
- sandbox = new Proxy ( sandbox , { has} )
38
31
let code = codeCache . get ( src )
39
32
if ( ! code ) {
40
33
code = new Function ( 'sandbox' , `with (sandbox) { ${ src } }` )
41
34
codeCache . set ( src , code )
42
35
}
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} )
44
44
}
45
45
46
46
function has ( ) {
0 commit comments