Skip to content

Commit

Permalink
throw proper errors on an incorrect context in some ArrayBuffer and…
Browse files Browse the repository at this point in the history
… `DataView` methods
  • Loading branch information
zloirock committed Feb 12, 2023
1 parent 7e0399d commit 39c079b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
- Improved some cases handling of array-replacer in `JSON.stringify` symbols handling fix
- Fixed many other old `JSON.{ parse, stringify }` bugs (numbers instead of strings as keys in replacer, handling negative zeroes, spaces, some more handling symbols cases, etc.)
- Fixed configurability and `ToString` conversion of some accessors
- Added throwing proper errors on an incorrect context in some `ArrayBuffer` and `DataView` methods
- Some minor `DataView` polyfill optimizations
- `RegExp.prototype.flags` marked as fixed from V8 ~ Chrome 111
- Added Opera Android 73 compat data mapping
- Added TypeScript definitions to `core-js-builder`
Expand Down
31 changes: 18 additions & 13 deletions packages/core-js/internals/array-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ var InternalStateModule = require('../internals/internal-state');

var PROPER_FUNCTION_NAME = FunctionName.PROPER;
var CONFIGURABLE_FUNCTION_NAME = FunctionName.CONFIGURABLE;
var getInternalState = InternalStateModule.get;
var setInternalState = InternalStateModule.set;
var ARRAY_BUFFER = 'ArrayBuffer';
var DATA_VIEW = 'DataView';
var PROTOTYPE = 'prototype';
var WRONG_LENGTH = 'Wrong length';
var WRONG_INDEX = 'Wrong index';
var getInternalArrayBufferState = InternalStateModule.getterFor(ARRAY_BUFFER);
var getInternalDataViewState = InternalStateModule.getterFor(DATA_VIEW);
var setInternalState = InternalStateModule.set;
var NativeArrayBuffer = global[ARRAY_BUFFER];
var $ArrayBuffer = NativeArrayBuffer;
var ArrayBufferPrototype = $ArrayBuffer && $ArrayBuffer[PROTOTYPE];
Expand Down Expand Up @@ -68,7 +69,7 @@ var packFloat64 = function (number) {
return packIEEE754(number, 52, 8);
};

var addGetter = function (Constructor, key) {
var addGetter = function (Constructor, key, getInternalState) {
defineBuiltInAccessor(Constructor[PROTOTYPE], key, {
configurable: true,
get: function () {
Expand All @@ -79,19 +80,19 @@ var addGetter = function (Constructor, key) {

var get = function (view, count, index, isLittleEndian) {
var intIndex = toIndex(index);
var store = getInternalState(view);
var store = getInternalDataViewState(view);
if (intIndex + count > store.byteLength) throw RangeError(WRONG_INDEX);
var bytes = getInternalState(store.buffer).bytes;
var bytes = store.bytes;
var start = intIndex + store.byteOffset;
var pack = arraySlice(bytes, start, start + count);
return isLittleEndian ? pack : reverse(pack);
};

var set = function (view, count, index, conversion, value, isLittleEndian) {
var intIndex = toIndex(index);
var store = getInternalState(view);
var store = getInternalDataViewState(view);
if (intIndex + count > store.byteLength) throw RangeError(WRONG_INDEX);
var bytes = getInternalState(store.buffer).bytes;
var bytes = store.bytes;
var start = intIndex + store.byteOffset;
var pack = conversion(+value);
for (var i = 0; i < count; i++) bytes[start + i] = pack[isLittleEndian ? i : count - i - 1];
Expand All @@ -102,6 +103,7 @@ if (!NATIVE_ARRAY_BUFFER) {
anInstance(this, ArrayBufferPrototype);
var byteLength = toIndex(length);
setInternalState(this, {
type: ARRAY_BUFFER,
bytes: fill(Array(byteLength), 0),
byteLength: byteLength
});
Expand All @@ -116,15 +118,18 @@ if (!NATIVE_ARRAY_BUFFER) {
$DataView = function DataView(buffer, byteOffset, byteLength) {
anInstance(this, DataViewPrototype);
anInstance(buffer, ArrayBufferPrototype);
var bufferLength = getInternalState(buffer).byteLength;
var bufferState = getInternalArrayBufferState(buffer);
var bufferLength = bufferState.byteLength;
var offset = toIntegerOrInfinity(byteOffset);
if (offset < 0 || offset > bufferLength) throw RangeError('Wrong offset');
byteLength = byteLength === undefined ? bufferLength - offset : toLength(byteLength);
if (offset + byteLength > bufferLength) throw RangeError(WRONG_LENGTH);
setInternalState(this, {
type: DATA_VIEW,
buffer: buffer,
byteLength: byteLength,
byteOffset: offset
byteOffset: offset,
bytes: bufferState.bytes
});
if (!DESCRIPTORS) {
this.buffer = buffer;
Expand All @@ -136,10 +141,10 @@ if (!NATIVE_ARRAY_BUFFER) {
DataViewPrototype = $DataView[PROTOTYPE];

if (DESCRIPTORS) {
addGetter($ArrayBuffer, 'byteLength');
addGetter($DataView, 'buffer');
addGetter($DataView, 'byteLength');
addGetter($DataView, 'byteOffset');
addGetter($ArrayBuffer, 'byteLength', getInternalArrayBufferState);
addGetter($DataView, 'buffer', getInternalDataViewState);
addGetter($DataView, 'byteLength', getInternalDataViewState);
addGetter($DataView, 'byteOffset', getInternalDataViewState);
}

defineBuiltIns(DataViewPrototype, {
Expand Down

0 comments on commit 39c079b

Please sign in to comment.