From dca580daad49b51ab525650b7e231df7ed6fa075 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 19 Apr 2016 11:44:57 -0400 Subject: [PATCH 1/2] improve queue output, so that we can use it in runtime caml_hash --- jscomp/js_exp_make.ml | 2 ++ jscomp/lam_compile.ml | 38 +++++++++++++++++++++++++++++++++----- jscomp/runtime/.depend | 4 ++++ jscomp/stdlib/queue.js | 10 +++------- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/jscomp/js_exp_make.ml b/jscomp/js_exp_make.ml index 11b7d35fbf..c8b892cf12 100644 --- a/jscomp/js_exp_make.ml +++ b/jscomp/js_exp_make.ml @@ -88,6 +88,8 @@ let anything_to_string ?comment (e : t) : t = let arr ?comment mt es : t = {expression_desc = Array (es,mt) ; comment} + + let make_block ?comment tag tag_info es mutable_flag : t = { expression_desc = Caml_block( es, mutable_flag, tag,tag_info) ; diff --git a/jscomp/lam_compile.ml b/jscomp/lam_compile.ml index 50c807e407..8c866c6cfd 100644 --- a/jscomp/lam_compile.ml +++ b/jscomp/lam_compile.ml @@ -245,8 +245,14 @@ and compile_let flag (cxt : Lam_compile_defs.cxt) id (arg : Lambda.lambda) : Js match flag, arg with | let_kind, _ -> compile_lambda {cxt with st = Declare (let_kind, id); should_return = False } arg - -and compile_recursive_let (cxt : Lam_compile_defs.cxt) (id : Ident.t) (arg : Lambda.lambda) = +(** + The second return values are values which need to be wrapped using + [caml_update_dummy] +*) +and compile_recursive_let + (cxt : Lam_compile_defs.cxt) + (id : Ident.t) + (arg : Lambda.lambda) : Js_output.t * Ident.t list = match arg with | Lfunction (kind, params, body) -> (* 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 else (* TODO: save computation of length several times *) E.fun_ params (Js_output.to_block output ) ), [] - | (Lprim(Pmakeblock _ , _) ) -> + | Lprim (Pmakeblock (0, _, _) , ls) + when List.for_all (function | Lambda.Lvar _ -> true | _ -> false) ls + -> + (* capture cases like for {!Queue} + {[let rec cell = { content = x; next = cell} ]} + *) + Js_output.of_block ( + S.define ~kind:Variable id (E.arr Mutable []) :: + (List.mapi (fun i x -> + match x with + | Lambda.Lvar lid + -> S.exp + (Js_array.set_array (E.var id) (E.int (Int32.of_int i)) (E.var lid)) + | _ -> assert false + ) ls) + ), [] + + | Lprim(Pmakeblock _ , _) -> + (* FIXME: also should fill tag *) (* Lconst should not appear here if we do [scc] optimization, since it's faked recursive value, 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 (* TODO: check recursive value .. could be improved for simple cases *) - Js_output.of_block ( - b @ [S.exp(E.runtime_call Js_config.obj_runtime "caml_update_dummy" [ E.var id; v])]), + Js_output.of_block + ( + b @ + [S.exp + (E.runtime_call Js_config.obj_runtime "caml_update_dummy" + [ E.var id; v])]), [id] (* S.define ~kind:Variable id (E.arr Mutable []):: *) | _ -> assert false diff --git a/jscomp/runtime/.depend b/jscomp/runtime/.depend index e07a5ad26f..2315c0689e 100644 --- a/jscomp/runtime/.depend +++ b/jscomp/runtime/.depend @@ -53,6 +53,8 @@ js.cmo : js.cmx : js_error.cmo : js_error.cmx : +queue.cmo : +queue.cmx : typed_array.cmo : typed_array.cmx : caml_array.cmo : js.cmo caml_array.cmi @@ -95,5 +97,7 @@ js.cmo : js.cmj : js_error.cmo : js_error.cmj : +queue.cmo : +queue.cmj : typed_array.cmo : typed_array.cmj : diff --git a/jscomp/stdlib/queue.js b/jscomp/stdlib/queue.js index 481a2a76af..22fd910d01 100644 --- a/jscomp/stdlib/queue.js +++ b/jscomp/stdlib/queue.js @@ -39,13 +39,9 @@ function add(x, q) { return /* () */0; } else { - var cell$1 = { - - }; - Caml_obj.caml_update_dummy(cell$1, /* record */[ - x, - cell$1 - ]); + var cell$1 = []; + cell$1[0] = x; + cell$1[1] = cell$1; q[/* length */0] = 1; q[/* tail */1] = cell$1; return /* () */0; From 6010c097805273b77a968f6e8d427ae7905219de Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 19 Apr 2016 11:46:51 -0400 Subject: [PATCH 2/2] clean up --- jscomp/runtime/.depend | 4 ---- 1 file changed, 4 deletions(-) diff --git a/jscomp/runtime/.depend b/jscomp/runtime/.depend index 2315c0689e..e07a5ad26f 100644 --- a/jscomp/runtime/.depend +++ b/jscomp/runtime/.depend @@ -53,8 +53,6 @@ js.cmo : js.cmx : js_error.cmo : js_error.cmx : -queue.cmo : -queue.cmx : typed_array.cmo : typed_array.cmx : caml_array.cmo : js.cmo caml_array.cmi @@ -97,7 +95,5 @@ js.cmo : js.cmj : js_error.cmo : js_error.cmj : -queue.cmo : -queue.cmj : typed_array.cmo : typed_array.cmj :