Skip to content

Commit 08137ef

Browse files
committed
Switch back to throw NaN for performance reasons, also commit an async compiler optimization
1 parent 21c825f commit 08137ef

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

compiler.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,12 @@ function buildString (method, buildState = {}) {
220220
} else {
221221
asyncDetected = Boolean(async && engine.methods[func] && engine.methods[func].asyncMethod)
222222
const argCount = countArguments(asyncDetected ? engine.methods[func].asyncMethod : engine.methods[func].method)
223-
const argumentsNeeded = argumentsDict[argCount - 1] || argumentsDict[2]
223+
let argumentsNeeded = argumentsDict[argCount - 1] || argumentsDict[2]
224+
225+
if (asyncDetected && typeof engine.methods[func][Sync] === 'function' && engine.methods[func][Sync](lower, { engine })) {
226+
asyncDetected = false
227+
argumentsNeeded = argumentsNeeded.replace('engine', 'engine.fallback')
228+
}
224229

225230
if (engine.methods[func] && !engine.methods[func].lazy) {
226231
return makeAsync(`engine.methods["${func}"]${asyncDetected ? '.asyncMethod' : '.method'}(${coerce}(` + buildString(lower, buildState) + ')' + argumentsNeeded + ')')

defaultMethods.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -59,58 +59,58 @@ const defaultMethods = {
5959
if (typeof data === 'string') return precoerceNumber(+data)
6060
if (typeof data === 'number') return precoerceNumber(+data)
6161
if (typeof data === 'boolean') return precoerceNumber(+data)
62-
if (typeof data === 'object' && !Array.isArray(data)) throw new Error('NaN')
62+
if (typeof data === 'object' && !Array.isArray(data)) throw NaN
6363
let res = 0
6464
for (let i = 0; i < data.length; i++) {
65-
if (data[i] && typeof data[i] === 'object') throw new Error('NaN')
65+
if (data[i] && typeof data[i] === 'object') throw NaN
6666
res += +data[i]
6767
}
68-
if (Number.isNaN(res)) throw new Error('NaN')
68+
if (Number.isNaN(res)) throw NaN
6969
return res
7070
},
7171
'*': (data) => {
7272
let res = 1
7373
for (let i = 0; i < data.length; i++) {
74-
if (data[i] && typeof data[i] === 'object') throw new Error('NaN')
74+
if (data[i] && typeof data[i] === 'object') throw NaN
7575
res *= +data[i]
7676
}
77-
if (Number.isNaN(res)) throw new Error('NaN')
77+
if (Number.isNaN(res)) throw NaN
7878
return res
7979
},
8080
'/': (data) => {
81-
if (data[0] && typeof data[0] === 'object') throw new Error('NaN')
81+
if (data[0] && typeof data[0] === 'object') throw NaN
8282
let res = +data[0]
8383
for (let i = 1; i < data.length; i++) {
84-
if ((data[i] && typeof data[i] === 'object') || !data[i]) throw new Error('NaN')
84+
if ((data[i] && typeof data[i] === 'object') || !data[i]) throw NaN
8585
res /= +data[i]
8686
}
87-
if (Number.isNaN(res)) throw new Error('NaN')
87+
if (Number.isNaN(res)) throw NaN
8888
return res
8989
},
9090
'-': (data) => {
9191
if (!data) return 0
9292
if (typeof data === 'string') return precoerceNumber(-data)
9393
if (typeof data === 'number') return precoerceNumber(-data)
9494
if (typeof data === 'boolean') return precoerceNumber(-data)
95-
if (typeof data === 'object' && !Array.isArray(data)) throw new Error('NaN')
96-
if (data[0] && typeof data[0] === 'object') throw new Error('NaN')
95+
if (typeof data === 'object' && !Array.isArray(data)) throw NaN
96+
if (data[0] && typeof data[0] === 'object') throw NaN
9797
if (data.length === 1) return -data[0]
9898
let res = data[0]
9999
for (let i = 1; i < data.length; i++) {
100-
if (data[i] && typeof data[i] === 'object') throw new Error('NaN')
100+
if (data[i] && typeof data[i] === 'object') throw NaN
101101
res -= +data[i]
102102
}
103-
if (Number.isNaN(res)) throw new Error('NaN')
103+
if (Number.isNaN(res)) throw NaN
104104
return res
105105
},
106106
'%': (data) => {
107-
if (data[0] && typeof data[0] === 'object') throw new Error('NaN')
107+
if (data[0] && typeof data[0] === 'object') throw NaN
108108
let res = +data[0]
109109
for (let i = 1; i < data.length; i++) {
110-
if (data[i] && typeof data[i] === 'object') throw new Error('NaN')
110+
if (data[i] && typeof data[i] === 'object') throw NaN
111111
res %= +data[i]
112112
}
113-
if (Number.isNaN(res)) throw new Error('NaN')
113+
if (Number.isNaN(res)) throw NaN
114114
return res
115115
},
116116
error: (type) => {

utilities/downgrade.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* @param {any} item
44
*/
55
export function precoerceNumber (item) {
6-
if (Number.isNaN(item)) throw new Error('NaN')
6+
if (Number.isNaN(item)) throw NaN
77
if (!item) return item
8-
if (typeof item === 'object') throw new Error('NaN')
8+
if (typeof item === 'object') throw NaN
99
return item
1010
}

0 commit comments

Comments
 (0)