Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 17 additions & 8 deletions jscomp/others/js_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
type symbol
(** Js symbol type only available in ES6 *)

type bigint_val
(** Js bigint type only available in ES2020 *)

type obj_val
type undefined_val
(** This type has only one value `undefined` *)
Expand All @@ -45,6 +48,7 @@ type _ t =
| Function : function_val t
| Object : obj_val t
| Symbol : symbol t
| BigInt : bigint_val t



Expand All @@ -58,6 +62,7 @@ type tagged_t =
| JSFunction of function_val
| JSObject of obj_val
| JSSymbol of symbol
| JSBigInt of bigint_val

let classify (x : 'a) : tagged_t =
let ty = Js.typeof x in
Expand All @@ -66,18 +71,20 @@ let classify (x : 'a) : tagged_t =
if x == (Obj.magic Js_null.empty) then
JSNull else
if ty = "number" then
JSNumber (Obj.magic x ) else
JSNumber (Obj.magic x) else
if ty = "bigint" then
JSBigInt (Obj.magic x) else
if ty = "string" then
JSString (Obj.magic x) else
if ty = "boolean" then
if (Obj.magic x) = true then JSTrue
if (Obj.magic x) = true then JSTrue
else JSFalse else
if ty = "symbol" then
JSSymbol (Obj.magic x) else
if ty = "function" then
JSFunction (Obj.magic x) else
if ty = "object" then
JSObject (Obj.magic x)
JSFunction (Obj.magic x)
else
JSSymbol (Obj.magic x)
JSObject (Obj.magic x)


let test (type a) (x : 'a) (v : a t) : bool =
Expand Down Expand Up @@ -105,5 +112,7 @@ let test (type a) (x : 'a) (v : a t) : bool =
Js.typeof x = "object"
| Symbol
->
Js.typeof x = "symbol"

Js.typeof x = "symbol"
| BigInt
->
Js.typeof x = "bigint"
5 changes: 5 additions & 0 deletions jscomp/others/js_types.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
type symbol
(** Js symbol type (only available in ES6) *)

type bigint_val
(** Js bigint type only available in ES2020 *)

type obj_val

type undefined_val
Expand All @@ -46,6 +49,7 @@ type _ t =
| Function : function_val t
| Object : obj_val t
| Symbol : symbol t
| BigInt : bigint_val t

val test : 'a -> 'b t -> bool
(**
Expand All @@ -69,5 +73,6 @@ type tagged_t =
| JSFunction of function_val
| JSObject of obj_val
| JSSymbol of symbol
| JSBigInt of bigint_val

val classify : 'a -> tagged_t
3 changes: 2 additions & 1 deletion jscomp/test/typeof_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ let string_or_number (type t) x =
| JSFalse | JSTrue -> false
| JSFunction _ -> Js.log ("Function"); false
| JSObject _ -> false
| JSSymbol _ -> false
| JSSymbol _ -> false
| JSBigInt _ -> false

let suites = Mt.[
"int_type", (fun _ -> Eq(Js.typeof 3, "number") );
Expand Down