Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/artifacts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,9 @@ lib/ocaml/Stdlib_Array.resi
lib/ocaml/Stdlib_ArrayBuffer.cmi
lib/ocaml/Stdlib_ArrayBuffer.cmj
lib/ocaml/Stdlib_ArrayBuffer.cmt
lib/ocaml/Stdlib_ArrayBuffer.cmti
lib/ocaml/Stdlib_ArrayBuffer.res
lib/ocaml/Stdlib_ArrayBuffer.resi
lib/ocaml/Stdlib_AsyncIterator.cmi
lib/ocaml/Stdlib_AsyncIterator.cmj
lib/ocaml/Stdlib_AsyncIterator.cmt
Expand Down
68 changes: 68 additions & 0 deletions runtime/Stdlib_ArrayBuffer.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/***
Functions for interacting with JavaScript ArrayBuffer.
See: [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer).
*/

/**
Type representing an ArrayBuffer object used to represent a generic raw binary data buffer.
*/
@notUndefined
type t

/**
`make(length)` creates a new ArrayBuffer with the specified length in bytes.
See [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/ArrayBuffer) on MDN.

## Examples

```rescript
let buffer = ArrayBuffer.make(8)
ArrayBuffer.byteLength(buffer) == 8
```

## Exceptions

- `RangeError`: If `length` is larger than `Number.MAX_SAFE_INTEGER` or negative.
*/
@new external make: int => t = "ArrayBuffer"

/**
`byteLength(arrayBuffer)` returns the size, in bytes, of the ArrayBuffer.
See [`ArrayBuffer.byteLength`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/byteLength) on MDN.

## Examples

```rescript
let buffer = ArrayBuffer.make(16)
ArrayBuffer.byteLength(buffer) == 16
```
*/
@get external byteLength: t => int = "byteLength"

/**
`slice(arrayBuffer, ~start, ~end)` returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's bytes from `start`, inclusive, up to `end`, exclusive.
See [`ArrayBuffer.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice) on MDN.

## Examples

```rescript
let buffer = ArrayBuffer.make(16)
let sliced = ArrayBuffer.slice(buffer, ~start=4, ~end=12)
ArrayBuffer.byteLength(sliced) == 8
```
*/
@send external slice: (t, ~start: int, ~end: int) => t = "slice"

/**
`sliceToEnd(arrayBuffer, ~start)` returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's bytes from `start` to the end of the buffer.
See [`ArrayBuffer.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice) on MDN.

## Examples

```rescript
let buffer = ArrayBuffer.make(16)
let sliced = ArrayBuffer.sliceToEnd(buffer, ~start=8)
ArrayBuffer.byteLength(sliced) == 8
```
*/
@send external sliceToEnd: (t, ~start: int) => t = "slice"
Copy link
Member

@cknitt cknitt Jul 3, 2025

Choose a reason for hiding this comment

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

Just realized: Do we actually need this? Can't we just make ~end optional in slice instead?

Same for Array.sliceToEnd, String.sliceToEnd.

Actually start is also optional according to MDN.

So I think we should have the signature

@send external slice: (t, ~start: int=?, ~end: int=?) => t = "slice"

and deprecate sliceToEnd.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good! I'll fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in 09f1172

Copy link
Member

Choose a reason for hiding this comment

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

Thanks a lot! Would you also create PRs for Array, String etc.?