{"id":16717562,"url":"https://github.com/dannywillems/web-ocaml-rust-tuto","last_synced_at":"2025-04-10T06:43:14.610Z","repository":{"id":75871939,"uuid":"292341450","full_name":"dannywillems/web-ocaml-rust-tuto","owner":"dannywillems","description":"List of tutorials/projects experiencing OCaml and Rust mixed code running together in the browser.","archived":false,"fork":false,"pushed_at":"2020-09-11T14:23:59.000Z","size":100,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T07:49:40.372Z","etag":null,"topics":["ocaml","rust","tutorials","wasm","wasm-pack","web"],"latest_commit_sha":null,"homepage":"","language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dannywillems.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["dannywillems"],"patreon":["dannywillems"]}},"created_at":"2020-09-02T16:52:51.000Z","updated_at":"2024-11-05T00:34:26.000Z","dependencies_parsed_at":"2023-02-24T10:15:51.164Z","dependency_job_id":null,"html_url":"https://github.com/dannywillems/web-ocaml-rust-tuto","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dannywillems%2Fweb-ocaml-rust-tuto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dannywillems%2Fweb-ocaml-rust-tuto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dannywillems%2Fweb-ocaml-rust-tuto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dannywillems%2Fweb-ocaml-rust-tuto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dannywillems","download_url":"https://codeload.github.com/dannywillems/web-ocaml-rust-tuto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248173434,"owners_count":21059595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ocaml","rust","tutorials","wasm","wasm-pack","web"],"created_at":"2024-10-12T21:32:32.009Z","updated_at":"2025-04-10T06:43:14.600Z","avatar_url":"https://github.com/dannywillems.png","language":"OCaml","funding_links":["https://github.com/sponsors/dannywillems","https://patreon.com/[\"dannywillems\"]"],"categories":[],"sub_categories":[],"readme":"# web-ocaml-rust-tuto\n\nList of tutorials/projects experiencing OCaml and Rust mixed code running\ntogether in the browser.\n\nBefore starting, read the section about [setting up the environment](#setup-the-environment).\n\n## Idea\n\nThe general idea is to compile Rust into wasm and exporting some functions in JavaScript as a ES module.\nOn the OCaml side, js_of_ocaml is used to compile OCaml code to JavaScript. The\nCaml code uses usual `require` function to use functions from the ES module\ngenerated by wasm-bindgen, and then get access to the Rust code.\nExchanging values between OCaml and Rust must be performed very carefully. **TODO: add link to the tutorials playing with values**\n\nMost of the tutorials will be focused on Caml calling Rust, and not the opposite.\nWebpack is then used to bundle the JavaScript generated by Caml and the ES\nmodule generated by wasm-binding, and potentially additional npm packages.\n\n\n## Notes\n\n- `wasm-pack build --target nodejs` will build a ES module than can be used in NodeJS.\nImagine you have a \n```rust\nuse wasm_bindgen::prelude::*;\n\n#[wasm_bindgen]\npub fn return_zero() -\u003e u64 {\n    0\n}\n```\nbuilt with `wasm-pack build --target nodejs`. You can after that use\n```\nnode --experimental-modules --experimental-wasm-modules\n\u003e var t = require(\"./pkg/js_of_ruml.js\")\n\u003e t.return_zero()\n```\n\nThe default build target is `bundler` which requires a bundler (like webpack) afterwards.\n\n- It looks like there are some issues related to `wasm-opt` (WASM optimizer)\n  while building with `wasm-pack`. Adding these build options in the toml fix\n  temporarily the issue\n```\n# https://github.com/rustwasm/wasm-pack/issues/886\n[package.metadata.wasm-pack.profile.release]\nwasm-opt = [\"-O2\", \"--enable-mutable-globals\"]\n```\n\n- \n\n### Rust\n\n- [wasm-pack](https://github.com/rustwasm/wasm-pack): tools to get a workflow to build wasm code from Rust.\n- [rustwasm](https://rustwasm.github.io/docs/wasm-bindgen/introduction.html): book about Rust and WASM\n- [Awesome Rust/WASM](https://github.com/rustwasm/awesome-rust-and-webassembly): list of projects related to Rust/WASM interop\n\n### WASM\n\n- [wabt](https://github.com/WebAssembly/wabt): toolset for wasm.\n- [binaryen](https://github.com/WebAssembly/binaryen): compiler toolchain for wasm.\n\n## Setup the environment\n\n### Rust\n\nInstall wasm-pack\n```\ncurl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh\n```\n\n### NodeJS\n\nTo build the Rust code and use it in Node, install node using nvm, and install\nthe latest Node version (`nvm install 12.18.3` followed by `nvm use 12.18.3`).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdannywillems%2Fweb-ocaml-rust-tuto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdannywillems%2Fweb-ocaml-rust-tuto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdannywillems%2Fweb-ocaml-rust-tuto/lists"}