Skip to content

Commit 48a8c9a

Browse files
committed
feat(temp vars): allow temp vars in expressions and rework them internally
1 parent 1ab1671 commit 48a8c9a

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

index.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ const modifiers = require('./src/modifiers')
55
const compiler = require('./src/compiler')
66
const rawCompiler = require('./src/rawCompiler')
77

8-
module.exports = {
9-
compileExpression: compiler.compileExpression,
10-
compileCode: compiler.compileCode,
11-
compileRawExpression: rawCompiler.compileExpression,
12-
compileRawCode: rawCompiler.compileCode,
13-
expose: context.expose,
14-
hide: context.hide,
15-
hideAll: context.hideAll,
16-
filter: modifiers.filter,
17-
limiter: modifiers.limiter
18-
}
8+
exports.compileExpression = compiler.compileExpression
9+
exports.compileCode = compiler.compileCode
10+
exports.compileRawExpression = rawCompiler.compileExpression
11+
exports.compileRawCode = rawCompiler.compileCode
12+
exports.expose = context.expose
13+
exports.hide = context.hide
14+
exports.hideAll = context.hideAll
15+
exports.filter = modifiers.filter
16+
exports.limiter = modifiers.limiter

src/compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ function compileExpression (src) {
2424
return expression
2525
}
2626

27-
return function evaluateExpression (context) {
28-
let value = expression.exec(context)
27+
return function evaluateExpression (context, tempVars) {
28+
let value = expression.exec(context, tempVars)
2929
for (let filter of expression.filters) {
3030
const args = filter.argExpressions.map(evaluateArgExpression, context)
3131
value = filter.effect(value, ...args)

src/context.js

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

33
const globals = new Set()
44
const proxies = new WeakMap()
5-
const handlers = {has}
5+
const tempVarStore = new WeakMap()
6+
const handlers = {has, get}
67

78
let globalObj
89
if (typeof window !== 'undefined') globalObj = window // eslint-disable-line
910
else if (typeof global !== 'undefined') globalObj = global // eslint-disable-line
1011
else if (typeof self !== 'undefined') globalObj = self // eslint-disable-line
1112
globalObj.$nxCompileToSandbox = toSandbox
12-
globalObj.$nxCompileCreateBackup = createBackup
13+
globalObj.$nxClearSandbox = clearSandbox
1314

1415
module.exports = {
1516
expose,
@@ -40,10 +41,16 @@ function has (target, key) {
4041
return globals.has(key) ? Reflect.has(target, key) : true
4142
}
4243

43-
function toSandbox (obj) {
44-
if (typeof obj !== 'object') {
45-
throw new TypeError(`First argument must be an object, instead it is a ${typeof obj}.`)
44+
function get (target, key, receiver) {
45+
const tempVars = tempVarStore.get(target)
46+
if (tempVars && (key in tempVars)) {
47+
return tempVars[key]
4648
}
49+
return Reflect.get(target, key, receiver)
50+
}
51+
52+
function toSandbox (obj, tempVars) {
53+
tempVarStore.set(obj, tempVars)
4754
let sandbox = proxies.get(obj)
4855
if (!sandbox) {
4956
sandbox = new Proxy(obj, handlers)
@@ -52,12 +59,6 @@ function toSandbox (obj) {
5259
return sandbox
5360
}
5461

55-
function createBackup (context, tempVars) {
56-
if (typeof tempVars === 'object') {
57-
const backup = {}
58-
for (let key of Object.keys(tempVars)) {
59-
backup[key] = context[key]
60-
}
61-
return backup
62-
}
62+
function clearSandbox (obj) {
63+
tempVarStore.delete(obj)
6364
}

src/rawCompiler.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@ module.exports = {
66
}
77

88
function compileExpression (src) {
9-
return new Function('context', // eslint-disable-line
10-
`const sandbox = $nxCompileToSandbox(context)
9+
return new Function('context', 'tempVars', // eslint-disable-line
10+
`const sandbox = $nxCompileToSandbox(context, tempVars)
1111
try { with (sandbox) { return ${src} } } catch (err) {
1212
if (!(err instanceof TypeError)) throw err
13-
}`)
13+
}
14+
$nxClearSandbox(context)`)
1415
}
1516

1617
function compileCode (src) {
1718
return new Function('context', 'tempVars', // eslint-disable-line
18-
`const backup = $nxCompileCreateBackup(context, tempVars)
19-
Object.assign(context, tempVars)
20-
const sandbox = $nxCompileToSandbox(context)
21-
try {
22-
with (sandbox) { ${src} }
23-
} finally {
24-
Object.assign(context, backup)
25-
}`)
19+
`const sandbox = $nxCompileToSandbox(context, tempVars)
20+
with (sandbox) { ${src} }
21+
$nxClearSandbox(context)`)
2622
}

0 commit comments

Comments
 (0)