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

Add require attribute. #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Add require attribute. #68

wants to merge 1 commit into from

Conversation

Aaylor
Copy link

@Aaylor Aaylor commented Dec 2, 2016

All information about how works the require attribute are in the document REQUIRE.md; it contains three kind of usage, and the meaning of each automatic rules when under the require context.

Related to the discussion #66.

```ocaml
type t = private Ojs.t

val module_t : t Lazy.t [@@js.require "myModule"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this work as well with a non-lazy binding?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will not work with non-lazy bindings yet. I force the use of lazyness to avoid requiring javascript module when they're not necessary.

But i can support both version (one lazy, and one non-lazy) if needed.

@dannywillems
Copy link

dannywillems commented Dec 5, 2016

@Aaylor I didn't read the PR, but is require returning functions (like nightmare, see this partial binding to Node with gen_js_api) supported?

@Aaylor
Copy link
Author

Aaylor commented Dec 5, 2016

Actually it's not supported, but it's just a simple case to add; but it will need a relaxed type for the require function (which is actually string -> Ojs.t).

A first solution would be to declare the require function as unsafe, and give it the type string -> 'a
The second solution should be to split in two part: a safe require and the unsafe one; and detect when the unsafe one should be called.

With one of the two solution above, we would be able to write:

val my_fun_module : (t1 -> ... -> tn) Lazy.t [@@js.require "myModule"]

@Aaylor
Copy link
Author

Aaylor commented Dec 10, 2016

@dannywillems Well infact, with this version, it's possible to have function but it's not convenient. Requiring a function could be written as

val my_fun_module : Ojs.t Lazy.t [@@js.require "myModule"]

and then used as following:

let _ =
  let my_fun_m = Lazy.force my_fun_module in
  let my_result_ojs = Ojs.apply my_fun_m [| (* ... *) |] in
  (* ... *)

I'm not sure that this is a philosophy of gen_js_api to give an unsafe function as I proposed earlier, @alainfrisch ? Is this a better thing to have an unsafe_require with type string -> 'a, or to avoid it ?

| _ -> Some (id_of_expr (expr_of_payload k.loc v))
in
require, require_ctx || require <> None

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure it's a good idea that this function returns a pair, since the second projection can be simply obtained from the first component and the require_ctx argument. My guess is that it would simplify the code a bit to move the corresponding logic at call sites (when needed).

@sbriais
Copy link
Collaborator

sbriais commented May 10, 2017

Could you please provide a full example (if I am not mistaken, the js part is missing)?
Ideally, a concrete example would even be better...

@Lupus
Copy link

Lupus commented Jul 11, 2019

Sad to see this abandoned. Just stumbled across this problem when trying to bind some nodejs to jsoo with gen_js_api myself and hand to resort to defining globals and using @@js.global "...." annotation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants