Skip to content

Add base types of ArrayBuffer, SharedArrayBuffer #6683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

# 11.1.0-rc.5 (Unreleased)

#### :nail_care: Polish

- Add `ArrayBuffer.t` and `SharedArrayBuffer.t` to be base types of the Core bindings. https://github.com/rescript-lang/rescript-compiler/pull/6683

# 11.1.0-rc.4

#### :bug: Bug Fix
Expand Down
25 changes: 10 additions & 15 deletions jscomp/others/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

[@@@bs.config { flags = [| "-unboxed-types"; "-w"; "-49" |] }]
[@@@bs.config {flags = [|"-unboxed-types"; "-w"; "-49"|]}]
(* DESIGN:
- It does not have any code, all its code will be inlined so that
there will never be
Expand Down Expand Up @@ -82,29 +82,23 @@ module Internal = struct
external opaqueFullApply : 'a -> 'a = "%uncurried_apply"

(* Use opaque instead of [._n] to prevent some optimizations happening *)
external run : (unit -> 'a [@bs]) -> 'a = "#run"
external run : ((unit -> 'a)[@bs]) -> 'a = "#run"
external opaque : 'a -> 'a = "%opaque"
end

(**/**)

type +'a null =
| Value of 'a
| Null [@as null]
[@@unboxed]
(**
Nullable value of this type can be either null or 'a. This type is equivalent to Js.Null.t.
*)
type +'a null = Value of 'a | Null [@as null] [@@unboxed]

type +'a undefined
(**
A value of this type can be either undefined or 'a. This type is equivalent to Js.Undefined.t.
*)

type +'a nullable =
| Value of 'a
| Null [@as null]
| Undefined [@as undefined]
type +'a nullable = Value of 'a | Null [@as null] | Undefined [@as undefined]
[@@unboxed]

(**
Expand Down Expand Up @@ -144,17 +138,17 @@ external typeof : 'a -> string = "#typeof"
*)

external log : 'a -> unit = "log"
[@@val] [@@scope "console"]
[@@val] [@@scope "console"]
(** Equivalent to console.log any value. *)

external log2 : 'a -> 'b -> unit = "log" [@@bs.val] [@@bs.scope "console"]
external log3 : 'a -> 'b -> 'c -> unit = "log" [@@bs.val] [@@bs.scope "console"]

external log4 : 'a -> 'b -> 'c -> 'd -> unit = "log"
[@@bs.val] [@@bs.scope "console"]
[@@bs.val] [@@bs.scope "console"]

external logMany : 'a array -> unit = "log"
[@@bs.val] [@@bs.scope "console"] [@@bs.splice]
[@@bs.val] [@@bs.scope "console"] [@@bs.splice]
(** A convenience function to console.log more than 4 arguments *)

external eqNull : 'a -> 'a null -> bool = "%bs_equal_null"
Expand Down Expand Up @@ -199,8 +193,9 @@ module Undefined = Js_undefined
module Nullable = Js_null_undefined
(** Provide utilities for `Js.null_undefined` *)

module Null_undefined = Js_null_undefined
[@deprecated "Please use `Js.Nullable`"]
module Null_undefined =
Js_null_undefined
[@deprecated "Please use `Js.Nullable`"]

module Exn = Js_exn
(** Provide utilities for dealing with Js exceptions *)
Expand Down
6 changes: 6 additions & 0 deletions jscomp/others/js_array_buffer.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type t = {
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest marking the record as private so it's not possible to create by hand.

Copy link
Member

Choose a reason for hiding this comment

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

Since not every record with the fields is the ArrayBuffer

Copy link
Member Author

Choose a reason for hiding this comment

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

That's basically prevented by nominal typing

Copy link
Member

Choose a reason for hiding this comment

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

I mean to prevent the code like this:

let myArrayBuffer: Js.ArrayBuffer.t = {
  byteLenght: 2,
  maxByteLength: 4,
  detached: false,
  resizable: false
}

Copy link
Member Author

Choose a reason for hiding this comment

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

If we don't want that, we shouldn't use the binding pattern of exposing getters as record fields for every this-bounded object.

I thought it was already a widely used (even encouraged) pattern, more for practicality than strictness. for example: TheSpyder/rescript-webapi#78 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

I personally use private for my projects. It kind of solves the problem:

type t = private {
  byteLength: int,
  maxByteLength: int,
  detached: bool,
  resizable: bool,
}

But I see one downside compared to getter pattern:

You need to annotate the value when you access the field

let fn = (arrayBuffer: ArrayBuffer.t) => arrayBuffer.byteLenght
// vs
let fn = (arrayBuffer) => arrayBuffer->Js.ArrayBuffer.byteLength

It's fine for this case but when you have a type with a lot of complicated type arguments I prefer not to write annotations.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we can just leave type t here, but what about the Core's direction? @zth

I'll tune in to that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Wait, so does it make sense to provide compatibility with Js' type in the first place?

byteLength: int,
maxByteLength: int,
detached: bool,
resizable: bool,
}
5 changes: 5 additions & 0 deletions jscomp/others/js_shared_array_buffer.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type t = {
byteLength: int,
maxByteLength: int,
growable: bool,
}
13 changes: 3 additions & 10 deletions jscomp/others/js_typed_array.res
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ module ArrayBuffer = {
@bs.send.pipe(: t) external slice: (~start: int, ~end_: int) => array_buffer = "slice" /* FIXME */
@bs.send.pipe(: t) external sliceFrom: int => array_buffer = "slice"
}

type typed_array<'a>

module type S = {
/*** Implements functionality common to all the typed arrays */

type elt
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -173,7 +175,6 @@ module type S = {
module Int8Array = {
/** */
type elt = int
type typed_array<'a> = Js_typed_array2.Int8Array.typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -296,7 +297,6 @@ module Int8Array = {
module Uint8Array = {
/** */
type elt = int
type typed_array<'a> = Js_typed_array2.Uint8Array.typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -419,7 +419,6 @@ module Uint8Array = {
module Uint8ClampedArray = {
/** */
type elt = int
type typed_array<'a> = Js_typed_array2.Uint8ClampedArray.typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -542,7 +541,6 @@ module Uint8ClampedArray = {
module Int16Array = {
/** */
type elt = int
type typed_array<'a> = Js_typed_array2.Int16Array.typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -665,7 +663,6 @@ module Int16Array = {
module Uint16Array = {
/** */
type elt = int
type typed_array<'a> = Js_typed_array2.Uint16Array.typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -788,7 +785,6 @@ module Uint16Array = {
module Int32Array = {
/** */
type elt = int
type typed_array<'a> = Js_typed_array2.Int32Array.typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -914,7 +910,6 @@ module Int32_array = Int32Array
module Uint32Array = {
/** */
type elt = int
type typed_array<'a> = Js_typed_array2.Uint32Array.typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -1040,7 +1035,6 @@ module Uint32Array = {
module Float32Array = {
/** */
type elt = float
type typed_array<'a> = Js_typed_array2.Float32Array.typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -1167,7 +1161,6 @@ module Float32_array = Float32Array
module Float64Array = {
/** */
type elt = float
type typed_array<'a> = Js_typed_array2.Float64Array.typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down
24 changes: 7 additions & 17 deletions jscomp/others/js_typed_array2.res
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,38 @@ JavaScript Typed Array API
**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
*/

type array_buffer
type array_like<'a> /* should be shared with js_array */
type array_buffer = Js_array_buffer.t
type array_like<'a> = Js_array2.array_like<'a>

module ArrayBuffer = {
/***
The underlying buffer that the typed arrays provide views of

**see** [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
*/
*/

type t = array_buffer
type t = array_buffer

@new /** takes length. initializes elements to 0 */
external make: int => t = "ArrayBuffer"

/* ArrayBuffer.isView: seems pointless with a type system */
/* experimental
external transfer : array_buffer -> t = "ArrayBuffer.transfer" [@@bs.val]
external transferWithLength : array_buffer -> int -> t = "ArrayBuffer.transfer" [@@bs.val]
*/

@get external byteLength: t => int = "byteLength"

@send external slice: (t, ~start: int, ~end_: int) => array_buffer = "slice"
@send external sliceFrom: (t, int) => array_buffer = "slice"
}

type typed_array<'a>

/* commented out until bs has a plan for iterators
external values : t -> elt array_iter = "" [@@bs.send]
*/

module Int8Array = {
/** */
type elt = int
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -184,7 +181,6 @@ module Int8Array = {
module Uint8Array = {
/** */
type elt = int
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -306,7 +302,6 @@ module Uint8Array = {
module Uint8ClampedArray = {
/** */
type elt = int
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -428,7 +423,6 @@ module Uint8ClampedArray = {
module Int16Array = {
/** */
type elt = int
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -550,7 +544,6 @@ module Int16Array = {
module Uint16Array = {
/** */
type elt = int
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -672,7 +665,6 @@ module Uint16Array = {
module Int32Array = {
/** */
type elt = int
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -794,7 +786,6 @@ module Int32Array = {
module Uint32Array = {
/** */
type elt = int
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -919,7 +910,6 @@ module Uint32Array = {
module Float32Array = {
/** */
type elt = float
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -1041,7 +1031,6 @@ module Float32Array = {
module Float64Array = {
/** */
type elt = float
type typed_array<'a>
type t = typed_array<elt>

@get_index external unsafe_get: (t, int) => elt = ""
Expand Down Expand Up @@ -1170,6 +1159,7 @@ module DataView = {
type t

@new external make: array_buffer => t = "DataView"

@new external fromBuffer: array_buffer => t = "DataView"
@new external fromBufferOffset: (array_buffer, int) => t = "DataView"
@new external fromBufferRange: (array_buffer, ~offset: int, ~length: int) => t = "DataView"
Expand Down
6 changes: 4 additions & 2 deletions jscomp/others/release.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ o others/belt_internals.cmi : cc others/belt_internals.resi | $bsc
o others/js_OO.cmi others/js_OO.cmj : cc others/js_OO.res | others/belt_internals.cmi others/js.cmi $bsc
o others/js_array.cmi others/js_array.cmj : cc others/js_array.res | others/belt_internals.cmi others/js.cmi others/js_array2.cmj $bsc
o others/js_array2.cmi others/js_array2.cmj : cc others/js_array2.res | others/belt_internals.cmi others/js.cmi $bsc
o others/js_array_buffer.cmi others/js_array_buffer.cmj : cc others/js_array_buffer.res | others/belt_internals.cmi others/js.cmi $bsc
o others/js_bigint.cmi others/js_bigint.cmj : cc others/js_bigint.res | others/belt_internals.cmi others/js.cmi $bsc
o others/js_blob.cmi others/js_blob.cmj : cc others/js_blob.res | others/belt_internals.cmi others/js.cmi $bsc
o others/js_cast.cmj : cc_cmi others/js_cast.res | others/belt_internals.cmi others/js.cmi others/js_cast.cmi $bsc
Expand Down Expand Up @@ -53,10 +54,11 @@ o others/js_re.cmi others/js_re.cmj : cc others/js_re.res | others/belt_internal
o others/js_result.cmj : cc_cmi others/js_result.res | others/belt_internals.cmi others/js.cmi others/js_result.cmi $bsc
o others/js_result.cmi : cc others/js_result.resi | others/belt_internals.cmi others/js.cmi $bsc
o others/js_set.cmi others/js_set.cmj : cc others/js_set.res | others/belt_internals.cmi others/js.cmi $bsc
o others/js_shared_array_buffer.cmi others/js_shared_array_buffer.cmj : cc others/js_shared_array_buffer.res | others/belt_internals.cmi others/js.cmi $bsc
o others/js_string.cmi others/js_string.cmj : cc others/js_string.res | others/belt_internals.cmi others/js.cmi others/js_array2.cmj others/js_re.cmj $bsc
o others/js_string2.cmi others/js_string2.cmj : cc others/js_string2.res | others/belt_internals.cmi others/js.cmi others/js_array2.cmj others/js_re.cmj $bsc
o others/js_typed_array.cmi others/js_typed_array.cmj : cc others/js_typed_array.res | others/belt_internals.cmi others/js.cmi others/js.cmj others/js_typed_array2.cmj $bsc
o others/js_typed_array2.cmi others/js_typed_array2.cmj : cc others/js_typed_array2.res | others/belt_internals.cmi others/js.cmi others/js.cmj $bsc
o others/js_typed_array2.cmi others/js_typed_array2.cmj : cc others/js_typed_array2.res | others/belt_internals.cmi others/js.cmi others/js.cmj others/js_array2.cmj others/js_array_buffer.cmj $bsc
o others/js_types.cmj : cc_cmi others/js_types.res | others/belt_internals.cmi others/js.cmi others/js.cmj others/js_bigint.cmj others/js_null.cmj others/js_types.cmi $bsc
o others/js_types.cmi : cc others/js_types.resi | others/belt_internals.cmi others/js.cmi $bsc
o others/js_undefined.cmj : cc_cmi others/js_undefined.res | others/belt_internals.cmi others/js.cmi others/js.cmj others/js_exn.cmj others/js_undefined.cmi $bsc
Expand All @@ -74,7 +76,7 @@ o others/jsxEventU.cmi others/jsxEventU.cmj : cc others/jsxEventU.res | others/b
o others/jsxPPXReactSupportC.cmi others/jsxPPXReactSupportC.cmj : cc others/jsxPPXReactSupportC.res | others/belt_internals.cmi others/js.cmi others/jsxC.cmj $bsc
o others/jsxPPXReactSupportU.cmi others/jsxPPXReactSupportU.cmj : cc others/jsxPPXReactSupportU.res | others/belt_internals.cmi others/js.cmi others/jsxU.cmj $bsc
o others/jsxU.cmi others/jsxU.cmj : cc others/jsxU.res | others/belt_internals.cmi others/js.cmi $bsc
o js_pkg : phony others/js_OO.cmi others/js_OO.cmj others/js_array.cmi others/js_array.cmj others/js_array2.cmi others/js_array2.cmj others/js_bigint.cmi others/js_bigint.cmj others/js_blob.cmi others/js_blob.cmj others/js_cast.cmi others/js_cast.cmj others/js_console.cmi others/js_console.cmj others/js_date.cmi others/js_date.cmj others/js_dict.cmi others/js_dict.cmj others/js_exn.cmi others/js_exn.cmj others/js_file.cmi others/js_file.cmj others/js_float.cmi others/js_float.cmj others/js_global.cmi others/js_global.cmj others/js_int.cmi others/js_int.cmj others/js_json.cmi others/js_json.cmj others/js_list.cmi others/js_list.cmj others/js_map.cmi others/js_map.cmj others/js_mapperRt.cmi others/js_mapperRt.cmj others/js_math.cmi others/js_math.cmj others/js_null.cmi others/js_null.cmj others/js_null_undefined.cmi others/js_null_undefined.cmj others/js_obj.cmi others/js_obj.cmj others/js_option.cmi others/js_option.cmj others/js_promise.cmi others/js_promise.cmj others/js_promise2.cmi others/js_promise2.cmj others/js_re.cmi others/js_re.cmj others/js_result.cmi others/js_result.cmj others/js_set.cmi others/js_set.cmj others/js_string.cmi others/js_string.cmj others/js_string2.cmi others/js_string2.cmj others/js_typed_array.cmi others/js_typed_array.cmj others/js_typed_array2.cmi others/js_typed_array2.cmj others/js_types.cmi others/js_types.cmj others/js_undefined.cmi others/js_undefined.cmj others/js_vector.cmi others/js_vector.cmj others/js_weakmap.cmi others/js_weakmap.cmj others/js_weakset.cmi others/js_weakset.cmj others/jsxC.cmi others/jsxC.cmj others/jsxDOMC.cmi others/jsxDOMC.cmj others/jsxDOMStyle.cmi others/jsxDOMStyle.cmj others/jsxDOMU.cmi others/jsxDOMU.cmj others/jsxEventC.cmi others/jsxEventC.cmj others/jsxEventU.cmi others/jsxEventU.cmj others/jsxPPXReactSupportC.cmi others/jsxPPXReactSupportC.cmj others/jsxPPXReactSupportU.cmi others/jsxPPXReactSupportU.cmj others/jsxU.cmi others/jsxU.cmj
o js_pkg : phony others/js_OO.cmi others/js_OO.cmj others/js_array.cmi others/js_array.cmj others/js_array2.cmi others/js_array2.cmj others/js_array_buffer.cmi others/js_array_buffer.cmj others/js_bigint.cmi others/js_bigint.cmj others/js_blob.cmi others/js_blob.cmj others/js_cast.cmi others/js_cast.cmj others/js_console.cmi others/js_console.cmj others/js_date.cmi others/js_date.cmj others/js_dict.cmi others/js_dict.cmj others/js_exn.cmi others/js_exn.cmj others/js_file.cmi others/js_file.cmj others/js_float.cmi others/js_float.cmj others/js_global.cmi others/js_global.cmj others/js_int.cmi others/js_int.cmj others/js_json.cmi others/js_json.cmj others/js_list.cmi others/js_list.cmj others/js_map.cmi others/js_map.cmj others/js_mapperRt.cmi others/js_mapperRt.cmj others/js_math.cmi others/js_math.cmj others/js_null.cmi others/js_null.cmj others/js_null_undefined.cmi others/js_null_undefined.cmj others/js_obj.cmi others/js_obj.cmj others/js_option.cmi others/js_option.cmj others/js_promise.cmi others/js_promise.cmj others/js_promise2.cmi others/js_promise2.cmj others/js_re.cmi others/js_re.cmj others/js_result.cmi others/js_result.cmj others/js_set.cmi others/js_set.cmj others/js_shared_array_buffer.cmi others/js_shared_array_buffer.cmj others/js_string.cmi others/js_string.cmj others/js_string2.cmi others/js_string2.cmj others/js_typed_array.cmi others/js_typed_array.cmj others/js_typed_array2.cmi others/js_typed_array2.cmj others/js_types.cmi others/js_types.cmj others/js_undefined.cmi others/js_undefined.cmj others/js_vector.cmi others/js_vector.cmj others/js_weakmap.cmi others/js_weakmap.cmj others/js_weakset.cmi others/js_weakset.cmj others/jsxC.cmi others/jsxC.cmj others/jsxDOMC.cmi others/jsxDOMC.cmj others/jsxDOMStyle.cmi others/jsxDOMStyle.cmj others/jsxDOMU.cmi others/jsxDOMU.cmj others/jsxEventC.cmi others/jsxEventC.cmj others/jsxEventU.cmi others/jsxEventU.cmj others/jsxPPXReactSupportC.cmi others/jsxPPXReactSupportC.cmj others/jsxPPXReactSupportU.cmi others/jsxPPXReactSupportU.cmj others/jsxU.cmi others/jsxU.cmj
o others/belt_Array.cmj : cc_cmi others/belt_Array.res | others/belt.cmi others/belt_Array.cmi others/belt_internals.cmi others/js.cmi others/js.cmj others/js_math.cmj $bsc js_pkg
o others/belt_Array.cmi : cc others/belt_Array.resi | others/belt.cmi others/belt_internals.cmi others/js.cmi others/js.cmj $bsc js_pkg
o others/belt_Float.cmj : cc_cmi others/belt_Float.res | others/belt.cmi others/belt_Float.cmi others/belt_internals.cmi others/js.cmi $bsc js_pkg
Expand Down
Loading