(executables
 (names test_toplevel1 test_toplevel2)
 (libraries js_of_ocaml js_of_ocaml_toplevel re)
 (flags
  (:standard -linkall))
 (modes byte))

(rule
 (targets export.txt)
 (deps
  (package js_of_ocaml-toplevel))
 (action
  (run
   jsoo_listunits
   -o
   %{targets}
   stdlib
   js_of_ocaml-toplevel
   js_of_ocaml-toplevel.common
   re)))

(rule
 (target re.cmis.js)
 (action
  (run jsoo_mkcmis -o %{target} re)))

(rule
 (target stdlib.cmis.js)
 (action
  (run jsoo_mkcmis -o %{target} stdlib)))

(rule
 (targets test_toplevel1.js)
 (action
  (run
   %{bin:js_of_ocaml}
   --export
   %{dep:export.txt}
   --toplevel
   --disable
   shortvar
   -w
   no-missing-effects-backend
   %{dep:test_toplevel1.bc}
   -o
   %{targets})))

(rule
 (targets test_toplevel2.js)
 (action
  (run
   %{bin:js_of_ocaml}
   --toplevel
   --no-cmis
   --disable
   shortvar
   -w
   no-missing-effects-backend
   %{dep:test_toplevel2.bc}
   -o
   %{targets})))

(rule
 (target test_toplevel1.actual)
 (deps test_toplevel1.js re.cmis.js)
 (action
  (with-stdout-to
   %{target}
   (run node ./test_toplevel1.js))))

; test_toplevel2 loads CMI bundles via CommonJS `require("./stdlib.cmis.js")`,
; which the QuickJS shim does not implement.

(rule
 (target test_toplevel2.actual)
 (enabled_if
  (<> %{profile} quickjs))
 (deps test_toplevel2.js stdlib.cmis.js re.cmis.js)
 (action
  (with-stdout-to
   %{target}
   (run node ./test_toplevel2.js))))

(rule
 (alias runtest-js)
 (action
  (diff test_toplevel.expected test_toplevel1.actual)))

(rule
 (alias runtest-js)
 (enabled_if
  (<> %{profile} quickjs))
 (action
  (diff test_toplevel.expected test_toplevel2.actual)))

(rule
 (alias runtest)
 (action
  (run jsoo_mkcmis -o a.js --no-runtime stdlib)))
