diff --git a/jscomp/core/js_exp_make.ml b/jscomp/core/js_exp_make.ml index bf9fd5b272..bc137a85c2 100644 --- a/jscomp/core/js_exp_make.ml +++ b/jscomp/core/js_exp_make.ml @@ -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 } diff --git a/jscomp/core/js_exp_make.mli b/jscomp/core/js_exp_make.mli index d2590e422b..5fcf6b4039 100644 --- a/jscomp/core/js_exp_make.mli +++ b/jscomp/core/js_exp_make.mli @@ -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 diff --git a/jscomp/core/lam_compile.ml b/jscomp/core/lam_compile.ml index 24943156c0..d4c320ce59 100644 --- a/jscomp/core/lam_compile.ml +++ b/jscomp/core/lam_compile.ml @@ -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 @@ -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 *) diff --git a/jscomp/test/UntaggedVariants.js b/jscomp/test/UntaggedVariants.js index 79b0d8c375..27283b73b8 100644 --- a/jscomp/test/UntaggedVariants.js +++ b/jscomp/test/UntaggedVariants.js @@ -154,7 +154,7 @@ var OnlyBlocks = { }; function classify$5(x) { - if (x instanceof Array) { + if (Array.isArray(x)) { return "array"; } switch (typeof x) { @@ -184,7 +184,7 @@ function classify$6(x) { } } else { - if (x instanceof Array) { + if (Array.isArray(x)) { return { TAG: "JSONArray", _0: x