Skip to content

Commit b12f852

Browse files
authored
Merge pull request #134 from ocaml-wasm/examples
Update examples
2 parents 4f76417 + 3656b8f commit b12f852

File tree

31 files changed

+180
-172
lines changed

31 files changed

+180
-172
lines changed

examples/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,17 @@ Or a single one:
1010
$> dune build @examples/<path-to-example-project>/default
1111
```
1212

13+
Add the `--profile wasm` option to compile to Wasm:
14+
```
15+
$> dune build @examples/boulderdash/default --profile wasm
16+
```
17+
1318
Compilation artifacts can be found in `${REPO_ROOT}/_build/default/examples/`.
19+
20+
When generating JavaScript code, you can directly open the
21+
`index.html` files in a browser. When generating Wasm code, you need
22+
to serve the files, for instance with the following command:
23+
```
24+
python -m http.server -d _build/default/examples/boulderdash/
25+
```
26+
and then open `http://localhost:8000` in a browser.

examples/boulderdash/boulderdash.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ let http_get url =
326326
let msg = r.XmlHttpRequest.content in
327327
if cod = 0 || cod = 200 then Lwt.return msg else fst (Lwt.wait ())
328328

329-
let getfile f = try Lwt.return (Sys_js.read_file ~name:f) with Not_found -> http_get f
329+
let getfile f = try Lwt.return (Sys_js.read_file ~name:f) with Sys_error _ -> http_get f
330330

331331
exception Eos
332332

@@ -507,8 +507,4 @@ let start _ =
507507
Dom.appendChild body div;
508508
Lwt.return ()
509509

510-
let _ =
511-
Html.window##.onload :=
512-
Html.handler (fun _ ->
513-
ignore (start ());
514-
Js._false)
510+
let () = Lwt.async start

examples/boulderdash/dune

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
(executables
22
(names boulderdash)
33
(libraries js_of_ocaml-lwt)
4-
(modes byte)
4+
(modes js wasm)
5+
(js_of_ocaml
6+
(flags :standard --file %{dep:maps.txt} --file maps))
7+
(link_deps
8+
(glob_files maps/*.map))
59
(preprocess
610
(pps js_of_ocaml-ppx)))
711

812
(rule
9-
(targets boulderdash.js)
10-
(deps
11-
(glob_files maps/*.map))
1213
(action
13-
(run
14-
%{bin:js_of_ocaml}
15-
--source-map
16-
%{dep:boulderdash.bc}
17-
-o
18-
%{targets}
19-
--pretty
20-
--file
21-
%{dep:maps.txt}
22-
--file
23-
maps)))
14+
(copy boulderdash.bc.wasm.js boulderdash.bc.js))
15+
(enabled_if
16+
(not %{env:js-enabled=})))
2417

2518
(alias
2619
(name default)
2720
(deps
28-
boulderdash.js
21+
boulderdash.bc.js
2922
index.html
23+
maps.txt
24+
(glob_files maps/*.map)
3025
(glob_files sprites/*.{png,svg})))

examples/boulderdash/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<head>
66
<title>Boulder Dash</title>
77
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8-
<script type="text/javascript" src="boulderdash.js"></script>
8+
<script type="text/javascript" src="boulderdash.bc.js" defer></script>
99
</head>
1010
<body id="boulderdash">
1111
</body>

examples/cubes/dune

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
(executables
22
(names cubes)
33
(libraries js_of_ocaml-lwt)
4-
(modes byte)
4+
(modes js wasm)
55
(preprocess
66
(pps js_of_ocaml-ppx)))
77

88
(rule
9-
(targets cubes.js)
109
(action
11-
(run
12-
%{bin:js_of_ocaml}
13-
--source-map
14-
%{dep:cubes.bc}
15-
-o
16-
%{targets}
17-
--pretty)))
10+
(copy cubes.bc.wasm.js cubes.bc.js))
11+
(enabled_if
12+
(not %{env:js-enabled=})))
1813

1914
(alias
2015
(name default)
21-
(deps cubes.js index.html))
16+
(deps cubes.bc.js index.html))

examples/cubes/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<html xmlns="http://www.w3.org/1999/xhtml">
55
<head>
66
<title>Cubes</title>
7-
<script type="text/javascript" src="cubes.js" defer></script>
7+
<script type="text/javascript" src="cubes.bc.js" defer></script>
88
</head>
99
<body>
1010
</body>

examples/graph_viewer/dune

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(executables
22
(names viewer_js) ;; add converter & viewer
33
(libraries js_of_ocaml-lwt)
4-
(modes byte)
4+
(modes js wasm)
55
(modules
66
(:standard
77
\
@@ -13,24 +13,19 @@
1313
dot_lexer
1414
dot_graph
1515
dot_render))
16+
(js_of_ocaml
17+
(flags :standard --file %{dep:scene.json}))
1618
(preprocess
17-
(pps js_of_ocaml-ppx)))
19+
(pps js_of_ocaml-ppx js_of_ocaml-ppx_deriving_json)))
1820

1921
(ocamllex dot_lexer)
2022

2123
(rule
22-
(targets viewer_js.js)
2324
(action
24-
(run
25-
%{bin:js_of_ocaml}
26-
--source-map
27-
%{dep:viewer_js.bc}
28-
-o
29-
%{targets}
30-
--pretty
31-
--file
32-
%{dep:scene.json})))
25+
(copy viewer_js.bc.wasm.js viewer_js.bc.js))
26+
(enabled_if
27+
(not %{env:js-enabled=})))
3328

3429
(alias
3530
(name default)
36-
(deps viewer_js.js index.html))
31+
(deps viewer_js.bc.js scene.json index.html))

examples/graph_viewer/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<html xmlns="http://www.w3.org/1999/xhtml">
55
<head>
66
<title>Graph viewer</title>
7-
<script type="text/javascript" src="viewer_js.js"></script>
7+
<script type="text/javascript" src="viewer_js.bc.js" defer></script>
88
</head>
99
<body>
1010
</body>

examples/graph_viewer/scene.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1818
*)
1919

20+
[@@@warning "-39"]
21+
2022
type command =
2123
| Move_to of float * float
2224
| Curve_to of float * float * float * float * float * float
25+
[@@deriving json]
2326

2427
type color = float * float * float
2528

@@ -28,6 +31,7 @@ type ('color, 'font, 'text) element =
2831
| Polygon of (float * float) array * 'color option * 'color option
2932
| Ellipse of float * float * float * float * 'color option * 'color option
3033
| Text of float * float * 'text * 'font * 'color option * 'color option
34+
[@@deriving json]
3135

3236
(****)
3337

examples/graph_viewer/scene.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
type command =
2121
| Move_to of float * float
2222
| Curve_to of float * float * float * float * float * float
23+
[@@deriving json]
2324

2425
type color = float * float * float
2526

@@ -28,6 +29,7 @@ type ('color, 'font, 'text) element =
2829
| Polygon of (float * float) array * 'color option * 'color option
2930
| Ellipse of float * float * float * float * 'color option * 'color option
3031
| Text of float * float * 'text * 'font * 'color option * 'color option
32+
[@@deriving json]
3133

3234
(****)
3335

0 commit comments

Comments
 (0)