[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $RangeError = require('es-errors/range');
|
---|
| 4 | var $TypeError = require('es-errors/type');
|
---|
| 5 |
|
---|
| 6 | var ToIndex = require('./ToIndex');
|
---|
| 7 |
|
---|
| 8 | var isTypedArray = require('is-typed-array');
|
---|
| 9 | var typedArrayByteOffset = require('typed-array-byte-offset');
|
---|
| 10 | var typedArrayLength = require('typed-array-length');
|
---|
| 11 | var whichTypedArray = require('which-typed-array');
|
---|
| 12 |
|
---|
| 13 | var tableTAO = require('./tables/typed-array-objects');
|
---|
| 14 |
|
---|
| 15 | // https://262.ecma-international.org/12.0/#sec-validateatomicaccess
|
---|
| 16 |
|
---|
| 17 | module.exports = function ValidateAtomicAccess(typedArray, requestIndex) {
|
---|
| 18 | if (!isTypedArray(typedArray)) {
|
---|
| 19 | throw new $TypeError('Assertion failed: `typedArray` must be a TypedArray'); // step 1
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | var length = typedArrayLength(typedArray); // step 2
|
---|
| 23 |
|
---|
| 24 | var accessIndex = ToIndex(requestIndex); // step 3
|
---|
| 25 |
|
---|
| 26 | /*
|
---|
| 27 | // this assertion can never be reached
|
---|
| 28 | if (!(accessIndex >= 0)) {
|
---|
| 29 | throw new $TypeError('Assertion failed: accessIndex >= 0'); // step 4
|
---|
| 30 | }
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | if (accessIndex >= length) {
|
---|
| 34 | throw new $RangeError('index out of range'); // step 5
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | var arrayTypeName = whichTypedArray(typedArray); // step 6
|
---|
| 38 |
|
---|
| 39 | var taType = tableTAO.name['$' + arrayTypeName];
|
---|
| 40 | var elementSize = tableTAO.size['$' + taType]; // step 7
|
---|
| 41 |
|
---|
| 42 | var offset = typedArrayByteOffset(typedArray); // step 8
|
---|
| 43 |
|
---|
| 44 | return (accessIndex * elementSize) + offset; // step 9
|
---|
| 45 | };
|
---|