@@ -167,6 +167,39 @@ let get_value lid ty =
167
167
else
168
168
failwith (Format. asprintf " Wrong type %a." Printtyp. type_sch val_type)
169
169
170
+ (* Replacement for [Toploop.print_value] that doesn't segfault on yet
171
+ unregistered extension constructors.
172
+
173
+ Note: re-instanciating [Genprintval.Make] means we lose any previously
174
+ defined printers through [Topdirs.dir_install_printer]. *)
175
+ let base_print_value, install_printer =
176
+ let module Printer = Genprintval. Make (Obj )(struct
177
+ type valu = Obj .t
178
+ exception Error
179
+ let eval_address = function
180
+ | Env. Aident id ->
181
+ if Ident. persistent id || Ident. global id then
182
+ Symtable. get_global_value id
183
+ else begin
184
+ let name = Translmod. toplevel_name id in
185
+ try Toploop. getvalue name
186
+ with _ -> raise Error
187
+ end
188
+ | Env. Adot (_ , _ ) ->
189
+ (* in this case we bail out because this may refer to a
190
+ yet-unregistered extension constructor within the current module.
191
+ The printer has a reasonable fallback. *)
192
+ raise Error
193
+ let same_value v1 v2 = (v1 == v2)
194
+ end )
195
+ in
196
+ let print_value env obj ppf ty =
197
+ ! Oprint. out_value ppf @@
198
+ Printer. outval_of_value 300 100 (fun _ _ _ -> None ) env obj ty
199
+ in
200
+ let install_printer pr = Printer. install_printer pr in
201
+ print_value, install_printer
202
+
170
203
let print_value ppf v ty =
171
204
let { Typedtree. ctyp_type = ty; _ } =
172
205
Typetexp. transl_type_scheme ! Toploop. toplevel_env (Ty. obj ty) in
@@ -192,17 +225,17 @@ let print_value ppf v ty =
192
225
done )
193
226
(fun () -> () ) in
194
227
begin try
195
- Toploop. print_value ! Toploop. toplevel_env (Obj. repr v) tmp_ppf ty ;
228
+ base_print_value ! Toploop. toplevel_env (Obj. repr v) tmp_ppf ty ;
196
229
Format. pp_print_flush tmp_ppf ()
197
230
with Exit -> () end ;
198
231
match ! state with `Start | `Decided false | `Undecided -> false | `Decided true -> true in
199
232
if needs_parentheses then begin
200
233
Format. fprintf ppf " @[<hv 1>(" ;
201
- Toploop. print_value ! Toploop. toplevel_env (Obj. repr v) ppf ty ;
234
+ base_print_value ! Toploop. toplevel_env (Obj. repr v) ppf ty ;
202
235
Format. fprintf ppf " )@]"
203
236
end else begin
204
237
Format. fprintf ppf " @[<hv 0>" ;
205
- Toploop. print_value ! Toploop. toplevel_env (Obj. repr v) ppf ty ;
238
+ base_print_value ! Toploop. toplevel_env (Obj. repr v) ppf ty ;
206
239
Format. fprintf ppf " @]"
207
240
end
208
241
@@ -414,6 +447,7 @@ let allow_introspection ~divert =
414
447
stderr_cb := bad_stderr_cb ;
415
448
res
416
449
450
+ let install_printer pr = install_printer pr
417
451
let get_printer ty = fun ppf v -> print_value ppf v ty
418
452
419
453
let register_sampler name f = register_sampler name f
0 commit comments