react / wstein / node_modules / browserify / node_modules / concat-stream / node_modules / typedarray / index.js
80540 viewsvar undefined = (void 0); // Paranoia12// Beyond this value, index getters/setters (i.e. array[0], array[1]) are so slow to3// create, and consume so much memory, that the browser appears frozen.4var MAX_ARRAY_LENGTH = 1e5;56// Approximations of internal ECMAScript conversion functions7var ECMAScript = (function() {8// Stash a copy in case other scripts modify these9var opts = Object.prototype.toString,10ophop = Object.prototype.hasOwnProperty;1112return {13// Class returns internal [[Class]] property, used to avoid cross-frame instanceof issues:14Class: function(v) { return opts.call(v).replace(/^\[object *|\]$/g, ''); },15HasProperty: function(o, p) { return p in o; },16HasOwnProperty: function(o, p) { return ophop.call(o, p); },17IsCallable: function(o) { return typeof o === 'function'; },18ToInt32: function(v) { return v >> 0; },19ToUint32: function(v) { return v >>> 0; }20};21}());2223// Snapshot intrinsics24var LN2 = Math.LN2,25abs = Math.abs,26floor = Math.floor,27log = Math.log,28min = Math.min,29pow = Math.pow,30round = Math.round;3132// ES5: lock down object properties33function configureProperties(obj) {34if (getOwnPropNames && defineProp) {35var props = getOwnPropNames(obj), i;36for (i = 0; i < props.length; i += 1) {37defineProp(obj, props[i], {38value: obj[props[i]],39writable: false,40enumerable: false,41configurable: false42});43}44}45}4647// emulate ES5 getter/setter API using legacy APIs48// http://blogs.msdn.com/b/ie/archive/2010/09/07/transitioning-existing-code-to-the-es5-getter-setter-apis.aspx49// (second clause tests for Object.defineProperty() in IE<9 that only supports extending DOM prototypes, but50// note that IE<9 does not support __defineGetter__ or __defineSetter__ so it just renders the method harmless)51var defineProp52if (Object.defineProperty && (function() {53try {54Object.defineProperty({}, 'x', {});55return true;56} catch (e) {57return false;58}59})()) {60defineProp = Object.defineProperty;61} else {62defineProp = function(o, p, desc) {63if (!o === Object(o)) throw new TypeError("Object.defineProperty called on non-object");64if (ECMAScript.HasProperty(desc, 'get') && Object.prototype.__defineGetter__) { Object.prototype.__defineGetter__.call(o, p, desc.get); }65if (ECMAScript.HasProperty(desc, 'set') && Object.prototype.__defineSetter__) { Object.prototype.__defineSetter__.call(o, p, desc.set); }66if (ECMAScript.HasProperty(desc, 'value')) { o[p] = desc.value; }67return o;68};69}7071var getOwnPropNames = Object.getOwnPropertyNames || function (o) {72if (o !== Object(o)) throw new TypeError("Object.getOwnPropertyNames called on non-object");73var props = [], p;74for (p in o) {75if (ECMAScript.HasOwnProperty(o, p)) {76props.push(p);77}78}79return props;80};8182// ES5: Make obj[index] an alias for obj._getter(index)/obj._setter(index, value)83// for index in 0 ... obj.length84function makeArrayAccessors(obj) {85if (!defineProp) { return; }8687if (obj.length > MAX_ARRAY_LENGTH) throw new RangeError("Array too large for polyfill");8889function makeArrayAccessor(index) {90defineProp(obj, index, {91'get': function() { return obj._getter(index); },92'set': function(v) { obj._setter(index, v); },93enumerable: true,94configurable: false95});96}9798var i;99for (i = 0; i < obj.length; i += 1) {100makeArrayAccessor(i);101}102}103104// Internal conversion functions:105// pack<Type>() - take a number (interpreted as Type), output a byte array106// unpack<Type>() - take a byte array, output a Type-like number107108function as_signed(value, bits) { var s = 32 - bits; return (value << s) >> s; }109function as_unsigned(value, bits) { var s = 32 - bits; return (value << s) >>> s; }110111function packI8(n) { return [n & 0xff]; }112function unpackI8(bytes) { return as_signed(bytes[0], 8); }113114function packU8(n) { return [n & 0xff]; }115function unpackU8(bytes) { return as_unsigned(bytes[0], 8); }116117function packU8Clamped(n) { n = round(Number(n)); return [n < 0 ? 0 : n > 0xff ? 0xff : n & 0xff]; }118119function packI16(n) { return [(n >> 8) & 0xff, n & 0xff]; }120function unpackI16(bytes) { return as_signed(bytes[0] << 8 | bytes[1], 16); }121122function packU16(n) { return [(n >> 8) & 0xff, n & 0xff]; }123function unpackU16(bytes) { return as_unsigned(bytes[0] << 8 | bytes[1], 16); }124125function packI32(n) { return [(n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff]; }126function unpackI32(bytes) { return as_signed(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], 32); }127128function packU32(n) { return [(n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff]; }129function unpackU32(bytes) { return as_unsigned(bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3], 32); }130131function packIEEE754(v, ebits, fbits) {132133var bias = (1 << (ebits - 1)) - 1,134s, e, f, ln,135i, bits, str, bytes;136137function roundToEven(n) {138var w = floor(n), f = n - w;139if (f < 0.5)140return w;141if (f > 0.5)142return w + 1;143return w % 2 ? w + 1 : w;144}145146// Compute sign, exponent, fraction147if (v !== v) {148// NaN149// http://dev.w3.org/2006/webapi/WebIDL/#es-type-mapping150e = (1 << ebits) - 1; f = pow(2, fbits - 1); s = 0;151} else if (v === Infinity || v === -Infinity) {152e = (1 << ebits) - 1; f = 0; s = (v < 0) ? 1 : 0;153} else if (v === 0) {154e = 0; f = 0; s = (1 / v === -Infinity) ? 1 : 0;155} else {156s = v < 0;157v = abs(v);158159if (v >= pow(2, 1 - bias)) {160e = min(floor(log(v) / LN2), 1023);161f = roundToEven(v / pow(2, e) * pow(2, fbits));162if (f / pow(2, fbits) >= 2) {163e = e + 1;164f = 1;165}166if (e > bias) {167// Overflow168e = (1 << ebits) - 1;169f = 0;170} else {171// Normalized172e = e + bias;173f = f - pow(2, fbits);174}175} else {176// Denormalized177e = 0;178f = roundToEven(v / pow(2, 1 - bias - fbits));179}180}181182// Pack sign, exponent, fraction183bits = [];184for (i = fbits; i; i -= 1) { bits.push(f % 2 ? 1 : 0); f = floor(f / 2); }185for (i = ebits; i; i -= 1) { bits.push(e % 2 ? 1 : 0); e = floor(e / 2); }186bits.push(s ? 1 : 0);187bits.reverse();188str = bits.join('');189190// Bits to bytes191bytes = [];192while (str.length) {193bytes.push(parseInt(str.substring(0, 8), 2));194str = str.substring(8);195}196return bytes;197}198199function unpackIEEE754(bytes, ebits, fbits) {200201// Bytes to bits202var bits = [], i, j, b, str,203bias, s, e, f;204205for (i = bytes.length; i; i -= 1) {206b = bytes[i - 1];207for (j = 8; j; j -= 1) {208bits.push(b % 2 ? 1 : 0); b = b >> 1;209}210}211bits.reverse();212str = bits.join('');213214// Unpack sign, exponent, fraction215bias = (1 << (ebits - 1)) - 1;216s = parseInt(str.substring(0, 1), 2) ? -1 : 1;217e = parseInt(str.substring(1, 1 + ebits), 2);218f = parseInt(str.substring(1 + ebits), 2);219220// Produce number221if (e === (1 << ebits) - 1) {222return f !== 0 ? NaN : s * Infinity;223} else if (e > 0) {224// Normalized225return s * pow(2, e - bias) * (1 + f / pow(2, fbits));226} else if (f !== 0) {227// Denormalized228return s * pow(2, -(bias - 1)) * (f / pow(2, fbits));229} else {230return s < 0 ? -0 : 0;231}232}233234function unpackF64(b) { return unpackIEEE754(b, 11, 52); }235function packF64(v) { return packIEEE754(v, 11, 52); }236function unpackF32(b) { return unpackIEEE754(b, 8, 23); }237function packF32(v) { return packIEEE754(v, 8, 23); }238239240//241// 3 The ArrayBuffer Type242//243244(function() {245246/** @constructor */247var ArrayBuffer = function ArrayBuffer(length) {248length = ECMAScript.ToInt32(length);249if (length < 0) throw new RangeError('ArrayBuffer size is not a small enough positive integer');250251this.byteLength = length;252this._bytes = [];253this._bytes.length = length;254255var i;256for (i = 0; i < this.byteLength; i += 1) {257this._bytes[i] = 0;258}259260configureProperties(this);261};262263exports.ArrayBuffer = exports.ArrayBuffer || ArrayBuffer;264265//266// 4 The ArrayBufferView Type267//268269// NOTE: this constructor is not exported270/** @constructor */271var ArrayBufferView = function ArrayBufferView() {272//this.buffer = null;273//this.byteOffset = 0;274//this.byteLength = 0;275};276277//278// 5 The Typed Array View Types279//280281function makeConstructor(bytesPerElement, pack, unpack) {282// Each TypedArray type requires a distinct constructor instance with283// identical logic, which this produces.284285var ctor;286ctor = function(buffer, byteOffset, length) {287var array, sequence, i, s;288289if (!arguments.length || typeof arguments[0] === 'number') {290// Constructor(unsigned long length)291this.length = ECMAScript.ToInt32(arguments[0]);292if (length < 0) throw new RangeError('ArrayBufferView size is not a small enough positive integer');293294this.byteLength = this.length * this.BYTES_PER_ELEMENT;295this.buffer = new ArrayBuffer(this.byteLength);296this.byteOffset = 0;297} else if (typeof arguments[0] === 'object' && arguments[0].constructor === ctor) {298// Constructor(TypedArray array)299array = arguments[0];300301this.length = array.length;302this.byteLength = this.length * this.BYTES_PER_ELEMENT;303this.buffer = new ArrayBuffer(this.byteLength);304this.byteOffset = 0;305306for (i = 0; i < this.length; i += 1) {307this._setter(i, array._getter(i));308}309} else if (typeof arguments[0] === 'object' &&310!(arguments[0] instanceof ArrayBuffer || ECMAScript.Class(arguments[0]) === 'ArrayBuffer')) {311// Constructor(sequence<type> array)312sequence = arguments[0];313314this.length = ECMAScript.ToUint32(sequence.length);315this.byteLength = this.length * this.BYTES_PER_ELEMENT;316this.buffer = new ArrayBuffer(this.byteLength);317this.byteOffset = 0;318319for (i = 0; i < this.length; i += 1) {320s = sequence[i];321this._setter(i, Number(s));322}323} else if (typeof arguments[0] === 'object' &&324(arguments[0] instanceof ArrayBuffer || ECMAScript.Class(arguments[0]) === 'ArrayBuffer')) {325// Constructor(ArrayBuffer buffer,326// optional unsigned long byteOffset, optional unsigned long length)327this.buffer = buffer;328329this.byteOffset = ECMAScript.ToUint32(byteOffset);330if (this.byteOffset > this.buffer.byteLength) {331throw new RangeError("byteOffset out of range");332}333334if (this.byteOffset % this.BYTES_PER_ELEMENT) {335// The given byteOffset must be a multiple of the element336// size of the specific type, otherwise an exception is raised.337throw new RangeError("ArrayBuffer length minus the byteOffset is not a multiple of the element size.");338}339340if (arguments.length < 3) {341this.byteLength = this.buffer.byteLength - this.byteOffset;342343if (this.byteLength % this.BYTES_PER_ELEMENT) {344throw new RangeError("length of buffer minus byteOffset not a multiple of the element size");345}346this.length = this.byteLength / this.BYTES_PER_ELEMENT;347} else {348this.length = ECMAScript.ToUint32(length);349this.byteLength = this.length * this.BYTES_PER_ELEMENT;350}351352if ((this.byteOffset + this.byteLength) > this.buffer.byteLength) {353throw new RangeError("byteOffset and length reference an area beyond the end of the buffer");354}355} else {356throw new TypeError("Unexpected argument type(s)");357}358359this.constructor = ctor;360361configureProperties(this);362makeArrayAccessors(this);363};364365ctor.prototype = new ArrayBufferView();366ctor.prototype.BYTES_PER_ELEMENT = bytesPerElement;367ctor.prototype._pack = pack;368ctor.prototype._unpack = unpack;369ctor.BYTES_PER_ELEMENT = bytesPerElement;370371// getter type (unsigned long index);372ctor.prototype._getter = function(index) {373if (arguments.length < 1) throw new SyntaxError("Not enough arguments");374375index = ECMAScript.ToUint32(index);376if (index >= this.length) {377return undefined;378}379380var bytes = [], i, o;381for (i = 0, o = this.byteOffset + index * this.BYTES_PER_ELEMENT;382i < this.BYTES_PER_ELEMENT;383i += 1, o += 1) {384bytes.push(this.buffer._bytes[o]);385}386return this._unpack(bytes);387};388389// NONSTANDARD: convenience alias for getter: type get(unsigned long index);390ctor.prototype.get = ctor.prototype._getter;391392// setter void (unsigned long index, type value);393ctor.prototype._setter = function(index, value) {394if (arguments.length < 2) throw new SyntaxError("Not enough arguments");395396index = ECMAScript.ToUint32(index);397if (index >= this.length) {398return undefined;399}400401var bytes = this._pack(value), i, o;402for (i = 0, o = this.byteOffset + index * this.BYTES_PER_ELEMENT;403i < this.BYTES_PER_ELEMENT;404i += 1, o += 1) {405this.buffer._bytes[o] = bytes[i];406}407};408409// void set(TypedArray array, optional unsigned long offset);410// void set(sequence<type> array, optional unsigned long offset);411ctor.prototype.set = function(index, value) {412if (arguments.length < 1) throw new SyntaxError("Not enough arguments");413var array, sequence, offset, len,414i, s, d,415byteOffset, byteLength, tmp;416417if (typeof arguments[0] === 'object' && arguments[0].constructor === this.constructor) {418// void set(TypedArray array, optional unsigned long offset);419array = arguments[0];420offset = ECMAScript.ToUint32(arguments[1]);421422if (offset + array.length > this.length) {423throw new RangeError("Offset plus length of array is out of range");424}425426byteOffset = this.byteOffset + offset * this.BYTES_PER_ELEMENT;427byteLength = array.length * this.BYTES_PER_ELEMENT;428429if (array.buffer === this.buffer) {430tmp = [];431for (i = 0, s = array.byteOffset; i < byteLength; i += 1, s += 1) {432tmp[i] = array.buffer._bytes[s];433}434for (i = 0, d = byteOffset; i < byteLength; i += 1, d += 1) {435this.buffer._bytes[d] = tmp[i];436}437} else {438for (i = 0, s = array.byteOffset, d = byteOffset;439i < byteLength; i += 1, s += 1, d += 1) {440this.buffer._bytes[d] = array.buffer._bytes[s];441}442}443} else if (typeof arguments[0] === 'object' && typeof arguments[0].length !== 'undefined') {444// void set(sequence<type> array, optional unsigned long offset);445sequence = arguments[0];446len = ECMAScript.ToUint32(sequence.length);447offset = ECMAScript.ToUint32(arguments[1]);448449if (offset + len > this.length) {450throw new RangeError("Offset plus length of array is out of range");451}452453for (i = 0; i < len; i += 1) {454s = sequence[i];455this._setter(offset + i, Number(s));456}457} else {458throw new TypeError("Unexpected argument type(s)");459}460};461462// TypedArray subarray(long begin, optional long end);463ctor.prototype.subarray = function(start, end) {464function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }465466start = ECMAScript.ToInt32(start);467end = ECMAScript.ToInt32(end);468469if (arguments.length < 1) { start = 0; }470if (arguments.length < 2) { end = this.length; }471472if (start < 0) { start = this.length + start; }473if (end < 0) { end = this.length + end; }474475start = clamp(start, 0, this.length);476end = clamp(end, 0, this.length);477478var len = end - start;479if (len < 0) {480len = 0;481}482483return new this.constructor(484this.buffer, this.byteOffset + start * this.BYTES_PER_ELEMENT, len);485};486487return ctor;488}489490var Int8Array = makeConstructor(1, packI8, unpackI8);491var Uint8Array = makeConstructor(1, packU8, unpackU8);492var Uint8ClampedArray = makeConstructor(1, packU8Clamped, unpackU8);493var Int16Array = makeConstructor(2, packI16, unpackI16);494var Uint16Array = makeConstructor(2, packU16, unpackU16);495var Int32Array = makeConstructor(4, packI32, unpackI32);496var Uint32Array = makeConstructor(4, packU32, unpackU32);497var Float32Array = makeConstructor(4, packF32, unpackF32);498var Float64Array = makeConstructor(8, packF64, unpackF64);499500exports.Int8Array = exports.Int8Array || Int8Array;501exports.Uint8Array = exports.Uint8Array || Uint8Array;502exports.Uint8ClampedArray = exports.Uint8ClampedArray || Uint8ClampedArray;503exports.Int16Array = exports.Int16Array || Int16Array;504exports.Uint16Array = exports.Uint16Array || Uint16Array;505exports.Int32Array = exports.Int32Array || Int32Array;506exports.Uint32Array = exports.Uint32Array || Uint32Array;507exports.Float32Array = exports.Float32Array || Float32Array;508exports.Float64Array = exports.Float64Array || Float64Array;509}());510511//512// 6 The DataView View Type513//514515(function() {516function r(array, index) {517return ECMAScript.IsCallable(array.get) ? array.get(index) : array[index];518}519520var IS_BIG_ENDIAN = (function() {521var u16array = new(exports.Uint16Array)([0x1234]),522u8array = new(exports.Uint8Array)(u16array.buffer);523return r(u8array, 0) === 0x12;524}());525526// Constructor(ArrayBuffer buffer,527// optional unsigned long byteOffset,528// optional unsigned long byteLength)529/** @constructor */530var DataView = function DataView(buffer, byteOffset, byteLength) {531if (arguments.length === 0) {532buffer = new exports.ArrayBuffer(0);533} else if (!(buffer instanceof exports.ArrayBuffer || ECMAScript.Class(buffer) === 'ArrayBuffer')) {534throw new TypeError("TypeError");535}536537this.buffer = buffer || new exports.ArrayBuffer(0);538539this.byteOffset = ECMAScript.ToUint32(byteOffset);540if (this.byteOffset > this.buffer.byteLength) {541throw new RangeError("byteOffset out of range");542}543544if (arguments.length < 3) {545this.byteLength = this.buffer.byteLength - this.byteOffset;546} else {547this.byteLength = ECMAScript.ToUint32(byteLength);548}549550if ((this.byteOffset + this.byteLength) > this.buffer.byteLength) {551throw new RangeError("byteOffset and length reference an area beyond the end of the buffer");552}553554configureProperties(this);555};556557function makeGetter(arrayType) {558return function(byteOffset, littleEndian) {559560byteOffset = ECMAScript.ToUint32(byteOffset);561562if (byteOffset + arrayType.BYTES_PER_ELEMENT > this.byteLength) {563throw new RangeError("Array index out of range");564}565byteOffset += this.byteOffset;566567var uint8Array = new exports.Uint8Array(this.buffer, byteOffset, arrayType.BYTES_PER_ELEMENT),568bytes = [], i;569for (i = 0; i < arrayType.BYTES_PER_ELEMENT; i += 1) {570bytes.push(r(uint8Array, i));571}572573if (Boolean(littleEndian) === Boolean(IS_BIG_ENDIAN)) {574bytes.reverse();575}576577return r(new arrayType(new exports.Uint8Array(bytes).buffer), 0);578};579}580581DataView.prototype.getUint8 = makeGetter(exports.Uint8Array);582DataView.prototype.getInt8 = makeGetter(exports.Int8Array);583DataView.prototype.getUint16 = makeGetter(exports.Uint16Array);584DataView.prototype.getInt16 = makeGetter(exports.Int16Array);585DataView.prototype.getUint32 = makeGetter(exports.Uint32Array);586DataView.prototype.getInt32 = makeGetter(exports.Int32Array);587DataView.prototype.getFloat32 = makeGetter(exports.Float32Array);588DataView.prototype.getFloat64 = makeGetter(exports.Float64Array);589590function makeSetter(arrayType) {591return function(byteOffset, value, littleEndian) {592593byteOffset = ECMAScript.ToUint32(byteOffset);594if (byteOffset + arrayType.BYTES_PER_ELEMENT > this.byteLength) {595throw new RangeError("Array index out of range");596}597598// Get bytes599var typeArray = new arrayType([value]),600byteArray = new exports.Uint8Array(typeArray.buffer),601bytes = [], i, byteView;602603for (i = 0; i < arrayType.BYTES_PER_ELEMENT; i += 1) {604bytes.push(r(byteArray, i));605}606607// Flip if necessary608if (Boolean(littleEndian) === Boolean(IS_BIG_ENDIAN)) {609bytes.reverse();610}611612// Write them613byteView = new exports.Uint8Array(this.buffer, byteOffset, arrayType.BYTES_PER_ELEMENT);614byteView.set(bytes);615};616}617618DataView.prototype.setUint8 = makeSetter(exports.Uint8Array);619DataView.prototype.setInt8 = makeSetter(exports.Int8Array);620DataView.prototype.setUint16 = makeSetter(exports.Uint16Array);621DataView.prototype.setInt16 = makeSetter(exports.Int16Array);622DataView.prototype.setUint32 = makeSetter(exports.Uint32Array);623DataView.prototype.setInt32 = makeSetter(exports.Int32Array);624DataView.prototype.setFloat32 = makeSetter(exports.Float32Array);625DataView.prototype.setFloat64 = makeSetter(exports.Float64Array);626627exports.DataView = exports.DataView || DataView;628629}());630631632