@@ -5,26 +5,24 @@ extensions to the OCaml language. These BuckleScript extensions
5
5
facilitate the integration of native JavaScript code and
6
6
improve the generated code.
7
7
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
11
11
`.d.ts` file,
12
- since the type declaration also written in OCaml.
12
+ since the type declarations is an integral part of OCaml.
13
13
14
14
The FFI is divided into several components:
15
15
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
21
21
code
22
22
23
23
### FFI to simple JS functions and supported attributes
24
24
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],
28
26
with syntax as described below:
29
27
30
28
[source,ocaml]
@@ -36,29 +34,44 @@ external-declaration := string-literal
36
34
Users need to declare types for foreign functions (JS functions here)
37
35
and provide customized `attributes` .
38
36
39
- #### ` bs.val`
37
+ #### Binding to global value: bs.val
40
38
41
39
[source,ocaml]
42
40
---------------
43
41
external imul : int -> int -> int = "Math.imul" [@@bs.val]
42
+ type dom
43
+ (* Abstract type for the DOM *)
44
+ external dom : dom = "document" [@@bs.val]
44
45
---------------
45
46
47
+ `bs.val` attribute is used to bind to a JavaScript value,
48
+ it can be a function or plain value.
49
+
50
+
46
51
47
52
[NOTE]
48
53
=====
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
50
63
JavaScript functions, you can
51
64
give the JavaScript foreign function a different name:
52
-
65
+ +
53
66
[source,ocaml]
54
67
---------------
55
68
external imul : int -> int -> int = "c_imul" [@@bs.val "Math.imul"]
56
69
---------------
57
70
=====
58
71
59
- #### ` bs.new`
72
+ #### Binding to JavaScript constructor: bs.new
60
73
61
- This attribute is used to create a JavaScript object.
74
+ `bs.new` is used to create a JavaScript object.
62
75
63
76
[source,ocaml]
64
77
----------
@@ -71,22 +84,58 @@ Output:
71
84
var date = new Date();
72
85
----------
73
86
74
- #### `bs.val`
75
87
76
- This attribute is used to bind to a JavaScript value
77
88
89
+ #### Binding to a value from a module: bs.module
90
+
91
+ Input:
78
92
[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
+
85
101
Output:
86
102
[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
+ ======
90
139
91
140
92
141
#### `bs.send`
@@ -137,40 +186,6 @@ module Int32Array = struct
137
186
end
138
187
--------
139
188
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
-
174
189
175
190
### FFI to high-order JS functions
176
191
0 commit comments