Skip to content

Commit 2960e69

Browse files
committed
Make the val implementation more robust, so that it can run as optimally as var.
1 parent 62b9e81 commit 2960e69

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

defaultMethods.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ const defaultMethods = {
206206
// Adding this to spec something out, not to merge it quite yet
207207
val: {
208208
method: (args, context, above) => {
209+
if (Array.isArray(args) && args.length === 1) args = args[0]
210+
if (!Array.isArray(args)) {
211+
if (args === null || args === undefined) return context
212+
const result = context[args]
213+
if (typeof result === 'undefined') return null
214+
return result
215+
}
209216
let result = context
210217
let start = 0
211218
if (Array.isArray(args[0]) && args[0].length === 1) {
@@ -221,7 +228,6 @@ const defaultMethods = {
221228
}
222229
}
223230
}
224-
225231
for (let i = start; i < args.length; i++) {
226232
if (args[i] === null) continue
227233
if (result === null || result === undefined) return null
@@ -230,7 +236,38 @@ const defaultMethods = {
230236
if (typeof result === 'undefined') return null
231237
return result
232238
},
233-
deterministic: false
239+
optimizeUnary: true,
240+
deterministic: (data, buildState) => {
241+
if (buildState.insideIterator) {
242+
if (Array.isArray(data) && Array.isArray(data[0]) && Math.abs(data[0][0]) >= 2) return false
243+
return true
244+
}
245+
return false
246+
},
247+
compile: (data, buildState) => {
248+
function wrapNull (data) {
249+
if (!chainingSupported) return buildState.compile`(((a) => a === null || a === undefined ? null : a)(${data}))`
250+
return buildState.compile`(${data} ?? null)`
251+
}
252+
if (Array.isArray(data) && Array.isArray(data[0])) {
253+
// A very, very specific optimization.
254+
if (buildState.iteratorCompile && Math.abs(data[0][0] || 0) === 1 && data[1] === 'index') return buildState.compile`index`
255+
return false
256+
}
257+
if (Array.isArray(data) && data.length === 1) data = data[0]
258+
if (data === null) return wrapNull(buildState.compile`context`)
259+
if (!Array.isArray(data)) return wrapNull(buildState.compile`context[${data}]`)
260+
if (Array.isArray(data)) {
261+
let res = buildState.compile`context`
262+
for (let i = 0; i < data.length; i++) {
263+
if (data[i] === null) continue
264+
if (chainingSupported) res = buildState.compile`${res}?.[${data[i]}]`
265+
else res = buildState.compile`(${res}|| 0)[${data[i]}]`
266+
}
267+
return wrapNull(buildState.compile`(${res})`)
268+
}
269+
return false
270+
}
234271
},
235272
var: (key, context, above, engine) => {
236273
let b

0 commit comments

Comments
 (0)