Skip to content

Commit 8c8ea13

Browse files
committed
Refactor wasm_wrappers: rename some folders; move code to different files; tests now use TypeScript
1 parent 5234be0 commit 8c8ea13

File tree

16 files changed

+429
-372
lines changed

16 files changed

+429
-372
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ test/config.ini
4545

4646
# wasm
4747
wasm-wrappers/pkg/
48+
wasm-wrappers/js-bindings-test/test.js
49+
wasm-wrappers/js-bindings-test/test.js.map
4850

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

.github/workflows/wasm.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ jobs:
3535
uses: actions/setup-node@v3
3636
with:
3737
node-version: ${{ matrix.node-version }}
38+
- name: Install TypeScript
39+
run: npm install typescript
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
4651

4752
wasm_artifacts:
4853
runs-on: ubuntu-latest

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ test/config.ini
4343

4444
# wasm
4545
wasm-wrappers/pkg/
46+
wasm-wrappers/js-bindings-test/test.js
47+
wasm-wrappers/js-bindings-test/test.js.map
4648

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

build-tools/codecheck/codecheck.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ def check_files_end_with_newline():
325325
# list of files exempted from checks
326326
exempted_files = [
327327
'wasm-wrappers/doc/*',
328+
'wasm-wrappers/js-bindings-test/test.js',
328329
]
329330

330331
ok = True

wasm-wrappers/README.md

Lines changed: 27 additions & 21 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,48 +17,53 @@ 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+
### Running 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, and then choose the file `js-bindings-test/index.html`:
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+
The ported wasm functions are exported to the file `js-bindings-test/index.js` and used in the file `js-bindings-test/index.html` with a basic test/example in them using JavaScript. Use your browser's console to see the output.
65+
66+
### Running the tests in Node.js
6167

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

@@ -68,7 +74,7 @@ wasm-pack build --target nodejs
6874
Finally, to run the example, run:
6975

7076
```
71-
node js-bindings/node-entry.js
77+
node --enable-source-maps js-bindings-test/node-entry.js
7278
```
7379

7480
### Further documentation on wasm

wasm-wrappers/js-bindings/node-entry.js renamed to wasm-wrappers/js-bindings-test/node-entry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { run_test } from "./wasm_test.js";
1+
import { run_test } from "./test.js";
22

33
async function node_run() {
44
await run_test();

0 commit comments

Comments
 (0)