es-shims / Array.prototype.toSorted

An ESnext spec-compliant `Array.prototype.toSorted` shim/polyfill/replacement that works as far down as ES3.
MIT License
9 stars 1 forks source link

Assertion failed: `SortCompare` must be an abstract closure taking 2 arguments #4

Open Grovespaz opened 1 month ago

Grovespaz commented 1 month ago

When using this shim, an assertion seems to be thrown in SortIndexedProperties.

Assertion failed: SortCompare must be an abstract closure taking 2 arguments

It seems to do this in Chrome 90 and Safari 15. I've only imported this shim and imported the /auto variant. If I can provide any additional information, please let me know.

ljharb commented 1 month ago

Thanks, that's helpful - I'll try to reproduce it.

ljharb commented 1 month ago

I can't seem to produce it - both of those sets of tests pass.

What version of es-abstract do you have installed, as well as this package? What package manager are you using? (yarn often installs incorrect dependency versions, eg)

Grovespaz commented 1 month ago

It's very odd, after some experimentation it only seems to happen on production builds. I'm using npm and Nuxt3 (which uses Vite under the hood), this leads me to think that their build step transforms the code in such a way that it fails.

My full list of packages is such:

"array.prototype.toreversed": "^1.1.2",
"array.prototype.tosorted": "^1.1.4",
"sass": "1.75.0",
"sass-loader": "14.2.1",
"vue-scrollto": "^2.20.0"
"nuxt": "^3.11.2",
"nuxt-gtag": "^1.1.2"

Feel free to close if it's outside of your scope, of course.

ljharb commented 1 month ago

what about npm ls es-abstract?

Grovespaz commented 1 month ago
├─┬ array.prototype.toreversed@1.1.2
│ └─┬ es-abstract@1.23.3
│   ├─┬ arraybuffer.prototype.slice@1.0.3
│   │ └── es-abstract@1.23.3 deduped
│   ├─┬ function.prototype.name@1.1.6
│   │ └── es-abstract@1.23.3 deduped
│   └─┬ string.prototype.trim@1.2.9
│     └── es-abstract@1.23.3 deduped
└─┬ array.prototype.tosorted@1.1.4
  └── es-abstract@1.23.3 deduped
ljharb commented 1 month ago

hmm, that should be right.

What bundler are you using? Can you confirm the literal function source that's being passed to SortCompare using the debugger?

goto-bus-stop commented 3 weeks ago

I'm using vite (esbuild), which turns this:

npx esbuild --minify node_modules/array.prototype.tosorted/implementation.js

https://github.com/es-shims/Array.prototype.toSorted/blob/850c48e17f4eeffd1eaeff5898b083916224dfef/implementation.js#L24-L26

into this:

      o = function (n, u) {
        return CompareArrayElements(n, u, e);
      },

causing it to fail the IsAbstractClosure fn.name check.

I don't think many minifiers respect (0,x) semantics. terser happens to work here despite not respecting (0,x) because it inlines the SortCompare definition into the SortIndexedProperties call site afterwards so the function coincidentally stays anonymous.

ljharb commented 3 weeks ago

Eliding function names isn't universally safe behavior, especially when minifying, because it's introspectable, so that seems like a bug worth filing on esbuild.