@@ -5,26 +5,24 @@ extensions to the OCaml language. These BuckleScript extensions
55facilitate the integration of native JavaScript code and
66improve the generated code.
77
8- Like TypeScript, when building type-safe bindings from JS to OCaml, the
9- user has to write type declarations.
10- In OCaml, unlike TypeScript, user does not need to create a separate
8+ Like TypeScript, when building type-safe bindings from JS to OCaml,
9+ users have to write type declarations.
10+ In OCaml, unlike TypeScript, users do not need to create a separate
1111`.d.ts` file,
12- since the type declaration also written in OCaml.
12+ since the type declarations is an integral part of OCaml.
1313
1414The FFI is divided into several components:
1515
16- - Binding to JS first-order functions
17- - Binding to JS high-order functions
18- - Binding to JS object literals
19- - Binding to JS classes
20- - Extensions to the language for debugger, regex and embedding raw JS
16+ - Binding to simple functions and values
17+ - Binding to high-order functions
18+ - Binding to object literals
19+ - Binding to classes
20+ - Extensions to the language for debugger, regex and embedding arbitrary JS
2121code
2222
2323### FFI to simple JS functions and supported attributes
2424
25- This part is similar to
26- http://caml.inria.fr/pub/docs/manual-ocaml-4.02/intfc.html[traditional
27- FFI],
25+ This part is similar to http://caml.inria.fr/pub/docs/manual-ocaml-4.02/intfc.html[traditional FFI],
2826with syntax as described below:
2927
3028[source,ocaml]
@@ -36,29 +34,44 @@ external-declaration := string-literal
3634Users need to declare types for foreign functions (JS functions here)
3735and provide customized `attributes` .
3836
39- #### ` bs.val`
37+ #### Binding to global value: bs.val
4038
4139[source,ocaml]
4240---------------
4341external imul : int -> int -> int = "Math.imul" [@@bs.val]
42+ type dom
43+ (* Abstract type for the DOM *)
44+ external dom : dom = "document" [@@bs.val]
4445---------------
4546
47+ `bs.val` attribute is used to bind to a JavaScript value,
48+ it can be a function or plain value.
49+
50+
4651
4752[NOTE]
4853=====
49- If you want to make a single FFI for both C functions and
54+ * If `external-declaration` is the same as `value-name`, user can leave `external-declaration` empty,
55+ for example:
56+ +
57+ [source,ocaml]
58+ -------------
59+ external document : dom = "" [@@bs.val]
60+ -------------
61+
62+ * If you want to make a single FFI for both C functions and
5063JavaScript functions, you can
5164give the JavaScript foreign function a different name:
52-
65+ +
5366[source,ocaml]
5467---------------
5568external imul : int -> int -> int = "c_imul" [@@bs.val "Math.imul"]
5669---------------
5770=====
5871
59- #### ` bs.new`
72+ #### Binding to JavaScript constructor: bs.new
6073
61- This attribute is used to create a JavaScript object.
74+ `bs.new` is used to create a JavaScript object.
6275
6376[source,ocaml]
6477----------
@@ -71,22 +84,58 @@ Output:
7184var date = new Date();
7285----------
7386
74- #### `bs.val`
7587
76- This attribute is used to bind to a JavaScript value
7788
89+ #### Binding to a value from a module: bs.module
90+
91+ Input:
7892[source,ocaml]
79- ----------
80- type dom
81- (* Abstract type for the DOM *)
82- external dom : dom = "document" [@@bs.val]
83- let dom = dom
84- ----------
93+ --------
94+ external add : int -> int -> int = "add" [@@bs.module "x"]
95+ external add2 : int -> int -> int = "add2"[@@bs.module "y", "U"] // <1>
96+ let f = add 3 4
97+ let g = add2 3 4
98+ --------
99+ <1> "U" will hint the compiler to generate a better name for the module, see output
100+
85101Output:
86102[source,js]
87- ----------
88- var dom = document
89- ----------
103+ -----------
104+ var U = require("y");
105+ var X = require("x");
106+ var f = X.add(3, 4);
107+ var g = U.add2(3, 4);
108+ -----------
109+
110+ [NOTE]
111+ ======
112+ * if `external-declaration` is the same as value-name, it can be left empty, for example,
113+ +
114+ [source,ocaml]
115+ --------------
116+ external add : int -> int -> int = "" [@@bs.module "x"]
117+ --------------
118+
119+ ======
120+
121+ #### Binding the whole module as a value or function
122+
123+ [source,ocaml]
124+ --------------
125+ type http
126+ external http : http = "http" [@@bs.module] // <1>
127+ --------------
128+ <1> `external-declaration` is the module name
129+
130+ [NOTE]
131+ ======
132+ * if `external-declaration` is the same as value-name, it can be left empty, for example,
133+ +
134+ [source,ocaml]
135+ --------------
136+ external http : http = "" [@@bs.module]
137+ --------------
138+ ======
90139
91140
92141#### `bs.send`
@@ -137,40 +186,6 @@ module Int32Array = struct
137186end
138187--------
139188
140- #### `bs.module`
141-
142- Qualify the JavaScript value by a module name
143-
144- Input:
145- [source,ocaml]
146- --------
147- external add : int -> int -> int = "add"
148- [@@bs.val] [@@bs.module "x"]
149- let f = add 3 4
150- --------
151-
152- Output:
153- [source,js]
154- -----------
155- var X = require("x")
156- var f = X.add(3,4)
157- -----------
158-
159- Input:
160- [source,ocaml]
161- --------
162- external add : int -> int -> int = "add"
163- [@@bs.val] [@@bs.module "x" "U"]
164- let f = add 3 4
165- --------
166-
167- Output:
168- [source,js]
169- -----------
170- var U = require("x")
171- var f = U.add(3,4)
172- -----------
173-
174189
175190### FFI to high-order JS functions
176191
0 commit comments