From 94486302f50b122b35ff32314b34a07282f03daa Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 20 Jul 2014 09:49:25 -0700 Subject: [PATCH] Correct `ArrayBuffer` bizarro shims. --- test/index.html | 30 ++++++++++++++++++++---------- test/test.js | 30 +++++++++++++++++++----------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/test/index.html b/test/index.html index b60e724fd..5ba72c52f 100644 --- a/test/index.html +++ b/test/index.html @@ -113,17 +113,27 @@ setProperty(window, '_ArrayBuffer', window.ArrayBuffer); if (window.ArrayBuffer && window.Uint8Array && !new ArrayBuffer(0).slice) { - setProperty(window, 'ArrayBuffer', function(byteLength) { - var buffer = new _ArrayBuffer(byteLength); - buffer.slice = !byteLength ? Array.prototype.slice : function() { - var newBuffer = new _ArrayBuffer(byteLength), - view = new Uint8Array(newBuffer); + setProperty(window, 'ArrayBuffer', (function() { + function ArrayBuffer(byteLength) { + var buffer = new _ArrayBuffer(byteLength); + buffer.slice = function() { + var newBuffer = new _ArrayBuffer(byteLength), + view = new Uint8Array(newBuffer); - view.set(new Uint8Array(this)); - return newBuffer; - }; - return buffer; - }); + view.set(new Uint8Array(this)); + return newBuffer; + }; + setProperty(buffer.slice, 'toString', sliceToString); + return buffer; + } + var reToString = /toString/g, + nativeString = Function.prototype.toString.call(toString), + bufferToString = _.constant(nativeString.replace(reToString, 'ArrayBuffer')), + sliceToString = _.constant(nativeString.replace(reToString, 'slice')); + + setProperty(ArrayBuffer, 'toString', bufferToString); + return ArrayBuffer; + }())); } setProperty(window, '_Float64Array', window.Float64Array); setProperty(window, 'Float64Array', window.Float64Array ? function() {} : window.Uint8Array); diff --git a/test/test.js b/test/test.js index ddcdc42eb..ef5976860 100644 --- a/test/test.js +++ b/test/test.js @@ -387,19 +387,27 @@ var _ArrayBuffer = ArrayBuffer; if (Uint8Array && new ArrayBuffer(0).slice) { - setProperty(root, 'ArrayBuffer', function(byteLength) { - var buffer = new _ArrayBuffer(byteLength); - buffer.slice = !byteLength ? slice : function() { - var newBuffer = new _ArrayBuffer(byteLength), - view = new Uint8Array(newBuffer); + setProperty(root, 'ArrayBuffer', (function() { + function ArrayBuffer(byteLength) { + var buffer = new _ArrayBuffer(byteLength); + buffer.slice = function() { + var newBuffer = new _ArrayBuffer(byteLength), + view = new Uint8Array(newBuffer); - view.set(new Uint8Array(this)); - return newBuffer; - }; - return buffer; - }); + view.set(new Uint8Array(this)); + return newBuffer; + }; + setProperty(buffer.slice, 'toString', sliceToString); + return buffer; + } + var reToString = /toString/g, + nativeString = _fnToString.call(toString), + bufferToString = _.constant(nativeString.replace(reToString, 'ArrayBuffer')), + sliceToString = _.constant(nativeString.replace(reToString, 'slice')); - setPropertu(ArrayBuffer, 'toString', _.constant(String(toString).replace(/toString/g, 'ArrayBuffer'))); + setProperty(ArrayBuffer, 'toString', bufferToString); + return ArrayBuffer; + }())); } var _Float64Array = root.Float64Array; setProperty(root, 'Float64Array', _Float64Array ? _.noop : root.Uint8Array);