diff --git a/jscomp/ext_pervasives.ml b/jscomp/ext_pervasives.ml index f427756f2e..614d86dedb 100644 --- a/jscomp/ext_pervasives.ml +++ b/jscomp/ext_pervasives.ml @@ -18,13 +18,13 @@ (* Author: Hongbo Zhang *) - +external reraise: exn -> 'a = "%reraise" let finally v f action = match f v with | exception e -> action v ; - raise e + reraise e | e -> action v ; e let with_file_as_chan filename f = diff --git a/jscomp/ext_pervasives.mli b/jscomp/ext_pervasives.mli index c8ee85baae..caa12396bb 100644 --- a/jscomp/ext_pervasives.mli +++ b/jscomp/ext_pervasives.mli @@ -23,6 +23,7 @@ (** Extension to standard library [Pervavives] module, safe to open *) +external reraise: exn -> 'a = "%reraise" val finally : 'a -> ('a -> 'b) -> ('a -> 'c) -> 'b val with_file_as_chan : string -> (out_channel -> 'a) -> 'a diff --git a/jscomp/js_implementation.ml b/jscomp/js_implementation.ml index 77e845198b..7091e334f2 100644 --- a/jscomp/js_implementation.ml +++ b/jscomp/js_implementation.ml @@ -49,9 +49,20 @@ let implementation ppf sourcefile outputprefix = |> Translmod.transl_implementation modulename |> print_if ppf Clflags.dump_rawlambda Printlambda.lambda |> (fun lambda -> - Lam_compile_group.lambda_as_module true + match + Lam_compile_group.lambda_as_module finalenv current_signature - sourcefile lambda); + sourcefile lambda with + | e -> e + | exception e -> + (* Save to a file instead so that it will not scare user *) + let file = "osc.dump" in + Ext_pervasives.with_file_as_chan file + (fun ch -> output_string ch @@ + Printexc.raw_backtrace_to_string (Printexc.get_raw_backtrace ())); + Ext_log.err __LOC__ "Compilation fatal error, stacktrace saved into %s" file ; + raise e + ); end; Stypes.dump (Some (outputprefix ^ ".annot")); with x -> diff --git a/jscomp/js_pass_flatten_and_mark_dead.ml b/jscomp/js_pass_flatten_and_mark_dead.ml index e0b9cc2cc4..a7bae8c6c8 100644 --- a/jscomp/js_pass_flatten_and_mark_dead.ml +++ b/jscomp/js_pass_flatten_and_mark_dead.ml @@ -143,8 +143,16 @@ let mark_dead_code js = Take away, it is really hard to change the code while collecting some information.. we should always collect info in a single pass + + Note that, we should avoid reuse object, i.e, + {[ + let v = + object + end + ]} + Since user may use `osc -c xx.ml xy.ml xz.ml` and we need clean up state *) -let subst_map = object (self) +let subst_map name = object (self) inherit Js_map.map as super val mutable substitution = Hashtbl.create 17 @@ -213,9 +221,19 @@ let subst_map = object (self) begin match Hashtbl.find self#get_substitution id with | {expression_desc = Array (ls, Immutable) } -> + (* user program can be wrong, we should not + turn a runtime crash into compile time crash : ) + *) begin match List.nth ls i with | {expression_desc = J.Var _ | Number _ | Str _ } as x -> x + | exception _ -> + begin + Ext_log.err __LOC__ "suspcious code %s when compiling %s@." + (Printf.sprintf "%s/%d" id.name id.stamp) + name ; + super#expression x ; + end | _ -> (** we can do here, however, we should be careful that it can only be done @@ -266,9 +284,9 @@ end there is no need to add another pass *) -let program js = +let program (js : J.program) = js - |> subst_map#program + |> (subst_map js.name )#program |> mark_dead_code (* |> mark_dead_code *) (* mark dead code twice does have effect in some cases, however, we disabled it diff --git a/jscomp/lam_compile_group.ml b/jscomp/lam_compile_group.ml index 2507a1d415..f7cdf87bff 100644 --- a/jscomp/lam_compile_group.ml +++ b/jscomp/lam_compile_group.ml @@ -297,7 +297,6 @@ let compile ~filename env sigs lam : J.program = let current_file_name : string option ref = ref None;; let lambda_as_module - (raw : bool) env (sigs : Types.signature) (filename : string) diff --git a/jscomp/lam_compile_group.mli b/jscomp/lam_compile_group.mli index 8798c0e377..4fa161b3c0 100644 --- a/jscomp/lam_compile_group.mli +++ b/jscomp/lam_compile_group.mli @@ -35,4 +35,4 @@ val compile : Lambda.lambda -> J.program -val lambda_as_module : bool -> Env.t -> Types.signature -> string -> Lambda.lambda -> unit +val lambda_as_module : Env.t -> Types.signature -> string -> Lambda.lambda -> unit diff --git a/jscomp/lam_register.ml b/jscomp/lam_register.ml index 562a28340b..dccff9cf9b 100644 --- a/jscomp/lam_register.ml +++ b/jscomp/lam_register.ml @@ -1,3 +1,3 @@ let () = - Printlambda.serialize_raw_js := Lam_compile_group.lambda_as_module true; + Printlambda.serialize_raw_js := Lam_compile_group.lambda_as_module; ;; diff --git a/jscomp/lam_register.mli b/jscomp/lam_register.mli new file mode 100644 index 0000000000..43bee504f9 --- /dev/null +++ b/jscomp/lam_register.mli @@ -0,0 +1 @@ +(** *)