Skip to content

Commit

Permalink
Adds update task to ArrayBuffer to fix incomplete browser implement…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
mhassan1 authored and JakeChampion committed Jul 8, 2022
1 parent c5e838d commit f27fd4f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions polyfills/ArrayBuffer/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ samsung_mob = "<2"
[install]
module = "js-polyfills"
paths = [ "typedarray.js" ]
postinstall = "update.task.js"
3 changes: 3 additions & 0 deletions polyfills/ArrayBuffer/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// use "Int8Array" as a proxy for support of "TypedArray" subclasses
// confirm that the prototype of "Int8Array" is NOT the "Object" prototype, which is a bug in IE11 and maybe other old browsers
'ArrayBuffer' in self && 'DataView' in self && 'Int8Array' in self && Object.getPrototypeOf(self.Int8Array) !== Object.getPrototypeOf(Object)
52 changes: 52 additions & 0 deletions polyfills/ArrayBuffer/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-env mocha, browser */
/* global proclaim, ArrayBuffer, DataView, Int8Array */

describe('ArrayBuffer', function () {
it('should expose a property named ArrayBuffer on the global object', function() {
proclaim.isTrue('ArrayBuffer' in window);
});

it('should be a function', function() {
proclaim.isFunction(ArrayBuffer);
});

it('should throw an error if called without `new` operator', function () {
proclaim.throws(function () {
ArrayBuffer();
}, TypeError);
});
});

describe('DataView', function () {
it('should expose a property named DataView on the global object', function() {
proclaim.isTrue('ArrayBuffer' in window);
});

it('should be a function', function() {
proclaim.isFunction(DataView);
});

it('should throw an error if called without `new` operator', function () {
proclaim.throws(function () {
DataView();
}, TypeError);
});
});

// use "Int8Array" as a proxy for all "TypedArray" subclasses

describe('Int8Array', function () {
it('should expose a property named Int8Array on the global object', function() {
proclaim.isTrue('Int8Array' in window);
});

it('should be a function', function() {
proclaim.isFunction(Int8Array);
});

it('should throw an error if called without `new` operator', function () {
proclaim.throws(function () {
Int8Array();
}, TypeError);
});
});
53 changes: 53 additions & 0 deletions polyfills/ArrayBuffer/update.task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-env node */

'use strict';

var fs = require('graceful-fs');
var path = require('path');
var arrayBufferPolyfillOutput = path.resolve('polyfills/ArrayBuffer/polyfill.js');

var polyfill = fs.readFileSync(arrayBufferPolyfillOutput, 'utf-8');

// always attach these to `self`, since the existing browser implementation may be incomplete (e.g. IE11)
[
'ArrayBuffer',
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array',
'DataView'
].forEach(function (kind) {
polyfill = polyfill.replace(
'global.' + kind + ' = global.' + kind + ' || ' + kind + ';',
'global.' + kind + ' = ' + kind + ';'
)
});

// add helper function for defining properties correctly (all properties should be `writable` and `configurable`)
polyfill = polyfill.replace(
/(function packF32\(v\).+?\n)/,
'$1\n' +
' function ObjectDefinePropertyConfigurable(o, prop, desc) {\n' +
' desc.writable = true;\n' +
' desc.configurable = true;\n' +
' return Object.defineProperty(o, prop, desc);\n' +
' }\n'
);

// replace usage of `Object.defineProperty` with helper function
[
'$TypedArray$',
'this',
'DataView'
].forEach(function (kind) {
polyfill = polyfill
.split('Object.defineProperty(' + kind)
.join('ObjectDefinePropertyConfigurable(' + kind)
});

fs.writeFileSync(arrayBufferPolyfillOutput, polyfill, 'utf-8');

0 comments on commit f27fd4f

Please sign in to comment.