Open ChALkeR opened 2 months ago
Thank you for the detailed and well-researched bug report. We appreciate the effort you put into diagnosing the issue and linking to the relevant sections of the ECMAScript specification.
To provide some context, the behavior you're observing is actually by design in Hermes. As documented in our Excluded from Support section, we deliberately do not support Symbol.species
or the use of .constructor
property when creating new arrays in Array.prototype methods (the same reasoning applies to TypedArray
.
Supporting this would require introducing a slow path in every case where a new object is returned that doesn't match the expected type. That would add a ton of complexity for a use case that didn't seem very important.
Frankly, we didn't realize the importance of this for implementing something Buffer
on top of TypedArray. Additionally, it looks like in this case the returned object is of the same type.
I wonder whether it would be sufficient, as an intermediate step, to only support the case where .constructor
is required to return an instance of the type in question.
@tmikov this is not about Symbol.species
TypedArraySpeciesCreate
falls back to defaultConstructor
on step 1 -- that is what this report is about
Missing Symbol.Species
is not the issue here
Unsupported Symbol.Species
should have been replaced with default constructor of the object, not with nothing
The testcase doesn't use Symbol.species
use of constructor property when creating new Arrays in Array.prototype methods
Ah, haven't noticed that! This breaks a lot of things though in a subtle unexpected way
I wonder whether it would be sufficient, as an intermediate step, to only support the case where .constructor is required to return an instance of the type in question.
Unsure Subtle spec differences may be breaking
A previous Buffer version relied on Symbol.species
even
@ChALkeR so, it appears the intermediate step we proposed is the actual spec-compliant behavior of TypedArray. That is fortunate. We will implement at least the partial support through constructor
and will consider adding incremental Symbol.species
support.
Symbol.species
is... at danger i think?
cc @ljharb perhaps?
Symbol.species
usage is being evaluated, and it's unclear which parts of it, if any, would be removed.
Hm: https://github.com/tc39/proposal-rm-builtin-subclassing?tab=readme-ov-file#prototype-methods-3
In that taxonomy:
Symbol.species
is Type 3 in that taxonomyI'm not sure if it's worth to add Symbol.species
but I don't think that Type 2 can realistically be removed
I agree that Type 2 is unlikely to be removable.
Bug Description
See explainer, test and a monkey-patch fix at https://github.com/ExodusMovement/patch-broken-hermes-typed-arrays
Sections of specification broken in Hermes:
%TypedArray%.prototype.subarray
%TypedArray%.prototype.map
%TypedArray%.prototype.filter
%TypedArray%.prototype.slice
See https://github.com/ExodusMovement/patch-broken-hermes-typed-arrays/blob/main/index.js for links to specific spec steps
See ecosystem fallout in https://github.com/feross/buffer/issues/329
gradle clean
and~ confirmed this bug does not occur with JSCHermes git revision (if applicable): 0410fb4 (latest
main
) React Native version: 🤷🏻 OS: Platform (most likely one of arm64-v8a, armeabi-v7a, x86, x86_64):Steps To Reproduce
See full explainer, test and a monkey-patch fix at https://github.com/ExodusMovement/patch-broken-hermes-typed-arrays
The Expected Behavior
Implementation of TypedArray per spec, perhaps without
Symbol.species
but with working inheritanceSee spec refs above