Skip to content

Commit 7754576

Browse files
committed
Add bindings to updated ArrayBuffer, SharedArrayBuffer, and Atomics
1 parent 5a0a21d commit 7754576

20 files changed

+528
-95
lines changed

jscomp/others/js.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,21 @@ module Math = Js_math
244244
module Obj = Js_obj
245245
(** Provide utilities for `Js.t` *)
246246

247+
module ArrayBuffer = Js_array_buffer
248+
(** Provide utilities for `ArrayBuffer` object *)
249+
250+
module SharedArrayBuffer = Js_shared_array_buffer
251+
(** Provide utilities for `SharedArrayBuffer` object *)
252+
247253
module Typed_array = Js_typed_array
248254
(** Provide bindings for JS typed array *)
249255

250256
module TypedArray2 = Js_typed_array2
251257
(** Provide bindings for JS typed array *)
252258

259+
module Atomics = Js_atomics
260+
(** Provide bindings for JS `Atomics` utilities *)
261+
253262
module Types = Js_types
254263
(** Provide utilities for manipulating JS types *)
255264

jscomp/others/js_array_buffer.res

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@@uncurried
2+
3+
/***
4+
The underlying buffer that the typed arrays provide views of
5+
6+
**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
7+
*/
8+
type t = {
9+
byteLength: int,
10+
maxByteLength: int,
11+
detached: bool,
12+
resizable: bool,
13+
}
14+
15+
type makeOptions = {
16+
maxByteLength?: int
17+
}
18+
19+
@new /** takes length. initializes elements to 0 */
20+
external make: (int, makeOptions) => t = "ArrayBuffer"
21+
let make = (length, ~maxByteLength=?) => make(length, { maxByteLength: ?maxByteLength })
22+
23+
/* ArrayBuffer.isView: seems pointless with a type system */
24+
25+
@get external byteLength: t => int = "byteLength"
26+
@get external maxByteLength: t => int = "maxByteLength"
27+
@get external detached: t => bool = "detached"
28+
@get external resizable: t => bool = "resizable"
29+
30+
@send external slice: (t, ~start: int, ~end_: int) => t = "slice"
31+
@send external sliceFrom: (t, int) => t = "slice"
32+
33+
@send external transfer: (t, ~newByteLength: int=?) => t = "transfer"
34+
@send external transferToFixedLength: (t, ~newByteLength: int=?) => t = "transferToFixedLength"

jscomp/others/js_atomics.res

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
@@uncurried
2+
3+
@scope("Atomics") external isLockFree: int => bool = "isLockFree"
4+
5+
module MakeOps = (TA: Js_typed_array2.S) => {
6+
@scope("Atomics") external add: (TA.t, int, int) => TA.elt = "add"
7+
@scope("Atomics") external sub: (TA.t, int, int) => TA.elt = "sub"
8+
@scope("Atomics") external and_: (TA.t, int, int) => TA.elt = "and"
9+
@scope("Atomics") external or_: (TA.t, int, int) => TA.elt = "or"
10+
@scope("Atomics") external xor: (TA.t, int, int) => TA.elt = "xor"
11+
@scope("Atomics") external load: (TA.t, ~index: int) => TA.elt = "load"
12+
@scope("Atomics") external store: (TA.t, ~index: int, ~value: TA.elt) => TA.elt = "store"
13+
@scope("Atomics") external exchange: (TA.t, ~index: int, ~value: TA.elt) => TA.elt = "exchange"
14+
@scope("Atomics") external compareExchange: (TA.t, ~index: int, ~expectedValue: TA.elt, ~replacementValue: TA.elt) => TA.elt = "compareExchange"
15+
}
16+
17+
module MakeFutex = (TA: Js_typed_array2.S) => {
18+
// These operations are supported only when TA is Int32Array or BigInt64Array, and also underlying buffer is SharedArrayBuffer
19+
20+
@scope("Atomics") external wait: (TA.t, ~index: int, ~value: TA.elt) => [#ok | #"not-equal"] = "wait"
21+
@scope("Atomics") external waitWithTimeout: (TA.t, ~index: int, ~value: TA.elt, ~timeout: int) => [#ok | #"not-equal" | #"timed-out"] = "wait"
22+
23+
@scope("Atomics") external waitAsync: (TA.t, ~index: int, ~value: TA.elt) => promise<[#ok | #"not-equal"]> = "waitAsync"
24+
@scope("Atomics") external waitAsyncWithTimeout: (TA.t, ~index: int, ~value: TA.elt, ~timeout: int) => promise<[#ok | #"not-equal" | #"timed-out"]> = "waitAsync"
25+
26+
@scope("Atomics") external notify: (TA.t, ~index: int) => int = "notify"
27+
@scope("Atomics") external notifyForCount: (TA.t, ~index: int, ~count: int) => int = "notify"
28+
}
29+
30+
module Int8Array = MakeOps(Js_typed_array2.Int8Array)
31+
32+
module Uint8Array = MakeOps(Js_typed_array2.Uint8Array)
33+
34+
module Int16Array = MakeOps(Js_typed_array2.Int16Array)
35+
36+
module Uint16Array = MakeOps(Js_typed_array2.Uint16Array)
37+
38+
module Int32Array = {
39+
include MakeOps(Js_typed_array2.Int32Array)
40+
include MakeFutex(Js_typed_array2.Int32Array)
41+
}
42+
43+
module Uint32Array = MakeOps(Js_typed_array2.Uint32Array)
44+
45+
/** TODO: uncomment this when ready
46+
module BigInt64Array = {
47+
include MakeOps(Js_typed_array2.BigInt64Array)
48+
include MakeFutex(Js_typed_array2.BigInt64Array)
49+
}
50+
51+
module BigUint64Array = MakeOps(Js_typed_array2.BigUint64Array)
52+
*/
53+
54+
module Float32Array = MakeOps(Js_typed_array2.Float32Array)
55+
56+
module Float64Array = MakeOps(Js_typed_array2.Float64Array)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
@@uncurried
2+
3+
/***
4+
The underlying buffer that the typed arrays provide views of
5+
6+
**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer)
7+
*/
8+
type t = {
9+
byteLength: int,
10+
maxByteLength: int,
11+
growable: bool,
12+
}
13+
14+
type makeOptions = {
15+
maxByteLength?: int
16+
}
17+
18+
@new /** takes length. initializes elements to 0 */
19+
external make: (int, makeOptions) => t = "SharedArrayBuffer"
20+
let make = (length, ~maxByteLength=?) => make(length, { maxByteLength: ?maxByteLength })
21+
22+
/* ArrayBuffer.isView: seems pointless with a type system */
23+
24+
@get external byteLength: t => int = "byteLength"
25+
@get external maxByteLength: t => int = "maxByteLength"
26+
@get external growable: t => bool = "growable"
27+
28+
@send external slice: (t, ~start: int, ~end_: int) => t = "slice"
29+
@send external sliceFrom: (t, int) => t = "slice"

jscomp/others/js_typed_array.res

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ module ArrayBuffer = {
5959
@bs.send.pipe(: t) external slice: (~start: int, ~end_: int) => array_buffer = "slice" /* FIXME */
6060
@bs.send.pipe(: t) external sliceFrom: int => array_buffer = "slice"
6161
}
62+
63+
type typed_array<'a>
64+
6265
module type S = {
6366
/*** Implements functionality common to all the typed arrays */
6467

6568
type elt
66-
type typed_array<'a>
6769
type t = typed_array<elt>
6870

6971
@get_index external unsafe_get: (t, int) => elt = ""
@@ -173,7 +175,6 @@ module type S = {
173175
module Int8Array = {
174176
/** */
175177
type elt = int
176-
type typed_array<'a> = Js_typed_array2.Int8Array.typed_array<'a>
177178
type t = typed_array<elt>
178179

179180
@get_index external unsafe_get: (t, int) => elt = ""
@@ -296,7 +297,6 @@ module Int8Array = {
296297
module Uint8Array = {
297298
/** */
298299
type elt = int
299-
type typed_array<'a> = Js_typed_array2.Uint8Array.typed_array<'a>
300300
type t = typed_array<elt>
301301

302302
@get_index external unsafe_get: (t, int) => elt = ""
@@ -419,7 +419,6 @@ module Uint8Array = {
419419
module Uint8ClampedArray = {
420420
/** */
421421
type elt = int
422-
type typed_array<'a> = Js_typed_array2.Uint8ClampedArray.typed_array<'a>
423422
type t = typed_array<elt>
424423

425424
@get_index external unsafe_get: (t, int) => elt = ""
@@ -542,7 +541,6 @@ module Uint8ClampedArray = {
542541
module Int16Array = {
543542
/** */
544543
type elt = int
545-
type typed_array<'a> = Js_typed_array2.Int16Array.typed_array<'a>
546544
type t = typed_array<elt>
547545

548546
@get_index external unsafe_get: (t, int) => elt = ""
@@ -665,7 +663,6 @@ module Int16Array = {
665663
module Uint16Array = {
666664
/** */
667665
type elt = int
668-
type typed_array<'a> = Js_typed_array2.Uint16Array.typed_array<'a>
669666
type t = typed_array<elt>
670667

671668
@get_index external unsafe_get: (t, int) => elt = ""
@@ -788,7 +785,6 @@ module Uint16Array = {
788785
module Int32Array = {
789786
/** */
790787
type elt = int
791-
type typed_array<'a> = Js_typed_array2.Int32Array.typed_array<'a>
792788
type t = typed_array<elt>
793789

794790
@get_index external unsafe_get: (t, int) => elt = ""
@@ -914,7 +910,6 @@ module Int32_array = Int32Array
914910
module Uint32Array = {
915911
/** */
916912
type elt = int
917-
type typed_array<'a> = Js_typed_array2.Uint32Array.typed_array<'a>
918913
type t = typed_array<elt>
919914

920915
@get_index external unsafe_get: (t, int) => elt = ""
@@ -1040,7 +1035,6 @@ module Uint32Array = {
10401035
module Float32Array = {
10411036
/** */
10421037
type elt = float
1043-
type typed_array<'a> = Js_typed_array2.Float32Array.typed_array<'a>
10441038
type t = typed_array<elt>
10451039

10461040
@get_index external unsafe_get: (t, int) => elt = ""
@@ -1167,7 +1161,6 @@ module Float32_array = Float32Array
11671161
module Float64Array = {
11681162
/** */
11691163
type elt = float
1170-
type typed_array<'a> = Js_typed_array2.Float64Array.typed_array<'a>
11711164
type t = typed_array<elt>
11721165

11731166
@get_index external unsafe_get: (t, int) => elt = ""

0 commit comments

Comments
 (0)