@@ -193,7 +193,7 @@ let has_null_undefined_other (sw_names : Ast_untagged_variants.switch_names opti
193193let no_effects_const = lazy true
194194(* let has_effects_const = lazy false *)
195195
196- (* * We drop the ability of cross-compiling
196+ (* We drop the ability of cross-compiling
197197 the compiler has to be the same running
198198*)
199199
@@ -224,6 +224,9 @@ type initialization = J.block
224224 -: we should not do functor application inlining in a
225225 non-toplevel, it will explode code very quickly
226226*)
227+
228+ let compile output_prefix =
229+
227230let rec compile_external_field (* Like [List.empty]*)
228231 (lamba_cxt : Lam_compile_context.t ) (id : Ident.t ) name : Js_output.t =
229232 match Lam_compile_env. query_external_id_info id name with
@@ -249,7 +252,7 @@ let rec compile_external_field (* Like [List.empty]*)
249252 @param args arguments
250253*)
251254
252- (* * This can not happen since this id should be already consulted by type checker
255+ (* This can not happen since this id should be already consulted by type checker
253256 Worst case
254257 {[
255258 E.array_index_by_int m pos
@@ -304,7 +307,7 @@ and compile_external_field_apply (appinfo : Lam.apply) (module_id : Ident.t)
304307 Js_output. output_of_block_and_expression lambda_cxt.continuation args_code
305308 expression
306309
307- (* *
310+ (*
308311 The second return values are values which need to be wrapped using
309312 [update_dummy]
310313
@@ -500,29 +503,29 @@ and compile_recursive_lets cxt id_args : Js_output.t =
500503
501504and compile_general_cases :
502505 'a .
503- ('a -> Ast_untagged_variants. literal option ) ->
504- ('a -> J. expression ) ->
505- ('a option -> J. expression -> 'a option -> J. expression -> J. expression ) ->
506- Lam_compile_context. t ->
507- (?default :J.block ->
508- ?declaration:Lam_compat.let_kind * Ident.t ->
509- _ ->
510- ('a * J.case_clause) list ->
511- J.statement ) ->
512- _ ->
513- ('a * Lam. t ) list ->
514- default_case ->
506+ get_cstr_name : ('a -> Ast_untagged_variants. literal option ) ->
507+ make_exp : ('a -> J. expression ) ->
508+ eq_exp : ('a option -> J. expression -> 'a option -> J. expression -> J. expression ) ->
509+ cxt : Lam_compile_context. t ->
510+ switch : (?default :J.block -> ?declaration:Lam_compat.let_kind * Ident.t ->
511+ _ -> ('a * J.case_clause) list -> J.statement ) ->
512+ switch_exp : J. expression ->
513+ cases : ('a * Lam. t ) list ->
514+ default : default_case ->
515515 J. block =
516- fun (get_cstr_name : _ -> Ast_untagged_variants.literal option ) (make_exp : _ -> J.expression )
517- (eq_exp : 'a option -> J.expression -> 'a option -> J.expression -> J.expression )
518- (cxt : Lam_compile_context.t )
519- (switch :
520- ?default:J.block ->
521- ?declaration:Lam_compat.let_kind * Ident.t ->
522- _ ->
523- (_ * J.case_clause) list ->
524- J.statement ) (switch_exp : J.expression ) (cases : (_ * Lam.t) list )
525- (default : default_case ) ->
516+ fun (type a )
517+ ~(get_cstr_name : a -> Ast_untagged_variants.literal option )
518+ ~(make_exp : a -> J.expression )
519+ ~(eq_exp : a option -> J.expression -> a option -> J.expression -> J.expression )
520+ ~(cxt : Lam_compile_context.t )
521+ ~(switch :
522+ ?default:J.block ->
523+ ?declaration:Lam_compat.let_kind * Ident.t ->
524+ _ -> (a * J.case_clause) list -> J.statement
525+ )
526+ ~(switch_exp : J.expression )
527+ ~(cases : (a * Lam.t) list )
528+ ~(default : default_case ) ->
526529 match (cases, default) with
527530 | [] , Default lam -> Js_output. output_as_block (compile_lambda cxt lam)
528531 | [] , (Complete | NonComplete ) -> []
@@ -538,6 +541,7 @@ and compile_general_cases :
538541 morph_declare_to_assign cxt (fun cxt define ->
539542 [
540543 S. if_ ?declaration:define
544+
541545 (eq_exp None switch_exp (Some id) (make_exp id))
542546 (Js_output. output_as_block (compile_lambda cxt lam));
543547 ])
@@ -624,22 +628,26 @@ and use_compile_literal_cases table get_name =
624628 | Some {name; literal_type = None } , Some string_table -> Some ((String name, lam) :: string_table)
625629 | _ , _ -> None
626630 ) table (Some [] )
627- and compile_cases ?(untagged =false ) cxt (switch_exp : E.t ) table default get_name =
631+ and compile_cases ?(untagged =false ) cxt (switch_exp : E.t ) table default get_name : initialization =
628632 match use_compile_literal_cases table get_name with
629633 | Some string_table ->
630634 if untagged
631635 then compile_untagged_cases cxt switch_exp string_table default
632636 else compile_string_cases cxt switch_exp string_table default
633637 | None ->
634- compile_general_cases get_name
635- (fun i -> match get_name i with
638+ compile_general_cases
639+ ~get_cstr_name: get_name
640+ ~make_exp: (fun i -> match get_name i with
636641 | None -> E. small_int i
637642 | Some {literal_type = Some (String s )} -> E. str s
638643 | Some {name} -> E. str name)
639- (fun _ x _ y -> E. int_equal x y) cxt
640- (fun ?default ?declaration e clauses ->
644+ ~eq_exp: (fun _ x _ y -> E. int_equal x y)
645+ ~cxt
646+ ~switch: (fun ?default ?declaration e clauses ->
641647 S. int_switch ?default ?declaration e clauses)
642- switch_exp table default
648+ ~switch_exp
649+ ~cases: table
650+ ~default
643651
644652and compile_switch (switch_arg : Lam.t ) (sw : Lam.lambda_switch )
645653 (lambda_cxt : Lam_compile_context.t ) =
@@ -691,6 +699,7 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
691699 else
692700 (* [e] will be used twice *)
693701 let dispatch e =
702+
694703 let is_a_literal_case =
695704 if block_cases <> []
696705 then
@@ -728,22 +737,22 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
728737 :: compile_whole { lambda_cxt with continuation = Assign id })
729738 | EffectCall _ | Assign _ -> Js_output. make (compile_whole lambda_cxt)
730739
731- and compile_string_cases cxt switch_exp table default =
732- let literal = function
740+
741+ and compile_string_cases cxt switch_exp cases default : initialization =
742+ let literal = function
733743 | literal -> E. literal literal
734744 in
735745 compile_general_cases
736- (fun _ -> None )
737- literal
738- (fun _ x _ y -> E. string_equal x y)
739- cxt
740- (fun ?default ?declaration e clauses ->
746+ ~get_cstr_name: (fun _ -> None )
747+ ~make_exp: literal
748+ ~eq_exp: (fun _ x _ y -> E. string_equal x y)
749+ ~ cxt
750+ ~switch: (fun ?default ?declaration e clauses ->
741751 S. string_switch ?default ?declaration e clauses)
742- switch_exp table default
743- and compile_untagged_cases cxt switch_exp table default =
744- let literal = function
745- | literal -> E. literal literal
746- in
752+ ~switch_exp
753+ ~cases
754+ ~default
755+ and compile_untagged_cases cxt switch_exp cases default =
747756 let add_runtime_type_check (literal : Ast_untagged_variants.literal_type ) x y = match literal with
748757 | Block IntType
749758 | Block StringType
@@ -762,7 +771,7 @@ and compile_untagged_cases cxt switch_exp table default =
762771 | _ -> E. string_equal x y
763772 in
764773 let is_array (l , _ ) = l = Ast_untagged_variants. Block Array in
765- let body ?default ?declaration e clauses =
774+ let switch ?default ?declaration e clauses =
766775 let array_clauses = Ext_list. filter clauses is_array in
767776 match array_clauses with
768777 | [(l, {J. switch_body})] when List. length clauses > 1 ->
@@ -774,12 +783,14 @@ and compile_untagged_cases cxt switch_exp table default =
774783 | _ ->
775784 S. string_switch ?default ?declaration (E. typeof e) clauses in
776785 compile_general_cases
777- (fun _ -> None )
778- literal
779- mk_eq
780- cxt
781- body
782- switch_exp table default
786+ ~get_cstr_name: (fun _ -> None )
787+ ~make_exp: E. literal
788+ ~eq_exp: mk_eq
789+ ~cxt
790+ ~switch
791+ ~switch_exp
792+ ~cases
793+ ~default
783794
784795and compile_stringswitch l cases default (lambda_cxt : Lam_compile_context.t ) =
785796 (* TODO might better optimization according to the number of cases
@@ -1077,7 +1088,7 @@ and compile_while (predicate : Lam.t) (body : Lam.t)
10771088 Js_output. output_of_block_and_expression lambda_cxt.continuation block
10781089 E. unit
10791090
1080- (* * all non-tail
1091+ (* all non-tail
10811092 TODO: check semantics should start, finish be executed each time in both
10821093 ocaml and js?, also check evaluation order..
10831094 in ocaml id is not in the scope of finish, so it should be safe here
@@ -1661,7 +1672,7 @@ and compile_prim (prim_info : Lam.prim_info)
16611672 let args_code : J.block = List. concat args_block in
16621673 let exp =
16631674 (* TODO: all can be done in [compile_primitive] *)
1664- Lam_compile_primitive. translate loc lambda_cxt primitive args_expr
1675+ Lam_compile_primitive. translate output_prefix loc lambda_cxt primitive args_expr
16651676 in
16661677 Js_output. output_of_block_and_expression lambda_cxt.continuation args_code
16671678 exp
@@ -1758,3 +1769,8 @@ and compile_lambda (lambda_cxt : Lam_compile_context.t) (cur_lam : Lam.t) :
17581769 | Ltrywith (lam , id , catch ) ->
17591770 (* generate documentation *)
17601771 compile_trywith lam id catch lambda_cxt
1772+
1773+ in compile_recursive_lets, compile_lambda
1774+
1775+ let compile_recursive_lets ~output_prefix = fst (compile output_prefix)
1776+ let compile_lambda ~output_prefix = snd (compile output_prefix)
0 commit comments