-
Notifications
You must be signed in to change notification settings - Fork 456
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
ffi (calling js from ocaml) #101
Comments
@jordwalke what do you think? |
What about combining efforts so that the same binding specification can be used through bucklescript and through js_of_ocaml. gen_js_api ( https://github.com/LexiFi/gen_js_api ) was designed to be rather independant of "how OCaml is compiled to JS", and it could serve as a good basis for discussion. I don't see immediately any problem to have gen_js_api support two variants of its code generation to support both js_of_ocaml and buckescript. It would be great to be able to write OCaml projects than can be compiled with any of the 2 compilers, without having to do the glue with JS libraries twice. |
@alainfrisch I am not very clear how js_of_ocaml ffi really works (cc @hhugo), from the user's point of view, I think js_of_ocaml ffi is a bit complicated (require non-trival ppx ) and the type signature is hard to read even for experienced ocaml programmers. Here we want to use js object just as normal ocaml objects, but you can not inherit it. If I understand correctly, |
I think the core question is how to specify how JS components are reflected in OCaml, i.e. the design of the FFI specification. It's not so much a question of runtime representation; except for the Ojs module, gen_js_api itself does not know how js_of_ocaml represents OCaml values. Btw, gen_js_api is independant of js_of_ocaml "native FFI" (which is tightly coupled to the way js_of_ocaml represents OCaml values in JS). |
@alainfrisch maybe we can formalize how to use js attributes? for js_of_ocaml user, they can run the note that we can refer other js api in module style external f : int -> int = ""
[@@js.call "ffi_from_js"]
[@@js.module "file_name.js" "the_opt_js_module_name"] @fxfactorial, based on your experience of writing bindings to ocaml-nodejs, does this fit your purpose? |
yes, that would be very nice. What would JS constructors look like? |
What's the difference between |
@copy @fxfactorial external new_obj : arg_type -> obj_type = "" [@@js.new "js_constructor_name"] |
@bobzhang and subclassing? Say like Node's streaming duplex, they were a little challenging for me to play with type system. What is the empty string, some minimal hack needed to satisfy the syntax of external declarations? EDIT: nvm, looks like you can't subclass yet, just saw that above. |
The core principle of gen_js_api is to derive implementation of glue code (.ml files) from plain OCaml interfaces (.mli files) annotated with custom js attributes (to specify how the binding is done). The generated glue code depends on a runtime support library ( The version of |
Here is the interface of https://github.com/LexiFi/gen_js_api/blob/master/ojs.mli Do you think it could be implemented for bucklescript? |
@alainfrisch Have a cursory look of the interface, I think it should be fine, I will look into more details about the design of |
For the record, we can do the same thing with ocaml object (for better performance when needed), |
we can introduce a ppx extension for type definition class type dom = object%js
method getElementById : name -> x
end will generate class type dom = object
method getElementById : name -> x
method getElementById__js_01 : name -> x
end |
see #174 |
…ng#101) When a comma is missing between nodes in a region and the current token looks like the start of something valid in the current region, we should report an error with regards to the missing comma. Example: type student<'extraInfo> = { name: string, age: int otherInfo: 'extraInfo } There is a missing comma between `int` and `otherInfo`. `otherInfo` looks like a valid start of the record declaration. We report the error here and then continue parsing the region. Fixes rescript-lang/syntax#100
note that call ocaml from js is very easy, and there is not much work to do. (see the runtime representation #24), we also plan to use some
ppx_derivings
to hide the internal representation.calling js from ocaml is a bit hard, since you have to write type declarations, for binding simple functions
for binding js objects we can do this
To make it more efficient user can write bindings
Note that
document#getElementById_js_01
anddocument#getElementByID
compiles to js code which behaves exactly except that the former will be as efficient as handwritten js codeThe text was updated successfully, but these errors were encountered: