the-forest/client/node_modules/es-abstract/2024/GetViewByteLength.js
2024-09-17 20:35:18 -04:00

46 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
var $TypeError = require('es-errors/type');
var IsFixedLengthArrayBuffer = require('./IsFixedLengthArrayBuffer');
var IsViewOutOfBounds = require('./IsViewOutOfBounds');
var isDataViewWithBufferWitnessRecord = require('../helpers/records/data-view-with-buffer-witness-record');
var dataViewBuffer = require('data-view-buffer');
var dataViewByteLength = require('data-view-byte-length');
var dataViewByteOffset = require('data-view-byte-offset');
// https://262.ecma-international.org/15.0/#sec-getviewbytelength
module.exports = function GetViewByteLength(viewRecord) {
if (!isDataViewWithBufferWitnessRecord(viewRecord)) {
throw new $TypeError('Assertion failed: `viewRecord` must be a DataView with Buffer Witness Record');
}
if (IsViewOutOfBounds(viewRecord)) {
throw new $TypeError('Assertion failed: `viewRecord` is out of bounds'); // step 1
}
var view = viewRecord['[[Object]]']; // step 2
var viewByteLength = dataViewByteLength(view); // view.[[ByteLength]]
if (viewByteLength !== 'AUTO') {
return viewByteLength; // step 3
}
if (IsFixedLengthArrayBuffer(dataViewBuffer(view))) {
throw new $TypeError('Assertion failed: DataViews ArrayBuffer is not fixed length'); // step 4
}
var byteOffset = dataViewByteOffset(view); // step 5
var byteLength = viewRecord['[[CachedBufferByteLength]]']; // step 6
if (byteLength === 'DETACHED') {
throw new $TypeError('Assertion failed: DataViews ArrayBuffer is detached'); // step 7
}
return byteLength - byteOffset; // step 8
};