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

Jono Wenger's avatar
Jono Wenger committed
This repository contains the WebAssembly bindings for xxDK. It also includes
examples and a test server to serve the compiled WebAssembly module.
Jono Wenger's avatar
Jono Wenger committed

**Note:** If you are updating the version of Go that this repository uses, you
Jono Wenger's avatar
Jono Wenger committed
need to ensure that you update the `wasm_exec.js` file as described
[below](#wasm_execjs).

Jono Wenger's avatar
Jono Wenger committed
## Updates

The current semantic version of this repository is stored in `SEMVER` in
`version.go`. When making major updates or updates that create an
incompatibility in the storage or databases, the semantic version needs to be
updated and an upgrade path needs to be provided.

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

The repository can only be compiled to a WebAssembly binary using `GOOS=js` and
`GOARCH=wasm`.

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

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

Because the bindings use `syscall/js`, tests cannot only be run in a browser. To
Jono Wenger's avatar
Jono Wenger committed
automate this process first get
[wasmbrowsertest](https://github.com/agnivade/wasmbrowsertest) and follow their
[installation instructions](https://github.com/agnivade/wasmbrowsertest#quickstart).
Then, tests can be run using the following command.
Jono Wenger's avatar
Jono Wenger committed

```shell
$ GOOS=js GOARCH=wasm go test ./...
Jono Wenger's avatar
Jono Wenger committed
```

Jono Wenger's avatar
Jono Wenger committed
Note, this will fail because `utils/utils_js.s` contains commands only
recognized by the Go WebAssembly compiler and for some reason are not recognized
by the test runner. To get tests to run, temporarily delete the body of
`utils/utils_js.s` during testing.
Jono Wenger's avatar
Jono Wenger committed
## Testing and Examples
Jono Wenger's avatar
Jono Wenger committed

Jono Wenger's avatar
Jono Wenger committed
The `test` directory contains a basic HTTP server that serves an HTML file
running the WebAssembly binary. This is used for basic testing of the binary.
Jono Wenger's avatar
Jono Wenger committed
To run the server, first compile the bindings and save them to the `test`
directory. Then run the server
Jono Wenger's avatar
Jono Wenger committed

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

Jono Wenger's avatar
Jono Wenger committed
Navigate to http://localhost:9090 to see the webpage.
## `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" .
Jono Wenger's avatar
Jono Wenger committed
```
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/wasm-utils/utils.throw': (sp) => {
Jono Wenger's avatar
Jono Wenger committed
                    const exception = loadString(sp + 8)
                    const message = loadString(sp + 24)
                    throw globalThis[exception](message)
                },
            }
        }
    }
}