1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var IsFixedLengthArrayBuffer = require('./IsFixedLengthArrayBuffer');
|
---|
6 | var IsViewOutOfBounds = require('./IsViewOutOfBounds');
|
---|
7 |
|
---|
8 | var isDataViewWithBufferWitnessRecord = require('../helpers/records/data-view-with-buffer-witness-record');
|
---|
9 |
|
---|
10 | var dataViewBuffer = require('data-view-buffer');
|
---|
11 | var dataViewByteLength = require('data-view-byte-length');
|
---|
12 | var dataViewByteOffset = require('data-view-byte-offset');
|
---|
13 |
|
---|
14 | // https://262.ecma-international.org/15.0/#sec-getviewbytelength
|
---|
15 |
|
---|
16 | module.exports = function GetViewByteLength(viewRecord) {
|
---|
17 | if (!isDataViewWithBufferWitnessRecord(viewRecord)) {
|
---|
18 | throw new $TypeError('Assertion failed: `viewRecord` must be a DataView with Buffer Witness Record');
|
---|
19 | }
|
---|
20 |
|
---|
21 | if (IsViewOutOfBounds(viewRecord)) {
|
---|
22 | throw new $TypeError('Assertion failed: `viewRecord` is out of bounds'); // step 1
|
---|
23 | }
|
---|
24 |
|
---|
25 | var view = viewRecord['[[Object]]']; // step 2
|
---|
26 |
|
---|
27 | var viewByteLength = dataViewByteLength(view); // view.[[ByteLength]]
|
---|
28 | if (viewByteLength !== 'AUTO') {
|
---|
29 | return viewByteLength; // step 3
|
---|
30 | }
|
---|
31 |
|
---|
32 | if (IsFixedLengthArrayBuffer(dataViewBuffer(view))) {
|
---|
33 | throw new $TypeError('Assertion failed: DataView’s ArrayBuffer is not fixed length'); // step 4
|
---|
34 | }
|
---|
35 |
|
---|
36 | var byteOffset = dataViewByteOffset(view); // step 5
|
---|
37 |
|
---|
38 | var byteLength = viewRecord['[[CachedBufferByteLength]]']; // step 6
|
---|
39 |
|
---|
40 | if (byteLength === 'DETACHED') {
|
---|
41 | throw new $TypeError('Assertion failed: DataView’s ArrayBuffer is detached'); // step 7
|
---|
42 | }
|
---|
43 |
|
---|
44 | return byteLength - byteOffset; // step 8
|
---|
45 | };
|
---|