Skip to content

Commit 3bd1478

Browse files
committed
add more docs [skip ci]
1 parent bf295f2 commit 3bd1478

File tree

4 files changed

+214
-11
lines changed

4 files changed

+214
-11
lines changed

docs/Manual.html

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,13 +541,14 @@ <h1><a href="https://github.com/bloomberg/bucklescript">BuckleScript</a> User Ma
541541
</ul>
542542
</li>
543543
<li><a href="#_splice_calling_convention_bs_splice">Splice calling convention: bs.splice</a></li>
544-
<li><a href="#_special_types_on_external_declarations_bs_string_bs_int_bs_ignore">Special types on external declarations: bs.string, bs.int, bs.ignore</a>
544+
<li><a href="#_special_types_on_external_declarations_bs_string_bs_int_bs_ignore_bs_as">Special types on external declarations: bs.string, bs.int, bs.ignore, bs.as</a>
545545
<ul class="sectlevel3">
546546
<li><a href="#_using_polymorphic_variant_to_model_enums_and_string_types">Using polymorphic variant to model enums and string types</a></li>
547547
<li><a href="#_using_polymorphic_variant_to_model_event_listener">Using polymorphic variant to model event listener</a></li>
548548
<li><a href="#_phantom_arguments_and_ad_hoc_polyrmophism">Phantom Arguments and ad-hoc polyrmophism</a></li>
549549
</ul>
550550
</li>
551+
<li><a href="#_fixed_arguments">Fixed Arguments</a></li>
551552
<li><a href="#_binding_to_nodejs_special_variables_bs_node">Binding to NodeJS special variables: bs.node</a></li>
552553
<li><a href="#_binding_to_callbacks_high_order_function">Binding to callbacks (high-order function)</a>
553554
<ul class="sectlevel3">
@@ -1717,7 +1718,7 @@ <h3 id="_splice_calling_convention_bs_splice"><a class="anchor" href="#_splice_c
17171718
</div>
17181719
</div>
17191720
<div class="sect2">
1720-
<h3 id="_special_types_on_external_declarations_bs_string_bs_int_bs_ignore"><a class="anchor" href="#_special_types_on_external_declarations_bs_string_bs_int_bs_ignore"></a>Special types on external declarations: bs.string, bs.int, bs.ignore</h3>
1721+
<h3 id="_special_types_on_external_declarations_bs_string_bs_int_bs_ignore_bs_as"><a class="anchor" href="#_special_types_on_external_declarations_bs_string_bs_int_bs_ignore_bs_as"></a>Special types on external declarations: bs.string, bs.int, bs.ignore, bs.as</h3>
17211722
<div class="sect3">
17221723
<h4 id="_using_polymorphic_variant_to_model_enums_and_string_types"><a class="anchor" href="#_using_polymorphic_variant_to_model_enums_and_string_types"></a>Using polymorphic variant to model enums and string types</h4>
17231724
<div class="paragraph">
@@ -1921,6 +1922,85 @@ <h4 id="_phantom_arguments_and_ad_hoc_polyrmophism"><a class="anchor" href="#_ph
19211922
</div>
19221923
</div>
19231924
<div class="sect2">
1925+
<h3 id="_fixed_arguments"><a class="anchor" href="#_fixed_arguments"></a>Fixed Arguments</h3>
1926+
<div class="paragraph">
1927+
<p>Contrary to the Phantom arguments, <code>_[@bs.as]</code> is introduced to attach the const
1928+
data.</p>
1929+
</div>
1930+
<div class="paragraph">
1931+
<p>For example:</p>
1932+
</div>
1933+
<div class="listingblock">
1934+
<div class="content">
1935+
<pre class="pygments highlight"><code data-lang="ocaml"><span class="tok-k">external</span> <span class="tok-n">process_on_exit</span> <span class="tok-o">:</span> <span class="tok-o">(_</span> <span class="tok-o">[@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-k">as</span> <span class="tok-s2">&quot;exit&quot;</span><span class="tok-o">])</span> <span class="tok-o">-&gt;</span> <span class="tok-o">(</span><span class="tok-kt">int</span> <span class="tok-o">-&gt;</span> <span class="tok-kt">unit</span><span class="tok-o">)</span> <span class="tok-o">-&gt;</span> <span class="tok-kt">unit</span> <span class="tok-o">=</span>
1936+
<span class="tok-s2">&quot;process.on&quot;</span> <span class="tok-o">[@@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-k">val</span><span class="tok-o">]</span>
1937+
1938+
<span class="tok-k">let</span> <span class="tok-bp">()</span> <span class="tok-o">=</span>
1939+
<span class="tok-n">process_on_exit</span> <span class="tok-o">(</span><span class="tok-k">fun</span> <span class="tok-n">exit_code</span> <span class="tok-o">-&gt;</span>
1940+
<span class="tok-nn">Js</span><span class="tok-p">.</span><span class="tok-n">log</span><span class="tok-o">(</span> <span class="tok-s2">&quot;error code: &quot;</span> <span class="tok-o">^</span> <span class="tok-n">string_of_int</span> <span class="tok-n">exit_code</span> <span class="tok-o">))</span></code></pre>
1941+
</div>
1942+
</div>
1943+
<div class="paragraph">
1944+
<p>Output</p>
1945+
</div>
1946+
<div class="listingblock">
1947+
<div class="content">
1948+
<pre class="pygments highlight"><code data-lang="js"><span class="tok-nx">process</span><span class="tok-p">.</span><span class="tok-nx">on</span><span class="tok-p">(</span><span class="tok-s2">&quot;exit&quot;</span><span class="tok-p">,</span> <span class="tok-kd">function</span> <span class="tok-p">(</span><span class="tok-nx">exit_code</span><span class="tok-p">)</span> <span class="tok-p">{</span>
1949+
<span class="tok-nx">console</span><span class="tok-p">.</span><span class="tok-nx">log</span><span class="tok-p">(</span><span class="tok-s2">&quot;error code: &quot;</span> <span class="tok-o">+</span> <span class="tok-nx">exit_code</span><span class="tok-p">);</span>
1950+
<span class="tok-k">return</span> <span class="tok-cm">/* () */</span><span class="tok-mi">0</span><span class="tok-p">;</span>
1951+
<span class="tok-p">});</span></code></pre>
1952+
</div>
1953+
</div>
1954+
<div class="paragraph">
1955+
<p>It can also be used in combination with other attributes, for example:</p>
1956+
</div>
1957+
<div class="listingblock">
1958+
<div class="content">
1959+
<pre class="pygments highlight"><code data-lang="ocaml"><span class="tok-k">type</span> <span class="tok-n">process</span>
1960+
1961+
<span class="tok-k">external</span> <span class="tok-n">on_exit</span> <span class="tok-o">:</span> <span class="tok-o">(_</span> <span class="tok-o">[@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-k">as</span> <span class="tok-s2">&quot;exit&quot;</span><span class="tok-o">])</span> <span class="tok-o">-&gt;</span> <span class="tok-o">(</span><span class="tok-kt">int</span> <span class="tok-o">-&gt;</span> <span class="tok-kt">unit</span><span class="tok-o">)</span> <span class="tok-o">-&gt;</span> <span class="tok-kt">unit</span> <span class="tok-o">=</span>
1962+
<span class="tok-s2">&quot;on&quot;</span> <span class="tok-o">[@@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-n">send</span><span class="tok-o">.</span><span class="tok-n">pipe</span><span class="tok-o">:</span> <span class="tok-n">process</span><span class="tok-o">]</span>
1963+
<span class="tok-k">let</span> <span class="tok-n">register</span> <span class="tok-o">(</span><span class="tok-n">p</span> <span class="tok-o">:</span> <span class="tok-n">process</span><span class="tok-o">)</span> <span class="tok-o">=</span>
1964+
<span class="tok-n">p</span> <span class="tok-o">|&gt;</span> <span class="tok-n">on_exit</span> <span class="tok-o">(</span><span class="tok-k">fun</span> <span class="tok-n">i</span> <span class="tok-o">-&gt;</span> <span class="tok-nn">Js</span><span class="tok-p">.</span><span class="tok-n">log</span> <span class="tok-n">i</span> <span class="tok-o">)</span></code></pre>
1965+
</div>
1966+
</div>
1967+
<div class="paragraph">
1968+
<p>Output</p>
1969+
</div>
1970+
<div class="listingblock">
1971+
<div class="content">
1972+
<pre class="pygments highlight"><code data-lang="js"><span class="tok-kd">function</span> <span class="tok-nx">register</span><span class="tok-p">(</span><span class="tok-nx">p</span><span class="tok-p">)</span> <span class="tok-p">{</span>
1973+
<span class="tok-k">return</span> <span class="tok-nx">p</span><span class="tok-p">.</span><span class="tok-nx">on</span><span class="tok-p">(</span><span class="tok-s2">&quot;exit&quot;</span><span class="tok-p">,</span> <span class="tok-kd">function</span> <span class="tok-p">(</span><span class="tok-nx">i</span><span class="tok-p">)</span> <span class="tok-p">{</span>
1974+
<span class="tok-nx">console</span><span class="tok-p">.</span><span class="tok-nx">log</span><span class="tok-p">(</span><span class="tok-nx">i</span><span class="tok-p">);</span>
1975+
<span class="tok-k">return</span> <span class="tok-cm">/* () */</span><span class="tok-mi">0</span><span class="tok-p">;</span>
1976+
<span class="tok-p">});</span>
1977+
<span class="tok-p">}</span></code></pre>
1978+
</div>
1979+
</div>
1980+
<div class="paragraph">
1981+
<p>Input</p>
1982+
</div>
1983+
<div class="listingblock">
1984+
<div class="content">
1985+
<pre class="pygments highlight"><code data-lang="ocaml"><span class="tok-k">external</span> <span class="tok-n">io_config</span> <span class="tok-o">:</span>
1986+
<span class="tok-n">stdio</span><span class="tok-o">:(</span> <span class="tok-o">_</span> <span class="tok-o">[@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-k">as</span> <span class="tok-s2">&quot;inherit&quot;</span><span class="tok-o">])</span> <span class="tok-o">-&gt;</span> <span class="tok-n">cwd</span><span class="tok-o">:</span><span class="tok-kt">string</span> <span class="tok-o">-&gt;</span> <span class="tok-kt">unit</span> <span class="tok-o">-&gt;</span> <span class="tok-o">_</span> <span class="tok-o">=</span> <span class="tok-s2">&quot;&quot;</span> <span class="tok-o">[@@</span><span class="tok-n">bs</span><span class="tok-o">.</span><span class="tok-n">obj</span><span class="tok-o">]</span>
1987+
1988+
<span class="tok-k">let</span> <span class="tok-n">config</span> <span class="tok-o">=</span> <span class="tok-n">io_config</span> <span class="tok-o">~</span><span class="tok-n">cwd</span><span class="tok-o">:</span><span class="tok-s2">&quot;.&quot;</span> <span class="tok-bp">()</span></code></pre>
1989+
</div>
1990+
</div>
1991+
<div class="paragraph">
1992+
<p>Output</p>
1993+
</div>
1994+
<div class="listingblock">
1995+
<div class="content">
1996+
<pre class="pygments highlight"><code data-lang="js"><span class="tok-kd">var</span> <span class="tok-nx">config</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
1997+
<span class="tok-nx">stdio</span><span class="tok-o">:</span> <span class="tok-s2">&quot;inherit&quot;</span><span class="tok-p">,</span>
1998+
<span class="tok-nx">cwd</span><span class="tok-o">:</span> <span class="tok-s2">&quot;.&quot;</span>
1999+
<span class="tok-p">};</span></code></pre>
2000+
</div>
2001+
</div>
2002+
</div>
2003+
<div class="sect2">
19242004
<h3 id="_binding_to_nodejs_special_variables_bs_node"><a class="anchor" href="#_binding_to_nodejs_special_variables_bs_node"></a>Binding to NodeJS special variables: bs.node</h3>
19252005
<div class="paragraph">
19262006
<p>NodeJS has several file local variables: <code><em>dirname</code>, <code></em>filename</code>, <code>module_</code>, and <code>require</code>,

jscomp/test/prepend_data_ffi.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,41 @@ process.on(function (i) {
3030
xx(3, 3, "xxx", "a", "b");
3131

3232
function f(x) {
33-
x.xx(91, /* int array */[
33+
x.xx(104, /* int array */[
3434
1,
3535
2,
3636
3
3737
]);
38-
x.xx(92, 3, "xxx", /* int array */[
38+
x.xx(105, 3, "xxx", /* int array */[
3939
1,
4040
2,
4141
3
4242
]);
43-
x.xx(93, 3, "xxx", 1, 2, 3);
44-
return x.xx(94, 3, "xxx", 0, "b", 1, 2, 3, 4, 5);
43+
x.xx(106, 3, "xxx", 1, 2, 3);
44+
x.xx(107, 3, "xxx", 0, "b", 1, 2, 3, 4, 5);
45+
return x.xx(108, 3, "xxx", 0, "yyy", "b", 1, 2, 3, 4, 5);
4546
}
4647

47-
exports.v1 = v1;
48-
exports.v2 = v2;
49-
exports.f = f;
48+
process.on("exit", function (exit_code) {
49+
console.log("error code: " + exit_code);
50+
return /* () */0;
51+
});
52+
53+
function register(p) {
54+
return p.on("exit", function (i) {
55+
console.log(i);
56+
return /* () */0;
57+
});
58+
}
59+
60+
var config = {
61+
stdio: "inherit",
62+
cwd: "."
63+
};
64+
65+
exports.v1 = v1;
66+
exports.v2 = v2;
67+
exports.f = f;
68+
exports.register = register;
69+
exports.config = config;
5070
/* v1 Not a pure module */

jscomp/test/prepend_data_ffi.ml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,46 @@ external on_exit_slice4 :
8484
"xx" [@@bs.send.pipe: t] [@@bs.splice]
8585

8686

87+
external on_exit_slice5 :
88+
int
89+
-> (_ [@bs.as 3])
90+
-> (_ [@bs.as "xxx"])
91+
-> ([`a|`b|`c] [@bs.int])
92+
-> (_ [@bs.as "yyy"])
93+
-> ([`a|`b|`c] [@bs.string])
94+
-> int array
95+
-> unit
96+
=
97+
"xx" [@@bs.send.pipe: t] [@@bs.splice]
98+
99+
87100
(**
88101
TODO: bs.send conflicts with bs.val: better error message
89102
*)
90103
let f (x : t) =
91104
x |> on_exit_slice1 __LINE__ [|1;2;3|];
92105
x |> on_exit_slice2 __LINE__ [|1;2;3|];
93106
x |> on_exit_slice3 __LINE__ [|1;2;3|];
94-
x |> on_exit_slice4 __LINE__ `a `b [|1;2;3;4;5|]
107+
x |> on_exit_slice4 __LINE__ `a `b [|1;2;3;4;5|];
108+
x |> on_exit_slice5 __LINE__ `a `b [|1;2;3;4;5|]
109+
110+
external process_on_exit : (_ [@bs.as "exit"]) -> (int -> unit) -> unit =
111+
"process.on" [@@bs.val]
112+
113+
let () =
114+
process_on_exit (fun exit_code ->
115+
Js.log( "error code: " ^ string_of_int exit_code ))
116+
117+
118+
type process
119+
120+
external on_exit : (_ [@bs.as "exit"]) -> (int -> unit) -> unit =
121+
"on" [@@bs.send.pipe: process]
122+
let register (p : process) =
123+
p |> on_exit (fun i -> Js.log i )
124+
125+
126+
external io_config :
127+
stdio:( _ [@bs.as "inherit"]) -> cwd:string -> unit -> _ = "" [@@bs.obj]
128+
129+
let config = io_config ~cwd:"." ()

site/docsource/OCaml-call-JS.adoc

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ the compiler will emit an error message
241241
======
242242

243243

244-
### Special types on external declarations: bs.string, bs.int, bs.ignore
244+
### Special types on external declarations: bs.string, bs.int, bs.ignore, bs.as
245245

246246
#### Using polymorphic variant to model enums and string types
247247
There are several patterns heavily used in existing JavaScript codebase, for example,
@@ -380,6 +380,74 @@ let add2 k x y =
380380
add_dyn k (string_of_kind k) x y
381381
-------------
382382
383+
### Fixed Arguments
384+
385+
Contrary to the Phantom arguments, `_[@bs.as]` is introduced to attach the const
386+
data.
387+
388+
For example:
389+
390+
[source,ocaml]
391+
--------------
392+
external process_on_exit : (_ [@bs.as "exit"]) -> (int -> unit) -> unit =
393+
"process.on" [@@bs.val]
394+
395+
let () =
396+
process_on_exit (fun exit_code ->
397+
Js.log( "error code: " ^ string_of_int exit_code ))
398+
--------------
399+
400+
Output
401+
[source,js]
402+
-----------
403+
process.on("exit", function (exit_code) {
404+
console.log("error code: " + exit_code);
405+
return /* () */0;
406+
});
407+
-----------
408+
409+
It can also be used in combination with other attributes, for example:
410+
411+
[source,ocaml]
412+
--------------
413+
type process
414+
415+
external on_exit : (_ [@bs.as "exit"]) -> (int -> unit) -> unit =
416+
"on" [@@bs.send.pipe: process]
417+
let register (p : process) =
418+
p |> on_exit (fun i -> Js.log i )
419+
420+
--------------
421+
422+
Output
423+
[source,js]
424+
-----------
425+
function register(p) {
426+
return p.on("exit", function (i) {
427+
console.log(i);
428+
return /* () */0;
429+
});
430+
}
431+
-----------
432+
433+
Input
434+
[source,ocaml]
435+
--------------
436+
external io_config :
437+
stdio:( _ [@bs.as "inherit"]) -> cwd:string -> unit -> _ = "" [@@bs.obj]
438+
439+
let config = io_config ~cwd:"." ()
440+
--------------
441+
442+
Output
443+
[source,js]
444+
-----------
445+
var config = {
446+
stdio: "inherit",
447+
cwd: "."
448+
};
449+
-----------
450+
383451
### Binding to NodeJS special variables: bs.node
384452
385453
NodeJS has several file local variables: `__dirname`, `__filename`, `module_`, and `require`,

0 commit comments

Comments
 (0)