zloirock / core-js

Standard Library
MIT License
24.03k stars 1.62k forks source link

Spec Mismatch in array-iteration-from-last (related to FindLast, FindLastIndex) #1313

Closed stonechoe closed 6 months ago

stonechoe commented 6 months ago

This code works different in node 17 and node 18:

const array = { length: { valueOf: function () { throw 42; } } };
findLastIndex(array, 1);

In node 17, TypeError is caught

Running node v17.9.1 (npm v8.11.0)
[Function: bound call]
/project/node_modules/core-js/internals/a-callable.js:10
  throw new $TypeError(tryToString(argument) + ' is not a function');
  ^

TypeError: 1 is not a function
    at module.exports (/project/node_modules/core-js/internals/a-callable.js:10:9)
    at module.exports (/project/node_modules/core-js/internals/function-bind-context.js:10:3)
    at /project/node_modules/core-js/internals/array-iteration-from-last.js:13:25
    at Object.findLastIndex (/project/node_modules/core-js/modules/es.array.find-last-index.js:10:12)
    at Object.<anonymous> (/project/example.js:7:13)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)

Node.js v17.9.1

but in node 18 ~ 20, code works as intended, following ecma-262

Running node v18.19.0 (npm v10.2.3)
[Function: bound call]

/project/example.js:6
const array = { length: { valueOf: function () { throw 42; } }, 1: 1, 2: 2 };
                                                 ^
42
(Use `node --trace-uncaught ...` to show where the exception was thrown)

Node.js v18.19.0

I think this error can be fixed by changing orders of those lines in array-iteration-from-last.js (and array-iteration.js)

https://github.com/zloirock/core-js/blob/c44bfe9ee82aec23e6ed04a09cd7e06a49e1f70d/packages/core-js/internals/array-iteration-from-last.js#L13-L14

https://github.com/zloirock/core-js/blob/c44bfe9ee82aec23e6ed04a09cd7e06a49e1f70d/packages/core-js/internals/array-iteration.js#L23-L24

zloirock commented 6 months ago

Thanks, you are right.