Skip to content

Commit 0fa9c8f

Browse files
committed
switch the encoding of poly-var
- in debug mode it attaches the name property - fix caml_hash to handle polyvar
1 parent 3e32220 commit 0fa9c8f

14 files changed

+94
-88
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+
(("HASH",
861+
if !Js_config.debug then hash
862+
else {hash with comment = Some name}
863+
) ::
864+
("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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,32 @@ 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+
| Array (l,_) (* Float i -- should not appear here *)
407+
| Caml_block (l,_, _, _) when no_side_effect e
408+
->
409+
(match l with
410+
| x ::_ -> x
411+
| [] ->
412+
assert false
413+
)
414+
| _ -> { expression_desc = Static_index (e, "HASH", Some 0l); comment = None}
415+
416+
417+
let poly_var_value_access (e : t) =
418+
match e.expression_desc with
419+
| Array (l,_)
420+
| Caml_block (l,_, _, _) when no_side_effect e
421+
->
422+
(match l with
423+
| _ :: v :: _ -> v
424+
| _ ->
425+
assert false
426+
)
427+
| _ -> { expression_desc = Static_index (e, "value", Some 1l); comment = None}
428+
404429
let extension_access (e : t) name (pos : int32) : t =
405430
match e.expression_desc with
406431
| 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ let option_unwrap (arg : J.expression) : J.expression =
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
| _ ->
105105
E.runtime_call Js_runtime_modules.option "option_unwrap" [arg]

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_util.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let comment_of_tag_info (x : Lam_tag_info.t) =
4545
| Blk_constructor {name = n} -> Some n
4646
| Blk_tuple -> Some "tuple"
4747
| Blk_class -> Some "class"
48-
| Blk_poly_var x -> Some ("`" ^ x)
48+
| Blk_poly_var _ -> None
4949
| Blk_record _ -> None
5050
| Blk_record_inlined {name = ctor} -> Some ctor
5151
| Blk_record_ext _ -> None

jscomp/runtime/caml_hash.ml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ let caml_hash (count : int) _limit (seed : nativeint)
129129
()
130130
else
131131
let size = Obj.size obj in
132-
if size <> 0 then
132+
if size <> 0 then begin
133133
let obj_tag = Obj.tag obj in
134134
let tag = (size lsl 10) lor obj_tag in
135-
if tag = 248 (* Obj.object_tag*) then
135+
if obj_tag = 248 (* Obj.object_tag*) then
136136
hash.contents <- caml_hash_mix_int hash.contents
137-
(Caml_nativeint_extern.of_int (Obj.obj (Obj.field obj 1) : int))
137+
(Caml_nativeint_extern.of_int (Obj.obj (Obj.field obj 1) : int))
138138
else
139139
begin
140140
hash.contents <- caml_hash_mix_int hash.contents (Caml_nativeint_extern.of_int tag) ;
@@ -144,6 +144,18 @@ let caml_hash (count : int) _limit (seed : nativeint)
144144
push_back queue (Obj.field obj i )
145145
done
146146
end
147+
end else
148+
begin
149+
let size : int = ([%raw {|function(obj,cb){
150+
var size = 0
151+
for(var k in obj){
152+
cb(obj[k])
153+
++ size
154+
}
155+
return size
156+
}|}] obj (fun [@bs] v -> push_back queue v ) [@bs]) in
157+
hash.contents <- caml_hash_mix_int hash.contents (Caml_nativeint_extern.of_int ((size lsl 10) lor 0)) (*tag*) ;
158+
end
147159
done;
148160
caml_hash_final_mix hash.contents
149161

0 commit comments

Comments
 (0)