From 66e9c68da40da6262dbf1b5d5ac393de315090a7 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 18 Jul 2014 08:48:05 -0700 Subject: [PATCH] Add bizarro tests for shimmed `ArrayBuffer#slice`. --- test/index.html | 34 ++++++++++++++++++++++++++-------- test/test.js | 46 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/test/index.html b/test/index.html index 94101934f..79128e540 100644 --- a/test/index.html +++ b/test/index.html @@ -108,14 +108,28 @@ setProperty(document, '_createDocumentFragment', document.createDocumentFragment); document.createDocumentFragment = function() {}; - setProperty(window, '_Float64Array', window.Float64Array); - if (!window._Float64Array) { - setProperty(window, 'Float64Array', window.Uint8Array); + 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); + + view.set(new Uint8Array(this)); + return newBuffer; + }; + return buffer; + }); } + setProperty(window, '_Float64Array', window.Float64Array); + setProperty(window, 'Float64Array', window.Float64Array ? function() {} : window.Uint8Array); + + setProperty(window, 'WinRTError', Error); + setProperty(window, 'exports', window); setProperty(window, 'global', window); setProperty(window, 'module', {}); - setProperty(window, 'WinRTError', Error); } function removeBizarroMethods() { @@ -159,19 +173,23 @@ } else { delete String.prototype.contains; } - if (!window._Float64Array) { - setProperty(window, 'Float64Array', undefined); - } setProperty(Function.prototype, 'toString', Function.prototype._toString); setProperty(Object.prototype, 'hasOwnProperty', Object.prototype._hasOwnProperty); document.createDocumentFragment = document._createDocumentFragment; setProperty(document, '_createDocumentFragment', undefined); + setProperty(window, 'ArrayBuffer', window._ArrayBuffer); + setProperty(window, '_ArrayBuffer', undefined); + + setProperty(window, 'Float64Array', window._Float64Array); + setProperty(window, '_Float64Array', undefined); + + setProperty(window, 'WinRTError', undefined); + setProperty(window, 'exports', undefined); setProperty(window, 'global', undefined); setProperty(window, 'module', undefined); - setProperty(window, 'WinRTError', undefined); delete Array._isArray; delete Date._now; diff --git a/test/test.js b/test/test.js index f3d1fe436..a0f95ecd1 100644 --- a/test/test.js +++ b/test/test.js @@ -339,16 +339,6 @@ return result; }); - // fake DOM - setProperty(root, 'window', {}); - setProperty(root.window, 'document', {}); - setProperty(root.window.document, 'createDocumentFragment', function() { - return { 'nodeType': 11 }; - }); - - // fake `WinRTError` - setProperty(root, 'WinRTError', Error); - // add extensions Function.prototype._method = _.noop; @@ -385,10 +375,33 @@ var _contains = String.prototype.contains; setProperty(String.prototype, 'contains', _contains ? _.noop : Boolean); - var _Float64Array = root.Float64Array; - if (!_Float64Array) { - setProperty(root, 'Float64Array', root.Uint8Array); + var _ArrayBuffer = root.ArrayBuffer; + if (root.Uint8Array && _ArrayBuffer && !new _ArrayBuffer(0).slice) { + setProperty(window, 'ArrayBuffer', function(byteLength) { + var buffer = new _ArrayBuffer(byteLength); + buffer.slice = !byteLength ? slice : function() { + var newBuffer = new _ArrayBuffer(byteLength), + view = new Uint8Array(newBuffer); + + view.set(new Uint8Array(this)); + return newBuffer; + }; + return buffer; + }); } + var _Float64Array = root.Float64Array; + setProperty(root, 'Float64Array', _Float64Array ? _.noop : root.Uint8Array); + + // fake `WinRTError` + setProperty(root, 'WinRTError', Error); + + // fake DOM + setProperty(root, 'window', {}); + setProperty(root.window, 'document', {}); + setProperty(root.window.document, 'createDocumentFragment', function() { + return { 'nodeType': 11 }; + }); + // clear cache so Lo-Dash can be reloaded emptyObject(require.cache); @@ -416,7 +429,12 @@ } else { delete String.prototype.contains; } - if (!_Float64Array) { + if (_ArrayBuffer) { + setProperty(root, 'ArrayBuffer', _ArrayBuffer); + } + if (_Float64Array) { + setProperty(root, 'Float64Array', _Float64Array); + } else { delete root.Float64Array; } delete root.window;