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 *)

compiler/ml/printlambda.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ let primitive ppf = function
229229
| Pmakelist Mutable -> fprintf ppf "makelist"
230230
| Pmakelist Immutable -> fprintf ppf "makelist_imm"
231231
| Pmakedict -> fprintf ppf "makedict"
232-
| Phasin -> fprintf ppf "has_in"
232+
| Phas -> fprintf ppf "has_in"
233233
| Pisint -> fprintf ppf "isint"
234234
| Pisout -> fprintf ppf "isout"
235235
| Pisnullable -> fprintf ppf "isnullable"

compiler/ml/translcore.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ let primitives_table =
346346
("%array_unsafe_set", Parraysetu);
347347
(* dict primitives *)
348348
("%makedict", Pmakedict);
349-
("%has_in", Phasin);
349+
("%has", Phas);
350350
(* promise *)
351351
("%await", Pawait);
352352
(* module *)

runtime/Stdlib_Dict.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ let mapValues = (dict, f) => {
4141
target
4242
}
4343

44-
external has: (dict<'a>, string) => bool = "%has_in"
44+
external has: (dict<'a>, string) => bool = "%has"
4545

4646
external ignore: dict<'a> => unit = "%ignore"

runtime/Stdlib_Dict.resi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,20 @@ let mapValues: (dict<'a>, 'a => 'b) => dict<'b>
246246
/**
247247
`has(dictionary, "key")` returns true if the "key" is present in the dictionary.
248248
249+
It uses the `in` operator under the hood and falls back to the `Object.hasOwn` method when safety cannot be guaranteed at compile time.
250+
249251
## Examples
250252
251253
```rescript
252254
let dict = dict{"key1": Some(1), "key2": None}
253255
254-
dict->Dict.has("key1") // true
255-
dict->Dict.has("key2") // true
256-
dict->Dict.has("key3") // false
257-
dict->Dict.has("__proto__") // true, since it uses in operator under the hood
256+
dict->Dict.has("key1")->assertEqual(true)
257+
dict->Dict.has("key2")->assertEqual(true)
258+
dict->Dict.has("key3")->assertEqual(false)
259+
dict->Dict.has("toString")->assertEqual(false)
258260
```
259261
*/
260-
external has: (dict<'a>, string) => bool = "%has_in"
262+
external has: (dict<'a>, string) => bool = "%has"
261263

262264
/**
263265
`ignore(dict)` ignores the provided dict and returns unit.

tests/tests/src/DictTests.mjs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ if (!("key1" in dict)) {
5151
RE_EXN_ID: "Assert_failure",
5252
_1: [
5353
"DictTests.res",
54-
43,
54+
44,
5555
2
5656
],
5757
Error: new Error()
@@ -63,7 +63,7 @@ if (!("key2" in dict)) {
6363
RE_EXN_ID: "Assert_failure",
6464
_1: [
6565
"DictTests.res",
66-
44,
66+
46,
6767
2
6868
],
6969
Error: new Error()
@@ -75,19 +75,31 @@ if ("key3" in dict !== false) {
7575
RE_EXN_ID: "Assert_failure",
7676
_1: [
7777
"DictTests.res",
78-
45,
78+
48,
7979
2
8080
],
8181
Error: new Error()
8282
};
8383
}
8484

85-
if (!("__proto__" in dict)) {
85+
if (Object.hasOwn(dict, "toString") !== false) {
8686
throw {
8787
RE_EXN_ID: "Assert_failure",
8888
_1: [
8989
"DictTests.res",
90-
46,
90+
50,
91+
2
92+
],
93+
Error: new Error()
94+
};
95+
}
96+
97+
if (!Object.hasOwn(dict, "key1")) {
98+
throw {
99+
RE_EXN_ID: "Assert_failure",
100+
_1: [
101+
"DictTests.res",
102+
52,
91103
2
92104
],
93105
Error: new Error()
@@ -99,7 +111,7 @@ if (typeof ("key1" in dict) !== "boolean") {
99111
RE_EXN_ID: "Assert_failure",
100112
_1: [
101113
"DictTests.res",
102-
47,
114+
54,
103115
2
104116
],
105117
Error: new Error()

tests/tests/src/DictTests.res

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ module DictHas = {
4040
"key2": None,
4141
}
4242

43+
// Test success path
4344
assert(dict->Dict.has("key1"))
45+
// Test undefined field
4446
assert(dict->Dict.has("key2"))
47+
// Test missing field
4548
assert(dict->Dict.has("key3") === false)
46-
assert(dict->Dict.has("__proto__"))
49+
// Test prototype field
50+
assert(dict->Dict.has("toString") === false)
51+
// Test without compile time knowledge
52+
assert(dict->Dict.has(%raw(`"key1"`)))
53+
// Test parantesis in generated code
4754
assert(typeof(dict->Dict.has("key1")) === #boolean)
4855
}

0 commit comments

Comments
 (0)