prof3ssorSt3v3 / crypto-js

Automatically exported from code.google.com/p/crypto-js
1 stars 0 forks source link

[PATCH] Simplify typed arrays ArrayBufferView checking #70

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Here's a patch that should simplify handling of the various array views. All of 
them subclass ArrayBufferView (which is the interface that provides the 
buffer/byteOffset/byteLength properties), so just check for that. This also 
makes the code support passing in a DataView.

Index: src/lib-typedarrays.js
===================================================================
--- src/lib-typedarrays.js  (revision 656)
+++ src/lib-typedarrays.js  (working copy)
@@ -20,19 +20,7 @@
         }

         // Convert other array views to uint8
-        if (
-            typedArray instanceof Int8Array ||
-            ( // Safari doesn't seem to support Uint8ClampedArray yet
-                typeof Uint8ClampedArray !== 'undefined' &&
-                typedArray instanceof Uint8ClampedArray
-            ) ||
-            typedArray instanceof Int16Array ||
-            typedArray instanceof Uint16Array ||
-            typedArray instanceof Int32Array ||
-            typedArray instanceof Uint32Array ||
-            typedArray instanceof Float32Array ||
-            typedArray instanceof Float64Array
-        ) {
+        if (typedArray instanceof ArrayBufferView && !(typedArray instanceof 
Uint8Array)) {
             typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);
         }

Original issue reported on code.google.com by davidben@davidben.net on 1 Feb 2013 at 7:59

GoogleCodeExporter commented 8 years ago
That would be incredibly convenient, but, unfortunately, as far as I can tell 
typeof ArrayBufferView returns undefined in both Chrome and Firefox.

Original comment by Jeff.Mott.OR on 1 Feb 2013 at 8:38

GoogleCodeExporter commented 8 years ago
It's "function" for me in Chrome (I'm running the beta. It's possible they only 
just made that work?). But now that I check in Firefox, it is missing on my end 
too. And in Opera. Ugh.

Original comment by davidben@davidben.net on 1 Feb 2013 at 9:09

GoogleCodeExporter commented 8 years ago
Found this issue in Chrome.

http://code.google.com/p/chromium/issues/detail?id=60449

It looks like ArrayBufferView is technically not supposed to be exposed, 
according to the specs, but Chrome exposed it anyway at popular request, which 
happened just one month ago.

One day, the other browsers might follow suit, but for now we unfortunately 
can't rely on it.

Original comment by Jeff.Mott.OR on 1 Feb 2013 at 11:48