Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Kenichi Asai <asai@is.ocha.ac.jp>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: [Caml-list] jsoo toplevel with ppx_deriving.show support?
Date: Fri, 8 Dec 2023 08:36:23 +0000	[thread overview]
Message-ID: <ZXLVh9J9LLXH7W5H@pllab.is.ocha.ac.jp> (raw)

[-- 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 --]

                 reply	other threads:[~2023-12-08  8:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZXLVh9J9LLXH7W5H@pllab.is.ocha.ac.jp \
    --to=asai@is.ocha.ac.jp \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox