Skip to content

Commit c3f18d7

Browse files
committed
Fallback to Object.hasOwn
1 parent 635ea1c commit c3f18d7

17 files changed

+72
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
#### :rocket: New Feature
1616

1717
- Add `Dict.has` and double `Dict.forEachWithKey`/`Dict.mapValues` performance. https://github.com/rescript-lang/rescript/pull/7316
18+
- Make `Dict.has` inline `in` operator check and fallback to `Object.hasOwn` to guarantee safety. https://github.com/rescript-lang/rescript/pull/7342
1819
- Add popover attributes to JsxDOM.domProps. https://github.com/rescript-lang/rescript/pull/7317
1920
- Add `inert` attribute to `JsxDOM.domProps`. https://github.com/rescript-lang/rescript/pull/7326
2021
- Make reanalyze exception tracking work with the new stdlib. https://github.com/rescript-lang/rescript/pull/7328
2122
- Fix Pervasive.max using boolean comparison for floats. https://github.com/rescript-lang/rescript/pull/7333
22-
- Add built-in support for the JavaScript in operator. https://github.com/rescript-lang/rescript/pull/7342
2323

2424
#### :boom: Breaking Change
2525

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
@@ -354,6 +354,8 @@ val or_ : ?comment:string -> t -> t -> t
354354

355355
val in_ : t -> t -> t
356356

357+
val has : t -> t -> t
358+
357359
(** we don't expose a general interface, since a general interface is generally not safe *)
358360

359361
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
@@ -77,7 +77,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
7777
(* list primitives *)
7878
| Pmakelist
7979
(* dict primitives *)
80-
| Pmakedict | Phasin
80+
| Pmakedict | Phas
8181
(* Test if the argument is a block or an immediate integer *)
8282
| Pisint | Pis_poly_var_block
8383
(* 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
@@ -566,12 +566,12 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
566566
Some (Js_op.Lit txt, expr)
567567
| _ -> None))
568568
| _ -> assert false)
569-
| Phasin -> (
569+
| Phas -> (
570570
match args with
571-
| [obj; prop] -> E.in_ prop obj
571+
| [obj; prop] -> E.has obj prop
572572
| _ ->
573573
Location.raise_errorf ~loc
574-
"Invalid external \"%%has_in\" type signature. Expected to have two \
574+
"Invalid external \"%%has\" type signature. Expected to have two \
575575
arguments.")
576576
| Parraysetu -> (
577577
match args with

compiler/core/lam_convert.ml

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

compiler/core/lam_primitive.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type t =
135135
| Pmakelist
136136
(* dict primitives *)
137137
| Pmakedict
138-
| Phasin
138+
| Phas
139139
(* promise *)
140140
| Pawait
141141
(* etc or deprecated *)
@@ -214,7 +214,7 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
214214
(* List primitives *)
215215
| Pmakelist
216216
(* dict primitives *)
217-
| Pmakedict | Phasin
217+
| Pmakedict | Phas
218218
(* promise *)
219219
| Pawait
220220
(* etc *)

compiler/core/lam_primitive.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type t =
130130
| Pmakelist
131131
(* dict primitives *)
132132
| Pmakedict
133-
| Phasin
133+
| Phas
134134
(* promise *)
135135
| Pawait
136136
(* etc or deprecated *)

compiler/core/lam_print.ml

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

compiler/ml/lambda.ml

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

0 commit comments

Comments
 (0)