Skip to content

Conversation

@anivar
Copy link
Contributor

@anivar anivar commented Nov 29, 2025

Implements ES2024 Resizable ArrayBuffer and ES2025 auto-length TypedArray views.

ES2024 Resizable ArrayBuffer

Constructor accepts maxByteLength option:

new ArrayBuffer(8, { maxByteLength: 16 })

Resizable buffers can grow/shrink in-place:

buffer.resize(12)  // grows to 12 bytes
buffer.resize(4)   // shrinks to 4 bytes

New properties:

  • resizable - true if buffer has maxByteLength
  • maxByteLength - maximum allowed size

ES2025 Auto-length TypedArrays

TypedArrays on resizable buffers without explicit length track buffer size dynamically:

const rab = new ArrayBuffer(16, { maxByteLength: 32 });
const ta = new Uint8Array(rab);  // auto-length view
console.log(ta.length);  // 16

rab.resize(24);
console.log(ta.length);  // 24

Fixed-length views throw TypeError when buffer resizes out of bounds:

const fixedView = new Uint8Array(rab, 0, 4);
rab.resize(2);
fixedView.at(0);  // TypeError: TypedArray is out of bounds

Implementation

NativeArrayBuffer:

  • Uses int primitive for maxByteLength (-1 for fixed-length)
  • Lambda pattern for new methods
  • Data preserved during resize via System.arraycopy, new bytes zeroed

NativeTypedArrayView:

  • isAutoLength flag for auto-length views
  • updateLength() recalculates length for auto-length views
  • isTypedArrayOutOfBounds() validates bounds on resized buffers
  • All methods call validateAndGetLength() before access

Errors:

  • TypeError if not resizable, detached, or out of bounds
  • RangeError if resize exceeds maxByteLength

Test262 Results

59 tests now passing (227→169 TypedArray failures)

All return-abrupt-from-this-out-of-bounds tests now pass across TypedArray methods: at, copyWithin, entries, every, fill, filter, find, findIndex, findLast, findLastIndex, forEach, includes, indexOf, join, keys, lastIndexOf, map, reduce, reduceRight, reverse, set, slice, some, sort, toLocaleString, values.

Fixed-length property tests (byteLength, byteOffset, length) on resizable buffers also passing.

Adds support for resizable ArrayBuffers as specified in ES2024.

Constructor accepts optional maxByteLength parameter:
  new ArrayBuffer(length, { maxByteLength })

New resize() method allows in-place buffer resizing:
  - Grows or shrinks buffer up to maxByteLength
  - Preserves existing data, zeros new bytes
  - Throws TypeError if not resizable or detached
  - Throws RangeError if exceeds maxByteLength

New properties:
  - resizable: true if buffer has maxByteLength
  - maxByteLength: maximum size (equals byteLength for fixed-length)

Includes 22 comprehensive tests covering all functionality and edge cases.
All tests passing.
@anivar anivar force-pushed the es2024-resizable-arraybuffer branch from 0bbd0a0 to 088d376 Compare November 29, 2025 20:33
Removed resizable-arraybuffer from unsupported features list.
Updated test262.properties removing {unsupported: [resizable-arraybuffer]} markers from all passing tests.
Copy link
Collaborator

@gbrail gbrail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this and for your patience as we ask lots of questions...

Address review feedback by using primitive int instead of boxed Integer
for maxByteLength field. Uses -1 as sentinel value for fixed-length
buffers instead of null.

Benefits:
- Better performance (no boxing/unboxing overhead)
- Less memory usage (no object wrapper)
- Follows Rhino coding conventions
- More cache-friendly

Changes:
- NativeArrayBuffer.java: Changed field from Integer to int
- Updated all null checks to use -1 sentinel value
- All tests passing (Test262SuiteTest + ResizableArrayBuffer tests)
Add support for TypedArray views that dynamically track the length of
resizable ArrayBuffers. When a TypedArray is constructed without an
explicit length parameter, it now operates in auto-length mode where
the length is recomputed based on the current buffer size.

Implementation:
- Added boolean isAutoLength field to track auto-length mode
- Removed final modifier from length field to allow dynamic updates
- Created updateLength() method that recomputes length for auto-length views
- Length is computed as: floor((bufferByteLength - offset) / elementSize)
- Returns 0 when view is out of bounds (offset > buffer length)
- updateLength() is called in isTypedArrayOutOfBounds() before bounds checks

This approach minimizes code changes while maintaining compatibility with
existing code that directly accesses the length field.

Per ES2025 specification section 10.4.5.3
- Fix auto-length logic to only apply to resizable ArrayBuffers
- Add isResizable() public method to NativeArrayBuffer
- Improve JavaDoc to follow Rhino's concise documentation style
- Add helper method determineViewLength() for clearer logic
- Update test262.properties: 32 additional tests now passing
  - Array: 4 tests (find family with resize callbacks)
  - TypedArray: 28 tests (from, of, and prototype methods)
- Updated js_at() to call validateAndGetLength() before accessing elements
- Enhanced isTypedArrayOutOfBounds() to check if fixed-length TypedArrays
  on resizable buffers have gone out of bounds when buffer is resized
- Properly throws TypeError when accessing out-of-bounds views per ES2024
TypedArray out-of-bounds checking improvements enable 58 more tests to pass.
All return-abrupt-from-this-out-of-bounds tests now passing across TypedArray
prototype methods. Fixed-length TypedArray property tests on resizable buffers
also passing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants