Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kritzcreek/purescript-wasm
Wasm library for PureScript
https://github.com/kritzcreek/purescript-wasm
Last synced: about 2 hours ago
JSON representation
Wasm library for PureScript
- Host: GitHub
- URL: https://github.com/kritzcreek/purescript-wasm
- Owner: kritzcreek
- License: apache-2.0
- Created: 2020-10-31T00:02:34.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-02T05:12:47.000Z (3 months ago)
- Last Synced: 2024-10-11T23:54:53.678Z (26 days ago)
- Language: PureScript
- Size: 93.8 KB
- Stars: 26
- Watchers: 5
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
purescript-wasm
===A PureScript library containing syntax definitions and encoders for WebAssembly. You can use this to generate Wasm for your own compiler projects. This library does *not* compile PureScript to Wasm.
## Minimal example
As a small example we'll define a module with a `main` function that takes no arguments and returns an i32. It'll add the numbers 20 and 22 and return 42 as its result. I'll include a bit of FFI to instantiate and run the generated Wasm.
```purescript
module Main whereimport Prelude
import Data.ArrayBuffer.Types (Uint8Array)
import Effect (Effect)
import Wasm.Encode as Encode
import Wasm.Syntax as Sforeign import execWasm :: Uint8Array -> Effect Unit
wasmModule :: S.Module
wasmModule = S.emptyModule
{ types = [ { arguments: [], results: [ S.NumType S.I32 ] } ]
, funcs = [ { locals: [], type: 0, body: [ S.I32Const 20, S.I32Const 22, S.I32Add ] } ]
, exports = [ { name: "main", desc: S.ExportFunc 0 } ]
}main :: Effect Unit
main = do
let bytes = Encode.encodeModule wasmModule
execWasm bytes
``````javascript
export const execWasm = (bytes) => () => {
WebAssembly.instantiate(bytes, {})
.then(module => console.log(module.instance.exports.main()))
}
```For a real compiler project you'll likely want to define your own builder on top of `Wasm.Syntax` to keep track of indices for you. An example of this can be seen in `example/src/WasmBuilder.purs`.