IsViewOutOfBounds.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var IsDetachedBuffer = require('./IsDetachedBuffer');
  4. var isDataViewWithBufferWitnessRecord = require('../helpers/records/data-view-with-buffer-witness-record');
  5. var dataViewBuffer = require('data-view-buffer');
  6. var dataViewByteLength = require('data-view-byte-length');
  7. var dataViewByteOffset = require('data-view-byte-offset');
  8. // https://262.ecma-international.org/15.0/#sec-isviewoutofbounds
  9. module.exports = function IsViewOutOfBounds(viewRecord) {
  10. if (!isDataViewWithBufferWitnessRecord(viewRecord)) {
  11. throw new $TypeError('Assertion failed: `viewRecord` must be a DataView With Buffer Witness Record');
  12. }
  13. var view = viewRecord['[[Object]]']; // step 1
  14. var bufferByteLength = viewRecord['[[CachedBufferByteLength]]']; // step 2
  15. if (IsDetachedBuffer(dataViewBuffer(view)) !== (bufferByteLength === 'DETACHED')) {
  16. // step 3
  17. throw new $TypeError('Assertion failed: `IsDetachedBuffer(dataViewBuffer(view))` must be true if and only if `bufferByteLength === ~DETACHED~');
  18. }
  19. if (bufferByteLength === 'DETACHED') {
  20. return true; // step 4
  21. }
  22. var byteOffsetStart = dataViewByteOffset(view); // step 5
  23. var dvByteLength = dataViewByteLength(view);
  24. var byteOffsetEnd = dvByteLength === 'AUTO' ? bufferByteLength : byteOffsetStart + dvByteLength; // steps 6 - 7
  25. if (byteOffsetStart > bufferByteLength || byteOffsetEnd > bufferByteLength) {
  26. return true; // step 8
  27. }
  28. // 9. NOTE: 0-length DataViews are not considered out-of-bounds.
  29. return false; // step 10
  30. };