Skip to content

Commit 85ef3a5

Browse files
authored
Merge pull request #1925 from mintlayer/wasm_tests_as_ts
Split large files in wasm bindings
2 parents dad2723 + 8375701 commit 85ef3a5

35 files changed

+3393
-2726
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ test/config.ini
4545

4646
# wasm
4747
wasm-wrappers/pkg/
48+
wasm-wrappers/js-bindings-test/dist/
4849

4950
# 'mintlayer-data' will be mapped to home directories of docker containers, so everything
5051
# inside it will be generated by the containers.

.github/workflows/wasm.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,22 @@ jobs:
3535
uses: actions/setup-node@v3
3636
with:
3737
node-version: ${{ matrix.node-version }}
38+
- name: Install TypeScript & Knip
39+
run: npm install typescript knip
3840
- name: Install wasm-pack
3941
run: cargo install wasm-pack
4042
- name: Build the wasm module
4143
working-directory: ./wasm-wrappers
4244
run: wasm-pack build --target nodejs
45+
- name: Compile the tests
46+
working-directory: ./wasm-wrappers
47+
run: tsc --project js-bindings-test/tsconfig.json
4348
- name: Run the tests
4449
working-directory: ./wasm-wrappers
45-
run: node js-bindings/node-entry.js
50+
run: node --enable-source-maps js-bindings-test/node-entry.js
51+
- name: Run Knip
52+
working-directory: ./wasm-wrappers/js-bindings-test
53+
run: npx knip
4654

4755
wasm_artifacts:
4856
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ test/config.ini
4343

4444
# wasm
4545
wasm-wrappers/pkg/
46+
wasm-wrappers/js-bindings-test/dist/
4647

4748
# 'mintlayer-data' will be mapped to home directories of docker containers, so everything
4849
# inside it will be generated by the containers.

build-tools/codecheck/codecheck.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
'.git',
3535
'build-tools/docker/example-mainnet/mintlayer-data',
3636
'build-tools/docker/example-mainnet-dns-server/mintlayer-data',
37-
'wasm-wrappers/pkg'
37+
'wasm-wrappers/pkg',
38+
'wasm-wrappers/js-bindings-test/dist',
3839
]
3940

4041

do_checks.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ set -e
55
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
66
PYTHON=$(which python || which python3)
77

8+
cd "$SCRIPT_DIR"
9+
810
cargo fmt --check -- --config newline_style=Unix
911

1012
# Install cargo deny first with: cargo install cargo-deny.
@@ -36,7 +38,7 @@ cargo clippy --all-features --workspace --lib --bins --examples -- \
3638
-D clippy::string_slice
3739

3840
# Install requirements with: pip install -r ./build-tools/codecheck/requirements.txt
39-
"$PYTHON" "$SCRIPT_DIR/build-tools/codecheck/codecheck.py"
41+
"$PYTHON" "build-tools/codecheck/codecheck.py"
4042

4143
# Ensure that wasm documentation is up-to-date
4244
cargo run -p wasm-doc-gen -- -o wasm-wrappers/WASM-API.md --check

wasm-wrappers/README.md

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
## Basic wasm bindings for mintlayer
1+
# Basic wasm bindings for mintlayer
22

33
This module has different basic functionalities of mintlayer compiled into wasm for various purposes, primarily interfacing with other systems and languages without having to rewrite code.
44

55
##### Note: This was tested on x86_64 Linux, and may not work on other platforms. It didn't work on M1 Mac directly (particularly the build. A pre-built wasm binary works fine on a browser, see below for more information).
66

7+
## Running the tests
78

8-
### To run in a web browser
9+
### Preparation
910

1011
Make sure you have wasm-pack and the wasm32-unknown-unknown target installed:
1112

@@ -16,61 +17,85 @@ cargo install wasm-pack
1617

1718
Also make sure you have `clang` installed. It's required.
1819

19-
To build the wasm package from the crate, run (in the wasm Cargo.toml directory):
20-
21-
```
22-
wasm-pack build --target web
23-
```
24-
2520
**Note for mac users**: `llvm` installed by Xcode doesn't support wasm targets, but the homebrew version does, these commands may make it possible to compile to wasm targets. Note that using these commands could have other side effects on your toolchain. Please consider researching the clang toolchain and how it works before using them. We do not recommend copying and pasting commands without fully understanding the side-effects.
2621
```
2722
brew install llvm
2823
AR=/opt/homebrew/opt/llvm/bin/llvm-ar CC=/opt/homebrew/opt/llvm/bin/clang wasm-pack build --target web
2924
```
3025

31-
To test the wasm binary. First, install `http-server` web server (feel free to use any other web-server of your choosing):
26+
Also, install TypeScript:
27+
```
28+
npm install -g typescript
29+
```
30+
31+
### Compile the tests via `tsc`
3232

33+
In the wasm Cargo.toml directory, run:
3334
```
34-
cargo install http-server
35+
tsc --project js-bindings-test/tsconfig.json
3536
```
3637

37-
Then run the http server, and then choose the file `js-bindings/index.html`:
38+
### Run the tests in a web browser
39+
40+
To build the wasm package from the crate, run (in the wasm Cargo.toml directory):
3841

3942
```
40-
http-server --port 8080 --verbose
43+
wasm-pack build --target web
4144
```
4245

43-
If you're using a remote server, either tunnel to port 8080, or expose that port and run this (assuming you understand the security risks):
46+
To test the wasm binary, first install `http-server` web server (feel free to use any other web-server of your choosing):
4447

4548
```
46-
http-server --port 8080 --host 0.0.0.0 --verbose
49+
cargo install http-server
4750
```
4851

49-
The ported wasm functions are exported to the file `js-bindings/index.js` and used in the file `js-bindings/index.html` with a basic test/example in them using JavaScript. Use your browser's console to see the output.
52+
Then run the http server:
5053

51-
### To run in Node.js
54+
```
55+
http-server --port 8080
56+
```
5257

53-
Make sure you have wasm-pack and the wasm32-unknown-unknown target installed:
58+
If you're using a remote server, either tunnel to port 8080, or expose that port and run this (assuming you understand the security risks):
5459

5560
```
56-
rustup target add wasm32-unknown-unknown
57-
cargo install wasm-pack
61+
http-server --port 8080 --host 0.0.0.0
5862
```
5963

60-
Also make sure you have `clang` installed. It's required.
64+
To run test tests, choose the file `js-bindings-test/index.html` in the browser. Use browser's console to see the output.
65+
66+
### Run the tests in Node.js
6167

6268
To build the wasm package from the crate, run (in the wasm Cargo.toml directory):
6369

6470
```
6571
wasm-pack build --target nodejs
6672
```
6773

68-
Finally, to run the example, run:
74+
Finally, to run the tests, run:
75+
76+
```
77+
node --enable-source-maps js-bindings-test/node-entry.js
78+
```
79+
80+
### Run `knip`
81+
82+
We use `knip` to make sure that there are no unused exports in `js-bindings-test/tests` (which could
83+
mean that some of the tests are never run).
84+
85+
**Note: unused local definitions are caught by the TypeScript compiler itself, via the `noUnusedLocals` setting.**
6986

87+
To run `knip` locally, first install it:
7088
```
71-
node js-bindings/node-entry.js
89+
npm install -g knip
7290
```
7391

92+
And then run (in the wasm Cargo.toml directory):
93+
```
94+
(cd js-bindings-test && npx knip)
95+
```
96+
97+
**Note: to explicitly exclude an export from `knip`'s report, annotate it with `/** @public */`.**
98+
7499
### Further documentation on wasm
75100

76101
- https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm

0 commit comments

Comments
 (0)