我有这个 js_of_ocaml 代码,它接受一个字符串并执行它(假设字符串是 OCaml 代码):
let () = JsooTop.initialize ()
let execute code =
let code = Js.to_string code in
let buffer = Buffer.create 100 in
let formatter = Format.formatter_of_buffer buffer in
JsooTop.execute true formatter code;
Js.string (Buffer.contents buffer)
(* Usage: *)
let () =
let ret_val_1 = execute "let x = 5;;" in
let ret_val_2 = execute "print_int (x * x);;" in (* 25 appears in the browser console. *)
print_endline ret_val_1; (* Prints: "val x : int = 5" *)
print_endline ret_val_2 (* Prints: "- : unit = ()" *)
25
代码运行后出现在浏览器控制台中,是因为该print_int (x * x)
行。我想25
在 DOM 中使用输出(即),而不是让它出现在浏览器控制台中。如何捕获 js_of_ocaml 的打印功能产生的标准输出?即我怎样才能25
从上面的代码中得到?