Like magic! But I had to comment out the parts of my code which use external C code of course - that is zlib and some encryption primitives. So now I wish to bind javascript libraries for those. I am experimenting with a simple library of my own, first, which is given on the command line to js_of_ocaml as foomod.js:
foo = 42;
I can get to this global variable easily from OCaml:
let foo = Js.Unsafe.global##.foo
But now I want to do things better, and I change foomod.js to:
exports.foo = 42;
How can I get to that? Giving foomod.js on the js_of_ocaml command line includes the contents of foomod.js in some way, but does not contain the string foomod, so I’m not sure how to get to the foomod’s variables and functions. How to I access them? In the node REPL, I can simply do:
Thanks, Nicolas. I think this is enough to get me started.
I would still be interested in the namespacing issue though. What happens if foo.js and bar.js, both given on the command line, both define a global x?
I am making progress, having successfully linked in the sjcl encryption primitives library by appending the following line to its .js source:
joo_global_object.sjcl = sjcl;
But now I am trying to do the same with pako, a zlib replacement. And I get:
ReferenceError: pako is not defined
at /Users/john/repos/cpdf.js/cpdf.js:352:520
at Object.<anonymous> (/Users/john/repos/cpdf.js/cpdf.js:352:525)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
It seems to work with
joo_global_object.pako = globalThis.pako;
But js_of_ocaml can’t find it. The beginning of pako.js has this, seemingly common JavaScript preamble: