MakeDataViewWithBufferWitnessRecord.js 936 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var ArrayBufferByteLength = require('./ArrayBufferByteLength');
  4. var IsDetachedBuffer = require('./IsDetachedBuffer');
  5. var dataViewBuffer = require('data-view-buffer');
  6. var isDataView = require('is-data-view');
  7. // https://262.ecma-international.org/15.0/#sec-makedataviewwithbufferwitnessrecord
  8. module.exports = function MakeDataViewWithBufferWitnessRecord(obj, order) {
  9. if (!isDataView(obj)) {
  10. throw new $TypeError('MakeDataViewWithBufferWitnessRecord called with non-DataView');
  11. }
  12. if (order !== 'SEQ-CST' && order !== 'UNORDERED') {
  13. throw new $TypeError('Assertion failed: `order` must be ~SEQ-CST~ or ~UNORDERED~');
  14. }
  15. var buffer = dataViewBuffer(obj); // step 1
  16. var byteLength = IsDetachedBuffer(buffer) ? 'DETACHED' : ArrayBufferByteLength(buffer, order); // steps 2 - 3
  17. return { '[[Object]]': obj, '[[CachedBufferByteLength]]': byteLength }; // step 4
  18. };