Skip to content

Commit c5a22fd

Browse files
committed
perf(context): remove context caching and support for async temp vars
1 parent 48a8c9a commit c5a22fd

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

src/context.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

3+
const hasHandler = { has }
4+
const allHandlers = { has, get }
35
const globals = new Set()
4-
const proxies = new WeakMap()
5-
const tempVarStore = new WeakMap()
6-
const handlers = {has, get}
6+
let temp
77

88
let globalObj
99
if (typeof window !== 'undefined') globalObj = window // eslint-disable-line
@@ -38,27 +38,21 @@ function hideAll () {
3838
}
3939

4040
function has (target, key) {
41-
return globals.has(key) ? Reflect.has(target, key) : true
41+
return globals.has(key) ? (key in target) : true
4242
}
4343

44-
function get (target, key, receiver) {
45-
const tempVars = tempVarStore.get(target)
46-
if (tempVars && (key in tempVars)) {
47-
return tempVars[key]
48-
}
49-
return Reflect.get(target, key, receiver)
44+
function get (target, key) {
45+
return key in temp ? temp[key] : target[key]
5046
}
5147

5248
function toSandbox (obj, tempVars) {
53-
tempVarStore.set(obj, tempVars)
54-
let sandbox = proxies.get(obj)
55-
if (!sandbox) {
56-
sandbox = new Proxy(obj, handlers)
57-
proxies.set(obj, sandbox)
49+
if (tempVars) {
50+
temp = tempVars
51+
return new Proxy(obj, allHandlers)
5852
}
59-
return sandbox
53+
return new Proxy(obj, hasHandler)
6054
}
6155

62-
function clearSandbox (obj) {
63-
tempVarStore.delete(obj)
56+
function clearSandbox () {
57+
temp = undefined
6458
}

src/rawCompiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ function compileExpression (src) {
1111
try { with (sandbox) { return ${src} } } catch (err) {
1212
if (!(err instanceof TypeError)) throw err
1313
}
14-
$nxClearSandbox(context)`)
14+
$nxClearSandbox()`)
1515
}
1616

1717
function compileCode (src) {
1818
return new Function('context', 'tempVars', // eslint-disable-line
1919
`const sandbox = $nxCompileToSandbox(context, tempVars)
2020
with (sandbox) { ${src} }
21-
$nxClearSandbox(context)`)
21+
$nxClearSandbox()`)
2222
}

0 commit comments

Comments
 (0)