Njs: Function.prototype.apply() should support array-like objects

Created on 4 Sep 2018  路  5Comments  路  Source: nginx/njs

>> (function(){ return Math.max.apply(null, arguments) })(1,2,3)
TypeError: second argument is not an array
    at native (native)
    at anonymous (:1)
    at main (native)
bug

Most helpful comment

@lexborisov
Hi Alexander!

>> String.prototype.concat.apply('', { length: Object(3), 0: 'a', 1: 'b', 2: 'c' })
''

maybe add some generic njs_arraylike_get_length implementation, if this possible?
It will be also needed in #72

also, if we're moving to ES6, this should be fixed too:

>> String.prototype.concat.apply('', { length: -1, 0: 'a', 1: 'b', 2: 'c' })
MemoryError

All 5 comments

Hi Artem (@drsm ),

Please, try this patch for solve the issue.

@drsm BTW,

non-primitive length properties, are not supported yet, as for example in:

> String.prototype.concat.apply('a', {length:{valueOf:function() {return 2}},  0:'b', 1:'c'})
'abc'

Some internal mechanisms need to be refactored.

@lexborisov
Hi Alexander!

>> String.prototype.concat.apply('', { length: Object(3), 0: 'a', 1: 'b', 2: 'c' })
''

maybe add some generic njs_arraylike_get_length implementation, if this possible?
It will be also needed in #72

also, if we're moving to ES6, this should be fixed too:

>> String.prototype.concat.apply('', { length: -1, 0: 'a', 1: 'b', 2: 'c' })
MemoryError

@xeioex
I didn't see your answer.

so, everything OK then, except ToLength, Array.prototype.slice is also affected, maybe fix this in a separate patch.

@drsm
I committed this patch to the master branch.
With the next patch, we will refactoring the code and add the implementation of toLength function.

Thank you for the report!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

drsm picture drsm  路  4Comments

reyou picture reyou  路  5Comments

xeioex picture xeioex  路  3Comments

drsm picture drsm  路  3Comments

rainerhenrichsen picture rainerhenrichsen  路  5Comments