* [Caml-list] jsoo toplevel with ppx_deriving.show support?
@ 2023-12-08 8:36 Kenichi Asai
0 siblings, 0 replies; only message in thread
From: Kenichi Asai @ 2023-12-08 8:36 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 766 bytes --]
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
[-- Attachment #2: dune --]
[-- Type: text/plain, Size: 704 bytes --]
(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))
[-- Attachment #3: dune-project --]
[-- Type: text/plain, Size: 17 bytes --]
(lang dune 1.2)
[-- Attachment #4: eval.ml --]
[-- Type: text/plain, Size: 1459 bytes --]
(* 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)
[-- Attachment #5: index.html --]
[-- Type: text/html, Size: 477 bytes --]
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-12-08 8:36 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-08 8:36 [Caml-list] jsoo toplevel with ppx_deriving.show support? Kenichi Asai
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox