-
Notifications
You must be signed in to change notification settings - Fork 541
Enable wasm-bindgen feature with target wasm32-unknown-emscripten #1349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DouglasDwyer
wants to merge
5
commits into
RustAudio:master
Choose a base branch
from
DouglasDwyer:webaudio-reach-any-wasm-target
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3846df3
Enable the WebAudio host for `wasm32-unknown-emscripten`
DouglasDwyer 7f20ece
Enable Stream creation from background threads on Emscripten target
DouglasDwyer 22cf4cc
Eliminate unnecessary temporary variables
DouglasDwyer 16dd706
Run cargo fmt on webaudio mod
DouglasDwyer 31691df
Use main_thread::try_run to prevent panics
DouglasDwyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| //! Utility for invoking functions on the browser's main thread. | ||
| //! This allows for creating audio contexts within workers, | ||
| //! when normally having access to the window object is required. | ||
|
|
||
| #[cfg(target_os = "emscripten")] | ||
| pub use self::emscripten::*; | ||
|
|
||
| #[cfg(target_os = "unknown")] | ||
| pub use self::unknown::*; | ||
|
|
||
| /// Proxying implementation for `wasm32-unknown-emscripten`: will send | ||
| /// functions through Emscripten's queue to run on the main thread. | ||
| #[cfg(target_os = "emscripten")] | ||
| mod emscripten { | ||
| use std::ffi::c_void; | ||
|
|
||
| // Functions provided by `emscripten/proxying.h` and `emscripten/threading.h` | ||
| unsafe extern "C" { | ||
| /// Returns true if the current thread is the thread that hosts the Emscripten | ||
| /// runtime. | ||
| fn emscripten_is_main_runtime_thread() -> bool; | ||
|
|
||
| /// Returns the thread ID of the thread that hosts the Emscripten runtime. | ||
| fn emscripten_main_runtime_thread_id() -> usize; | ||
|
|
||
| /// Get the queue used for proxying low-level runtime work. | ||
| fn emscripten_proxy_get_system_queue() -> *mut c_void; | ||
|
|
||
| /// Enqueue `func` to be called with argument `arg` on the given queue | ||
| /// and thread then wait for `func` to be executed synchronously before returning. | ||
| fn emscripten_proxy_sync( | ||
| queue: *mut c_void, | ||
| target_thread: usize, | ||
| func: extern "C" fn(*mut c_void), | ||
| arg: *mut c_void, | ||
| ) -> bool; | ||
| } | ||
|
|
||
| /// Runs `func` on the browser main thread. For the Emscripten target, | ||
| /// always succeeds with [`Some`]. | ||
| pub fn try_run<F, R>(func: F) -> Option<R> | ||
| where | ||
| F: FnOnce() -> R + Send, | ||
| R: Send, | ||
| { | ||
| Some(run(func)) | ||
| } | ||
|
|
||
| /// Run `func` on the browser main thread and return its result, blocking | ||
| /// the caller until it completes. Runs inline when the caller already is | ||
| /// the main thread. | ||
| fn run<F, R>(func: F) -> R | ||
| where | ||
| F: FnOnce() -> R + Send, | ||
| R: Send, | ||
| { | ||
| /// Data that gets sent between the two browser threads. | ||
| struct SyncSlot<F, R> { | ||
| /// The function to execute. | ||
| func: Option<F>, | ||
| /// The value that was returned. | ||
| ret: Option<R>, | ||
| } | ||
|
|
||
| /// Callback that gets invoked on the main thread. | ||
| extern "C" fn trampoline<F, R>(arg: *mut c_void) | ||
| where | ||
| F: FnOnce() -> R, | ||
| { | ||
| // SAFETY: `arg` points at a `SyncSlot` on the calling thread's stack. | ||
| // `emscripten_proxy_sync` keeps that thread blocked until this returns, | ||
| // so the pointer stays valid and unaliased for the call. | ||
| let slot = unsafe { &mut *arg.cast::<SyncSlot<F, R>>() }; | ||
| let func = slot.func.take().expect("proxied task run twice"); | ||
| slot.ret = Some(func()); | ||
| } | ||
|
|
||
| // SAFETY: `func` and its return value are Send and can be moved | ||
| // between threads. `emscripten_proxy_sync` guarantees that it | ||
| // will invoke `trampoline` and not return until it is finished. | ||
| unsafe { | ||
| if emscripten_is_main_runtime_thread() { | ||
| return func(); | ||
| } | ||
|
|
||
| let mut slot = SyncSlot { | ||
| func: Some(func), | ||
| ret: None, | ||
| }; | ||
|
|
||
| let ok = emscripten_proxy_sync( | ||
| emscripten_proxy_get_system_queue(), | ||
| emscripten_main_runtime_thread_id(), | ||
| trampoline::<F, R>, | ||
| (&raw mut slot).cast(), | ||
| ); | ||
|
|
||
| assert!( | ||
| ok, | ||
| "emscripten_proxy_sync to the browser main thread failed" | ||
| ); | ||
| slot.ret.take().expect("proxied task did not run") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Proxying implementation for `wasm32-unknown-unknown`: will check to see | ||
| /// if closures are running on the main thread, and fail if ever called from a worker. | ||
| #[cfg(target_os = "unknown")] | ||
| mod unknown { | ||
| /// Attempts to run `func`. If this was not already the main browser thread, | ||
| /// then returns [`None`] because proxying is not possible on this target. | ||
| pub fn try_run<F, R>(func: F) -> Option<R> | ||
| where | ||
| F: FnOnce() -> R + Send, | ||
| R: Send, | ||
| { | ||
| if is_main_thread() { | ||
| Some(run(func)) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| /// Asserts that this is the main browser thread and runs `func`. | ||
| fn run<F, R>(func: F) -> R | ||
| where | ||
| F: FnOnce() -> R + Send, | ||
| R: Send, | ||
| { | ||
| assert!( | ||
| is_main_thread(), | ||
| "proxying closures is not supported on wasm32-unknown-unknown" | ||
| ); | ||
| func() | ||
| } | ||
|
|
||
| /// Whether this is the main browser thread. | ||
| fn is_main_thread() -> bool { | ||
| web_sys::window().is_some() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.