diff --git a/CHANGELOG.md b/CHANGELOG.md index f48e96495c..77e942144f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Add location information to duplicate type definition error messages. https://github.com/rescript-lang/rescript-compiler/pull/6199 - Replace normal module errors with Super_error module, and clean up Super_error. https://github.com/rescript-lang/rescript-compiler/pull/6199 +- `Js.Json.t`, `Js.null` and `Js.nullable` are now untagged variants representing their runtime values, instead of abstract types. https://github.com/rescript-lang/rescript-compiler/pull/6218 # 11.0.0-alpha.5 diff --git a/jscomp/others/js.ml b/jscomp/others/js.ml index 90b8c28833..39ae6a3a30 100644 --- a/jscomp/others/js.ml +++ b/jscomp/others/js.ml @@ -86,7 +86,10 @@ end (**/**) -type +'a null +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. *) @@ -96,7 +99,12 @@ 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 +type +'a nullable = + | Value of 'a + | Null [@as null] + | Undefined [@as undefined] +[@@unboxed] + (** A value of this type can be undefined, null or 'a. This type is equivalent to Js.Null_undefined.t. *) diff --git a/jscomp/others/js_json.ml b/jscomp/others/js_json.ml index f4d369f33c..d135cdad32 100644 --- a/jscomp/others/js_json.ml +++ b/jscomp/others/js_json.ml @@ -24,7 +24,15 @@ (** Efficient JSON encoding using JavaScript API *) -type t +type t = + | False [@as false] + | True [@as true] + | Null [@as null] + | String of string + | Number of float + | Object of t Js.Dict.t + | Array of t array +[@@unboxed] type _ kind = | String : Js_string.t kind diff --git a/jscomp/others/js_json.mli b/jscomp/others/js_json.mli index 5b0f403ccc..87b69ba7dd 100644 --- a/jscomp/others/js_json.mli +++ b/jscomp/others/js_json.mli @@ -30,7 +30,15 @@ (** ## Types *) -type t +type t = + | False [@as false] + | True [@as true] + | Null [@as null] + | String of string + | Number of float + | Object of t Js.Dict.t + | Array of t array +[@@unboxed] (** The JSON data structure *) (** Underlying type of a JSON value *) diff --git a/jscomp/others/js_null.ml b/jscomp/others/js_null.ml index d9fa7dfd88..19e2fd7915 100644 --- a/jscomp/others/js_null.ml +++ b/jscomp/others/js_null.ml @@ -25,7 +25,10 @@ (** Provides functionality for dealing with the `'a Js.null` type *) -type + 'a t = 'a Js.null +type + 'a t = 'a Js.null = + | Value of 'a + | Null [@as null] +[@@unboxed] external to_opt : 'a t -> 'a option = "#null_to_opt" external toOption : 'a t -> 'a option = "#null_to_opt" diff --git a/jscomp/others/js_null.mli b/jscomp/others/js_null.mli index c3e6a24f77..d7976f50c9 100644 --- a/jscomp/others/js_null.mli +++ b/jscomp/others/js_null.mli @@ -26,7 +26,10 @@ (** Provides functionality for dealing with the `Js.null('a)` type *) -type +'a t = 'a Js.null +type +'a t = 'a Js.null = + | Value of 'a + | Null [@as null] +[@@unboxed] (** Local alias for `Js.null('a)` *) external return : 'a -> 'a t = "%identity" diff --git a/jscomp/others/js_null_undefined.ml b/jscomp/others/js_null_undefined.ml index 415f966642..d91319e0fa 100644 --- a/jscomp/others/js_null_undefined.ml +++ b/jscomp/others/js_null_undefined.ml @@ -24,7 +24,12 @@ (** Contains functionality for dealing with values that can be both `null` and `undefined` *) -type + 'a t = 'a Js.nullable +type + 'a t = 'a Js.nullable = + | Value of 'a + | Null [@as null] + | Undefined [@as undefined] +[@@unboxed] + external toOption : 'a t -> 'a option = "#nullable_to_opt" external to_opt : 'a t -> 'a option = "#nullable_to_opt" external return : 'a -> 'a t = "%identity" diff --git a/jscomp/others/js_null_undefined.mli b/jscomp/others/js_null_undefined.mli index b81a928e62..9f5d8655e5 100644 --- a/jscomp/others/js_null_undefined.mli +++ b/jscomp/others/js_null_undefined.mli @@ -24,7 +24,11 @@ (** Contains functionality for dealing with values that can be both `null` and `undefined` *) -type +'a t = 'a Js.null_undefined +type +'a t = 'a Js.nullable = + | Value of 'a + | Null [@as null] + | Undefined [@as undefined] +[@@unboxed] (** Local alias for `Js.null_undefined('a)`. *) external return : 'a -> 'a t = "%identity" diff --git a/jscomp/others/release.ninja b/jscomp/others/release.ninja index a6335e32c2..226767ce46 100644 --- a/jscomp/others/release.ninja +++ b/jscomp/others/release.ninja @@ -32,7 +32,7 @@ o others/js_float.cmi others/js_float.cmj : cc others/js_float.ml | others/belt_ o others/js_global.cmi others/js_global.cmj : cc others/js_global.ml | others/belt_internals.cmi others/js.cmi $bsc o others/js_int.cmi others/js_int.cmj : cc others/js_int.ml | others/belt_internals.cmi others/js.cmi $bsc o others/js_json.cmj : cc_cmi others/js_json.ml | others/belt_internals.cmi others/js.cmi others/js.cmj others/js_array2.cmj others/js_dict.cmj others/js_json.cmi others/js_string.cmj others/js_types.cmj $bsc -o others/js_json.cmi : cc others/js_json.mli | others/belt_internals.cmi others/js.cmi others/js_dict.cmi others/js_null.cmi others/js_string.cmj others/js_types.cmi $bsc +o others/js_json.cmi : cc others/js_json.mli | others/belt_internals.cmi others/js.cmi others/js.cmj others/js_dict.cmi others/js_null.cmi others/js_string.cmj others/js_types.cmi $bsc o others/js_list.cmj : cc_cmi others/js_list.ml | others/belt_internals.cmi others/js.cmi others/js_array2.cmj others/js_list.cmi others/js_vector.cmj $bsc o others/js_list.cmi : cc others/js_list.mli | others/belt_internals.cmi others/js.cmi $bsc o others/js_map.cmi others/js_map.cmj : cc others/js_map.ml | others/belt_internals.cmi others/js.cmi $bsc diff --git a/jscomp/runtime/js.ml b/jscomp/runtime/js.ml index dacc4aceca..38c7d9949e 100644 --- a/jscomp/runtime/js.ml +++ b/jscomp/runtime/js.ml @@ -57,7 +57,10 @@ end (**/**) -type + 'a null +type + 'a null = + | Value of 'a + | Null [@as null] +[@@unboxed] (** nullable, value of this type can be either [null] or ['a] this type is the same as type [t] in {!Null} *) @@ -66,7 +69,11 @@ type + 'a undefined (** value of this type can be either [undefined] or ['a] this type is the same as type [t] in {!Undefined} *) -type + 'a nullable +type + 'a nullable = + | Value of 'a + | Null [@as null] + | Undefined [@as undefined] +[@@unboxed] (** value of this type can be [undefined], [null] or ['a] this type is the same as type [t] n {!Null_undefined} *) diff --git a/jscomp/test/custom_error_test.js b/jscomp/test/custom_error_test.js index 93b4a7a751..5d2c407636 100644 --- a/jscomp/test/custom_error_test.js +++ b/jscomp/test/custom_error_test.js @@ -2,7 +2,6 @@ 'use strict'; var Js_exn = require("../../lib/js/js_exn.js"); -var Caml_option = require("../../lib/js/caml_option.js"); var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js"); function test_js_error(param) { @@ -18,7 +17,7 @@ function test_js_error(param) { } throw err; } - return Caml_option.some(e); + return e; } function test_js_error2(param) { @@ -48,12 +47,12 @@ function example1(param) { } throw err; } - return Caml_option.some(v); + return v; } function example2(param) { try { - return Caml_option.some(JSON.parse(" {\"x\"}")); + return JSON.parse(" {\"x\"}"); } catch (raw_exn){ var exn = Caml_js_exceptions.internalToOCamlException(raw_exn); diff --git a/jscomp/test/js_json_test.js b/jscomp/test/js_json_test.js index 4248f81147..8068a48e4c 100644 --- a/jscomp/test/js_json_test.js +++ b/jscomp/test/js_json_test.js @@ -81,7 +81,7 @@ add_test("File \"js_json_test.res\", line 22, characters 11-18", (function (para _0: false }; } - var ty2 = Js_json.classify(Caml_option.valFromOption(v$1)); + var ty2 = Js_json.classify(v$1); if (typeof ty2 !== "object") { return { TAG: "Ok",