Skip to content

Commit 6eae904

Browse files
committed
If environment does not support eval variants, don't use inline object reader, #40
1 parent 3a6c46e commit 6eae904

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

unpack.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export class C1Type {}
2727
export const C1 = new C1Type()
2828
C1.name = 'MessagePack 0xC1'
2929
var sequentialMode = false
30+
var inlineObjectReadThreshold = 2
31+
try {
32+
new Function('')
33+
} catch(error) {
34+
// if eval variants are not supported, do not create inline object readers ever
35+
inlineObjectReadThreshold = Infinity
36+
}
3037

3138
export class Unpackr {
3239
constructor(options) {
@@ -440,7 +447,7 @@ const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/
440447
function createStructureReader(structure, firstId) {
441448
function readObject() {
442449
// This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
443-
if (readObject.count++ > 2) {
450+
if (readObject.count++ > inlineObjectReadThreshold) {
444451
let readObject = structure.read = (new Function('r', 'return function(){return {' + structure.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}}'))(read)
445452
if (structure.highByte === 0)
446453
structure.read = createSecondByteReader(firstId, structure.read)

0 commit comments

Comments
 (0)