ocaml-multicore / eio_js

Eio for JavaScript environments
ISC License
9 stars 3 forks source link

question: can `eio_js_backend.Start` provide env? #3

Open jetjinser opened 3 months ago

jetjinser commented 3 months ago

I've just started using OCaml and eio, and what I'm working on is making a library that uses eio_main be compiled into JavaScript that is nodejs callable via jsoo.

As far as I understand, functions in Eio.Stdenv will only access the capabilities in env that they need. Does this mean that only part of the env need to be implemented?

For example, in nodejs, there is the fs api, so is it possible to make functions like Eio.Stdenv.cwd work?

Another little question is, if I want to construct an env myself, is it difficult? How can I do it?

talex5 commented 3 months ago

Yes, env is just a (polymorphic) record, so you can create your own with whatever fields you want:

let env =
  object
    method cwd = ...
  end
in
...

For example, here is Eio_mock.Backend creating a mock environment: https://github.com/ocaml-multicore/eio/blob/33d4e01a30aae0ae55747e29c1fc4ea6e741571b/lib_eio/mock/backend.ml#L55-L60

Note that libraries don't normally use eio_main (only the application should use that).

jetjinser commented 3 months ago

Thanks!

Note that libraries don't normally use eio_main (only the application should use that).

Yes, you are right, I was wrong. The part using eio_main is an application. In fact, it is a project including bin and lib, and the lib part uses Eio_main.Stdenv.base.

Another thing I want to ask is, the reason why the env of Eio_js_backend.start is unit is that the compiled JavaScript may be used in the browser or node/bun/anything, and the api of each is different? Should this be provided by some kind of eio_node_backend?

talex5 commented 3 months ago

Another thing I want to ask is, the reason why the env of Eio_js_backend.start is unit is that the compiled JavaScript may be used in the browser or node/bun/anything, and the api of each is different? Should this be provided by some kind of eio_node_backend?

I don't know (I didn't write eio_js), but that sounds like a reasonable thing to do.