Skip to content

Untagged variants: use Array.isArray #6121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ let typeof ?comment (e : t) : t =
let instanceof ?comment (e0 : t) (e1: t) : t =
{ expression_desc = Bin (InstanceOf, e0, e1); comment }

let is_array (e0 : t) : t =
let f = str "Array.isArray" ~delim:DNoQuotes in
{ expression_desc = Call (f, [e0], Js_call_info.ml_full_call); comment=None }

let new_ ?comment e0 args : t =
{ expression_desc = New (e0, Some args); comment }

Expand Down
1 change: 1 addition & 0 deletions jscomp/core/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ val is_type_object : t -> t

val typeof : ?comment:string -> t -> t
val instanceof : ?comment:string -> t -> t -> t
val is_array : t -> t

val to_int32 : ?comment:string -> t -> t

Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ and compile_untagged_cases cxt switch_exp table default =
| Block StringType
| Block FloatType
| Block Object -> E.string_equal (E.typeof y) x
| Block Array -> E.instanceof y x
| Block Array -> E.is_array y
| Block Unknown ->
(* This should not happen because unknown must be the only non-literal case *)
assert false
Expand All @@ -767,7 +767,7 @@ and compile_untagged_cases cxt switch_exp table default =
match array_clauses with
| [(l, {J.switch_body})] when List.length clauses > 1 ->
let rest = Ext_list.filter clauses (fun c -> not (is_array c)) in
S.if_ (E.instanceof e (E.literal l))
S.if_ (E.is_array e)
(switch_body)
~else_:([S.string_switch ?default ?declaration (E.typeof e) rest])
| _ :: _ :: _ -> assert false (* at most 1 array case *)
Expand Down
4 changes: 2 additions & 2 deletions jscomp/test/UntaggedVariants.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ var OnlyBlocks = {
};

function classify$5(x) {
if (x instanceof Array) {
if (Array.isArray(x)) {
return "array";
}
switch (typeof x) {
Expand Down Expand Up @@ -184,7 +184,7 @@ function classify$6(x) {

}
} else {
if (x instanceof Array) {
if (Array.isArray(x)) {
return {
TAG: "JSONArray",
_0: x
Expand Down