jsoo toplevel with ppx_deriving.show support?

Kenichi Asai <[email protected]>
Newsgroups gmane.comp.lang.caml.inria
Message-ID <[email protected]>
I want to have an OCaml toplevel in a browser having ppx_deriving.show
support.  I tried to include "ppx_deriving.show" as in the attached
dune file, but the resulting toplevel does not appear to understand
[@@deriving show] directive.

With the attached files, I can at least print a message in a browser:

- create a directory
- save the attached four files
- dune build
- open index.html

In index.html, I run the toplevel with

print_endline "hello";;

and 

type test_t = {x:int}[@@deriving show];;
print_endline (show_test_t {x=1});;

The former succeeds, but the latter produces:

Error: Unbound value show_test_t

How can I create an OCaml toplevel that produces

{ x = 1 }
- : unit = ()

for the latter?  Thank you in advance!

Sincerely,

-- 
Kenichi Asai
dune (text/plain, 673 B)
(executables
  (names eval)
  (libraries
    ppx_deriving.show
    js_of_ocaml-compiler
    js_of_ocaml-toplevel)
  (link_flags (:standard -linkall))
  (preprocess (pps js_of_ocaml-ppx ppx_deriving.show)))

(rule
 (targets export.txt)
 (deps eval.bc)
 (action (run jsoo_listunits -o %{targets} stdlib)))

(rule
 (targets eval.js)
 (action
    (run %{bin:js_of_ocaml}
	  --export %{dep:export.txt}
          --toplevel
          --noruntime
	  %{lib:js_of_ocaml-compiler:runtime.js}
	  %{lib:js_of_ocaml-compiler:toplevel.js}
          %{lib:js_of_ocaml-compiler:dynlink.js}
          %{dep:eval.bc}
	  -o %{targets}
          )))

(alias
  (name default)
  (deps eval.js))
dune-project (text/plain, 16 B)
(lang dune 1.2)
eval.ml (text/plain, 1.4 KB)
(* see: https://khoanguyen.me/sketch/part-2-the-engine/ *)

open Js_of_ocaml_toplevel
open Js_of_ocaml

let execute code =
  let code = Js_of_ocaml.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_of_ocaml.Js.string (Buffer.contents buffer)

let append_string output cl s =
  let open Js_of_ocaml in
  let d = Dom_html.window##.document in
  let span = Dom_html.createDiv d in
  span##.classList##add (Js.string cl);
  Dom.appendChild span (d##createTextNode (Js.string s));
  Dom.appendChild output span

let ocamlInitProgram =
"let _my_printer_ ppf = Format.fprintf ppf \"\\\"%s\\\"\";;
#install_printer _my_printer_;;
"

let runCode str =
  let toploop_ = open_out "/dev/null" in
  let toploop_ppf = Format.formatter_of_out_channel toploop_ in
  JsooTop.initialize ();
  let dom = Dom_html.getElementById "toplevel" in
  Sys_js.set_channel_flusher stdout (append_string dom "stdout");
  Sys_js.set_channel_flusher stderr (append_string dom "stderr");
  let _ret = JsooTop.execute true toploop_ppf ocamlInitProgram in
  Sys_js.set_channel_flusher toploop_ (append_string dom "toploop");
  let txt = Js.to_string str in
  let _ret = JsooTop.execute true toploop_ppf txt in
  ()

let () =
  JsooTop.initialize ();
  Js_of_ocaml.Js.export
    "evaluator"
    (object%js
       val runCode = runCode
    end)
index.html (text/html, 461 B)
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="_build/default/eval.js"></script>
  </head>
  <body>
    <div id="toplevel"></div>
    <script type="text/javascript">
      evaluator.runCode('print_endline "hello";;\n');
    </script>
    <script type="text/javascript">
      evaluator.runCode('type test_t = {x:int}[@@deriving show];; print_endline (show_test_t {x=1});;\n');
    </script>
  </body>
</html>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.