source: imaps-frontend/node_modules/es-abstract/2016/GetValueFromBuffer.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.3 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $TypeError = require('es-errors/type');
6var $Uint8Array = GetIntrinsic('%Uint8Array%', true);
7
8var callBound = require('call-bind/callBound');
9
10var $charAt = callBound('String.prototype.charAt');
11var $reverse = callBound('Array.prototype.reverse');
12var $slice = callBound('Array.prototype.slice');
13
14var bytesAsFloat32 = require('../helpers/bytesAsFloat32');
15var bytesAsFloat64 = require('../helpers/bytesAsFloat64');
16var bytesAsInteger = require('../helpers/bytesAsInteger');
17var defaultEndianness = require('../helpers/defaultEndianness');
18var isInteger = require('../helpers/isInteger');
19
20var IsDetachedBuffer = require('./IsDetachedBuffer');
21
22var isArrayBuffer = require('is-array-buffer');
23var safeConcat = require('safe-array-concat');
24
25var tableTAO = require('./tables/typed-array-objects');
26
27var isUnsignedElementType = function isUnsignedElementType(type) { return $charAt(type, 0) === 'U'; };
28
29// https://262.ecma-international.org/6.0/#sec-getvaluefrombuffer
30
31module.exports = function GetValueFromBuffer(arrayBuffer, byteIndex, type) {
32 if (!isArrayBuffer(arrayBuffer)) {
33 throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer');
34 }
35
36 if (!isInteger(byteIndex)) {
37 throw new $TypeError('Assertion failed: `byteIndex` must be an integer');
38 }
39
40 if (typeof type !== 'string') {
41 throw new $TypeError('Assertion failed: `type` must be a string');
42 }
43
44 if (arguments.length > 3 && typeof arguments[3] !== 'boolean') {
45 throw new $TypeError('Assertion failed: `isLittleEndian` must be a boolean, if present');
46 }
47
48 if (IsDetachedBuffer(arrayBuffer)) {
49 throw new $TypeError('Assertion failed: ArrayBuffer is detached'); // step 1
50 }
51
52 // 2. Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
53
54 if (byteIndex < 0) {
55 throw new $TypeError('Assertion failed: `byteIndex` must be non-negative'); // step 3
56 }
57
58 // 4. Let block be arrayBuffer’s [[ArrayBufferData]] internal slot.
59
60 var elementSize = tableTAO.size['$' + type]; // step 5
61 if (!elementSize) {
62 throw new $TypeError('Assertion failed: `type` must be one of "Int8", "Uint8", "Uint8C", "Int16", "Uint16", "Int32", "Uint32", "Float32", or "Float64"');
63 }
64
65 // 6. Let rawValue be a List of elementSize containing, in order, the elementSize sequence of bytes starting with block[byteIndex].
66 var rawValue = $slice(new $Uint8Array(arrayBuffer, byteIndex), 0, elementSize); // step 6
67
68 // 8. If isLittleEndian is not present, set isLittleEndian to either true or false. The choice is implementation dependent and should be the alternative that is most efficient for the implementation. An implementation must use the same value each time this step is executed and the same value must be used for the corresponding step in the SetValueInBuffer abstract operation.
69 var isLittleEndian = arguments.length > 3 ? arguments[3] : defaultEndianness === 'little'; // step 7
70
71 if (!isLittleEndian) {
72 $reverse(rawValue); // step 8
73 }
74
75 var bytes = $slice(safeConcat([0, 0, 0, 0, 0, 0, 0, 0], rawValue), -elementSize);
76
77 if (type === 'Float32') { // step 3
78 return bytesAsFloat32(bytes, true);
79 }
80
81 if (type === 'Float64') { // step 4
82 return bytesAsFloat64(bytes, true);
83 }
84
85 return bytesAsInteger(bytes, elementSize, isUnsignedElementType(type), false);
86};
Note: See TracBrowser for help on using the repository browser.