Skip to content
Snippets Groups Projects
README.md 1.62 KiB
Newer Older
Jono Wenger's avatar
Jono Wenger committed
# xxdk-WASM

WebAssembly bindings for xxDK.

Jono Wenger's avatar
Jono Wenger committed
## Building
Jono Wenger's avatar
Jono Wenger committed

Jono Wenger's avatar
Jono Wenger committed
```shell
$ GOOS=js GOARCH=wasm go build -o xxdk.wasm
Jono Wenger's avatar
Jono Wenger committed
```

Jono Wenger's avatar
Jono Wenger committed
### Running Tests

To run unit tests, you need to first install
[wasmbrowsertest](https://github.com/agnivade/wasmbrowsertest).

`wasm/wasm_js.s` contains commands only recognized by the Go WebAssembly
compiler and thus cause compile errors when running tests. It needs to be
excluded when running tests.

```shell
$ GOOS=js GOARCH=wasm go test
```

Jono Wenger's avatar
Jono Wenger committed
## Testing
Jono Wenger's avatar
Jono Wenger committed

Jono Wenger's avatar
Jono Wenger committed
The `test` directory contains a website and server to run the compiled
WebAssembly module. `assets` contains the website and `server` contains a small
Go HTTP server.
Jono Wenger's avatar
Jono Wenger committed

Jono Wenger's avatar
Jono Wenger committed
```shell
$ GOOS=js GOARCH=wasm go build -o test/assets/xxdk.wasm
$ go run test/server/main.go
```
Jono Wenger's avatar
Jono Wenger committed

Jono Wenger's avatar
Jono Wenger committed
### `wasm_exec.js`
Jono Wenger's avatar
Jono Wenger committed

Jono Wenger's avatar
Jono Wenger committed
`wasm_exec.js` is provided by Go and is used to import the WebAssembly module in
the browser. It can be retrieved from Go using the following command.
Jono Wenger's avatar
Jono Wenger committed

Jono Wenger's avatar
Jono Wenger committed
```shell
$ cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" test/assets/
```
Jono Wenger's avatar
Jono Wenger committed

Jono Wenger's avatar
Jono Wenger committed
Note that this repository makes edits to `wasm_exec.js` and you must either use
the one in this repository or add the following lines in the `go` `importObject`
on `global.Go`.

```javascript
global.Go = class {
    constructor() {
        // ...
        this.importObject = {
            go: {
                // ...
                // func Throw(exception string, message string)
                'gitlab.com/elixxir/client/wasm.Throw': (sp) => {
                    const exception = loadString(sp + 8)
                    const message = loadString(sp + 24)
                    throw globalThis[exception](message)
                },
            }
        }
    }
}
```