Skip to content

Commit 04fd8e0

Browse files
committed
Fallback to Object.hasOwn
1 parent f8963e5 commit 04fd8e0

16 files changed

+71
-26
lines changed

compiler/core/js_exp_make.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,28 @@ let or_ ?comment (e1 : t) (e2 : t) =
11311131
let in_ (prop : t) (obj : t) : t =
11321132
{expression_desc = In (prop, obj); comment = None}
11331133

1134+
let has (obj : t) (prop : t) : t =
1135+
let non_prototype_prop =
1136+
match prop.expression_desc with
1137+
| Str
1138+
{
1139+
txt =
1140+
( "__proto__" | "toString" | "toLocaleString" | "valueOf"
1141+
| "hasOwnProperty" | "isPrototypeOf" | "propertyIsEnumerable" );
1142+
} ->
1143+
false
1144+
(* Optimize to use the in operator when property is a known string which is not a prototype property *)
1145+
| Str _ -> true
1146+
(* We can be sure in this case that the prop is not a prototype property like __proto__ or toString *)
1147+
| _ -> false
1148+
in
1149+
if non_prototype_prop then in_ prop obj
1150+
else
1151+
call
1152+
~info:{arity = Full; call_info = Call_na}
1153+
(js_global "Object.hasOwn")
1154+
[obj; prop]
1155+
11341156
let not (e : t) : t =
11351157
match e.expression_desc with
11361158
| Number (Int {i; _}) -> bool (i = 0l)

compiler/core/js_exp_make.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ val or_ : ?comment:string -> t -> t -> t
358358

359359
val in_ : t -> t -> t
360360

361+
val has : t -> t -> t
362+
361363
(** we don't expose a general interface, since a general interface is generally not safe *)
362364

363365
val dummy_obj : ?comment:string -> Lam_tag_info.t -> t

compiler/core/lam_analysis.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
7878
(* list primitives *)
7979
| Pmakelist
8080
(* dict primitives *)
81-
| Pmakedict | Phasin
81+
| Pmakedict | Phas
8282
(* Test if the argument is a block or an immediate integer *)
8383
| Pisint | Pis_poly_var_block
8484
(* Test if the (integer) argument is outside an interval *)

compiler/core/lam_compile_primitive.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,12 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
574574
Some (Js_op.Lit txt, expr)
575575
| _ -> None))
576576
| _ -> assert false)
577-
| Phasin -> (
577+
| Phas -> (
578578
match args with
579-
| [obj; prop] -> E.in_ prop obj
579+
| [obj; prop] -> E.has obj prop
580580
| _ ->
581581
Location.raise_errorf ~loc
582-
"Invalid external \"%%has_in\" type signature. Expected to have two \
582+
"Invalid external \"%%has\" type signature. Expected to have two \
583583
arguments.")
584584
| Parraysetu -> (
585585
match args with

compiler/core/lam_convert.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
314314
| Parraysets -> prim ~primitive:Parraysets ~args loc
315315
| Pmakelist _mutable_flag (*FIXME*) -> prim ~primitive:Pmakelist ~args loc
316316
| Pmakedict -> prim ~primitive:Pmakedict ~args loc
317-
| Phasin -> prim ~primitive:Phasin ~args loc
317+
| Phas -> prim ~primitive:Phas ~args loc
318318
| Pawait -> prim ~primitive:Pawait ~args loc
319319
| Pimport -> prim ~primitive:Pimport ~args loc
320320
| Pinit_mod -> (

compiler/core/lam_primitive.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ type t =
137137
| Pmakelist
138138
(* dict primitives *)
139139
| Pmakedict
140-
| Phasin
140+
| Phas
141141
(* promise *)
142142
| Pawait
143143
(* etc or deprecated *)
@@ -216,7 +216,7 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
216216
(* List primitives *)
217217
| Pmakelist
218218
(* dict primitives *)
219-
| Pmakedict | Phasin
219+
| Pmakedict | Phas
220220
(* promise *)
221221
| Pawait
222222
(* etc *)

compiler/core/lam_primitive.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ type t =
132132
| Pmakelist
133133
(* dict primitives *)
134134
| Pmakedict
135-
| Phasin
135+
| Phas
136136
(* promise *)
137137
| Pawait
138138
(* etc or deprecated *)

compiler/core/lam_print.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ let primitive ppf (prim : Lam_primitive.t) =
194194
| Pmakearray -> fprintf ppf "makearray"
195195
| Pmakelist -> fprintf ppf "makelist"
196196
| Pmakedict -> fprintf ppf "makedict"
197-
| Phasin -> fprintf ppf "has_in"
197+
| Phas -> fprintf ppf "has_in"
198198
| Parrayrefu -> fprintf ppf "array.unsafe_get"
199199
| Parraysetu -> fprintf ppf "array.unsafe_set"
200200
| Parrayrefs -> fprintf ppf "array.get"

compiler/ml/lambda.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ type primitive =
271271
| Pmakelist of Asttypes.mutable_flag
272272
(* dict primitives *)
273273
| Pmakedict
274-
| Phasin
274+
| Phas
275275
(* promise *)
276276
| Pawait
277277
(* module *)

compiler/ml/lambda.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ type primitive =
238238
| Pmakelist of Asttypes.mutable_flag
239239
(* dict primitives *)
240240
| Pmakedict
241-
| Phasin
241+
| Phas
242242
(* promise *)
243243
| Pawait
244244
(* modules *)

0 commit comments

Comments
 (0)