Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to export a value? #60

Open
francoisthire opened this issue Jun 5, 2024 · 7 comments
Open

How to export a value? #60

francoisthire opened this issue Jun 5, 2024 · 7 comments
Labels
question Further information is requested

Comments

@francoisthire
Copy link

I am writing a VScode extension using the Brr library.

I don't have a minimal reproducible example but here is my snippet:

 let activate =
    Jv.callback ~arity:1 (fun context ->
        Console.log "Congratulations, your extension for kalkulu is now active!";
        let callback () = Vscode.Window.show "Hello world!" in
        let disposable =
          Vscode.Command.register ~command:"extension.helloWorld" ~callback
        in
        Vscode.Extension_context.subscribe context disposable)
  in
  (* Js.export "activate" @@ activate *)
  Jv.set Jv.global "activate" activate

If I uncomment the Js.export line, it works, but the Jv.set does not.

I am not sure to understand what is going on. I observe the compiled JS is a little bit different. In particular Js.export uses "jsoo_exports` value.

I have tried to read the FFI cookbook, but I did not find an explanation. Is this expected? Why?

@dbuenzli dbuenzli added the question Further information is requested label Jun 5, 2024
@dbuenzli
Copy link
Owner

dbuenzli commented Jun 5, 2024

It's not clear what "works" mean. I guess it depends a bit when activate needs to be available. Perhaps the extension needs to have it before the OCaml module initialisation code is executed ?

@francoisthire
Copy link
Author

What VScode prompts when I use the Jv.set approach is similar to a not found error. Somehow it does not find the activate function.

I am not sure how I can debug that, the generated code is complex. What changes seems to be the following lines:

With Js.export:

 activate =
    (* ... *)
   caml_call1(caml_call1(Js_of_ocaml_Js[50], "activate"), activate);
   var Dune_exe_Main = [0];
   runtime.caml_register_global(16, Dune_exe_Main, "Dune__exe__Main");
   return;
  }
  (globalThis));

While with the Jv.set approach:

  activate =
     (* ... *)
   Jv[12]["activate"] = activate;
   var Dune_exe_Main = [0];
   runtime.caml_register_global(15, Dune_exe_Main, "Dune__exe__Main");
   return;
  }
  (globalThis));

@dbuenzli
Copy link
Owner

dbuenzli commented Jun 5, 2024

Perhaps @hhugo can shed a light ?

@dbuenzli
Copy link
Owner

dbuenzli commented Jun 5, 2024

For reference this is how Jv.global is defined

@dbuenzli
Copy link
Owner

dbuenzli commented Jun 5, 2024

Could you perhaps try to rename the activate OCaml value to something else.

@francoisthire
Copy link
Author

I have tried to rename the OCaml value but I get the same error.

@dbuenzli
Copy link
Owner

dbuenzli commented Sep 6, 2024

In eeeff51 I tried to export a function as mentioned in the cookbook and it works in FF, Safari and Chrome. So I'm not sure exactly why the Js.export is needed.

I tentatively try to ping @hhugo again if he can maybe shed some light.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants