Skip to content

Commit ca95e06

Browse files
committed
improve queue output, so that we can use it in runtime caml_hash (#268)
* improve queue output, so that we can use it in runtime caml_hash * clean up
1 parent 0e2fb93 commit ca95e06

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

jscomp/js_exp_make.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ let anything_to_string ?comment (e : t) : t =
8888

8989
let arr ?comment mt es : t =
9090
{expression_desc = Array (es,mt) ; comment}
91+
92+
9193
let make_block ?comment tag tag_info es mutable_flag : t =
9294
{
9395
expression_desc = Caml_block( es, mutable_flag, tag,tag_info) ;

jscomp/lam_compile.ml

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,14 @@ and compile_let flag (cxt : Lam_compile_defs.cxt) id (arg : Lambda.lambda) : Js
245245
match flag, arg with
246246
| let_kind, _ ->
247247
compile_lambda {cxt with st = Declare (let_kind, id); should_return = False } arg
248-
249-
and compile_recursive_let (cxt : Lam_compile_defs.cxt) (id : Ident.t) (arg : Lambda.lambda) =
248+
(**
249+
The second return values are values which need to be wrapped using
250+
[caml_update_dummy]
251+
*)
252+
and compile_recursive_let
253+
(cxt : Lam_compile_defs.cxt)
254+
(id : Ident.t)
255+
(arg : Lambda.lambda) : Js_output.t * Ident.t list =
250256
match arg with
251257
| Lfunction (kind, params, body) ->
252258
(* Invariant: jmp_table can not across function boundary,
@@ -296,7 +302,25 @@ and compile_recursive_let (cxt : Lam_compile_defs.cxt) (id : Ident.t) (arg : Lam
296302
else (* TODO: save computation of length several times *)
297303
E.fun_ params (Js_output.to_block output )
298304
), []
299-
| (Lprim(Pmakeblock _ , _) ) ->
305+
| Lprim (Pmakeblock (0, _, _) , ls)
306+
when List.for_all (function | Lambda.Lvar _ -> true | _ -> false) ls
307+
->
308+
(* capture cases like for {!Queue}
309+
{[let rec cell = { content = x; next = cell} ]}
310+
*)
311+
Js_output.of_block (
312+
S.define ~kind:Variable id (E.arr Mutable []) ::
313+
(List.mapi (fun i x ->
314+
match x with
315+
| Lambda.Lvar lid
316+
-> S.exp
317+
(Js_array.set_array (E.var id) (E.int (Int32.of_int i)) (E.var lid))
318+
| _ -> assert false
319+
) ls)
320+
), []
321+
322+
| Lprim(Pmakeblock _ , _) ->
323+
(* FIXME: also should fill tag *)
300324
(* Lconst should not appear here if we do [scc]
301325
optimization, since it's faked recursive value,
302326
however it would affect scope issues, we have to declare it first
@@ -308,8 +332,12 @@ and compile_recursive_let (cxt : Lam_compile_defs.cxt) (id : Ident.t) (arg : Lam
308332
(* TODO: check recursive value ..
309333
could be improved for simple cases
310334
*)
311-
Js_output.of_block (
312-
b @ [S.exp(E.runtime_call Js_config.obj_runtime "caml_update_dummy" [ E.var id; v])]),
335+
Js_output.of_block
336+
(
337+
b @
338+
[S.exp
339+
(E.runtime_call Js_config.obj_runtime "caml_update_dummy"
340+
[ E.var id; v])]),
313341
[id]
314342
(* S.define ~kind:Variable id (E.arr Mutable []):: *)
315343
| _ -> assert false

jscomp/stdlib/queue.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,9 @@ function add(x, q) {
3939
return /* () */0;
4040
}
4141
else {
42-
var cell$1 = {
43-
44-
};
45-
Caml_obj.caml_update_dummy(cell$1, /* record */[
46-
x,
47-
cell$1
48-
]);
42+
var cell$1 = [];
43+
cell$1[0] = x;
44+
cell$1[1] = cell$1;
4945
q[/* length */0] = 1;
5046
q[/* tail */1] = cell$1;
5147
return /* () */0;

0 commit comments

Comments
 (0)