Skip to content

Latest commit

 

History

History
26 lines (19 loc) · 2.31 KB

interop.md

File metadata and controls

26 lines (19 loc) · 2.31 KB

js_of_ocaml: Interop with JavaScript

There are three approaches to interop with JavaScript code in Js_of_ocaml:

  1. Use the functions available in the Js module in order to translate the data inside Reason/OCaml files, so it can be used from JavaScript.
  2. Use OCaml external statements to define the types, and then do the transformations on the JavaScript side. External functions are the same way that OCaml offers to interop with C code as well.
  3. Use gen_js_api and create interface files so that the tool generates all the mapping code for you :)

The approach being used in jsoo-react is the third, as it automates all the conversions needed and doesn't require to think about them

gen_js_api: Comparison with BuckleScript

Some notes on how interop in BuckleScript and gen_js_api relate:

Feature BuckleScript interop gen_js_api interface files
Use JS data types array, string No work needed, conversions will be added in generated code automatically
Require JS modules [@bs.module] Add require("my-lib") statement in .js file (example), include it in Dune (example)
Bind to JS values [@bs.val] [@js.global]
Variadic params [@bs.splice] [@js.variadic]
Raw expressions [%bs.raw] Js.Unsafe.js_expr();
Create JS objects [%bs.obj] [@js.builder]
Access JS objects properties ## ##.
Nullable values Js.nullable No work needed, conversions will be added in generated code automatically

For more information about gen_js_api, check the project documentation.