Skip to content

Commit

Permalink
feat: respect Sys.opaque_identity
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Jan 4, 2025
1 parent 61a6ed1 commit bd161cf
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 11 deletions.
2 changes: 2 additions & 0 deletions jscomp/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ let rec no_side_effects (lam : Lam.t) : bool =
(* byte swap *)
| Pbswap16 | Pbbswap _ | Parraysetu | Poffsetref _ | Praise | Plazyforce
| Psetfield _ | Psetfield_computed ->
false
| Popaque ->
false)
| Llet (_, _, arg, body) | Lmutlet (_, arg, body) ->
no_side_effects arg && no_side_effects body
Expand Down
25 changes: 21 additions & 4 deletions jscomp/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1626,9 +1626,17 @@ and compile_prim (prim_info : Lam.prim_info)
[
Lprim
{
primitive =
Pjs_unsafe_downgrade { name = property; setter = true; _ };
args = [ obj ];
primitive = Popaque;
args =
[
Lprim
{
primitive =
Pjs_unsafe_downgrade { name = property; setter = true; _ };
args = [ obj ];
_;
};
];
_;
};
setter_val;
Expand All @@ -1655,7 +1663,16 @@ and compile_prim (prim_info : Lam.prim_info)
| {
primitive = Pfull_apply;
args =
Lprim { primitive = Pjs_unsafe_downgrade { setter = true; _ }; _ } :: _;
Lprim
{
primitive = Popaque;
args =
[
Lprim { primitive = Pjs_unsafe_downgrade { setter = true; _ }; _ };
];
_;
}
:: _;
_;
} ->
assert false
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ let translate loc (cxt : Lam_compile_context.t) (prim : Lam_primitive.t)
| Pbbswap Pint32 ->
E.runtime_call ~module_name:Js_runtime_modules.bytes ~fn_name:"bswap32"
args
| Popaque -> List.hd args
| Pbbswap Pint64 ->
E.runtime_call ~module_name:Js_runtime_modules.bytes ~fn_name:"bswap64"
args
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/lam_convert.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
| Pnativeint | Pint32 -> Lam.prim ~primitive:(Pintcomp b) ~args loc
| Pint64 -> Lam.prim ~primitive:(Pint64comp b) ~args loc)
| Pfield_computed -> Lam.prim ~primitive:Pfield_computed ~args loc
| Popaque -> List.hd args
| Popaque -> Lam.prim ~primitive:Popaque ~args loc
| Psetfield_computed _ -> Lam.prim ~primitive:Psetfield_computed ~args loc
| Pbbswap i -> Lam.prim ~primitive:(Pbbswap i) ~args loc
| Pbswap16 -> Lam.prim ~primitive:Pbswap16 ~args loc
Expand Down
4 changes: 4 additions & 0 deletions jscomp/core/lam_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ type t =
Lam_compat.compile_time_constant (* Integer to external pointer *)
| Pbswap16
| Pbbswap of Lam_compat.boxed_integer
(* Inhibition of optimisation *)
| Popaque
(* JS specific *)
| Pdebugger
| Pjs_unsafe_downgrade of { name : string; setter : bool; loc : Location.t }
| Pinit_mod
Expand Down Expand Up @@ -372,6 +375,7 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
| _ -> false)
| Pbswap16 -> rhs = Pbswap16
| Pbbswap i1 -> ( match rhs with Pbbswap i2 -> i1 = i2 | _ -> false)
| Popaque -> ( match rhs with Popaque -> true | _ -> false)
| Pjs_unsafe_downgrade { name; loc = _; setter } -> (
match rhs with
| Pjs_unsafe_downgrade rhs -> name = rhs.name && setter = rhs.setter
Expand Down
2 changes: 2 additions & 0 deletions jscomp/core/lam_primitive.mli
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ type t =
| Pctconst of Lam_compat.compile_time_constant
| Pbswap16
| Pbbswap of Lam_compat.boxed_integer
(* Inhibition of optimisation *)
| Popaque
(* Integer to external pointer *)
| Pdebugger
| Pjs_unsafe_downgrade of { name : string; setter : bool; loc : Location.t }
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ let primitive ppf (prim : Lam_primitive.t) =
| Pbbswap Pnativeint -> fprintf ppf "bswap_nativeint"
| Pbbswap Pint32 -> fprintf ppf "bswap32"
| Pbbswap Pint64 -> fprintf ppf "bswap64"
| Popaque -> fprintf ppf "opaque"
| Pisint -> fprintf ppf "isint"
| Pis_poly_var_const -> fprintf ppf "#is_poly_var_const"
| Pisout i -> fprintf ppf "isout %d" i
Expand Down
4 changes: 2 additions & 2 deletions jscomp/test/dist/jscomp/test/js_obj_test.js

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

4 changes: 3 additions & 1 deletion jscomp/test/dist/jscomp/test/ppx_apply_test.js

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

4 changes: 1 addition & 3 deletions jscomp/test/dist/jscomp/test/unsafe_full_apply_primitive.js

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

0 comments on commit bd161cf

Please sign in to comment.