Skip to content

Commit c4ba486

Browse files
authored
Improve output of Dict.has to use inlined JS' in operator (#7342)
1 parent f37ff99 commit c4ba486

29 files changed

+239
-166
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
- Remove `Stdlib_Char` module for now. https://github.com/rescript-lang/rescript/pull/7367
2626
- Convert internal JavaScript codebase into ESM, ReScript package itself is now ESM (`"type": "module"`). https://github.com/rescript-lang/rescript/pull/6899
27+
- Add built-in support for the JavaScript `in` operator. https://github.com/rescript-lang/rescript/pull/7342
2728

2829
#### :nail_care: Polish
2930

compiler/core/j.ml

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ and expression_desc =
8686
[typeof] is an operator
8787
*)
8888
| Typeof of expression
89+
| In of expression * expression (* prop in obj *)
8990
| Js_not of expression (* !v *)
9091
(* TODO: Add some primitives so that [js inliner] can do a better job *)
9192
| Seq of expression * expression

compiler/core/js_analyzer.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
111111
&& Ext_list.for_all strings no_side_effect
112112
&& Ext_list.for_all values no_side_effect
113113
| Js_not e -> no_side_effect e
114+
| In (prop, obj) -> no_side_effect prop && no_side_effect obj
114115
| Cond (a, b, c) -> no_side_effect a && no_side_effect b && no_side_effect c
115116
| Call ({expression_desc = Str {txt = "Array.isArray"}}, [e], _) ->
116117
no_side_effect e
@@ -227,7 +228,7 @@ let rec eq_expression ({expression_desc = x0} : J.expression)
227228
eq_expression_list ls0 ls1 && flag0 = flag1 && eq_expression tag0 tag1
228229
| _ -> false)
229230
| Length _ | Is_null_or_undefined _ | String_append _ | Typeof _ | Js_not _
230-
| Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _ | Array _
231+
| In _ | Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _ | Array _
231232
| Caml_block_tag _ | Object _ | Tagged_template _ | Await _ ->
232233
false
233234
| Spread _ -> false

compiler/core/js_dump.ml

+7-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ let rec exp_need_paren ?(arrow = false) (e : J.expression) =
166166
| Length _ | Call _ | Caml_block_tag _ | Seq _ | Static_index _ | Cond _
167167
| Bin _ | Is_null_or_undefined _ | String_index _ | Array_index _
168168
| String_append _ | Var _ | Undefined _ | Null | Str _ | Array _
169-
| Caml_block _ | FlatCall _ | Typeof _ | Number _ | Js_not _ | Bool _ | New _
170-
->
169+
| Caml_block _ | FlatCall _ | Typeof _ | Number _ | Js_not _ | In _ | Bool _
170+
| New _ ->
171171
false
172172
| Await _ -> false
173173
| Spread _ -> false
@@ -677,6 +677,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
677677
P.cond_paren_group f (level > 13) (fun _ ->
678678
P.string f "!";
679679
expression ~level:13 cxt f e)
680+
| In (prop, obj) ->
681+
P.cond_paren_group f (level > 12) (fun _ ->
682+
let cxt = expression ~level:0 cxt f prop in
683+
P.string f " in ";
684+
expression ~level:0 cxt f obj)
680685
| Typeof e ->
681686
P.string f "typeof";
682687
P.space f;

compiler/core/js_exp_make.ml

+3
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,9 @@ let or_ ?comment (e1 : t) (e2 : t) =
11281128
| Some e -> e
11291129
| None -> {expression_desc = Bin (Or, e1, e2); comment})
11301130

1131+
let in_ (prop : t) (obj : t) : t =
1132+
{expression_desc = In (prop, obj); comment = None}
1133+
11311134
let not (e : t) : t =
11321135
match e.expression_desc with
11331136
| Number (Int {i; _}) -> bool (i = 0l)

compiler/core/js_exp_make.mli

+2
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ val and_ : ?comment:string -> t -> t -> t
356356

357357
val or_ : ?comment:string -> t -> t -> t
358358

359+
val in_ : t -> t -> t
360+
359361
(** we don't expose a general interface, since a general interface is generally not safe *)
360362

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

compiler/core/js_fold.ml

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class fold =
103103
| Js_not _x0 ->
104104
let _self = _self#expression _x0 in
105105
_self
106+
| In (_x0, _x1) ->
107+
let _self = _self#expression _x0 in
108+
let _self = _self#expression _x1 in
109+
_self
106110
| Seq (_x0, _x1) ->
107111
let _self = _self#expression _x0 in
108112
let _self = _self#expression _x1 in

compiler/core/js_record_fold.ml

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ let expression_desc : 'a. ('a, expression_desc) fn =
109109
| Js_not _x0 ->
110110
let st = _self.expression _self st _x0 in
111111
st
112+
| In (_x0, _x1) ->
113+
let st = _self.expression _self st _x0 in
114+
let st = _self.expression _self st _x1 in
115+
st
112116
| Seq (_x0, _x1) ->
113117
let st = _self.expression _self st _x0 in
114118
let st = _self.expression _self st _x1 in

compiler/core/js_record_iter.ml

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ let expression_desc : expression_desc fn =
9191
| Bool _ -> ()
9292
| Typeof _x0 -> _self.expression _self _x0
9393
| Js_not _x0 -> _self.expression _self _x0
94+
| In (_x0, _x1) ->
95+
_self.expression _self _x0;
96+
_self.expression _self _x1
9497
| Seq (_x0, _x1) ->
9598
_self.expression _self _x0;
9699
_self.expression _self _x1

compiler/core/js_record_map.ml

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ let expression_desc : expression_desc fn =
109109
| Js_not _x0 ->
110110
let _x0 = _self.expression _self _x0 in
111111
Js_not _x0
112+
| In (_x0, _x1) ->
113+
let _x0 = _self.expression _self _x0 in
114+
let _x1 = _self.expression _self _x1 in
115+
In (_x0, _x1)
112116
| Seq (_x0, _x1) ->
113117
let _x0 = _self.expression _self _x0 in
114118
let _x1 = _self.expression _self _x1 in

compiler/core/lam_analysis.ml

+1-1
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
81+
| Pmakedict | Pdict_has
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

+7
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,13 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
574574
Some (Js_op.Lit txt, expr)
575575
| _ -> None))
576576
| _ -> assert false)
577+
| Pdict_has -> (
578+
match args with
579+
| [obj; prop] -> E.in_ prop obj
580+
| _ ->
581+
Location.raise_errorf ~loc
582+
"Invalid external \"%%dict_has\" type signature. Expected to have two \
583+
arguments.")
577584
| Parraysetu -> (
578585
match args with
579586
(* wrong*)

compiler/core/lam_convert.ml

+1
Original file line numberDiff line numberDiff line change
@@ -314,6 +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+
| Pdict_has -> prim ~primitive:Pdict_has ~args loc
317318
| Pawait -> prim ~primitive:Pawait ~args loc
318319
| Pimport -> prim ~primitive:Pimport ~args loc
319320
| Pinit_mod -> (

compiler/core/lam_primitive.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ type t =
137137
| Pmakelist
138138
(* dict primitives *)
139139
| Pmakedict
140+
| Pdict_has
140141
(* promise *)
141142
| Pawait
142143
(* etc or deprecated *)
@@ -215,7 +216,7 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
215216
(* List primitives *)
216217
| Pmakelist
217218
(* dict primitives *)
218-
| Pmakedict
219+
| Pmakedict | Pdict_has
219220
(* promise *)
220221
| Pawait
221222
(* etc *)

compiler/core/lam_primitive.mli

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type t =
132132
| Pmakelist
133133
(* dict primitives *)
134134
| Pmakedict
135+
| Pdict_has
135136
(* promise *)
136137
| Pawait
137138
(* etc or deprecated *)

compiler/core/lam_print.ml

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +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+
| Pdict_has -> fprintf ppf "dict.has"
197198
| Parrayrefu -> fprintf ppf "array.unsafe_get"
198199
| Parraysetu -> fprintf ppf "array.unsafe_set"
199200
| Parrayrefs -> fprintf ppf "array.get"

compiler/ml/lambda.ml

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ type primitive =
271271
| Pmakelist of Asttypes.mutable_flag
272272
(* dict primitives *)
273273
| Pmakedict
274+
| Pdict_has
274275
(* promise *)
275276
| Pawait
276277
(* module *)

compiler/ml/lambda.mli

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ type primitive =
238238
| Pmakelist of Asttypes.mutable_flag
239239
(* dict primitives *)
240240
| Pmakedict
241+
| Pdict_has
241242
(* promise *)
242243
| Pawait
243244
(* modules *)

compiler/ml/printlambda.ml

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +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+
| Pdict_has -> fprintf ppf "dict.has"
232233
| Pisint -> fprintf ppf "isint"
233234
| Pisout -> fprintf ppf "isout"
234235
| Pisnullable -> fprintf ppf "isnullable"

compiler/ml/translcore.ml

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ let primitives_table =
346346
("%array_unsafe_set", Parraysetu);
347347
(* dict primitives *)
348348
("%makedict", Pmakedict);
349+
("%dict_has", Pdict_has);
349350
(* promise *)
350351
("%await", Pawait);
351352
(* module *)

lib/es6/Stdlib_Dict.js

-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ function mapValues(dict, f) {
2222
return target;
2323
}
2424

25-
let has = ((dict, key) => key in dict);
26-
2725
export {
2826
$$delete$1 as $$delete,
2927
forEach,
3028
forEachWithKey,
3129
mapValues,
32-
has,
3330
}
3431
/* No side effect */

lib/js/Stdlib_Dict.js

-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ function mapValues(dict, f) {
2222
return target;
2323
}
2424

25-
let has = ((dict, key) => key in dict);
26-
2725
exports.$$delete = $$delete$1;
2826
exports.forEach = forEach;
2927
exports.forEachWithKey = forEachWithKey;
3028
exports.mapValues = mapValues;
31-
exports.has = has;
3229
/* No side effect */

runtime/Stdlib_Dict.res

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

44-
let has: (dict<'a>, string) => bool = %raw(`(dict, key) => key in dict`)
44+
external has: (dict<'a>, string) => bool = "%dict_has"
4545

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

runtime/Stdlib_Dict.resi

+7-4
Original file line numberDiff line numberDiff line change
@@ -246,17 +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+
Be aware that it uses the JavaScript `in` operator under the hood.
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
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(true)
257260
```
258261
*/
259-
let has: (dict<'a>, string) => bool
262+
external has: (dict<'a>, string) => bool = "%dict_has"
260263

261264
/**
262265
`ignore(dict)` ignores the provided dict and returns unit.

tests/tests/src/DictTests.mjs

-100
This file was deleted.

0 commit comments

Comments
 (0)