Skip to content

Instantly share code, notes, and snippets.

@killercup
Last active November 29, 2017 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killercup/e4b5a1c63949f8b04e3e5a2e582ba537 to your computer and use it in GitHub Desktop.
Save killercup/e4b5a1c63949f8b04e3e5a2e582ba537 to your computer and use it in GitHub Desktop.
cargo build --target=wasm32-unknown-unknown --release
Finished release [optimized] target(s) in 0.0 secscp target/wasm32-unknown-unknown/release/semver_steve.wasm .
ls -lah *wasm
-rw-r--r-- 1 pascal staff 78K Nov 29 17:42 semver_steve.wasm
wasm-opt -Oz semver_steve.wasm -o semver_steve.wasm
ls -lah *wasm
-rw-r--r-- 1 pascal staff 73K Nov 29 17:42 semver_steve.wasm
wasm-gc semver_steve.wasm semver_steve.wasm
ls -lah *wasm
-rw-r--r-- 1 pascal staff 59K Nov 29 17:42 semver_steve.wasm
[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "semver-steve"
version = "0.1.0"
dependencies = [
"semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[package]
authors = ["Pascal Hertleif <killercup@gmail.com>"]
name = "semver-steve"
version = "0.1.0"
homepage = "https://www.steveklabnik.com/wasm/demos/semver.html"
[lib]
path = "lib.rs"
crate-type = ["cdylib"]
[dependencies]
semver = "0.9.0"
[profile.release]
opt-level = "s"
extern crate semver;
use semver::Version;
use semver::VersionReq;
use std::mem;
use std::ffi::CStr;
use std::os::raw::{c_char, c_void};
macro_rules! check {
($expr:expr) => (match $expr {
Ok(val) => val,
Err(_) => return false,
})
}
#[no_mangle]
pub extern fn is_match(v: *mut c_char, r: *mut c_char) -> bool {
unsafe {
let v = CStr::from_ptr(v);
let v = check!(v.to_str());
let r = CStr::from_ptr(r);
let r = check!(r.to_str());
let v = check!(Version::parse(v));
let r = check!(VersionReq::parse(r));
r.matches(&v)
}
}
// this shenanigans is due to it being tough to deal with JS strings into wasm,
// see the JS code for more.
#[no_mangle]
pub extern "C" fn alloc(size: usize) -> *mut c_void {
let mut buf = Vec::with_capacity(size);
let ptr = buf.as_mut_ptr();
mem::forget(buf); // This is JS' responsibility now
return ptr as *mut c_void;
}
#[no_mangle]
pub extern "C" fn dealloc(ptr: *mut c_void, cap: usize) {
unsafe {
let _buf = Vec::from_raw_parts(ptr, 0, cap);
}
}
#[cfg(test)]
mod tests {
use std::ffi::CString;
use super::is_match;
#[test]
fn matches() {
// these are going to leak but it's two tests so I don't care
let v = CString::new("1.0.0").unwrap().into_raw();
let r = CString::new(">= 1.0.0").unwrap().into_raw();
assert!(is_match(v, r));
}
#[test]
fn does_not_match() {
// these are going to leak but it's two tests so I don't care
let v = CString::new("0.1.0").unwrap().into_raw();
let r = CString::new(">= 1.0.0").unwrap().into_raw();
assert!(!is_match(v, r));
}
}
build:
cargo build --target=wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/semver_steve.wasm .
ls -lah *wasm
wasm-opt -Oz semver_steve.wasm -o semver_steve.wasm
ls -lah *wasm
wasm-gc semver_steve.wasm semver_steve.wasm
ls -lah *wasm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment