ocsigen / js_of_ocaml

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

[BUG] Using two Js_of_ocaml compiled libraries in the same runtime causes issues with js stubs #1622

Open troederjs opened 3 months ago

troederjs commented 3 months ago

Describe the bug If two separate ocaml libraries have JS stubs which define the same symbol ion joo_global_object (e.g. two different versions of src-bindings/node/dune in the OCaml VSCode extension) they will conflict when both libraries are loaded by different Js_of_ocaml executable.

This happens because joo_global_object is the globalThis, and so the two values override each other.

Normally, when running a single application this is not a problem since we can control all the dependencies and ensure they don't conflict. But specifically in the case of the VSCode extension, this causes issues when there are two Js_of_ocaml extensions that use incompatible versions of the nodejs binding stubs.

Expected behavior Both Js_of_ocaml executable should be able to run independently in the same JS environment.

hhugo commented 3 months ago

First note that joo_global_object should be avoided, support will be removed at some point. One should use globalThis instead.

It seems to me that vscode-ocaml-platform is part of the problem here. It should not update the global object,it should defined its own stubs.

troederjs commented 3 months ago

What is the correct way to access a JS stub without making it available on some global context like globalThis?

hhugo commented 3 months ago

https://ocsigen.org/js_of_ocaml/latest/manual/linker gives some information.

In short,


//Provides: customAdd
function customAdd(a,b){
  return a + b
}

 external add : int -> int -> int = "customAdd"