Skip to content
Merged
Prev Previous commit
Next Next commit
change approach to use builtin primitive for creating dicts
  • Loading branch information
zth committed Sep 3, 2024
commit ed5995ea64a2ab997cb7baa02f4deb9acadc2ae0
11 changes: 11 additions & 0 deletions jscomp/core/lam_dispatch_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
match args with
| [e] -> {e with expression_desc = Await e}
| _ -> assert false)
| "?create_dict" -> (
match args with
| [{expression_desc = Array (items, _)}] ->
E.obj
(items
|> List.filter_map (fun (exp : J.expression) ->
match exp.expression_desc with
| Caml_block ([{expression_desc = Str {txt}}; expr], _, _, _) ->
Some (Js_op.Lit txt, expr)
| _ -> None))
| _ -> assert false)
| missing_impl ->
let msg = Warnings.message (Bs_unimplemented_primitive missing_impl) in
Location.raise_errorf ~loc "%s" msg
2 changes: 2 additions & 0 deletions jscomp/others/js_dict.res
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,5 @@ let map = (f, source) => {
}
target
}

external unsafe_create: array<(string, 'a)> => dict<'a> = "?create_dict"
2 changes: 2 additions & 0 deletions jscomp/others/js_dict.resi
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,5 @@ salePrices == Js.Dict.fromList(list{("pen", 0.90), ("book", 4.50), ("stapler", 6
```
*/
let map: ('a => 'b, t<'a>) => t<'b>

external unsafe_create: array<(string, 'a)> => dict<'a> = "?create_dict"
2 changes: 1 addition & 1 deletion jscomp/syntax/src/res_comments_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ and walk_expression expr t comments =
{
txt =
Longident.Ldot
(Longident.Ldot (Lident "Js", "Dict"), "fromArray");
(Longident.Ldot (Lident "Js", "Dict"), "unsafe_create");
};
},
[(Nolabel, key_values)] )
Expand Down
2 changes: 1 addition & 1 deletion jscomp/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3947,7 +3947,7 @@ and parse_dict_expr ~start_pos p =
(Ast_helper.Exp.ident ~loc
(Location.mkloc
(Longident.Ldot
(Longident.Ldot (Longident.Lident "Js", "Dict"), "fromArray"))
(Longident.Ldot (Longident.Lident "Js", "Dict"), "unsafe_create"))
loc))
[(Asttypes.Nolabel, Ast_helper.Exp.array ~loc key_value_pairs)]

Expand Down
2 changes: 1 addition & 1 deletion jscomp/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4081,7 +4081,7 @@ and print_pexp_apply ~state expr cmt_tbl =
{
txt =
Longident.Ldot
(Longident.Ldot (Lident "Js", "Dict"), "fromArray");
(Longident.Ldot (Lident "Js", "Dict"), "unsafe_create");
};
},
[(Nolabel, key_values)] )
Expand Down
28 changes: 7 additions & 21 deletions jscomp/test/DictTests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 4 additions & 17 deletions jscomp/test/DictTests.res
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
let dictCreationCanBeInlined = dict{
let someString = "hello"

let createdDict = dict{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add a Dict<int> for testing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, adding.

"name": "hello",
"age": "what",
"more": "stuff",
"otherStr": someString,
}

external imaginaryExternalArgs: array<(string, string)> = "imaginary"

let dictCreationCanNotBeInlined = Js.Dict.fromArray(imaginaryExternalArgs)

open Js

let dictCreationCanBeInlined3 = Dict.fromArray([
("name", "hello"),
("age", "what"),
("more", "stuff"),
])

open Dict

let dictCreationCanBeInlined4 = fromArray([("name", "hello"), ("age", "what"), ("more", "stuff")])