Skip to content

Commit b38fa14

Browse files
committed
Move one more case.
1 parent 7653b16 commit b38fa14

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

jscomp/core/js_exp_make.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ val assign : ?comment:string -> t -> t -> t
188188

189189
val tag_type : Ast_untagged_variants.tag_type -> t
190190

191+
val emit_check : t Ast_untagged_variants.DynamiChecks.t -> t
192+
191193
val triple_equal : ?comment:string -> t -> t -> t
192194
(* TODO: reduce [triple_equal] use *)
193195

jscomp/core/lam_compile.ml

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -750,22 +750,13 @@ and compile_string_cases ~cxt ~switch_exp ~default cases: initialization =
750750
~switch_exp
751751
~default
752752
and compile_untagged_cases ~cxt ~switch_exp ~default cases =
753-
let add_runtime_type_check (literal: Ast_untagged_variants.tag_type) x y = match literal with
754-
| Untagged IntType
755-
| Untagged StringType
756-
| Untagged FloatType
757-
| Untagged ObjectType -> E.string_equal (E.typeof y) x
758-
| Untagged ArrayType -> E.is_array y
759-
| Untagged UnknownType ->
760-
(* This should not happen because unknown must be the only non-literal case *)
761-
assert false
762-
| Bool _ | Float _ | Int _ | String _ | Null | Undefined -> x in
763753
let mk_eq (i : Ast_untagged_variants.tag_type option) x j y = match i, j with
764-
| Some literal, _ ->
765-
add_runtime_type_check literal x y
766-
| _, Some literal ->
767-
add_runtime_type_check literal y x
768-
| _ -> E.string_equal x y
754+
| Some tag_type, _ ->
755+
Ast_untagged_variants.DynamiChecks.add_runtime_type_check ~tag_type (Expr x) (Expr y) |> E.emit_check
756+
| _, Some tag_type ->
757+
Ast_untagged_variants.DynamiChecks.add_runtime_type_check ~tag_type (Expr y) (Expr x) |> E.emit_check
758+
| _ ->
759+
Ast_untagged_variants.DynamiChecks.(==) (Expr x) (Expr y) |> E.emit_check
769760
in
770761
let is_array (l, _) = l = Ast_untagged_variants.Untagged ArrayType in
771762
let switch ?default ?declaration e clauses =
@@ -780,7 +771,7 @@ and compile_untagged_cases ~cxt ~switch_exp ~default cases =
780771
| _ ->
781772
S.string_switch ?default ?declaration (E.typeof e) clauses in
782773
cases |> compile_general_cases
783-
~make_exp:E.tag_type
774+
~make_exp: E.tag_type
784775
~eq_exp: mk_eq
785776
~cxt
786777
~switch

jscomp/ml/ast_untagged_variants.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,15 @@ module DynamiChecks = struct
327327
e == nil ||| typeof e != object_
328328
else (* (undefiled + other) || other *)
329329
typeof e != object_
330+
331+
let add_runtime_type_check ~tag_type x y = match tag_type with
332+
| Untagged IntType
333+
| Untagged StringType
334+
| Untagged FloatType
335+
| Untagged ObjectType -> typeof y == x
336+
| Untagged ArrayType -> is_array y
337+
| Untagged UnknownType ->
338+
(* This should not happen because unknown must be the only non-literal case *)
339+
assert false
340+
| Bool _ | Float _ | Int _ | String _ | Null | Undefined -> x
330341
end

0 commit comments

Comments
 (0)