source: imaps-frontend/node_modules/es-abstract/2019/GetValueFromBuffer.js@ d565449

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[d565449]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 $slice = callBound('Array.prototype.slice');
11
12var isInteger = require('../helpers/isInteger');
13
14var IsDetachedBuffer = require('./IsDetachedBuffer');
15var RawBytesToNumber = require('./RawBytesToNumber');
16
17var isArrayBuffer = require('is-array-buffer');
18var isSharedArrayBuffer = require('is-shared-array-buffer');
19var safeConcat = require('safe-array-concat');
20
21var tableTAO = require('./tables/typed-array-objects');
22
23var defaultEndianness = require('../helpers/defaultEndianness');
24
25// https://262.ecma-international.org/10.0/#sec-getvaluefrombuffer
26
27module.exports = function GetValueFromBuffer(arrayBuffer, byteIndex, type, isTypedArray, order) {
28 var isSAB = isSharedArrayBuffer(arrayBuffer);
29 if (!isArrayBuffer(arrayBuffer) && !isSAB) {
30 throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or a SharedArrayBuffer');
31 }
32
33 if (!isInteger(byteIndex)) {
34 throw new $TypeError('Assertion failed: `byteIndex` must be an integer');
35 }
36
37 if (typeof type !== 'string') {
38 throw new $TypeError('Assertion failed: `type` must be a string');
39 }
40
41 if (typeof isTypedArray !== 'boolean') {
42 throw new $TypeError('Assertion failed: `isTypedArray` must be a boolean');
43 }
44
45 if (typeof order !== 'string') {
46 throw new $TypeError('Assertion failed: `order` must be a string');
47 }
48
49 if (arguments.length > 5 && typeof arguments[5] !== 'boolean') {
50 throw new $TypeError('Assertion failed: `isLittleEndian` must be a boolean, if present');
51 }
52
53 if (IsDetachedBuffer(arrayBuffer)) {
54 throw new $TypeError('Assertion failed: `arrayBuffer` is detached'); // step 1
55 }
56
57 // 2. Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
58
59 if (byteIndex < 0) {
60 throw new $TypeError('Assertion failed: `byteIndex` must be non-negative'); // step 3
61 }
62
63 // 4. Let block be arrayBuffer.[[ArrayBufferData]].
64
65 var elementSize = tableTAO.size['$' + type]; // step 5
66 if (!elementSize) {
67 throw new $TypeError('Assertion failed: `type` must be one of "Int8", "Uint8", "Uint8C", "Int16", "Uint16", "Int32", "Uint32", "Float32", or "Float64"');
68 }
69
70 var rawValue;
71 if (isSAB) { // step 6
72 /*
73 a. Let execution be the [[CandidateExecution]] field of the surrounding agent's Agent Record.
74 b. Let eventList be the [[EventList]] field of the element in execution.[[EventsRecords]] whose [[AgentSignifier]] is AgentSignifier().
75 c. If isTypedArray is true and type is "Int8", "Uint8", "Int16", "Uint16", "Int32", or "Uint32", let noTear be true; otherwise let noTear be false.
76 d. Let rawValue be a List of length elementSize of nondeterministically chosen byte values.
77 e. NOTE: In implementations, rawValue is the result of a non-atomic or atomic read instruction on the underlying hardware. The nondeterminism is a semantic prescription of the memory model to describe observable behaviour of hardware with weak consistency.
78 f. Let readEvent be ReadSharedMemory{ [[Order]]: order, [[NoTear]]: noTear, [[Block]]: block, [[ByteIndex]]: byteIndex, [[ElementSize]]: elementSize }.
79 g. Append readEvent to eventList.
80 h. Append Chosen Value Record { [[Event]]: readEvent, [[ChosenValue]]: rawValue } to execution.[[ChosenValues]].
81 */
82 } else {
83 // 7. Let rawValue be a List of elementSize containing, in order, the elementSize sequence of bytes starting with block[byteIndex].
84 rawValue = $slice(new $Uint8Array(arrayBuffer, byteIndex), 0, elementSize); // step 6
85 }
86
87 // 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.
88 var isLittleEndian = arguments.length > 5 ? arguments[5] : defaultEndianness === 'little'; // step 8
89
90 var bytes = isLittleEndian
91 ? $slice(safeConcat([0, 0, 0, 0, 0, 0, 0, 0], rawValue), -elementSize)
92 : $slice(safeConcat(rawValue, [0, 0, 0, 0, 0, 0, 0, 0]), 0, elementSize);
93
94 return RawBytesToNumber(type, bytes, isLittleEndian);
95};
Note: See TracBrowser for help on using the repository browser.