@@ -658,13 +658,34 @@ let configure formatter =
658658 Code.Var. set_pretty (pretty && not (Config.Flag. shortvar () ));
659659 Code.Var. set_stable (Config.Flag. stable_var () )
660660
661- let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
662- let exported_runtime = not standalone in
661+ type 'a target =
662+ | JavaScript : Pretty_print .t -> Source_map .t option target
663+ | Wasm
664+ : (Deadcode .variable_uses * Effects .in_cps * Code .program * Parse_bytecode.Debug .t )
665+ target
666+
667+ let link_and_pack ?(standalone = true ) ?(wrap_with_fun = `Iife ) ?(link = `No ) p =
663668 let export_runtime =
664669 match link with
665670 | `All | `All_from _ -> true
666671 | `Needed | `No -> false
667672 in
673+ p
674+ |> link' ~export_runtime ~standalone ~link
675+ |> pack ~wrap_with_fun ~standalone
676+ |> coloring
677+ |> check_js
678+
679+ let full
680+ (type result )
681+ ~(target : result target )
682+ ~standalone
683+ ~wrap_with_fun
684+ ~profile
685+ ~link
686+ ~source_map
687+ d
688+ p : result =
668689 let deadcode_sentinal =
669690 (* If deadcode is disabled, this field is just fresh variable *)
670691 Code.Var. fresh_n " undef"
@@ -677,56 +698,71 @@ let full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p =
677698 | O3 -> o3)
678699 +> exact_calls ~deadcode_sentinal profile
679700 +> effects ~deadcode_sentinal
680- +> map_fst (if Config.Flag. effects () then fun x -> x else Generate_closure. f)
701+ +> map_fst
702+ (match target with
703+ | JavaScript _ -> if Config.Flag. effects () then Fun. id else Generate_closure. f
704+ | Wasm -> Fun. id)
681705 +> map_fst deadcode'
682706 in
683- let emit =
684- generate
685- d
686- ~exported_runtime
687- ~wrap_with_fun
688- ~warn_on_unhandled_effect: standalone
689- ~deadcode_sentinal
690- +> link' ~export_runtime ~standalone ~link
691- +> pack ~wrap_with_fun ~standalone
692- +> coloring
693- +> check_js
694- +> output formatter ~source_map ()
695- in
696707 if times () then Format. eprintf " Start Optimizing...@." ;
697708 let t = Timer. make () in
698709 let r = opt p in
699710 let () = if times () then Format. eprintf " optimizations : %a@." Timer. print t in
700- emit r
711+ match target with
712+ | JavaScript formatter ->
713+ let exported_runtime = not standalone in
714+ let emit formatter =
715+ generate
716+ d
717+ ~exported_runtime
718+ ~wrap_with_fun
719+ ~warn_on_unhandled_effect: standalone
720+ ~deadcode_sentinal
721+ +> link_and_pack ~standalone ~wrap_with_fun ~link
722+ +> output formatter ~source_map ()
723+ in
724+ let source_map = emit formatter r in
725+ source_map
726+ | Wasm ->
727+ let (p, live_vars), _, in_cps = r in
728+ live_vars, in_cps, p, d
701729
702- let full_no_source_map ~standalone ~wrap_with_fun ~profile ~link formatter d p =
730+ let full_no_source_map ~formatter ~ standalone ~wrap_with_fun ~profile ~link d p =
703731 let (_ : Source_map.t option ) =
704- full ~standalone ~wrap_with_fun ~profile ~link ~source_map: None formatter d p
732+ full
733+ ~target: (JavaScript formatter)
734+ ~standalone
735+ ~wrap_with_fun
736+ ~profile
737+ ~link
738+ ~source_map: None
739+ d
740+ p
705741 in
706742 ()
707743
708744let f
745+ ~target
709746 ?(standalone = true )
710747 ?(wrap_with_fun = `Iife )
711748 ?(profile = O1 )
712749 ~link
713750 ?source_map
714- formatter
715751 d
716752 p =
717- full ~standalone ~wrap_with_fun ~profile ~link ~source_map formatter d p
753+ full ~target ~ standalone ~wrap_with_fun ~profile ~link ~source_map d p
718754
719755let f' ?(standalone = true ) ?(wrap_with_fun = `Iife ) ?(profile = O1 ) ~link formatter d p =
720- full_no_source_map ~standalone ~wrap_with_fun ~profile ~link formatter d p
756+ full_no_source_map ~formatter ~ standalone ~wrap_with_fun ~profile ~link d p
721757
722758let from_string ~prims ~debug s formatter =
723759 let p, d = Parse_bytecode. from_string ~prims ~debug s in
724760 full_no_source_map
761+ ~formatter
725762 ~standalone: false
726763 ~wrap_with_fun: `Anonymous
727764 ~profile: O1
728765 ~link: `No
729- formatter
730766 d
731767 p
732768
0 commit comments