Skip to content

Commit b248877

Browse files
committed
Untagged variants: use Array.isArray
See #6103 (comment)
1 parent 13a3f63 commit b248877

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

jscomp/core/js_exp_make.ml

+4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ let typeof ?comment (e : t) : t =
176176
let instanceof ?comment (e0 : t) (e1: t) : t =
177177
{ expression_desc = Bin (InstanceOf, e0, e1); comment }
178178

179+
let is_array (e0 : t) : t =
180+
let f = str "Array.isArray" ~delim:DNoQuotes in
181+
{ expression_desc = Call (f, [e0], Js_call_info.ml_full_call); comment=None }
182+
179183
let new_ ?comment e0 args : t =
180184
{ expression_desc = New (e0, Some args); comment }
181185

jscomp/core/js_exp_make.mli

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ val is_type_object : t -> t
212212

213213
val typeof : ?comment:string -> t -> t
214214
val instanceof : ?comment:string -> t -> t -> t
215+
val is_array : t -> t
215216

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

jscomp/core/lam_compile.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ and compile_untagged_cases cxt switch_exp table default =
749749
| Block StringType
750750
| Block FloatType
751751
| Block Object -> E.string_equal (E.typeof y) x
752-
| Block Array -> E.instanceof y x
752+
| Block Array -> E.is_array y
753753
| Block Unknown ->
754754
(* This should not happen because unknown must be the only non-literal case *)
755755
assert false
@@ -767,7 +767,7 @@ and compile_untagged_cases cxt switch_exp table default =
767767
match array_clauses with
768768
| [(l, {J.switch_body})] when List.length clauses > 1 ->
769769
let rest = Ext_list.filter clauses (fun c -> not (is_array c)) in
770-
S.if_ (E.instanceof e (E.literal l))
770+
S.if_ (E.is_array e)
771771
(switch_body)
772772
~else_:([S.string_switch ?default ?declaration (E.typeof e) rest])
773773
| _ :: _ :: _ -> assert false (* at most 1 array case *)

jscomp/test/UntaggedVariants.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ var OnlyBlocks = {
154154
};
155155

156156
function classify$5(x) {
157-
if (x instanceof Array) {
157+
if (Array.isArray(x)) {
158158
return "array";
159159
}
160160
switch (typeof x) {
@@ -184,7 +184,7 @@ function classify$6(x) {
184184

185185
}
186186
} else {
187-
if (x instanceof Array) {
187+
if (Array.isArray(x)) {
188188
return {
189189
TAG: "JSONArray",
190190
_0: x

0 commit comments

Comments
 (0)