123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 'use strict';
- var $SyntaxError = require('es-errors/syntax');
- var $TypeError = require('es-errors/type');
- var isInteger = require('../helpers/isInteger');
- var IsBigIntElementType = require('./IsBigIntElementType');
- var IsDetachedBuffer = require('./IsDetachedBuffer');
- var NumericToRawBytes = require('./NumericToRawBytes');
- var isArrayBuffer = require('is-array-buffer');
- var isSharedArrayBuffer = require('is-shared-array-buffer');
- var hasOwn = require('hasown');
- var table60 = {
- __proto__: null,
- Int8: 1,
- Uint8: 1,
- Uint8C: 1,
- Int16: 2,
- Uint16: 2,
- Int32: 4,
- Uint32: 4,
- BigInt64: 8,
- BigUint64: 8,
- Float32: 4,
- Float64: 8
- };
- var defaultEndianness = require('../helpers/defaultEndianness');
- var forEach = require('../helpers/forEach');
- module.exports = function SetValueInBuffer(arrayBuffer, byteIndex, type, value, isTypedArray, order) {
- var isSAB = isSharedArrayBuffer(arrayBuffer);
- if (!isArrayBuffer(arrayBuffer) && !isSAB) {
- throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or a SharedArrayBuffer');
- }
- if (!isInteger(byteIndex) || byteIndex < 0) {
- throw new $TypeError('Assertion failed: `byteIndex` must be a non-negative integer');
- }
- if (typeof type !== 'string' || !hasOwn(table60, type)) {
- throw new $TypeError('Assertion failed: `type` must be a Typed Array Element Type');
- }
- if (typeof value !== 'number' && typeof value !== 'bigint') {
- throw new $TypeError('Assertion failed: `value` must be a Number or a BigInt');
- }
- if (typeof isTypedArray !== 'boolean') {
- throw new $TypeError('Assertion failed: `isTypedArray` must be a boolean');
- }
- if (order !== 'SeqCst' && order !== 'Unordered' && order !== 'Init') {
- throw new $TypeError('Assertion failed: `order` must be `"SeqCst"`, `"Unordered"`, or `"Init"`');
- }
- if (arguments.length > 6 && typeof arguments[6] !== 'boolean') {
- throw new $TypeError('Assertion failed: `isLittleEndian` must be a boolean, if present');
- }
- if (IsDetachedBuffer(arrayBuffer)) {
- throw new $TypeError('Assertion failed: ArrayBuffer is detached');
- }
-
- if (IsBigIntElementType(type) ? typeof value !== 'bigint' : typeof value !== 'number') {
- throw new $TypeError('Assertion failed: `value` must be a BigInt if type is BigInt64 or BigUint64, otherwise a Number');
- }
-
- var elementSize = table60[type];
-
- var isLittleEndian = arguments.length > 6 ? arguments[6] : defaultEndianness === 'little';
- var rawBytes = NumericToRawBytes(type, value, isLittleEndian);
- if (isSAB) {
-
- throw new $SyntaxError('SharedArrayBuffer is not supported by this implementation');
- } else {
-
- var arr = new Uint8Array(arrayBuffer, byteIndex, elementSize);
- forEach(rawBytes, function (rawByte, i) {
- arr[i] = rawByte;
- });
- }
-
- };
|