(executables
 (names test_toplevel)
 (libraries js_of_ocaml-compiler.dynlink compiler-libs.toplevel)
 (flags
  (:standard -linkall))
 (js_of_ocaml
  (flags :standard --toplevel))
 (modes byte js))

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

(rule
 (target test_toplevel.bc.js.actual)
 (enabled_if
  (>= %{ocaml_version} 5.2))
 (action
  (with-stdout-to
   %{target}
   (run node %{dep:test_toplevel.bc.js}))))

(rule
 (target test_toplevel.js.actual)
 (enabled_if
  (>= %{ocaml_version} 5.2))
 (action
  (with-stdout-to
   %{target}
   (run node %{dep:test_toplevel.js}))))

(rule
 (alias runtest-js)
 (enabled_if
  (>= %{ocaml_version} 5.4))
 (action
  (progn
   (diff test_toplevel.expected test_toplevel.bc.js.actual)
   (diff test_toplevel.expected test_toplevel.js.actual))))

(executables
 (names test_toplevel_wasm)
 (libraries wasm_of_ocaml-compiler.dynlink compiler-libs.toplevel)
 (flags
  (:standard -linkall))
 (wasm_of_ocaml
  (flags :standard --toplevel))
 (modes byte wasm))

(rule
 (target test_toplevel_wasm.ml)
 (action
  (copy %{dep:test_toplevel.ml} %{target})))

(rule
 (targets
  test_toplevel_wasm.js
  (dir test_toplevel_wasm.assets))
 (action
  (run
   %{bin:wasm_of_ocaml}
   --pretty
   --toplevel
   -w
   no-missing-effects-backend
   %{dep:test_toplevel_wasm.bc}
   -o
   test_toplevel_wasm.js)))

(rule
 (target test_toplevel_wasm.bc.wasm.js.actual)
 (enabled_if
  (and
   %{env:WASM_OF_OCAML=false}
   (<> %{profile} wasi)
   (<> %{profile} wasi-with-native-effects)
   (>= %{ocaml_version} 5.4)))
 (action
  (with-stdout-to
   %{target}
   (run node %{dep:test_toplevel_wasm.bc.wasm.js}))))

(rule
 (target test_toplevel_wasm.js.actual)
 (enabled_if
  (>= %{ocaml_version} 5.4))
 (deps
  (glob_files test_toplevel_wasm.assets/*))
 (action
  (with-stdout-to
   %{target}
   (run node %{dep:test_toplevel.js}))))

(rule
 (alias runtest-wasm)
 (enabled_if
  (and
   %{env:WASM_OF_OCAML=false}
   (<> %{profile} wasi)
   (<> %{profile} wasi-with-native-effects)
   (>= %{ocaml_version} 5.4)))
 (action
  (progn
   (diff test_toplevel.expected test_toplevel_wasm.bc.wasm.js.actual)
   (diff test_toplevel.expected test_toplevel_wasm.js.actual))))

(rule
 (alias runtest-js)
 (enabled_if
  (and
   (>= %{ocaml_version} 5.2)
   (< %{ocaml_version} 5.3)))
 (action
  (progn
   (diff test_toplevel-5.2.expected test_toplevel.bc.js.actual)
   (diff test_toplevel-5.2.expected test_toplevel.js.actual))))
