123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 'use strict';
- var GetIntrinsic = require('get-intrinsic');
- var $SyntaxError = require('es-errors/syntax');
- var $TypeError = require('es-errors/type');
- 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 RawBytesToNumeric = require('./RawBytesToNumeric');
- var isArrayBuffer = require('is-array-buffer');
- var isSharedArrayBuffer = require('is-shared-array-buffer');
- var safeConcat = require('safe-array-concat');
- var table61 = {
- __proto__: null,
- $Int8: 1,
- $Uint8: 1,
- $Uint8C: 1,
- $Int16: 2,
- $Uint16: 2,
- $Int32: 4,
- $Uint32: 4,
- $BigInt64: 8,
- $BigUint64: 8,
- $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' || typeof table61['$' + type] !== 'number') {
- throw new $TypeError('Assertion failed: `type` must be a Typed Array element type');
- }
- if (typeof isTypedArray !== 'boolean') {
- throw new $TypeError('Assertion failed: `isTypedArray` must be a boolean');
- }
- if (order !== 'SeqCst' && order !== 'Unordered') {
- throw new $TypeError('Assertion failed: `order` must be either `SeqCst` or `Unordered`');
- }
- 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 = table61['$' + type];
- if (!elementSize) {
- throw new $TypeError('Assertion failed: `type` must be one of "Int8", "Uint8", "Uint8C", "Int16", "Uint16", "Int32", "Uint32", "BigInt64", "BigUint64", "Float32", or "Float64"');
- }
- var rawValue;
- if (isSAB) {
-
- throw new $SyntaxError('SharedArrayBuffer is not supported by this implementation');
- } 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 RawBytesToNumeric(type, bytes, isLittleEndian);
- };
|