js_of_ocaml
js_of_ocaml copied to clipboard
Using js_of_ocaml generated code in node top level alters node's default error handling
Consider a blank a.ml
let _ = 1
Now, in node, an error normally allows us to resume, putting us back at the prompt:
$ node
Welcome to Node.js v18.0.0.
Type ".help" for more information.
> b
Uncaught ReferenceError: b is not defined
>
But after requiring our new a.js operation, an error completely unrelated to the js_of_ocaml code causes the whole node process to exit:
$ node
Welcome to Node.js v18.0.0.
Type ".help" for more information.
> const a = require('./a.js');
undefined
> b
/Users/john/Desktop/jsootest/a.js:300
throw b}function
^
[ReferenceError: b is not defined]
$
Is this expected? I should say this also happens with my real project, which has an exports.ml like this:
open Js_of_ocaml
let _ =
Js.export "cpdflib"
(object%js
(* CHAPTER 0. Preliminaries *)
method getLastError = Cpdflib.getLastError ()
method getLastErrorString = Cpdflib.getLastErrorString ()
......
I understood that would make it usable from node via require. It is usable, but it has this odd error behaviour.
I'm using js_of_ocaml 4.0.0 and node 18.0.0.
It is expected and due to https://github.com/ocsigen/js_of_ocaml/blob/d7117ff551015fc27b583fe67f54a61c2950354f/runtime/sys.js#L338. Maybe we should only have that logic turned on for standalone programs.
Thanks for the explanation. Yes, it would be a useful option.
I'm very new to JavaScript, so I don't know how common it is to use the REPL, or whether libraries commonly have non-modular effects like this.