File tree Expand file tree Collapse file tree 3 files changed +20
-16
lines changed Expand file tree Collapse file tree 3 files changed +20
-16
lines changed Original file line number Diff line number Diff line change @@ -188,6 +188,8 @@ val assign : ?comment:string -> t -> t -> t
188
188
189
189
val tag_type : Ast_untagged_variants .tag_type -> t
190
190
191
+ val emit_check : t Ast_untagged_variants.DynamiChecks .t -> t
192
+
191
193
val triple_equal : ?comment : string -> t -> t -> t
192
194
(* TODO: reduce [triple_equal] use *)
193
195
Original file line number Diff line number Diff line change @@ -750,22 +750,13 @@ and compile_string_cases ~cxt ~switch_exp ~default cases: initialization =
750
750
~switch_exp
751
751
~default
752
752
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
763
753
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
769
760
in
770
761
let is_array (l , _ ) = l = Ast_untagged_variants. Untagged ArrayType in
771
762
let switch ?default ?declaration e clauses =
@@ -780,7 +771,7 @@ and compile_untagged_cases ~cxt ~switch_exp ~default cases =
780
771
| _ ->
781
772
S. string_switch ?default ?declaration (E. typeof e) clauses in
782
773
cases |> compile_general_cases
783
- ~make_exp: E. tag_type
774
+ ~make_exp: E. tag_type
784
775
~eq_exp: mk_eq
785
776
~cxt
786
777
~switch
Original file line number Diff line number Diff line change @@ -327,4 +327,15 @@ module DynamiChecks = struct
327
327
e == nil ||| typeof e != object_
328
328
else (* (undefiled + other) || other *)
329
329
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
330
341
end
You can’t perform that action at this time.
0 commit comments