Skip to content

Commit bcf30f9

Browse files
committed
[Fix] node < v0.6 lacks proper toString behavior on Typed Arrays
1 parent 7684f32 commit bcf30f9

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

polyfill.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,53 @@ if (typeof Uint8Array === 'function' && !Uint8Array.prototype.slice && new Uint8
2323
$Uint32Array: callBind(new Uint32Array(0).slice),
2424
__proto__: null
2525
};
26+
var dispatch = function dispatchSlice(fn, arr, args) {
27+
if (args.length < 2) {
28+
return fn(arr, args.length > 0 ? args[0] : 0);
29+
}
30+
return fn(arr, args[0], args[1]);
31+
};
32+
// eslint-disable-next-line no-unused-vars
2633
ownSlice = function slice(start, end) {
2734
var fn = map['$' + $strSlice($toString(this), 8, -1)];
28-
/* eslint no-invalid-this: 0 */
29-
if (arguments.length < 2) {
30-
return fn(this, arguments.length > 0 ? start : 0);
35+
if (!fn) {
36+
/* eslint max-depth: 0 */
37+
try {
38+
return dispatch(map.$Float32Array, this, arguments);
39+
} catch (e) {
40+
try {
41+
return dispatch(map.$Float64Array, this, arguments);
42+
} catch (e1) {
43+
try {
44+
return dispatch(map.$Int8Array, this, arguments);
45+
} catch (e2) {
46+
try {
47+
return dispatch(map.$Uint8Array, this, arguments);
48+
} catch (e3) {
49+
try {
50+
return dispatch(map.$Uint8ClampedArray, this, arguments);
51+
} catch (e4) {
52+
try {
53+
return dispatch(map.$Int16Array, this, arguments);
54+
} catch (e5) {
55+
try {
56+
return dispatch(map.$Uint16Array, this, arguments);
57+
} catch (e6) {
58+
try {
59+
return dispatch(map.$Int32Array, this, arguments);
60+
} catch (e7) {
61+
return dispatch(map.$Uint32Array, this, arguments);
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
3170
}
32-
return fn(this, start, end);
71+
/* eslint no-invalid-this: 0 */
72+
return dispatch(fn, this, arguments);
3373
};
3474
}
3575

0 commit comments

Comments
 (0)