ocsigen / js_of_ocaml

Compiler from OCaml to Javascript.
http://ocsigen.org/js_of_ocaml/
Other
951 stars 185 forks source link

Using js_of_ocaml generated code in node top level alters node's default error handling #1277

Open johnwhitington opened 2 years ago

johnwhitington commented 2 years ago

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.

hhugo commented 2 years ago

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.

johnwhitington commented 2 years ago

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.