[Webkit-unassigned] [Bug 181011] JavaScript subclasses of typed arrays produce incorrect type when .of() is called

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Sat Apr 14 12:19:43 PDT 2018


https://bugs.webkit.org/show_bug.cgi?id=181011

--- Comment #1 from garett.ridge at gmail.com ---
Discovered this breaks from() as well.

Basically, TypedArray.from() and TypedArray.of() are broken if you subclass a TypedArray.

It may be seen as uncommon to extend these built-in-types, but it intuitively seems like you should be able to, for example to add functionality like vector math to Float32Arrays.  That's extremely useful for a number of reasons.  When I did this to my WebGL framework it sped up my ray tracing demos by nearly an order of magnitude to with extending plain Arrays.  

Other options do not look so good.  Making a vector class wrap/contain a TypedArray instead of extending Float32Array outright is significantly less useful, since you no longer can use square bracket syntax, and since you'd have to also wrap every individual method of Array that you'd like to expose, in what would otherwise be a very short class (vector math is otherwise very short to implement due to es6 syntax and Array.map()).

When you extend TypedArray, the way it's built practically forces you to use .of() and .from() for everything so I imagine this bug will be hit frequently.  That's because the constructor of TypedArray is inconvenient; unlike Array, you cannot instantiate a filled Float32Array by just passing in the elements (if you have more than one) like "new Vector( 0, 0, 1 )".  This constructor does not exist like for Array counterparts.  You'll instead get a Float32Array of size 0.  The only way to declare vectors is then to either pass an array literal like "new Vector( [ 0, 0, 1 ] )" every time which is messier, or to use Vec.of() instead.

For now I have added a workaround to my framework for iOS by detecting the type of a subclass of Float32Array and then overriding of() and from() if necessary.  

This is an unfortunate workaround, since it measurably slows down my demo by a factor of 5 times.  If this bug in Safari were addressed, I could avoid that and my application would be able to run without such a drastic, unnecessary slowdown on phones.

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20180414/26561365/attachment.html>


More information about the webkit-unassigned mailing list