diff options
Diffstat (limited to 'vendor/wasm-bindgen/src/lib.rs')
-rw-r--r-- | vendor/wasm-bindgen/src/lib.rs | 107 |
1 files changed, 75 insertions, 32 deletions
diff --git a/vendor/wasm-bindgen/src/lib.rs b/vendor/wasm-bindgen/src/lib.rs index f8576ed4c..fd2feae6f 100644 --- a/vendor/wasm-bindgen/src/lib.rs +++ b/vendor/wasm-bindgen/src/lib.rs @@ -95,7 +95,7 @@ pub struct JsValue { } const JSIDX_OFFSET: u32 = 128; // keep in sync with js/mod.rs -const JSIDX_UNDEFINED: u32 = JSIDX_OFFSET + 0; +const JSIDX_UNDEFINED: u32 = JSIDX_OFFSET; const JSIDX_NULL: u32 = JSIDX_OFFSET + 1; const JSIDX_TRUE: u32 = JSIDX_OFFSET + 2; const JSIDX_FALSE: u32 = JSIDX_OFFSET + 3; @@ -103,31 +103,19 @@ const JSIDX_RESERVED: u32 = JSIDX_OFFSET + 4; impl JsValue { /// The `null` JS value constant. - pub const NULL: JsValue = JsValue { - idx: JSIDX_NULL, - _marker: marker::PhantomData, - }; + pub const NULL: JsValue = JsValue::_new(JSIDX_NULL); /// The `undefined` JS value constant. - pub const UNDEFINED: JsValue = JsValue { - idx: JSIDX_UNDEFINED, - _marker: marker::PhantomData, - }; + pub const UNDEFINED: JsValue = JsValue::_new(JSIDX_UNDEFINED); /// The `true` JS value constant. - pub const TRUE: JsValue = JsValue { - idx: JSIDX_TRUE, - _marker: marker::PhantomData, - }; + pub const TRUE: JsValue = JsValue::_new(JSIDX_TRUE); /// The `false` JS value constant. - pub const FALSE: JsValue = JsValue { - idx: JSIDX_FALSE, - _marker: marker::PhantomData, - }; + pub const FALSE: JsValue = JsValue::_new(JSIDX_FALSE); #[inline] - fn _new(idx: u32) -> JsValue { + const fn _new(idx: u32) -> JsValue { JsValue { idx, _marker: marker::PhantomData, @@ -138,6 +126,7 @@ impl JsValue { /// /// The utf-8 string provided is copied to the JS heap and the string will /// be owned by the JS garbage collector. + #[allow(clippy::should_implement_trait)] // cannot fix without breaking change #[inline] pub fn from_str(s: &str) -> JsValue { unsafe { JsValue::_new(__wbindgen_string_new(s.as_ptr(), s.len())) } @@ -166,7 +155,7 @@ impl JsValue { /// This function creates a JS object representing a boolean (a heap /// allocated boolean) and returns a handle to the JS version of it. #[inline] - pub fn from_bool(b: bool) -> JsValue { + pub const fn from_bool(b: bool) -> JsValue { if b { JsValue::TRUE } else { @@ -176,13 +165,13 @@ impl JsValue { /// Creates a new JS value representing `undefined`. #[inline] - pub fn undefined() -> JsValue { + pub const fn undefined() -> JsValue { JsValue::UNDEFINED } /// Creates a new JS value representing `null`. #[inline] - pub fn null() -> JsValue { + pub const fn null() -> JsValue { JsValue::NULL } @@ -620,10 +609,10 @@ impl TryFrom<&JsValue> for f64 { #[inline] fn try_from(val: &JsValue) -> Result<Self, Self::Error> { let jsval = unsafe { JsValue::_new(__wbindgen_try_into_number(val.idx)) }; - return match jsval.as_f64() { + match jsval.as_f64() { Some(num) => Ok(num), None => Err(jsval), - }; + } } } @@ -1082,6 +1071,7 @@ externs! { fn __wbindgen_not(idx: u32) -> u32; + fn __wbindgen_exports() -> u32; fn __wbindgen_memory() -> u32; fn __wbindgen_module() -> u32; fn __wbindgen_function_table() -> u32; @@ -1308,17 +1298,31 @@ pub fn anyref_heap_live_count() -> u32 { pub trait UnwrapThrowExt<T>: Sized { /// Unwrap this `Option` or `Result`, but instead of panicking on failure, /// throw an exception to JavaScript. + #[cfg_attr(debug_assertions, track_caller)] fn unwrap_throw(self) -> T { - self.expect_throw("`unwrap_throw` failed") + if cfg!(all(debug_assertions, feature = "std")) { + let loc = core::panic::Location::caller(); + let msg = std::format!( + "`unwrap_throw` failed ({}:{}:{})", + loc.file(), + loc.line(), + loc.column() + ); + self.expect_throw(&msg) + } else { + self.expect_throw("`unwrap_throw` failed") + } } /// Unwrap this container's `T` value, or throw an error to JS with the /// given message if the `T` value is unavailable (e.g. an `Option<T>` is /// `None`). + #[cfg_attr(debug_assertions, track_caller)] fn expect_throw(self, message: &str) -> T; } impl<T> UnwrapThrowExt<T> for Option<T> { + #[cfg_attr(debug_assertions, track_caller)] fn expect_throw(self, message: &str) -> T { if cfg!(all(target_arch = "wasm32", not(target_os = "emscripten"))) { match self { @@ -1335,6 +1339,7 @@ impl<T, E> UnwrapThrowExt<T> for Result<T, E> where E: core::fmt::Debug, { + #[cfg_attr(debug_assertions, track_caller)] fn expect_throw(self, message: &str) -> T { if cfg!(all(target_arch = "wasm32", not(target_os = "emscripten"))) { match self { @@ -1358,6 +1363,11 @@ pub fn module() -> JsValue { unsafe { JsValue::_new(__wbindgen_module()) } } +/// Returns a handle to this wasm instance's `WebAssembly.Instance.prototype.exports` +pub fn exports() -> JsValue { + unsafe { JsValue::_new(__wbindgen_exports()) } +} + /// Returns a handle to this wasm instance's `WebAssembly.Memory` pub fn memory() -> JsValue { unsafe { JsValue::_new(__wbindgen_memory()) } @@ -1374,6 +1384,7 @@ pub mod __rt { use crate::JsValue; use core::borrow::{Borrow, BorrowMut}; use core::cell::{Cell, UnsafeCell}; + use core::convert::Infallible; use core::ops::{Deref, DerefMut}; pub extern crate core; @@ -1555,11 +1566,9 @@ pub mod __rt { if_std! { use std::alloc::{alloc, dealloc, realloc, Layout}; - use std::mem; #[no_mangle] - pub extern "C" fn __wbindgen_malloc(size: usize) -> *mut u8 { - let align = mem::align_of::<usize>(); + pub extern "C" fn __wbindgen_malloc(size: usize, align: usize) -> *mut u8 { if let Ok(layout) = Layout::from_size_align(size, align) { unsafe { if layout.size() > 0 { @@ -1577,8 +1586,7 @@ pub mod __rt { } #[no_mangle] - pub unsafe extern "C" fn __wbindgen_realloc(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 { - let align = mem::align_of::<usize>(); + pub unsafe extern "C" fn __wbindgen_realloc(ptr: *mut u8, old_size: usize, new_size: usize, align: usize) -> *mut u8 { debug_assert!(old_size > 0); debug_assert!(new_size > 0); if let Ok(layout) = Layout::from_size_align(old_size, align) { @@ -1600,13 +1608,12 @@ pub mod __rt { } #[no_mangle] - pub unsafe extern "C" fn __wbindgen_free(ptr: *mut u8, size: usize) { + pub unsafe extern "C" fn __wbindgen_free(ptr: *mut u8, size: usize, align: usize) { // This happens for zero-length slices, and in that case `ptr` is // likely bogus so don't actually send this to the system allocator if size == 0 { return } - let align = mem::align_of::<usize>(); let layout = Layout::from_size_align_unchecked(size, align); dealloc(ptr, layout); } @@ -1667,7 +1674,7 @@ pub mod __rt { }; GLOBAL_EXNDATA[0] = 0; GLOBAL_EXNDATA[1] = 0; - return ret; + ret } } @@ -1728,6 +1735,42 @@ pub mod __rt { } } } + + /// An internal helper struct for usage in `#[wasm_bindgen(main)]` + /// functions to throw the error (if it is `Err`). + pub struct MainWrapper<T>(pub Option<T>); + + pub trait Main { + fn __wasm_bindgen_main(&mut self); + } + + impl Main for &mut &mut MainWrapper<()> { + #[inline] + fn __wasm_bindgen_main(&mut self) {} + } + + impl Main for &mut &mut MainWrapper<Infallible> { + #[inline] + fn __wasm_bindgen_main(&mut self) {} + } + + impl<E: Into<JsValue>> Main for &mut &mut MainWrapper<Result<(), E>> { + #[inline] + fn __wasm_bindgen_main(&mut self) { + if let Err(e) = self.0.take().unwrap() { + crate::throw_val(e.into()); + } + } + } + + impl<E: std::fmt::Debug> Main for &mut MainWrapper<Result<(), E>> { + #[inline] + fn __wasm_bindgen_main(&mut self) { + if let Err(e) = self.0.take().unwrap() { + crate::throw_str(&std::format!("{:?}", e)); + } + } + } } /// A wrapper type around slices and vectors for binding the `Uint8ClampedArray` |