Skip to content

Commit 3c8e007

Browse files
authored
Merge pull request #4405 from BuckleScript/switch_poly_var
switch poly-var encoding
2 parents e180e35 + ea85fc8 commit 3c8e007

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1073
-1098
lines changed

jscomp/core/js_dump.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,26 @@ and expression_desc cxt ~(level:int) f x : cxt =
849849
(List.combine (Array.to_list fields) el )))
850850
(* name convention of Record is slight different from modules
851851
*)
852+
| Caml_block(el,_,_, Blk_poly_var name) ->
853+
begin match el with
854+
| [hash;value] ->
855+
expression_desc
856+
cxt
857+
~level
858+
f
859+
(Object
860+
((Literals.polyvar_hash,
861+
if !Js_config.debug then hash
862+
else {hash with comment = Some name}
863+
) ::
864+
(Literals.polyvar_value, value) ::
865+
if !Js_config.debug then
866+
["name", E.str name]
867+
else []
868+
)
869+
)
870+
| _ -> assert false
871+
end
852872
| Caml_block(el,_, _, (Blk_extension | Blk_record_ext _ as ext )) ->
853873
expression_desc cxt ~level f (exn_block_as_obj ~stack:false el ext)
854874
| Caml_block( el, mutable_flag, tag, tag_info)

jscomp/core/js_exp_make.ml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,30 @@ let record_access (e : t) (name : string) (pos : int32) =
400400
{ expression_desc = Static_index (e, name, Some pos); comment = None}
401401
)
402402
| _ -> { expression_desc = Static_index (e, name, Some pos); comment = None}
403-
403+
404+
let poly_var_tag_access (e : t) =
405+
match e.expression_desc with
406+
| Caml_block (l,_, _, _) when no_side_effect e
407+
->
408+
(match l with
409+
| x ::_ -> x
410+
| [] ->
411+
assert false
412+
)
413+
| _ -> { expression_desc = Static_index (e, Literals.polyvar_hash, Some 0l); comment = None}
414+
415+
416+
let poly_var_value_access (e : t) =
417+
match e.expression_desc with
418+
| Caml_block (l,_, _, _) when no_side_effect e
419+
->
420+
(match l with
421+
| _ :: v :: _ -> v
422+
| _ ->
423+
assert false
424+
)
425+
| _ -> { expression_desc = Static_index (e, Literals.polyvar_value, Some 1l); comment = None}
426+
404427
let extension_access (e : t) name (pos : int32) : t =
405428
match e.expression_desc with
406429
| Array (l,_) (* Float i -- should not appear here *)

jscomp/core/js_exp_make.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ val record_assign :
201201
t ->
202202
t
203203

204+
val poly_var_tag_access :
205+
t -> t
206+
207+
val poly_var_value_access :
208+
t -> t
209+
210+
204211
val extension_assign :
205212
t ->
206213
int32 ->

jscomp/core/js_of_lam_block.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,18 @@ let field (field_info : Lam_compat.field_dbg_info) e i =
5252
match field_info with
5353
| Fld_na _
5454
| Fld_tuple
55-
| Fld_poly_var_tag
56-
| Fld_poly_var_content
55+
56+
5757
| Fld_record_inline _
5858
| Fld_variant
5959
| Fld_array
6060
->
6161
E.array_index_by_int
6262
?comment:(Lam_compat.str_of_field_info field_info) e i
63+
| Fld_poly_var_content
64+
-> E.poly_var_value_access e
65+
| Fld_poly_var_tag
66+
-> E.poly_var_tag_access e
6367
| Fld_record_extension {name} ->
6468
E.extension_access e (Some name) i
6569
| Fld_extension ->

jscomp/core/js_of_lam_option.ml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,16 @@ let get_default_undefined_from_optional
9393
else
9494
(E.runtime_call Js_runtime_modules.option "option_get" [arg])
9595

96-
let get_default_undefined (arg : J.expression) : J.expression =
96+
let option_unwrap (arg : J.expression) : J.expression =
9797
let desc = arg.expression_desc in
9898
if is_none_static desc then E.undefined else
9999
match desc with
100100
| Optional_block (x,_)
101101
->
102-
Js_of_lam_polyvar.get_field x
102+
E.poly_var_value_access x
103103
(* invariant: option encoding *)
104104
| _ ->
105-
(* FIXME: no need do such inlining*)
106-
if Js_analyzer.is_okay_to_duplicate arg then
107-
E.econd (is_not_none arg)
108-
(Js_of_lam_polyvar.get_field (val_from_option arg)) E.undefined
109-
else
110-
E.runtime_call Js_runtime_modules.option "option_get_unwrap" [arg]
105+
E.runtime_call Js_runtime_modules.option "option_unwrap" [arg]
111106

112107
let destruct_optional
113108
~for_sure_none

jscomp/core/js_of_lam_option.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ val get_default_undefined_from_optional:
4444

4545
(** Given [Some (`a x)] or [None],
4646
return [x] *)
47-
val get_default_undefined :
47+
val option_unwrap :
4848
J.expression ->
4949
J.expression
5050

jscomp/core/js_of_lam_polyvar.ml

Lines changed: 0 additions & 32 deletions
This file was deleted.

jscomp/core/js_of_lam_polyvar.mli

Lines changed: 0 additions & 34 deletions
This file was deleted.

jscomp/core/js_of_lam_variant.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) =
5858
| _ ->
5959
Splice2
6060
(E.of_block
61-
[(S.int_switch (Js_of_lam_polyvar.get_tag arg)
61+
[(S.int_switch (E.poly_var_tag_access arg)
6262
(Ext_list.map dispatches (fun (i,r) ->
6363
{J.switch_case = i ;
6464
switch_body = [S.return_stmt (E.str r)];
@@ -69,7 +69,7 @@ let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) =
6969
the problem is that we can not create bindings
7070
due to the
7171
*)
72-
(Js_of_lam_polyvar.get_field arg)
72+
(E.poly_var_value_access arg)
7373
)
7474
(** FIXME:
7575
1. duplicated evaluation of expressions arg
@@ -100,7 +100,7 @@ let eval_as_unwrap (arg : J.expression) : E.t =
100100
| Caml_block ([{expression_desc = Number _}; cb], _, _, _) ->
101101
cb
102102
| _ ->
103-
Js_of_lam_polyvar.get_field arg
103+
E.poly_var_value_access arg
104104

105105

106106

jscomp/core/lam_compile_external_call.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ let ocaml_to_js_eff
139139
- if ocaml arg is `None`, let js arg be `undefined` (no unwrapping)
140140
- if ocaml arg is `Some x`, unwrap the arg to get the `x`, then
141141
unwrap the `x` itself
142+
- Here `Some x` is `x` due to the current encoding
143+
Lets inline here since it depends on the runtime encoding
142144
*)
143-
Js_of_lam_option.get_default_undefined raw_arg
145+
Js_of_lam_option.option_unwrap raw_arg
144146
| _ ->
145147
Js_of_lam_variant.eval_as_unwrap raw_arg
146148
in

0 commit comments

Comments
 (0)