Skip to content

Commit 9c753b4

Browse files
committed
Add Rust binding
Only support fizzy_validate so far.
1 parent 5687aef commit 9c753b4

7 files changed

Lines changed: 128 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/build
22
/cmake-build-*
33
/.idea
4+
Cargo.lock
5+
/target

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[workspace]
2+
members = [
3+
"bindings/rust"
4+
]

bindings/rust/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
/Cargo.lock

bindings/rust/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Fizzy: A fast WebAssembly interpreter
2+
# Copyright 2019-2020 The Fizzy Authors.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
[package]
6+
name = "fizzy"
7+
version = "0.6.0-dev"
8+
authors = ["Alex Beregszaszi <alex@rtfs.hu>"]
9+
license = "Apache-2.0"
10+
repository = "https://github.com/wasmx/fizzy"
11+
description = "Bindings to Fizzy"
12+
categories = ["external-ffi-bindings"]
13+
edition = "2018"
14+
15+
[build-dependencies]
16+
bindgen = "0.54.0"
17+
cmake = "0.1"

bindings/rust/build.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Fizzy: A fast WebAssembly interpreter
2+
// Copyright 2019-2020 The Fizzy Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
extern crate bindgen;
6+
extern crate cmake;
7+
8+
use cmake::Config;
9+
10+
use std::env;
11+
use std::path::PathBuf;
12+
13+
fn main() {
14+
// This is the root directory.
15+
let src = "../../";
16+
17+
let dst = Config::new(src).define("FIZZY_TESTING", "OFF").build();
18+
19+
println!("cargo:rustc-link-lib=static={}", "fizzy");
20+
println!("cargo:rustc-link-search=native={}/lib", dst.display());
21+
22+
// We need to link against C++ std lib
23+
if let Some(cpp_stdlib) = get_cpp_stdlib() {
24+
println!("cargo:rustc-link-lib={}", cpp_stdlib);
25+
}
26+
27+
let bindings = bindgen::Builder::default()
28+
.header(format!("{}/include/fizzy/fizzy.h", src))
29+
// See https://github.com/rust-lang-nursery/rust-bindgen/issues/947
30+
.trust_clang_mangling(false)
31+
.generate_comments(true)
32+
// https://github.com/rust-lang-nursery/rust-bindgen/issues/947#issuecomment-327100002
33+
.layout_tests(false)
34+
.whitelist_function("fizzy_.*")
35+
// TODO: consider removing this
36+
.size_t_is_usize(true)
37+
.generate()
38+
.expect("Unable to generate bindings");
39+
40+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
41+
bindings
42+
.write_to_file(out_path.join("bindings.rs"))
43+
.expect("Could not write bindings");
44+
}
45+
46+
// See https://github.com/alexcrichton/gcc-rs/blob/88ac58e25/src/lib.rs#L1197
47+
fn get_cpp_stdlib() -> Option<String> {
48+
env::var("TARGET").ok().and_then(|target| {
49+
if target.contains("msvc") {
50+
None
51+
} else if target.contains("darwin") {
52+
Some("c++".to_string())
53+
} else if target.contains("freebsd") {
54+
Some("c++".to_string())
55+
} else if target.contains("musl") {
56+
Some("static=stdc++".to_string())
57+
} else {
58+
Some("stdc++".to_string())
59+
}
60+
})
61+
}

bindings/rust/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Fizzy: A fast WebAssembly interpreter
2+
// Copyright 2019-2020 The Fizzy Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
mod sys;
6+
7+
pub fn validate(input: &[u8]) -> bool {
8+
unsafe { sys::fizzy_validate(input.as_ptr(), input.len()) }
9+
}
10+
11+
#[cfg(test)]
12+
mod tests {
13+
use super::*;
14+
15+
#[test]
16+
fn validate_wasm() {
17+
// Empty
18+
assert_eq!(validate(&[]), false);
19+
// Too short
20+
assert_eq!(validate(&[0x00]), false);
21+
// Valid
22+
assert_eq!(
23+
validate(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]),
24+
true
25+
);
26+
// Invalid version
27+
assert_eq!(
28+
validate(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x01]),
29+
false
30+
);
31+
}
32+
}

bindings/rust/src/sys.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Fizzy: A fast WebAssembly interpreter
2+
// Copyright 2019-2020 The Fizzy Authors.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#![allow(non_upper_case_globals)]
6+
#![allow(non_camel_case_types)]
7+
#![allow(non_snake_case)]
8+
9+
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

0 commit comments

Comments
 (0)