Skip to content

Convert js.ml files to .res. #6835

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

Merged
merged 1 commit into from
Jul 2, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- Remove a number of ast nodes never populated by the .res parser, and resulting dead code. https://github.com/rescript-lang/rescript-compiler/pull/6830
- Remove coercion with 2 types from internal representation. Coercion `e : t1 :> t2` was only supported in `.ml` syntax and never by the `.res` parser. https://github.com/rescript-lang/rescript-compiler/pull/6829
- Convert `caml_format` and `js_math` to `.res`. https://github.com/rescript-lang/rescript-compiler/pull/6834
- Convert `js.ml` files to `.res`. https://github.com/rescript-lang/rescript-compiler/pull/6835

#### :nail_care: Polish

Expand Down
229 changes: 114 additions & 115 deletions jscomp/others/js.ml → jscomp/others/js.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
/* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -20,17 +20,17 @@
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

[@@@config {flags = [|"-unboxed-types"; "-w"; "-49"|]}]
(* DESIGN:
@@config({flags: ["-unboxed-types", "-w", "-49"]})
@@ocaml.text(/* DESIGN:
- It does not have any code, all its code will be inlined so that
there will never be
{[ require('js')]}
- Its interface should be minimal
*)
*/

(**
"
The Js module mostly contains ReScript bindings to _standard JavaScript APIs_
like [console.log](https://developer.mozilla.org/en-US/docs/Web/API/Console/log),
or the JavaScript
Expand All @@ -57,234 +57,233 @@ In the meantime, there are several options for dealing with the data-last APIs:

```rescript
/* Js.String (data-last API used with pipe last operator) */
Js.log("2019-11-10" |> Js.String.split("-"))
Js.log("ReScript" |> Js.String.startsWith("Re"))
Js.log(\"2019-11-10\" |> Js.String.split(\"-\"))
Js.log(\"ReScript\" |> Js.String.startsWith(\"Re\"))

/* Js.String (data-last API used with pipe first operator) */
Js.log("2019-11-10"->Js.String.split("-", _))
Js.log("ReScript"->Js.String.startsWith("Re", _))
Js.log(\"2019-11-10\"->Js.String.split(\"-\", _))
Js.log(\"ReScript\"->Js.String.startsWith(\"Re\", _))

/* Js.String (data-last API used without any piping) */
Js.log(Js.String.split("-", "2019-11-10"))
Js.log(Js.String.startsWith("Re", "ReScript"))
Js.log(Js.String.split(\"-\", \"2019-11-10\"))
Js.log(Js.String.startsWith(\"Re\", \"ReScript\"))
```
## Js.Xxx2 Modules

Prefer `Js.Array2` over `Js.Array`, `Js.String2` over `Js.String`, etc. The latters are old modules.
*)
")

type 'a t = < .. > as 'a
(** JS object type *)
@ocaml.doc(" JS object type ")
type t<'a> = {..} as 'a

module MapperRt = Js_mapperRt

module Internal = struct
external opaqueFullApply : 'a -> 'a = "%uncurried_apply"
module Internal = {
external opaqueFullApply: 'a => 'a = "%uncurried_apply"

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

(**/**)
@@ocaml.text("/*")

(**
@ocaml.doc("
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]
")
@unboxed
type null<+'a> = Value('a) | @as(null) Null

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

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

(**
@@ocaml.text("
A value of this type can be undefined, null or 'a. This type is equivalent to Js.Null_undefined.t.
*)
")

type +'a null_undefined = 'a nullable
type null_undefined<+'a> = nullable<'a>

external toOption : 'a nullable -> 'a option = "#nullable_to_opt"
external undefinedToOption : 'a undefined -> 'a option = "#undefined_to_opt"
external nullToOption : 'a null -> 'a option = "#null_to_opt"
external isNullable : 'a nullable -> bool = "#is_nullable"
external import : 'a -> 'a promise = "#import"
external toOption: nullable<'a> => option<'a> = "#nullable_to_opt"
external undefinedToOption: undefined<'a> => option<'a> = "#undefined_to_opt"
external nullToOption: null<'a> => option<'a> = "#null_to_opt"
external isNullable: nullable<'a> => bool = "#is_nullable"
external import: 'a => promise<'a> = "#import"

external testAny : 'a -> bool = "#is_nullable"
(** The same as {!test} except that it is more permissive on the types of input *)
@ocaml.doc(" The same as {!test} except that it is more permissive on the types of input ")
external testAny: 'a => bool = "#is_nullable"

type (+'a, +'e) promise
(**
@ocaml.doc("
The promise type, defined here for interoperation across packages.
*)
")
type promise<+'a, +'e>

external null : 'a null = "#null"
(**
@ocaml.doc("
The same as empty in `Js.Null`. Compiles to `null`.
*)
")
external null: null<'a> = "#null"

external undefined : 'a undefined = "#undefined"
(**
@ocaml.doc("
The same as empty `Js.Undefined`. Compiles to `undefined`.
*)
")
external undefined: undefined<'a> = "#undefined"

external typeof : 'a -> string = "#typeof"
(**
@ocaml.doc("
`typeof x` will be compiled as `typeof x` in JS. Please consider functions in
`Js.Types` for a type safe way of reflection.
*)
")
external typeof: 'a => string = "#typeof"

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

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

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

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

external eqNull : 'a -> 'a null -> bool = "%bs_equal_null"
external eqUndefined : 'a -> 'a undefined -> bool = "%bs_equal_undefined"
external eqNullable : 'a -> 'a nullable -> bool = "%bs_equal_nullable"
external eqNull: ('a, null<'a>) => bool = "%bs_equal_null"
external eqUndefined: ('a, undefined<'a>) => bool = "%bs_equal_undefined"
external eqNullable: ('a, nullable<'a>) => bool = "%bs_equal_nullable"

(** ## Operators *)
@@ocaml.text(" ## Operators ")

external unsafe_lt : 'a -> 'a -> bool = "#unsafe_lt"
(**
@ocaml.doc("
`unsafe_lt(a, b)` will be compiled as `a < b`.
It is marked as unsafe, since it is impossible
to give a proper semantics for comparision which applies to any type
*)
")
external unsafe_lt: ('a, 'a) => bool = "#unsafe_lt"

external unsafe_le : 'a -> 'a -> bool = "#unsafe_le"
(**
@ocaml.doc("
`unsafe_le(a, b)` will be compiled as `a <= b`.
See also `Js.unsafe_lt`.
*)
")
external unsafe_le: ('a, 'a) => bool = "#unsafe_le"

external unsafe_gt : 'a -> 'a -> bool = "#unsafe_gt"
(**
@ocaml.doc("
`unsafe_gt(a, b)` will be compiled as `a > b`.
See also `Js.unsafe_lt`.
*)
")
external unsafe_gt: ('a, 'a) => bool = "#unsafe_gt"

external unsafe_ge : 'a -> 'a -> bool = "#unsafe_ge"
(**
@ocaml.doc("
`unsafe_ge(a, b)` will be compiled as `a >= b`.
See also `Js.unsafe_lt`.
*)
")
external unsafe_ge: ('a, 'a) => bool = "#unsafe_ge"

(** ## Nested Modules *)
@@ocaml.text(" ## Nested Modules ")

@ocaml.doc(" Provide utilities for `Js.null<'a>` ")
module Null = Js_null
(** Provide utilities for `Js.null<'a>` *)

@ocaml.doc(" Provide utilities for `Js.undefined<'a>` ")
module Undefined = Js_undefined
(** Provide utilities for `Js.undefined<'a>` *)

@ocaml.doc(" Provide utilities for `Js.null_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

@ocaml.doc(" Provide utilities for dealing with Js exceptions ")
module Exn = Js_exn
(** Provide utilities for dealing with Js exceptions *)

@ocaml.doc(" Provide bindings to JS array")
module Array = Js_array
(** Provide bindings to JS array*)

@ocaml.doc(" Provide bindings to JS array")
module Array2 = Js_array2
(** Provide bindings to JS array*)

@ocaml.doc(" Provide bindings to JS string ")
module String = Js_string
(** Provide bindings to JS string *)

@ocaml.doc(" Provide bindings to JS string ")
module String2 = Js_string2
(** Provide bindings to JS string *)

@ocaml.doc(" Provide bindings to JS regex expression ")
module Re = Js_re
(** Provide bindings to JS regex expression *)

@ocaml.doc(" Provide bindings to JS Promise ")
module Promise = Js_promise
(** Provide bindings to JS Promise *)

@ocaml.doc(" Provide bindings to JS Promise ")
module Promise2 = Js_promise2
(** Provide bindings to JS Promise *)

@ocaml.doc(" Provide bindings for JS Date ")
module Date = Js_date
(** Provide bindings for JS Date *)

@ocaml.doc(" Provide utilities for JS dictionary object ")
module Dict = Js_dict
(** Provide utilities for JS dictionary object *)

@ocaml.doc(" Provide bindings to JS global functions in global namespace")
module Global = Js_global
(** Provide bindings to JS global functions in global namespace*)

@ocaml.doc(" Provide utilities for json ")
module Json = Js_json
(** Provide utilities for json *)

@ocaml.doc(" Provide bindings for JS `Math` object ")
module Math = Js_math
(** Provide bindings for JS `Math` object *)

@ocaml.doc(" Provide utilities for `Js.t` ")
module Obj = Js_obj
(** Provide utilities for `Js.t` *)

@ocaml.doc(" Provide bindings for JS typed array ")
module Typed_array = Js_typed_array
(** Provide bindings for JS typed array *)

@ocaml.doc(" Provide bindings for JS typed array ")
module TypedArray2 = Js_typed_array2
(** Provide bindings for JS typed array *)

@ocaml.doc(" Provide utilities for manipulating JS types ")
module Types = Js_types
(** Provide utilities for manipulating JS types *)

@ocaml.doc(" Provide utilities for JS float ")
module Float = Js_float
(** Provide utilities for JS float *)

@ocaml.doc(" Provide utilities for int ")
module Int = Js_int
(** Provide utilities for int *)

@ocaml.doc(" Provide utilities for bigint ")
module BigInt = Js_bigint
(** Provide utilities for bigint *)

@ocaml.doc(" Provide utilities for File ")
module File = Js_file
(** Provide utilities for File *)

@ocaml.doc(" Provide utilities for Blob ")
module Blob = Js_blob
(** Provide utilities for Blob *)

@ocaml.doc(" Provide utilities for option ")
module Option = Js_option
(** Provide utilities for option *)

@ocaml.doc(" Define the interface for result ")
module Result = Js_result
(** Define the interface for result *)

@ocaml.doc(" Provide utilities for list ")
module List = Js_list
(** Provide utilities for list *)

@ocaml.doc(" Provides bindings for JS Vector ")
module Vector = Js_vector
(** Provides bindings for JS Vector *)

@ocaml.doc(" Provides bindings for console ")
module Console = Js_console
(** Provides bindings for console *)

@ocaml.doc(" Provides bindings for ES6 Set ")
module Set = Js_set
(** Provides bindings for ES6 Set *)

@ocaml.doc(" Provides bindings for ES6 WeakSet ")
module WeakSet = Js_weakset
(** Provides bindings for ES6 WeakSet *)

@ocaml.doc(" Provides bindings for ES6 Map ")
module Map = Js_map
(** Provides bindings for ES6 Map *)

@ocaml.doc(" Provides bindings for ES6 WeakMap ")
module WeakMap = Js_weakmap
(** Provides bindings for ES6 WeakMap *)
Loading