-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds update task to
ArrayBuffer
to fix incomplete browser implement…
…ations
- Loading branch information
1 parent
c5e838d
commit f27fd4f
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,4 @@ samsung_mob = "<2" | |
[install] | ||
module = "js-polyfills" | ||
paths = [ "typedarray.js" ] | ||
postinstall = "update.task.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |