Skip to content

Commit 012f9c2

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

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

jscomp/core/js_exp_make.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ let tag_type = function
776776
(* TODO: this should not happen *)
777777
assert false
778778

779-
let rec emit_check (check : t Ast_untagged_variants.DynamiChecks.t) = match check with
779+
let rec emit_check (check : t Ast_untagged_variants.DynamicChecks.t) = match check with
780780
| TagType t -> tag_type t
781781
| BinOp(op, x, y) ->
782782
let op = match op with
@@ -792,11 +792,11 @@ let rec emit_check (check : t Ast_untagged_variants.DynamiChecks.t) = match chec
792792
| Expr x -> x
793793

794794
let is_a_literal_case ~literal_cases ~block_cases (e:t) =
795-
let check = Ast_untagged_variants.DynamiChecks.is_a_literal_case ~literal_cases ~block_cases (Expr e) in
795+
let check = Ast_untagged_variants.DynamicChecks.is_a_literal_case ~literal_cases ~block_cases (Expr e) in
796796
emit_check check
797797

798798
let is_int_tag ?has_null_undefined_other e =
799-
let check = Ast_untagged_variants.DynamiChecks.is_int_tag ?has_null_undefined_other (Expr e) in
799+
let check = Ast_untagged_variants.DynamicChecks.is_int_tag ?has_null_undefined_other (Expr e) in
800800
emit_check check
801801

802802
let is_type_string ?comment (e : t) : t =

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.DynamicChecks.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: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -750,22 +750,16 @@ 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
763-
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
753+
let mk_eq (i : Ast_untagged_variants.tag_type option) x j y =
754+
let check = match i, j with
755+
| Some tag_type, _ ->
756+
Ast_untagged_variants.DynamicChecks.add_runtime_type_check ~tag_type (Expr x) (Expr y)
757+
| _, Some tag_type ->
758+
Ast_untagged_variants.DynamicChecks.add_runtime_type_check ~tag_type (Expr y) (Expr x)
759+
| _ ->
760+
Ast_untagged_variants.DynamicChecks.(==) (Expr x) (Expr y)
761+
in
762+
E.emit_check check
769763
in
770764
let is_array (l, _) = l = Ast_untagged_variants.Untagged ArrayType in
771765
let switch ?default ?declaration e clauses =
@@ -780,7 +774,7 @@ and compile_untagged_cases ~cxt ~switch_exp ~default cases =
780774
| _ ->
781775
S.string_switch ?default ?declaration (E.typeof e) clauses in
782776
cases |> compile_general_cases
783-
~make_exp:E.tag_type
777+
~make_exp: E.tag_type
784778
~eq_exp: mk_eq
785779
~cxt
786780
~switch

jscomp/ml/ast_untagged_variants.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ let names_from_type_variant ?(isUntaggedDef=false) ~env (cstrs : Types.construct
242242
let check_well_formed ~env ~isUntaggedDef (cstrs: Types.constructor_declaration list) =
243243
ignore (names_from_type_variant ~env ~isUntaggedDef cstrs)
244244

245-
module DynamiChecks = struct
245+
module DynamicChecks = struct
246246

247247
type op = EqEqEq | NotEqEq | Or | And
248248
type 'a t = BinOp of op * 'a t * 'a t | TagType of tag_type | TypeOf of 'a t | IsArray of 'a t | Not of 'a t | Expr of 'a
@@ -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)