|
'use strict'; |
|
|
|
var GetIntrinsic = require('get-intrinsic'); |
|
|
|
var $SyntaxError = GetIntrinsic('%SyntaxError%'); |
|
var $TypeError = GetIntrinsic('%TypeError%'); |
|
var $Uint8Array = GetIntrinsic('%Uint8Array%', true); |
|
|
|
var callBound = require('call-bind/callBound'); |
|
|
|
var $slice = callBound('Array.prototype.slice'); |
|
|
|
var isInteger = require('../helpers/isInteger'); |
|
|
|
var IsDetachedBuffer = require('./IsDetachedBuffer'); |
|
var RawBytesToNumber = require('./RawBytesToNumber'); |
|
|
|
var isArrayBuffer = require('is-array-buffer'); |
|
var isSharedArrayBuffer = require('is-shared-array-buffer'); |
|
var safeConcat = require('safe-array-concat'); |
|
|
|
var table50 = { |
|
__proto__: null, |
|
$Int8: 1, |
|
$Uint8: 1, |
|
$Uint8C: 1, |
|
$Int16: 2, |
|
$Uint16: 2, |
|
$Int32: 4, |
|
$Uint32: 4, |
|
$Float32: 4, |
|
$Float64: 8 |
|
}; |
|
|
|
var defaultEndianness = require('../helpers/defaultEndianness'); |
|
|
|
|
|
|
|
module.exports = function GetValueFromBuffer(arrayBuffer, byteIndex, type, isTypedArray, order) { |
|
var isSAB = isSharedArrayBuffer(arrayBuffer); |
|
if (!isArrayBuffer(arrayBuffer) && !isSAB) { |
|
throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or a SharedArrayBuffer'); |
|
} |
|
|
|
if (!isInteger(byteIndex)) { |
|
throw new $TypeError('Assertion failed: `byteIndex` must be an integer'); |
|
} |
|
|
|
if (typeof type !== 'string') { |
|
throw new $TypeError('Assertion failed: `type` must be a string'); |
|
} |
|
|
|
if (typeof isTypedArray !== 'boolean') { |
|
throw new $TypeError('Assertion failed: `isTypedArray` must be a boolean'); |
|
} |
|
|
|
if (typeof order !== 'string') { |
|
throw new $TypeError('Assertion failed: `order` must be a string'); |
|
} |
|
|
|
if (arguments.length > 5 && typeof arguments[5] !== 'boolean') { |
|
throw new $TypeError('Assertion failed: `isLittleEndian` must be a boolean, if present'); |
|
} |
|
|
|
if (IsDetachedBuffer(arrayBuffer)) { |
|
throw new $TypeError('Assertion failed: `arrayBuffer` is detached'); |
|
} |
|
|
|
|
|
|
|
if (byteIndex < 0) { |
|
throw new $TypeError('Assertion failed: `byteIndex` must be non-negative'); |
|
} |
|
|
|
|
|
|
|
var elementSize = table50['$' + type]; |
|
if (!elementSize) { |
|
throw new $TypeError('Assertion failed: `type` must be one of "Int8", "Uint8", "Uint8C", "Int16", "Uint16", "Int32", "Uint32", "Float32", or "Float64"'); |
|
} |
|
|
|
var rawValue; |
|
if (isSAB) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new $SyntaxError('TODO: support SharedArrayBuffers'); |
|
} else { |
|
|
|
rawValue = $slice(new $Uint8Array(arrayBuffer, byteIndex), 0, elementSize); |
|
} |
|
|
|
|
|
var isLittleEndian = arguments.length > 5 ? arguments[5] : defaultEndianness === 'little'; |
|
|
|
var bytes = isLittleEndian |
|
? $slice(safeConcat([0, 0, 0, 0, 0, 0, 0, 0], rawValue), -elementSize) |
|
: $slice(safeConcat(rawValue, [0, 0, 0, 0, 0, 0, 0, 0]), 0, elementSize); |
|
|
|
return RawBytesToNumber(type, bytes, isLittleEndian); |
|
}; |
|
|