From 452366b0abbae346b2beec761c910b72f3dd2fd6 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 28 Dec 2017 23:06:19 +0200 Subject: Updated WASM build to use the new wasm32-unknown-unknown target It's only in the nightly compiler at the moment. --- Cargo.toml | 2 +- Makefile | 11 +++----- src/emscripten_api.rs | 26 ++++++++++++++++++ src/lib.rs | 12 ++++----- src/main.rs | 6 ++--- web/index.html | 75 ++++++++++++++++++++++++++------------------------- web/main.js | 72 +++++++++++++++++++++++++++++++++++++++++-------- 7 files changed, 139 insertions(+), 65 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f448215..680a811 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ authors = ["Justin Worthe "] [dependencies] bencher = "0.1.2" -[target.'cfg(not(target_os = "emscripten"))'.dependencies] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] portaudio = "0.7.0" gtk = "0.3.0" cairo-rs = "0.3.0" diff --git a/Makefile b/Makefile index 0f0d7e2..229494b 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,17 @@ all: build build-web: - cargo build --target=wasm32-unknown-emscripten --release + cargo +nightly build --target=wasm32-unknown-unknown --release mkdir -p target/site - cp target/wasm32-unknown-emscripten/release/rusty_microphone.js target/site/ - cp target/wasm32-unknown-emscripten/release/deps/*.wasm target/site/ + cp target/wasm32-unknown-unknown/release/*.wasm target/site/ cp web/* target/site/ build-web-debug: - cargo build --target=wasm32-unknown-emscripten + cargo +nightly build --target=wasm32-unknown-unknown mkdir -p target/site - cp target/wasm32-unknown-emscripten/debug/rusty_microphone.js target/site/ - cp target/wasm32-unknown-emscripten/debug/deps/*.wasm target/site/ + cp target/wasm32-unknown-unknown/debug/*.wasm target/site/ cp web/* target/site/ - build-desktop: cargo build --release diff --git a/src/emscripten_api.rs b/src/emscripten_api.rs index e296818..c7f980f 100644 --- a/src/emscripten_api.rs +++ b/src/emscripten_api.rs @@ -3,10 +3,36 @@ use signal::Signal; use pitch::Pitch; use std::os::raw::c_char; +use std::os::raw::c_void; +use std::mem; use std::ffi::CString; use std::slice; use std::f32; +#[no_mangle] +pub extern "C" fn malloc(size: usize) -> *mut c_void { + let mut buf = Vec::with_capacity(size); + let ptr = buf.as_mut_ptr(); + mem::forget(buf); + return ptr as *mut c_void; +} + +#[no_mangle] +pub extern "C" fn free(ptr: *mut c_void, cap: usize) { + unsafe { + // after it's in scope, it can go out of scope in the normal + // RAII cleanup. + let _buf = Vec::from_raw_parts(ptr, 0, cap); + } +} + +#[no_mangle] +pub extern "C" fn free_str(ptr: *mut c_char) { + unsafe { + let _ = CString::from_raw(ptr); + } +} + #[no_mangle] pub extern "C" fn find_fundamental_frequency(signal_ptr: *const f32, signal_length: isize, sample_rate: f32) -> f32 { let signal_slice = unsafe { diff --git a/src/lib.rs b/src/lib.rs index c2b73d1..f778f3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,18 +3,18 @@ pub mod signal; pub mod correlation; pub mod pitch; -#[cfg(not(target_os = "emscripten"))] +#[cfg(not(target_arch = "wasm32"))] extern crate gtk; -#[cfg(not(target_os = "emscripten"))] +#[cfg(not(target_arch = "wasm32"))] extern crate cairo; -#[cfg(not(target_os = "emscripten"))] +#[cfg(not(target_arch = "wasm32"))] pub mod gui; -#[cfg(not(target_os = "emscripten"))] +#[cfg(not(target_arch = "wasm32"))] extern crate portaudio; -#[cfg(not(target_os = "emscripten"))] +#[cfg(not(target_arch = "wasm32"))] pub mod audio; -#[cfg(target_os = "emscripten")] +#[cfg(target_arch = "wasm32")] pub mod emscripten_api; diff --git a/src/main.rs b/src/main.rs index cfc5629..a05c2f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ extern crate rusty_microphone; -#[cfg(not(target_os = "emscripten"))] +#[cfg(not(target_arch = "wasm32"))] fn main() { use rusty_microphone::*; @@ -11,7 +11,7 @@ fn main() { } } -#[cfg(target_os = "emscripten")] +#[cfg(target_arch = "wasm32")] fn main() { - println!("Hello Emscripten"); + println!("Hello Wasm"); } diff --git a/web/index.html b/web/index.html index d3f6a4b..2d957cc 100644 --- a/web/index.html +++ b/web/index.html @@ -1,47 +1,48 @@ + + - - -

Rusty Microphone

-
- Loading... -
-