Skip to content

Commit

Permalink
feat: remove Js.Array2, use labeled arguments for Js.Array (#895)
Browse files Browse the repository at this point in the history
* feat: remove `Js.Array2`, use labeled arguments for `Js.Array`

* fix: tests

* code review

* revert lastIndexOf
  • Loading branch information
anmonteiro authored Nov 16, 2023
1 parent 4c018fc commit a154b01
Show file tree
Hide file tree
Showing 26 changed files with 2,646 additions and 2,898 deletions.
3 changes: 0 additions & 3 deletions jscomp/runtime/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ type +'a nullable = 'a Js_null_undefined.t

type +'a null_undefined = 'a nullable

module Array2 = Js_array2
(** Provide bindings to Js array*)

module Exn = Js_exn
(** Provide utilities for dealing with Js exceptions *)

Expand Down
232 changes: 98 additions & 134 deletions jscomp/runtime/js_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,216 +25,180 @@
(** JavaScript Array API *)

type 'a t = 'a array
type 'a array_like = 'a Js_array2.array_like
type 'a array_like

(* commented out until bs has a plan for iterators
(* commented out until Melange has a plan for iterators
type 'a array_iter = 'a array_like
*)

external from : 'a array_like -> 'a array = "Array.from"
(* ES2015 *)

external fromMap : 'a array_like -> (('a -> 'b)[@mel.uncurry]) -> 'b array
external fromMap : 'a array_like -> f:(('a -> 'b)[@mel.uncurry]) -> 'b array
= "Array.from"

(* ES2015 *)

external isArray : 'a -> bool = "Array.isArray"

(* ES2015 *)
(* ES2015 *)

(* Array.of: seems pointless unless you can bind *)
external length : 'a array -> int = "length" [@@mel.get]

(* Mutator functions
*)
external copyWithin : to_:int -> 'this = "copyWithin"
[@@mel.send.pipe: 'a t as 'this]
(* ES2015 *)

external copyWithinFrom : to_:int -> from:int -> 'this = "copyWithin"
[@@mel.send.pipe: 'a t as 'this]
(* ES2015 *)
(** Mutating functions *)

external copyWithinFromRange : to_:int -> start:int -> end_:int -> 'this
external copyWithin : 'a t -> to_:int -> ?start:int -> ?end_:int -> unit -> 'a t
= "copyWithin"
[@@mel.send.pipe: 'a t as 'this]
(* ES2015 *)

external fillInPlace : 'a -> 'this = "fill" [@@mel.send.pipe: 'a t as 'this]
[@@mel.send]
(* ES2015 *)

external fillFromInPlace : 'a -> from:int -> 'this = "fill"
[@@mel.send.pipe: 'a t as 'this]
external fill : 'a t -> value:'a -> ?start:int -> ?end_:int -> unit -> 'a t
= "fill"
[@@mel.send]
(* ES2015 *)

external fillRangeInPlace : 'a -> start:int -> end_:int -> 'this = "fill"
[@@mel.send.pipe: 'a t as 'this]
(* ES2015 *)

external pop : 'a option = "pop"
[@@mel.send.pipe: 'a t as 'this] [@@mel.return undefined_to_opt]
external pop : 'a t -> 'a option = "pop"
[@@mel.send] [@@mel.return undefined_to_opt]
(** https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push *)

external push : 'a -> int = "push" [@@mel.send.pipe: 'a t as 'this]
external push : 'a t -> value:'a -> int = "push" [@@mel.send]

external pushMany : 'a array -> int = "push"
[@@mel.send.pipe: 'a t as 'this] [@@mel.splice]
external pushMany : 'a t -> values:'a array -> int = "push"
[@@mel.send] [@@mel.splice]

external reverseInPlace : 'this = "reverse" [@@mel.send.pipe: 'a t as 'this]
external reverseInPlace : 'a t -> 'a t = "reverse" [@@mel.send]

external shift : 'a option = "shift"
[@@mel.send.pipe: 'a t as 'this] [@@mel.return { undefined_to_opt }]
external shift : 'a t -> 'a option = "shift"
[@@mel.send] [@@mel.return undefined_to_opt]

external sortInPlace : 'this = "sort" [@@mel.send.pipe: 'a t as 'this]
external sortInPlace : 'a t -> 'a t = "sort" [@@mel.send]

external sortInPlaceWith : (('a -> 'a -> int)[@mel.uncurry]) -> 'this = "sort"
[@@mel.send.pipe: 'a t as 'this]
external sortInPlaceWith : 'a t -> f:(('a -> 'a -> int)[@mel.uncurry]) -> 'a t
= "sort"
[@@mel.send]

external spliceInPlace : pos:int -> remove:int -> add:'a array -> 'this
external spliceInPlace : 'a t -> pos:int -> remove:int -> add:'a array -> 'a t
= "splice"
[@@mel.send.pipe: 'a t as 'this] [@@mel.splice]
[@@mel.send] [@@mel.splice]

external removeFromInPlace : pos:int -> 'this = "splice"
[@@mel.send.pipe: 'a t as 'this]
external removeFromInPlace : 'a t -> pos:int -> 'a t = "splice" [@@mel.send]

external removeCountInPlace : pos:int -> count:int -> 'this = "splice"
[@@mel.send.pipe: 'a t as 'this]
external removeCountInPlace : 'a t -> pos:int -> count:int -> 'a t = "splice"
[@@mel.send]
(* screwy naming, but screwy function *)

external unshift : 'a -> int = "unshift" [@@mel.send.pipe: 'a t as 'this]

external unshiftMany : 'a array -> int = "unshift"
[@@mel.send.pipe: 'a t as 'this] [@@mel.splice]
external unshift : 'a t -> value:'a -> int = "unshift" [@@mel.send]

(* Accessor functions
*)
external append : 'a -> 'this = "concat"
[@@mel.send.pipe: 'a t as 'this]
[@@deprecated "append is not type-safe. Use `concat` instead, and see #1884"]

external concat : 'this -> 'this = "concat" [@@mel.send.pipe: 'a t as 'this]

external concatMany : 'this array -> 'this = "concat"
[@@mel.send.pipe: 'a t as 'this] [@@mel.splice]

(* TODO: Not available in Node V4 *)
external includes : 'a -> bool = "includes"
[@@mel.send.pipe: 'a t as 'this]
(** ES2016 *)

external indexOf : 'a -> int = "indexOf" [@@mel.send.pipe: 'a t as 'this]

external indexOfFrom : 'a -> from:int -> int = "indexOf"
[@@mel.send.pipe: 'a t as 'this]
external unshiftMany : 'a t -> values:'a array -> int = "unshift"
[@@mel.send] [@@mel.splice]

external join : 'a t -> string = "join"
[@@mel.send] [@@deprecated "please use joinWith instead"]
external concat : 'a t -> other:'a t -> 'a t = "concat" [@@mel.send]

external joinWith : string -> string = "join" [@@mel.send.pipe: 'a t as 'this]
external concatMany : 'a t -> arrays:'a t array -> 'a t = "concat"
[@@mel.send] [@@mel.splice]

external lastIndexOf : 'a -> int = "lastIndexOf"
[@@mel.send.pipe: 'a t as 'this]
external includes : 'a t -> value:'a -> bool = "includes"
[@@mel.send]
(** ES2015 *)

external lastIndexOfFrom : 'a -> from:int -> int = "lastIndexOf"
[@@mel.send.pipe: 'a t as 'this]
external join : 'a t -> ?sep:string -> string = "join" [@@mel.send]

external lastIndexOf_start : 'a -> int = "lastIndexOf"
[@@mel.send.pipe: 'a t as 'this] [@@deprecated "Please use `lastIndexOf"]
(** Accessor functions *)

external slice : start:int -> end_:int -> 'this = "slice"
[@@mel.send.pipe: 'a t as 'this]
external indexOf : 'a t -> value:'a -> ?start:int -> unit -> int = "indexOf"
[@@mel.send]

external copy : 'this = "slice" [@@mel.send.pipe: 'a t as 'this]
external lastIndexOf : 'a t -> value:'a -> int = "lastIndexOf" [@@mel.send]

external slice_copy : unit -> 'this = "slice"
[@@mel.send.pipe: 'a t as 'this] [@@deprecated "Please use `copy`"]
external lastIndexOfFrom : 'a t -> value:'a -> start:int -> int = "lastIndexOf"
[@@mel.send]

external sliceFrom : int -> 'this = "slice" [@@mel.send.pipe: 'a t as 'this]
external copy : 'a t -> 'a t = "slice" [@@mel.send]

external slice_start : int -> 'this = "slice"
[@@mel.send.pipe: 'a t as 'this] [@@deprecated "Please use `sliceFrom`"]
external slice : 'a t -> ?start:int -> ?end_:int -> unit -> 'a t = "slice"
[@@mel.send]

external toString : string = "toString" [@@mel.send.pipe: 'a t as 'this]

external toLocaleString : string = "toLocaleString"
[@@mel.send.pipe: 'a t as 'this]
external toString : 'a t -> string = "toString" [@@mel.send]
external toLocaleString : 'a t -> string = "toLocaleString" [@@mel.send]

(* Iteration functions
*)
(* commented out until bs has a plan for iterators
external entries : (int * 'a) array_iter = "" [@@mel.send.pipe: 'a t as 'this] (* ES2015 *)
(* commented out until Melange has a plan for iterators
external entries : 'a t -> (int * 'a) array_iter = "" [@@mel.send] (* ES2015 *)
*)

external every : (('a -> bool)[@mel.uncurry]) -> bool = "every"
[@@mel.send.pipe: 'a t as 'this]
external every : 'a t -> f:(('a -> bool)[@mel.uncurry]) -> bool = "every"
[@@mel.send]

external everyi : (('a -> int -> bool)[@mel.uncurry]) -> bool = "every"
[@@mel.send.pipe: 'a t as 'this]
external everyi : 'a t -> f:(('a -> int -> bool)[@mel.uncurry]) -> bool
= "every"
[@@mel.send]

external filter : (('a -> bool)[@mel.uncurry]) -> 'this = "filter"
[@@mel.send.pipe: 'a t as 'this]
(** should we use [bool] or [boolean] seems they are intechangeable here *)
external filter : 'a t -> f:(('a -> bool)[@mel.uncurry]) -> 'a t = "filter"
[@@mel.send]

external filteri : (('a -> int -> bool)[@mel.uncurry]) -> 'this = "filter"
[@@mel.send.pipe: 'a t as 'this]
external filteri : 'a t -> f:(('a -> int -> bool)[@mel.uncurry]) -> 'a t
= "filter"
[@@mel.send]

external find : (('a -> bool)[@mel.uncurry]) -> 'a option = "find"
[@@mel.send.pipe: 'a t as 'this] [@@mel.return { undefined_to_opt }]
external find : 'a t -> f:(('a -> bool)[@mel.uncurry]) -> 'a option = "find"
[@@mel.send] [@@mel.return { undefined_to_opt }]
(* ES2015 *)

external findi : (('a -> int -> bool)[@mel.uncurry]) -> 'a option = "find"
[@@mel.send.pipe: 'a t as 'this] [@@mel.return { undefined_to_opt }]
external findi : 'a t -> f:(('a -> int -> bool)[@mel.uncurry]) -> 'a option
= "find"
[@@mel.send] [@@mel.return { undefined_to_opt }]
(* ES2015 *)

external findIndex : (('a -> bool)[@mel.uncurry]) -> int = "findIndex"
[@@mel.send.pipe: 'a t as 'this]
external findIndex : 'a t -> f:(('a -> bool)[@mel.uncurry]) -> int = "findIndex"
[@@mel.send]
(* ES2015 *)

external findIndexi : (('a -> int -> bool)[@mel.uncurry]) -> int = "findIndex"
[@@mel.send.pipe: 'a t as 'this]
external findIndexi : 'a t -> f:(('a -> int -> bool)[@mel.uncurry]) -> int
= "findIndex"
[@@mel.send]
(* ES2015 *)

external forEach : (('a -> unit)[@mel.uncurry]) -> unit = "forEach"
[@@mel.send.pipe: 'a t as 'this]
external forEach : 'a t -> f:(('a -> unit)[@mel.uncurry]) -> unit = "forEach"
[@@mel.send]

external forEachi : (('a -> int -> unit)[@mel.uncurry]) -> unit = "forEach"
[@@mel.send.pipe: 'a t as 'this]
external forEachi : 'a t -> f:(('a -> int -> unit)[@mel.uncurry]) -> unit
= "forEach"
[@@mel.send]

(* commented out until bs has a plan for iterators
external keys : int array_iter = "" [@@mel.send.pipe: 'a t as 'this] (* ES2015 *)
(* commented out until Melange has a plan for iterators
external keys : 'a t -> int array_iter = "" [@@mel.send] (* ES2015 *)
*)

external map : (('a -> 'b)[@mel.uncurry]) -> 'b t = "map"
[@@mel.send.pipe: 'a t as 'this]

external mapi : (('a -> int -> 'b)[@mel.uncurry]) -> 'b t = "map"
[@@mel.send.pipe: 'a t as 'this]
external map : 'a t -> f:(('a -> 'b)[@mel.uncurry]) -> 'b t = "map" [@@mel.send]

external reduce : (('b -> 'a -> 'b)[@mel.uncurry]) -> 'b -> 'b = "reduce"
[@@mel.send.pipe: 'a t as 'this]
external mapi : 'a t -> f:(('a -> int -> 'b)[@mel.uncurry]) -> 'b t = "map"
[@@mel.send]

external reducei : (('b -> 'a -> int -> 'b)[@mel.uncurry]) -> 'b -> 'b
external reduce : 'a t -> f:(('b -> 'a -> 'b)[@mel.uncurry]) -> init:'b -> 'b
= "reduce"
[@@mel.send.pipe: 'a t as 'this]
[@@mel.send]

external reduceRight : (('b -> 'a -> 'b)[@mel.uncurry]) -> 'b -> 'b
= "reduceRight"
[@@mel.send.pipe: 'a t as 'this]
external reducei :
'a t -> f:(('b -> 'a -> int -> 'b)[@mel.uncurry]) -> init:'b -> 'b = "reduce"
[@@mel.send]

external reduceRighti : (('b -> 'a -> int -> 'b)[@mel.uncurry]) -> 'b -> 'b
external reduceRight :
'a t -> f:(('b -> 'a -> 'b)[@mel.uncurry]) -> init:'b -> 'b = "reduceRight"
[@@mel.send]

external reduceRighti :
'a t -> f:(('b -> 'a -> int -> 'b)[@mel.uncurry]) -> init:'b -> 'b
= "reduceRight"
[@@mel.send.pipe: 'a t as 'this]
[@@mel.send]

external some : (('a -> bool)[@mel.uncurry]) -> bool = "some"
[@@mel.send.pipe: 'a t as 'this]
external some : 'a t -> f:(('a -> bool)[@mel.uncurry]) -> bool = "some"
[@@mel.send]

external somei : (('a -> int -> bool)[@mel.uncurry]) -> bool = "some"
[@@mel.send.pipe: 'a t as 'this]
external somei : 'a t -> f:(('a -> int -> bool)[@mel.uncurry]) -> bool = "some"
[@@mel.send]

(* commented out until Melange has a plan for iterators
external values : 'a t -> 'a array_iter = "" [@@mel.send] (* ES2015 *) *)

(* commented out until bs has a plan for iterators
external values : 'a array_iter = "" [@@mel.send.pipe: 'a t as 'this] (* ES2015 *)
*)
external unsafe_get : 'a array -> int -> 'a = "%array_unsafe_get"
external unsafe_set : 'a array -> int -> 'a -> unit = "%array_unsafe_set"
Loading

0 comments on commit a154b01

Please sign in to comment.