Skip to content

Commit b14726b

Browse files
committed
Set up example w/ wasm-bindgen, clang 8 wasm target
1 parent ec6565c commit b14726b

File tree

10 files changed

+214
-12
lines changed

10 files changed

+214
-12
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/target
22
**/*.rs.bk
3+
node_modules
4+
pkg
5+
package-lock.json

Cargo.lock

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ edition = "2018"
66

77
[lib]
88
name = "wasm_lib_with_c"
9-
path = "src/lib.rs"
10-
crate-type = ["staticlib"]
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
wasm-bindgen = "0.2.51"
1113

1214
[build-dependencies]
1315
cc = "1.0"

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
### Setup
2+
3+
1. Run `npm install` to install the `TextEncoder` polyfill.
4+
2. Install Rust and the `wasm32-unknown-unknown` target with `rustup`.
5+
3. Install LLVM >= 8.0. ([downloads](http://releases.llvm.org/download.html))
6+
4. Make sure the `PATH` variable in `test.sh` points to LLVM >= 8.0
7+
8+
### Testing
9+
10+
The `test.sh` script will compile this project to Wasm/JS and test it with Node.

build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
extern crate cc;
22

33
fn main() {
4-
let mut config = cc::Build::new();
5-
config
4+
cc::Build::new()
65
.file("src/lib.c")
7-
.compile("wasm-lib-with-c");
6+
.compile("wasm-c-component");
87
}

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"text-encoding": "0.7"
4+
}
5+
}

src/lib.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#include <stdint.h>
22

3-
uint32_t increment_in_c(uint32_t i) {
4-
return i + 1;
3+
typedef struct {
4+
uint32_t line;
5+
uint32_t column;
6+
} Point;
7+
8+
Point add_points_in_c(Point a, Point b) {
9+
if (b.line > 0) {
10+
return (Point) {a.line + b.line, b.column};
11+
} else {
12+
return (Point) {a.line, a.column + b.column};
13+
}
514
}

src/lib.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
#[repr(C)]
5+
pub struct Point {
6+
pub line: u32,
7+
pub column: u32,
8+
}
9+
110
extern "C" {
2-
#[no_mangle]
3-
fn increment_in_c(i: u32) -> u32;
11+
fn add_points_in_c(a: Point, b: Point) -> Point;
12+
}
13+
14+
#[wasm_bindgen]
15+
pub fn add_points_in_rust(a: Point, b: Point) -> Point {
16+
unsafe { add_points_in_c(a, b) }
417
}
518

6-
#[no_mangle]
7-
extern "C" fn increment_in_rust(i: u32) -> u32 {
8-
unsafe { increment_in_c(i) }
19+
#[wasm_bindgen]
20+
pub fn new_point(line: u32, column: u32) -> Point {
21+
Point { line, column }
922
}

test.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import init, {add_points_in_rust, new_point} from "./pkg/wasm_lib_with_c.mjs"
2+
3+
run();
4+
5+
async function run() {
6+
await init('./pkg/wasm_lib_with_c_bg.wasm');
7+
8+
let p = add_points_in_rust(new_point(4, 5), new_point(1, 10));
9+
console.log(p.line, p.column);
10+
11+
p = add_points_in_rust(new_point(4, 5), new_point(0, 10));
12+
console.log(p.line, p.column);
13+
}

test.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
# Must be LLVM version >= 8
4+
export PATH=/opt/local/llvm/bin:${PATH}
5+
6+
# Compile the Rust/C into JS/Wasm
7+
AR=llvm-ar cargo build --target=wasm32-unknown-unknown
8+
wasm-bindgen \
9+
target/wasm32-unknown-unknown/debug/wasm_lib_with_c.wasm \
10+
--target web \
11+
--out-dir pkg
12+
13+
# Convert the generated `.js` file into a node-compatible `.mjs` file.
14+
cat > pkg/wasm_lib_with_c.mjs <<JS
15+
import encoding from 'text-encoding';
16+
import fs from 'fs';
17+
18+
const {TextDecoder} = encoding;
19+
20+
async function fetch(path) {
21+
return {
22+
async arrayBuffer() {
23+
return fs.readFileSync(path);
24+
}
25+
}
26+
}
27+
JS
28+
cat pkg/wasm_lib_with_c.js >> pkg/wasm_lib_with_c.mjs
29+
30+
# Run the script with Node.
31+
node --experimental-modules test.mjs

0 commit comments

Comments
 (0)