-
Notifications
You must be signed in to change notification settings - Fork 943
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly handle the case where Navigator.gpu is undefined and WebGPU …
…is the only compiled backend (#6197) * Properly handle the case where `Navigator.gpu` is undefined and WebGPU is the only compiled backend. Previously, `Instance::request_adapter` would invoke a wasm binding with an undefined arg0, thus crashing the program. Now it will cleanly return `None` instead. Fixes #6196. * Fix typo in `Instance::new` doc comment. * Add note to CHANGELOG.md * Introduce `DefinedNonNullJsValue` type. * Assert definedness of self.gpu in surface_get_capabilities. * Use DefinedNonNullJsValue in signature of get_browser_gpu_property(). * Clarify meaning of gpu field with a comment.
- Loading branch information
Showing
4 changed files
with
126 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,46 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use wasm_bindgen::JsValue; | ||
|
||
/// Derefs to a [`JsValue`] that's known not to be `undefined` or `null`. | ||
#[derive(Debug)] | ||
pub struct DefinedNonNullJsValue<T>(T); | ||
|
||
impl<T> DefinedNonNullJsValue<T> | ||
where | ||
T: AsRef<JsValue>, | ||
{ | ||
pub fn new(value: T) -> Option<Self> { | ||
if value.as_ref().is_undefined() || value.as_ref().is_null() { | ||
None | ||
} else { | ||
Some(Self(value)) | ||
} | ||
} | ||
} | ||
|
||
impl<T> Deref for DefinedNonNullJsValue<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T> DerefMut for DefinedNonNullJsValue<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl<T> AsRef<T> for DefinedNonNullJsValue<T> { | ||
fn as_ref(&self) -> &T { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T> AsMut<T> for DefinedNonNullJsValue<T> { | ||
fn as_mut(&mut self) -> &mut T { | ||
&mut self.0 | ||
} | ||
} |