source: imaps-frontend/node_modules/es-abstract/2021/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: 4.5 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $SyntaxError = require('es-errors/syntax');
6var $TypeError = require('es-errors/type');
7var $Uint8Array = GetIntrinsic('%Uint8Array%', true);
8
9var callBound = require('call-bind/callBound');
10
11var $slice = callBound('Array.prototype.slice');
12
13var isInteger = require('../helpers/isInteger');
14
15var IsDetachedBuffer = require('./IsDetachedBuffer');
16var RawBytesToNumeric = require('./RawBytesToNumeric');
17
18var isArrayBuffer = require('is-array-buffer');
19var isSharedArrayBuffer = require('is-shared-array-buffer');
20var safeConcat = require('safe-array-concat');
21
22var tableTAO = require('./tables/typed-array-objects');
23
24var defaultEndianness = require('../helpers/defaultEndianness');
25
26// https://262.ecma-international.org/11.0/#sec-getvaluefrombuffer
27
28module.exports = function GetValueFromBuffer(arrayBuffer, byteIndex, type, isTypedArray, order) {
29 var isSAB = isSharedArrayBuffer(arrayBuffer);
30 if (!isArrayBuffer(arrayBuffer) && !isSAB) {
31 throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or a SharedArrayBuffer');
32 }
33
34 if (!isInteger(byteIndex)) {
35 throw new $TypeError('Assertion failed: `byteIndex` must be an integer');
36 }
37
38 if (typeof type !== 'string' || typeof tableTAO.size['$' + type] !== 'number') {
39 throw new $TypeError('Assertion failed: `type` must be a Typed Array element type');
40 }
41
42 if (typeof isTypedArray !== 'boolean') {
43 throw new $TypeError('Assertion failed: `isTypedArray` must be a boolean');
44 }
45
46 if (order !== 'SeqCst' && order !== 'Unordered') {
47 throw new $TypeError('Assertion failed: `order` must be either `SeqCst` or `Unordered`');
48 }
49
50 if (arguments.length > 5 && typeof arguments[5] !== 'boolean') {
51 throw new $TypeError('Assertion failed: `isLittleEndian` must be a boolean, if present');
52 }
53
54 if (IsDetachedBuffer(arrayBuffer)) {
55 throw new $TypeError('Assertion failed: `arrayBuffer` is detached'); // step 1
56 }
57
58 // 2. Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
59
60 if (byteIndex < 0) {
61 throw new $TypeError('Assertion failed: `byteIndex` must be non-negative'); // step 3
62 }
63
64 // 4. Let block be arrayBuffer.[[ArrayBufferData]].
65
66 var elementSize = tableTAO.size['$' + type]; // step 5
67 if (!elementSize) {
68 throw new $TypeError('Assertion failed: `type` must be one of "Int8", "Uint8", "Uint8C", "Int16", "Uint16", "Int32", "Uint32", "BigInt64", "BigUint64", "Float32", or "Float64"');
69 }
70
71 var rawValue;
72 if (isSAB) { // step 6
73 /*
74 a. Let execution be the [[CandidateExecution]] field of the surrounding agent's Agent Record.
75 b. Let eventList be the [[EventList]] field of the element in execution.[[EventLists]] whose [[AgentSignifier]] is AgentSignifier().
76 c. If isTypedArray is true and type is "Int8", "Uint8", "Int16", "Uint16", "Int32", or "Uint32", let noTear be true; otherwise let noTear be false.
77 d. Let rawValue be a List of length elementSize of nondeterministically chosen byte values.
78 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.
79 f. Let readEvent be ReadSharedMemory{ [[Order]]: order, [[NoTear]]: noTear, [[Block]]: block, [[ByteIndex]]: byteIndex, [[ElementSize]]: elementSize }.
80 g. Append readEvent to eventList.
81 h. Append Chosen Value Record { [[Event]]: readEvent, [[ChosenValue]]: rawValue } to execution.[[ChosenValues]].
82 */
83 throw new $SyntaxError('SharedArrayBuffer is not supported by this implementation');
84 } else {
85 // 7. Let rawValue be a List of elementSize containing, in order, the elementSize sequence of bytes starting with block[byteIndex].
86 rawValue = $slice(new $Uint8Array(arrayBuffer, byteIndex), 0, elementSize); // step 6
87 }
88
89 // 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.
90 var isLittleEndian = arguments.length > 5 ? arguments[5] : defaultEndianness === 'little'; // step 8
91
92 var bytes = isLittleEndian
93 ? $slice(safeConcat([0, 0, 0, 0, 0, 0, 0, 0], rawValue), -elementSize)
94 : $slice(safeConcat(rawValue, [0, 0, 0, 0, 0, 0, 0, 0]), 0, elementSize);
95
96 return RawBytesToNumeric(type, bytes, isLittleEndian);
97};
Note: See TracBrowser for help on using the repository browser.