-
Notifications
You must be signed in to change notification settings - Fork 22.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add New Guide: Compiling Haskell to WebAssembly #36849
Conversation
|
||
{{WebAssemblySidebar}} | ||
|
||
# Compiling Haskell to WebAssembly: A Step-by-Step Guide |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[markdownlint] reported by reviewdog 🐶
MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "Compiling Haskell to WebAssemb..."]
As much as I love Haskell, I don't think it would be maintainable as I doubt others on the team know Haskell and in particular compiling it to Wasm. Is it possible to make this a blog post and link to it in the "see also" instead? |
|
||
### Installing the Custom GHC for WebAssembly | ||
|
||
To compile Haskell to WebAssembly, you need a custom GHC version that supports the `wasm32-wasi` target. Detailed installation instructions can be found in the [GHC WebAssembly meta repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta). Here’s a quick setup guide: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
To compile Haskell to WebAssembly, you need a custom GHC version that supports the `wasm32-wasi` target. Detailed installation instructions can be found in the [GHC WebAssembly meta repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta). Here’s a quick setup guide: | |
To compile Haskell to WebAssembly, you need a custom GHC version that supports the `wasm32-wasi` target. Detailed installation instructions can be found in the [GHC WebAssembly meta repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta). Here's a quick setup guide: |
|
||
To compile Haskell to WebAssembly, you need a custom GHC version that supports the `wasm32-wasi` target. Detailed installation instructions can be found in the [GHC WebAssembly meta repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta). Here’s a quick setup guide: | ||
|
||
1. **(Optional): Choose a GHC flavor.** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
1. **(Optional): Choose a GHC flavor.** | |
1. **(Optional): Choose a GHC flavor.** |
|
||
1. **(Optional): Choose a GHC flavor.** | ||
This determines the specific GHC version. For stability, we'll use GHC 9.8.3: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
wasm32-wasi-cabal --version | ||
``` | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
|
||
### Writing the Haskell Code | ||
|
||
Let’s start with a simple Haskell function that takes an integer and adds 10: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
Let’s start with a simple Haskell function that takes an integer and adds 10: | |
Let's start with a simple Haskell function that takes an integer and adds 10: |
inst.exports.freeBuffer(ptr); | ||
inst.exports.freeBuffer(resultPtr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
inst.exports.freeBuffer(ptr); | |
inst.exports.freeBuffer(resultPtr); | |
inst.exports.freeBuffer(ptr); | |
inst.exports.freeBuffer(resultPtr); |
return result; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
return result; | |
} | |
return result; | |
} |
wasi.initialize(inst); | ||
inst.exports.hs_init(0, 0); | ||
console.log(callWithString(inst.exports.hs_sayHello, "John")); // Outputs: Hello, John |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
wasi.initialize(inst); | |
inst.exports.hs_init(0, 0); | |
console.log(callWithString(inst.exports.hs_sayHello, "John")); // Outputs: Hello, John | |
wasi.initialize(inst); | |
inst.exports.hs_init(0, 0); | |
console.log(callWithString(inst.exports.hs_sayHello, "John")); // Outputs: Hello, John |
Congratulations! You’ve successfully compiled Haskell to WebAssembly and integrated it into a web app. This guide covered the basics, but there’s much more to explore, including advanced data types and optimizations. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
Congratulations! You’ve successfully compiled Haskell to WebAssembly and integrated it into a web app. This guide covered the basics, but there’s much more to explore, including advanced data types and optimizations. | |
Congratulations! You've successfully compiled Haskell to WebAssembly and integrated it into a web app. This guide covered the basics, but there's much more to explore, including advanced data types and optimizations. |
|
||
- [GHC WebAssembly Meta Repository](https://gitlab.haskell.org/ghc/ghc-wasm-meta) | ||
- [Ormolu live](https://github.com/tweag/ormolu/tree/master/ormolu-live) | ||
- [Sql2er](https://github.com/tusharad/sql2er) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶
- [Sql2er](https://github.com/tusharad/sql2er) | |
- [Sql2er](https://github.com/tusharad/sql2er) |
Sure @Josh-Cena, no problem! Closing the PR. |
Description
This PR introduces a new guide titled Compiling Haskell to WebAssembly under the WebAssembly section. The guide provides step-by-step instructions on how to set up the environment, compile Haskell code into WebAssembly, and integrate it into a web application. Additionally, it covers compiling a Haskell Cabal project for WebAssembly and handling data types such as strings.
Motivation
This guide aims to broaden the range of languages covered in the WebAssembly section on MDN, providing valuable resources for Haskell developers interested in WebAssembly.
Additional details
Related issues and pull requests
Question for Reviewers:
Before proceeding further, I'd like to confirm whether new content of this nature is allowed in the MDN documentation. If there are any specific guidelines or prerequisites for contributing new topics, please let me know, and I'll adjust accordingly.
Looking forward to your feedback!