|
| 1 | +# OCaml (JS) |
| 2 | + |
| 3 | +[OCaml](https://ocaml.org/) is an industrial-strength programming language supporting functional, imperative and object-oriented styles. |
| 4 | + |
| 5 | +LiveCodes runs the OCaml compiler itself, built with [js_of_ocaml](https://github.com/ocsigen/js_of_ocaml), so compilation happens in the browser and nothing is sent to a server. The compiler emits a JavaScript program, which the result page then runs. |
| 6 | + |
| 7 | +:::info Note |
| 8 | + |
| 9 | +LiveCodes also has an `ocaml` language, which runs the [ReScript](https://rescript-lang.org/) compiler. It is deprecated in favour of this one. Read documentation [here](./ocaml.mdx). |
| 10 | + |
| 11 | +::: |
| 12 | + |
| 13 | +## Demo |
| 14 | + |
| 15 | +import LiveCodes from '../../src/components/LiveCodes.tsx'; |
| 16 | + |
| 17 | +export const params = { |
| 18 | + 'ocaml-js': 'let () =\n print_endline "Hello, World!"\n', |
| 19 | + console: 'full', |
| 20 | +}; |
| 21 | + |
| 22 | +<LiveCodes params={params}></LiveCodes> |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +LiveCodes runs OCaml in the browser, including the standard library. |
| 27 | + |
| 28 | +Because the compiled program *is* JavaScript by the time it runs, it can interact with the page. JavaScript interoperability and DOM access come from [js_of_ocaml](https://github.com/ocsigen/js_of_ocaml)'s `Js` and `Dom_html` with `open Js_of_ocaml` (see the [example below](#example-usage)). |
| 29 | + |
| 30 | +## Modules and Libraries |
| 31 | + |
| 32 | +The standard library is always available. Beyond it, these are linked on demand — the archive is fetched the first time a program names one of its modules, so a program that uses none of them downloads none of them. |
| 33 | + |
| 34 | +- **js_of_ocaml** — `Js`, `Dom_html`, and the rest of the browser APIs |
| 35 | +- **Str**, **Unix**, **Graphics** — from the OCaml distribution |
| 36 | +- **Lwt**, **js_of_ocaml-lwt** — promises and concurrency |
| 37 | +- **Re** — regular expressions |
| 38 | +- **jsonm**, **xmlm**, **uutf** — JSON, XML and Unicode text |
| 39 | +- **fmt**, **logs** — formatting and logging |
| 40 | +- **base64** — base64 encoding |
| 41 | +- **Zarith** — `Z` and `Q`, arbitrary-precision integers and rationals |
| 42 | + |
| 43 | +Everything here runs in the browser, so a library has to be pure OCaml, or have a JavaScript replacement for its C stubs — Zarith's are provided by `zarith_stubs_js`. Anything needing real threads, processes or a filesystem will not work, whatever is linked. |
| 44 | + |
| 45 | +This is the set built with the compiler. Other opam packages are not available. |
| 46 | + |
| 47 | +## Language Info |
| 48 | + |
| 49 | +### Name |
| 50 | + |
| 51 | +`ocaml-js` |
| 52 | + |
| 53 | +### Extensions |
| 54 | + |
| 55 | +`js.ocaml`, `js.ml` |
| 56 | + |
| 57 | +### Editor |
| 58 | + |
| 59 | +`script` |
| 60 | + |
| 61 | +## Compiler |
| 62 | + |
| 63 | +The OCaml compiler — the same `compiler-libs` frontend that compiles ordinary OCaml — compiled to JavaScript with [js_of_ocaml](https://github.com/ocsigen/js_of_ocaml). It is published as [@live-codes/ocaml](https://www.npmjs.com/package/@live-codes/ocaml). |
| 64 | + |
| 65 | +### Version |
| 66 | + |
| 67 | +`@live-codes/ocaml`: v0.2.0 |
| 68 | + |
| 69 | +## Code Formatting |
| 70 | + |
| 71 | +Not supported. |
| 72 | + |
| 73 | +## Limitations |
| 74 | + |
| 75 | +There is no ppx support, since a ppx would have to run at compile time and compilation happens at run time here. So `##`, `##.` and `[%js]` are not available; use `Js.Unsafe` and the rest of the bindings instead. |
| 76 | + |
| 77 | +## Example Usage |
| 78 | + |
| 79 | +This example counts button clicks in the DOM, using `Dom_html`: |
| 80 | + |
| 81 | +<LiveCodes template="ocaml-js" height="80vh"></LiveCodes> |
| 82 | + |
| 83 | +## Starter Template |
| 84 | + |
| 85 | +https://livecodes.io/?template=ocaml-js |
| 86 | + |
| 87 | +## Links |
| 88 | + |
| 89 | +- [OCaml website](https://ocaml.org/) |
| 90 | +- [OCaml documentation](https://ocaml.org/docs/) |
| 91 | +- [@live-codes/ocaml on npm](https://www.npmjs.com/package/@live-codes/ocaml) |
| 92 | +- [js_of_ocaml](https://github.com/ocsigen/js_of_ocaml) |
| 93 | +- [OCaml with the ReScript compiler](./ocaml.mdx) in LiveCodes |
0 commit comments