Skip to content

ffi (calling js from ocaml) #101

Closed
Closed
@bobzhang

Description

@bobzhang

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

external array_map : 'a array -> ('a -> 'b) -> 'b array = "" [@@js.call "Array.prototype.map.call"]

for binding js objects we can do this

class type dom = object
   method getElementById : string -> node
end
external document : dom = ""[@@js.global "document"]

let f v = document#getElementById v

To make it more efficient user can write bindings

class type dom = object
   method getElementById : string -> node
   method getElementById_js_01 : string -> node
   method properties_js_set : string -> node
end
external document : dom = ""[@@js.global "document"]

let f v = document#getElementById_js_01 v 

Note that document#getElementById_js_01 and document#getElementByID compiles to js code which behaves exactly except that the former will be as efficient as handwritten js code

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions