Skip to main content

wasm_bindgen/
lib.rs

1//! Runtime support for the `wasm-bindgen` tool
2//!
3//! This crate contains the runtime support necessary for `wasm-bindgen` the
4//! attribute and tool. Crates pull in the `#[wasm_bindgen]` attribute through
5//! this crate and this crate also provides JS bindings through the `JsValue`
6//! interface.
7//!
8//! ## Features
9//!
10//! ### `enable-interning`
11//!
12//! Enables the internal cache for [`wasm_bindgen::intern`].
13//!
14//! This feature currently enables the `std` feature, meaning that it is not
15//! compatible with `no_std` environments.
16//!
17//! ### `std` (default)
18//!
19//! Enabling this feature will make the crate depend on the Rust standard library.
20//!
21//! Disable this feature to use this crate in `no_std` environments.
22//!
23//! ### `strict-macro`
24//!
25//! All warnings the `#[wasm_bindgen]` macro emits are turned into hard errors.
26//! This mainly affects unused attribute options.
27//!
28//! ### Deprecated features
29//!
30//! #### `serde-serialize`
31//!
32//! **Deprecated:** Use the [`serde-wasm-bindgen`](https://docs.rs/serde-wasm-bindgen/latest/serde_wasm_bindgen/) crate instead.
33//!
34//! Enables the `JsValue::from_serde` and `JsValue::into_serde` methods for
35//! serializing and deserializing Rust types to and from JavaScript.
36//!
37//! #### `spans`
38//!
39//! **Deprecated:** This feature became a no-op in wasm-bindgen v0.2.20 (Sep 7, 2018).
40
41#![no_std]
42#![cfg_attr(wasm_bindgen_unstable_test_coverage, feature(coverage_attribute))]
43#![cfg_attr(target_feature = "atomics", feature(thread_local))]
44#![cfg_attr(
45    any(target_feature = "atomics", wasm_bindgen_unstable_test_coverage),
46    feature(allow_internal_unstable),
47    allow(internal_features)
48)]
49#![cfg_attr(
50    all(not(debug_assertions), not(feature = "std"), target_arch = "wasm64"),
51    feature(simd_wasm64)
52)]
53#![doc(html_root_url = "https://docs.rs/wasm-bindgen/0.2")]
54
55extern crate alloc;
56#[cfg(feature = "std")]
57extern crate std;
58
59use crate::convert::{TryFromJsValue, UpcastFrom, VectorIntoWasmAbi};
60use crate::sys::Promising;
61use alloc::boxed::Box;
62use alloc::string::String;
63use alloc::vec::Vec;
64use core::convert::TryFrom;
65use core::marker::PhantomData;
66use core::ops::{
67    Add, BitAnd, BitOr, BitXor, Deref, DerefMut, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub,
68};
69use core::ptr::NonNull;
70
71const _: () = {
72    /// Dummy empty function provided in order to detect linker-injected functions like `__wasm_call_ctors` and others that should be skipped by the wasm-bindgen interpreter.
73    ///
74    /// ## About `__wasm_call_ctors`
75    ///
76    /// There are several ways `__wasm_call_ctors` is introduced by the linker:
77    ///
78    /// * Using `#[link_section = ".init_array"]`;
79    /// * Linking with a C library that uses `__attribute__((constructor))`.
80    ///
81    /// The Wasm linker will insert a call to the `__wasm_call_ctors` function at the beginning of every
82    /// function that your module exports if it regards a module as having "command-style linkage".
83    /// Specifically, it regards a module as having "command-style linkage" if:
84    ///
85    /// * it is not relocatable;
86    /// * it is not a position-independent executable;
87    /// * and it does not call `__wasm_call_ctors`, directly or indirectly, from any
88    ///   exported function.
89    #[no_mangle]
90    pub extern "C" fn __wbindgen_skip_interpret_calls() {}
91};
92
93macro_rules! externs {
94    ($(#[$attr:meta])* extern "C" { $(fn $name:ident($($args:tt)*) -> $ret:ty;)* }) => (
95        #[cfg(target_family = "wasm")]
96        $(#[$attr])*
97        extern "C" {
98            $(fn $name($($args)*) -> $ret;)*
99        }
100
101        $(
102            #[cfg(not(target_family = "wasm"))]
103            #[allow(unused_variables)]
104            unsafe extern "C" fn $name($($args)*) -> $ret {
105                panic!("function not implemented on non-wasm32 targets")
106            }
107        )*
108    )
109}
110
111/// A module which is typically glob imported.
112///
113/// ```
114/// use wasm_bindgen::prelude::*;
115/// ```
116pub mod prelude {
117    pub use crate::closure::{Closure, ScopedClosure};
118    pub use crate::convert::Upcast; // provides upcast() and upcast_ref()
119    pub use crate::JsCast;
120    pub use crate::JsValue;
121    pub use crate::UnwrapThrowExt;
122    #[doc(hidden)]
123    pub use wasm_bindgen_macro::__wasm_bindgen_class_marker;
124    pub use wasm_bindgen_macro::wasm_bindgen;
125
126    pub use crate::JsError;
127}
128
129pub use wasm_bindgen_macro::link_to;
130
131pub mod closure;
132pub mod convert;
133pub mod describe;
134mod link;
135pub mod sys;
136
137#[cfg(wbg_reference_types)]
138mod externref;
139#[cfg(wbg_reference_types)]
140use externref::__wbindgen_externref_heap_live_count;
141
142pub use crate::__rt::marker::ErasableGeneric;
143pub use crate::convert::{IntoJsGeneric, JsGeneric};
144
145#[doc(hidden)]
146pub mod handler;
147
148mod cast;
149pub use crate::cast::JsCast;
150
151mod parent;
152pub use crate::parent::Parent;
153
154mod cache;
155pub use cache::intern::{intern, unintern};
156
157#[doc(hidden)]
158#[path = "rt/mod.rs"]
159pub mod __rt;
160use __rt::wbg_cast;
161
162/// Representation of an object owned by JS.
163///
164/// A `JsValue` doesn't actually live in Rust right now but actually in a table
165/// owned by the `wasm-bindgen` generated JS glue code. Eventually the ownership
166/// will transfer into Wasm directly and this will likely become more efficient,
167/// but for now it may be slightly slow.
168pub struct JsValue {
169    idx: u32,
170    _marker: PhantomData<*mut u8>, // not at all threadsafe
171}
172
173#[cfg(not(target_feature = "atomics"))]
174unsafe impl Send for JsValue {}
175#[cfg(not(target_feature = "atomics"))]
176unsafe impl Sync for JsValue {}
177
178unsafe impl ErasableGeneric for JsValue {
179    type Repr = JsValue;
180}
181
182impl Promising for JsValue {
183    type Resolution = JsValue;
184}
185
186impl JsValue {
187    /// The `null` JS value constant.
188    pub const NULL: JsValue = JsValue::_new(__rt::JSIDX_NULL);
189
190    /// The `undefined` JS value constant.
191    pub const UNDEFINED: JsValue = JsValue::_new(__rt::JSIDX_UNDEFINED);
192
193    /// The `true` JS value constant.
194    pub const TRUE: JsValue = JsValue::_new(__rt::JSIDX_TRUE);
195
196    /// The `false` JS value constant.
197    pub const FALSE: JsValue = JsValue::_new(__rt::JSIDX_FALSE);
198
199    #[inline]
200    const fn _new(idx: u32) -> JsValue {
201        JsValue {
202            idx,
203            _marker: PhantomData,
204        }
205    }
206
207    /// Creates a new JS value which is a string.
208    ///
209    /// The utf-8 string provided is copied to the JS heap and the string will
210    /// be owned by the JS garbage collector.
211    #[allow(clippy::should_implement_trait)] // cannot fix without breaking change
212    #[inline]
213    pub fn from_str(s: &str) -> JsValue {
214        wbg_cast(s)
215    }
216
217    /// Creates a new JS value which is a number.
218    ///
219    /// This function creates a JS value representing a number (a heap
220    /// allocated number) and returns a handle to the JS version of it.
221    #[inline]
222    pub fn from_f64(n: f64) -> JsValue {
223        wbg_cast(n)
224    }
225
226    /// Creates a new JS value which is a bigint from a string representing a number.
227    ///
228    /// This function creates a JS value representing a bigint (a heap
229    /// allocated large integer) and returns a handle to the JS version of it.
230    #[inline]
231    pub fn bigint_from_str(s: &str) -> JsValue {
232        __wbindgen_bigint_from_str(s)
233    }
234
235    /// Creates a new JS value which is a boolean.
236    ///
237    /// This function creates a JS object representing a boolean (a heap
238    /// allocated boolean) and returns a handle to the JS version of it.
239    #[inline]
240    pub const fn from_bool(b: bool) -> JsValue {
241        if b {
242            JsValue::TRUE
243        } else {
244            JsValue::FALSE
245        }
246    }
247
248    /// Creates a new JS value representing `undefined`.
249    #[inline]
250    pub const fn undefined() -> JsValue {
251        JsValue::UNDEFINED
252    }
253
254    /// Creates a new JS value representing `null`.
255    #[inline]
256    pub const fn null() -> JsValue {
257        JsValue::NULL
258    }
259
260    /// Creates a new JS symbol with the optional description specified.
261    ///
262    /// This function will invoke the `Symbol` constructor in JS and return the
263    /// JS object corresponding to the symbol created.
264    pub fn symbol(description: Option<&str>) -> JsValue {
265        __wbindgen_symbol_new(description)
266    }
267
268    /// Creates a new `JsValue` from the JSON serialization of the object `t`
269    /// provided.
270    ///
271    /// **This function is deprecated**, due to [creating a dependency cycle in
272    /// some circumstances][dep-cycle-issue]. Use [`serde-wasm-bindgen`] or
273    /// [`gloo_utils::format::JsValueSerdeExt`] instead.
274    ///
275    /// [dep-cycle-issue]: https://github.com/wasm-bindgen/wasm-bindgen/issues/2770
276    /// [`serde-wasm-bindgen`]: https://docs.rs/serde-wasm-bindgen
277    /// [`gloo_utils::format::JsValueSerdeExt`]: https://docs.rs/gloo-utils/latest/gloo_utils/format/trait.JsValueSerdeExt.html
278    ///
279    /// This function will serialize the provided value `t` to a JSON string,
280    /// send the JSON string to JS, parse it into a JS object, and then return
281    /// a handle to the JS object. This is unlikely to be super speedy so it's
282    /// not recommended for large payloads, but it's a nice to have in some
283    /// situations!
284    ///
285    /// Usage of this API requires activating the `serde-serialize` feature of
286    /// the `wasm-bindgen` crate.
287    ///
288    /// # Errors
289    ///
290    /// Returns any error encountered when serializing `T` into JSON.
291    #[cfg(feature = "serde-serialize")]
292    #[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
293    pub fn from_serde<T>(t: &T) -> serde_json::Result<JsValue>
294    where
295        T: serde::ser::Serialize + ?Sized,
296    {
297        let s = serde_json::to_string(t)?;
298        Ok(__wbindgen_json_parse(s))
299    }
300
301    /// Invokes `JSON.stringify` on this value and then parses the resulting
302    /// JSON into an arbitrary Rust value.
303    ///
304    /// **This function is deprecated**, due to [creating a dependency cycle in
305    /// some circumstances][dep-cycle-issue]. Use [`serde-wasm-bindgen`] or
306    /// [`gloo_utils::format::JsValueSerdeExt`] instead.
307    ///
308    /// [dep-cycle-issue]: https://github.com/wasm-bindgen/wasm-bindgen/issues/2770
309    /// [`serde-wasm-bindgen`]: https://docs.rs/serde-wasm-bindgen
310    /// [`gloo_utils::format::JsValueSerdeExt`]: https://docs.rs/gloo-utils/latest/gloo_utils/format/trait.JsValueSerdeExt.html
311    ///
312    /// This function will first call `JSON.stringify` on the `JsValue` itself.
313    /// The resulting string is then passed into Rust which then parses it as
314    /// JSON into the resulting value.
315    ///
316    /// Usage of this API requires activating the `serde-serialize` feature of
317    /// the `wasm-bindgen` crate.
318    ///
319    /// # Errors
320    ///
321    /// Returns any error encountered when parsing the JSON into a `T`.
322    #[cfg(feature = "serde-serialize")]
323    #[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
324    pub fn into_serde<T>(&self) -> serde_json::Result<T>
325    where
326        T: for<'a> serde::de::Deserialize<'a>,
327    {
328        let s = __wbindgen_json_serialize(self);
329        // Turns out `JSON.stringify(undefined) === undefined`, so if
330        // we're passed `undefined` reinterpret it as `null` for JSON
331        // purposes.
332        serde_json::from_str(s.as_deref().unwrap_or("null"))
333    }
334
335    /// Returns the `f64` value of this JS value if it's an instance of a
336    /// number.
337    ///
338    /// If this JS value is not an instance of a number then this returns
339    /// `None`.
340    #[inline]
341    pub fn as_f64(&self) -> Option<f64> {
342        __wbindgen_number_get(self)
343    }
344
345    /// Tests whether this JS value is a JS string.
346    #[inline]
347    pub fn is_string(&self) -> bool {
348        __wbindgen_is_string(self)
349    }
350
351    /// If this JS value is a string value, this function copies the JS string
352    /// value into Wasm linear memory, encoded as UTF-8, and returns it as a
353    /// Rust `String`.
354    ///
355    /// To avoid the copying and re-encoding, consider the
356    /// `JsString::try_from()` function from [js-sys](https://docs.rs/js-sys)
357    /// instead.
358    ///
359    /// If this JS value is not an instance of a string or if it's not valid
360    /// utf-8 then this returns `None`.
361    ///
362    /// # UTF-16 vs UTF-8
363    ///
364    /// JavaScript strings in general are encoded as UTF-16, but Rust strings
365    /// are encoded as UTF-8. This can cause the Rust string to look a bit
366    /// different than the JS string sometimes. For more details see the
367    /// [documentation about the `str` type][caveats] which contains a few
368    /// caveats about the encodings.
369    ///
370    /// [caveats]: https://wasm-bindgen.github.io/wasm-bindgen/reference/types/str.html
371    #[inline]
372    pub fn as_string(&self) -> Option<String> {
373        __wbindgen_string_get(self)
374    }
375
376    /// Returns the `bool` value of this JS value if it's an instance of a
377    /// boolean.
378    ///
379    /// If this JS value is not an instance of a boolean then this returns
380    /// `None`.
381    #[inline]
382    pub fn as_bool(&self) -> Option<bool> {
383        __wbindgen_boolean_get(self)
384    }
385
386    /// Tests whether this JS value is `null`
387    #[inline]
388    pub fn is_null(&self) -> bool {
389        __wbindgen_is_null(self)
390    }
391
392    /// Tests whether this JS value is `undefined`
393    #[inline]
394    pub fn is_undefined(&self) -> bool {
395        __wbindgen_is_undefined(self)
396    }
397
398    /// Tests whether this JS value is `null` or `undefined`
399    #[inline]
400    pub fn is_null_or_undefined(&self) -> bool {
401        __wbindgen_is_null_or_undefined(self)
402    }
403
404    /// Tests whether the type of this JS value is `symbol`
405    #[inline]
406    pub fn is_symbol(&self) -> bool {
407        __wbindgen_is_symbol(self)
408    }
409
410    /// Tests whether `typeof self == "object" && self !== null`.
411    #[inline]
412    pub fn is_object(&self) -> bool {
413        __wbindgen_is_object(self)
414    }
415
416    /// Tests whether this JS value is an instance of Array.
417    #[inline]
418    pub fn is_array(&self) -> bool {
419        __wbindgen_is_array(self)
420    }
421
422    /// Tests whether the type of this JS value is `function`.
423    #[inline]
424    pub fn is_function(&self) -> bool {
425        __wbindgen_is_function(self)
426    }
427
428    /// Tests whether the type of this JS value is `bigint`.
429    #[inline]
430    pub fn is_bigint(&self) -> bool {
431        __wbindgen_is_bigint(self)
432    }
433
434    /// Applies the unary `typeof` JS operator on a `JsValue`.
435    ///
436    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
437    #[inline]
438    pub fn js_typeof(&self) -> JsValue {
439        __wbindgen_typeof(self)
440    }
441
442    /// Applies the binary `in` JS operator on the two `JsValue`s.
443    ///
444    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in)
445    #[inline]
446    pub fn js_in(&self, obj: &JsValue) -> bool {
447        __wbindgen_in(self, obj)
448    }
449
450    /// Tests whether the value is ["truthy"].
451    ///
452    /// ["truthy"]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
453    #[inline]
454    pub fn is_truthy(&self) -> bool {
455        !self.is_falsy()
456    }
457
458    /// Tests whether the value is ["falsy"].
459    ///
460    /// ["falsy"]: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
461    #[inline]
462    pub fn is_falsy(&self) -> bool {
463        __wbindgen_is_falsy(self)
464    }
465
466    /// Get a string representation of the JavaScript object for debugging.
467    fn as_debug_string(&self) -> String {
468        __wbindgen_debug_string(self)
469    }
470
471    /// Compare two `JsValue`s for equality, using the `==` operator in JS.
472    ///
473    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality)
474    #[inline]
475    pub fn loose_eq(&self, other: &Self) -> bool {
476        __wbindgen_jsval_loose_eq(self, other)
477    }
478
479    /// Applies the unary `~` JS operator on a `JsValue`.
480    ///
481    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT)
482    #[inline]
483    pub fn bit_not(&self) -> JsValue {
484        __wbindgen_bit_not(self)
485    }
486
487    /// Applies the binary `>>>` JS operator on the two `JsValue`s.
488    ///
489    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unsigned_right_shift)
490    #[inline]
491    pub fn unsigned_shr(&self, rhs: &Self) -> u32 {
492        __wbindgen_unsigned_shr(self, rhs)
493    }
494
495    /// Applies the binary `/` JS operator on two `JsValue`s, catching and returning any `RangeError` thrown.
496    ///
497    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Division)
498    #[inline]
499    pub fn checked_div(&self, rhs: &Self) -> Self {
500        __wbindgen_checked_div(self, rhs)
501    }
502
503    /// Applies the binary `**` JS operator on the two `JsValue`s.
504    ///
505    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation)
506    #[inline]
507    pub fn pow(&self, rhs: &Self) -> Self {
508        __wbindgen_pow(self, rhs)
509    }
510
511    /// Applies the binary `<` JS operator on the two `JsValue`s.
512    ///
513    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than)
514    #[inline]
515    pub fn lt(&self, other: &Self) -> bool {
516        __wbindgen_lt(self, other)
517    }
518
519    /// Applies the binary `<=` JS operator on the two `JsValue`s.
520    ///
521    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than_or_equal)
522    #[inline]
523    pub fn le(&self, other: &Self) -> bool {
524        __wbindgen_le(self, other)
525    }
526
527    /// Applies the binary `>=` JS operator on the two `JsValue`s.
528    ///
529    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than_or_equal)
530    #[inline]
531    pub fn ge(&self, other: &Self) -> bool {
532        __wbindgen_ge(self, other)
533    }
534
535    /// Applies the binary `>` JS operator on the two `JsValue`s.
536    ///
537    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than)
538    #[inline]
539    pub fn gt(&self, other: &Self) -> bool {
540        __wbindgen_gt(self, other)
541    }
542
543    /// Applies the unary `+` JS operator on a `JsValue`. Can throw.
544    ///
545    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus)
546    #[inline]
547    pub fn unchecked_into_f64(&self) -> f64 {
548        // Can't use `wbg_cast` here because it expects that the value already has a correct type
549        // and will fail with an assertion error in debug mode.
550        __wbindgen_as_number(self)
551    }
552}
553
554impl PartialEq for JsValue {
555    /// Compares two `JsValue`s for equality, using the `===` operator in JS.
556    ///
557    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality)
558    #[inline]
559    fn eq(&self, other: &Self) -> bool {
560        __wbindgen_jsval_eq(self, other)
561    }
562}
563
564impl PartialEq<bool> for JsValue {
565    #[inline]
566    fn eq(&self, other: &bool) -> bool {
567        self.as_bool() == Some(*other)
568    }
569}
570
571impl PartialEq<str> for JsValue {
572    #[inline]
573    fn eq(&self, other: &str) -> bool {
574        *self == JsValue::from_str(other)
575    }
576}
577
578impl<'a> PartialEq<&'a str> for JsValue {
579    #[inline]
580    fn eq(&self, other: &&'a str) -> bool {
581        <JsValue as PartialEq<str>>::eq(self, other)
582    }
583}
584
585impl PartialEq<String> for JsValue {
586    #[inline]
587    fn eq(&self, other: &String) -> bool {
588        <JsValue as PartialEq<str>>::eq(self, other)
589    }
590}
591impl<'a> PartialEq<&'a String> for JsValue {
592    #[inline]
593    fn eq(&self, other: &&'a String) -> bool {
594        <JsValue as PartialEq<str>>::eq(self, other)
595    }
596}
597
598macro_rules! forward_deref_unop {
599    (impl $imp:ident, $method:ident for $t:ty) => {
600        impl $imp for $t {
601            type Output = <&'static $t as $imp>::Output;
602
603            #[inline]
604            fn $method(self) -> <&'static $t as $imp>::Output {
605                $imp::$method(&self)
606            }
607        }
608    };
609}
610
611macro_rules! forward_deref_binop {
612    (impl $imp:ident, $method:ident for $t:ty) => {
613        impl<'a> $imp<$t> for &'a $t {
614            type Output = <&'static $t as $imp<&'static $t>>::Output;
615
616            #[inline]
617            fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
618                $imp::$method(self, &other)
619            }
620        }
621
622        impl $imp<&$t> for $t {
623            type Output = <&'static $t as $imp<&'static $t>>::Output;
624
625            #[inline]
626            fn $method(self, other: &$t) -> <&'static $t as $imp<&'static $t>>::Output {
627                $imp::$method(&self, other)
628            }
629        }
630
631        impl $imp<$t> for $t {
632            type Output = <&'static $t as $imp<&'static $t>>::Output;
633
634            #[inline]
635            fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
636                $imp::$method(&self, &other)
637            }
638        }
639    };
640}
641
642impl Not for &JsValue {
643    type Output = bool;
644
645    /// Applies the `!` JS operator on a `JsValue`.
646    ///
647    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT)
648    #[inline]
649    fn not(self) -> Self::Output {
650        JsValue::is_falsy(self)
651    }
652}
653
654forward_deref_unop!(impl Not, not for JsValue);
655
656impl TryFrom<JsValue> for f64 {
657    type Error = JsValue;
658
659    /// Applies the unary `+` JS operator on a `JsValue`.
660    /// Returns the numeric result on success, or the JS error value on error.
661    ///
662    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus)
663    #[inline]
664    fn try_from(val: JsValue) -> Result<Self, Self::Error> {
665        f64::try_from(&val)
666    }
667}
668
669impl TryFrom<&JsValue> for f64 {
670    type Error = JsValue;
671
672    /// Applies the unary `+` JS operator on a `JsValue`.
673    /// Returns the numeric result on success, or the JS error value on error.
674    ///
675    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus)
676    #[inline]
677    fn try_from(val: &JsValue) -> Result<Self, Self::Error> {
678        let jsval = __wbindgen_try_into_number(val);
679        match jsval.as_f64() {
680            Some(num) => Ok(num),
681            None => Err(jsval),
682        }
683    }
684}
685
686impl Neg for &JsValue {
687    type Output = JsValue;
688
689    /// Applies the unary `-` JS operator on a `JsValue`.
690    ///
691    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_negation)
692    #[inline]
693    fn neg(self) -> Self::Output {
694        __wbindgen_neg(self)
695    }
696}
697
698forward_deref_unop!(impl Neg, neg for JsValue);
699
700impl BitAnd for &JsValue {
701    type Output = JsValue;
702
703    /// Applies the binary `&` JS operator on two `JsValue`s.
704    ///
705    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND)
706    #[inline]
707    fn bitand(self, rhs: Self) -> Self::Output {
708        __wbindgen_bit_and(self, rhs)
709    }
710}
711
712forward_deref_binop!(impl BitAnd, bitand for JsValue);
713
714impl BitOr for &JsValue {
715    type Output = JsValue;
716
717    /// Applies the binary `|` JS operator on two `JsValue`s.
718    ///
719    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_OR)
720    #[inline]
721    fn bitor(self, rhs: Self) -> Self::Output {
722        __wbindgen_bit_or(self, rhs)
723    }
724}
725
726forward_deref_binop!(impl BitOr, bitor for JsValue);
727
728impl BitXor for &JsValue {
729    type Output = JsValue;
730
731    /// Applies the binary `^` JS operator on two `JsValue`s.
732    ///
733    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_XOR)
734    #[inline]
735    fn bitxor(self, rhs: Self) -> Self::Output {
736        __wbindgen_bit_xor(self, rhs)
737    }
738}
739
740forward_deref_binop!(impl BitXor, bitxor for JsValue);
741
742impl Shl for &JsValue {
743    type Output = JsValue;
744
745    /// Applies the binary `<<` JS operator on two `JsValue`s.
746    ///
747    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Left_shift)
748    #[inline]
749    fn shl(self, rhs: Self) -> Self::Output {
750        __wbindgen_shl(self, rhs)
751    }
752}
753
754forward_deref_binop!(impl Shl, shl for JsValue);
755
756impl Shr for &JsValue {
757    type Output = JsValue;
758
759    /// Applies the binary `>>` JS operator on two `JsValue`s.
760    ///
761    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Right_shift)
762    #[inline]
763    fn shr(self, rhs: Self) -> Self::Output {
764        __wbindgen_shr(self, rhs)
765    }
766}
767
768forward_deref_binop!(impl Shr, shr for JsValue);
769
770impl Add for &JsValue {
771    type Output = JsValue;
772
773    /// Applies the binary `+` JS operator on two `JsValue`s.
774    ///
775    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition)
776    #[inline]
777    fn add(self, rhs: Self) -> Self::Output {
778        __wbindgen_add(self, rhs)
779    }
780}
781
782forward_deref_binop!(impl Add, add for JsValue);
783
784impl Sub for &JsValue {
785    type Output = JsValue;
786
787    /// Applies the binary `-` JS operator on two `JsValue`s.
788    ///
789    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Subtraction)
790    #[inline]
791    fn sub(self, rhs: Self) -> Self::Output {
792        __wbindgen_sub(self, rhs)
793    }
794}
795
796forward_deref_binop!(impl Sub, sub for JsValue);
797
798impl Div for &JsValue {
799    type Output = JsValue;
800
801    /// Applies the binary `/` JS operator on two `JsValue`s.
802    ///
803    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Division)
804    #[inline]
805    fn div(self, rhs: Self) -> Self::Output {
806        __wbindgen_div(self, rhs)
807    }
808}
809
810forward_deref_binop!(impl Div, div for JsValue);
811
812impl Mul for &JsValue {
813    type Output = JsValue;
814
815    /// Applies the binary `*` JS operator on two `JsValue`s.
816    ///
817    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Multiplication)
818    #[inline]
819    fn mul(self, rhs: Self) -> Self::Output {
820        __wbindgen_mul(self, rhs)
821    }
822}
823
824forward_deref_binop!(impl Mul, mul for JsValue);
825
826impl Rem for &JsValue {
827    type Output = JsValue;
828
829    /// Applies the binary `%` JS operator on two `JsValue`s.
830    ///
831    /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder)
832    #[inline]
833    fn rem(self, rhs: Self) -> Self::Output {
834        __wbindgen_rem(self, rhs)
835    }
836}
837
838forward_deref_binop!(impl Rem, rem for JsValue);
839
840impl<'a> From<&'a str> for JsValue {
841    #[inline]
842    fn from(s: &'a str) -> JsValue {
843        JsValue::from_str(s)
844    }
845}
846
847impl<T> From<*mut T> for JsValue {
848    #[inline]
849    fn from(s: *mut T) -> JsValue {
850        JsValue::from(s as usize)
851    }
852}
853
854impl<T> From<*const T> for JsValue {
855    #[inline]
856    fn from(s: *const T) -> JsValue {
857        JsValue::from(s as usize)
858    }
859}
860
861impl<T> From<NonNull<T>> for JsValue {
862    #[inline]
863    fn from(s: NonNull<T>) -> JsValue {
864        JsValue::from(s.as_ptr() as usize)
865    }
866}
867
868impl<'a> From<&'a String> for JsValue {
869    #[inline]
870    fn from(s: &'a String) -> JsValue {
871        JsValue::from_str(s)
872    }
873}
874
875impl From<String> for JsValue {
876    #[inline]
877    fn from(s: String) -> JsValue {
878        JsValue::from_str(&s)
879    }
880}
881
882impl TryFrom<JsValue> for String {
883    type Error = JsValue;
884
885    fn try_from(value: JsValue) -> Result<Self, Self::Error> {
886        match value.as_string() {
887            Some(s) => Ok(s),
888            None => Err(value),
889        }
890    }
891}
892
893impl TryFromJsValue for String {
894    fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
895        value.as_string()
896    }
897}
898
899impl From<bool> for JsValue {
900    #[inline]
901    fn from(s: bool) -> JsValue {
902        JsValue::from_bool(s)
903    }
904}
905
906impl TryFromJsValue for bool {
907    fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
908        value.as_bool()
909    }
910}
911
912impl TryFromJsValue for char {
913    fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
914        let s = value.as_string()?;
915        if s.len() == 1 {
916            Some(s.chars().nth(0).unwrap())
917        } else {
918            None
919        }
920    }
921}
922
923impl<'a, T> From<&'a T> for JsValue
924where
925    T: JsCast,
926{
927    #[inline]
928    fn from(s: &'a T) -> JsValue {
929        s.as_ref().clone()
930    }
931}
932
933impl<T> From<Option<T>> for JsValue
934where
935    JsValue: From<T>,
936{
937    #[inline]
938    fn from(s: Option<T>) -> JsValue {
939        match s {
940            Some(s) => s.into(),
941            None => JsValue::undefined(),
942        }
943    }
944}
945
946// everything is a `JsValue`!
947impl JsCast for JsValue {
948    #[inline]
949    fn instanceof(_val: &JsValue) -> bool {
950        true
951    }
952    #[inline]
953    fn unchecked_from_js(val: JsValue) -> Self {
954        val
955    }
956    #[inline]
957    fn unchecked_from_js_ref(val: &JsValue) -> &Self {
958        val
959    }
960}
961
962impl AsRef<JsValue> for JsValue {
963    #[inline]
964    fn as_ref(&self) -> &JsValue {
965        self
966    }
967}
968
969impl UpcastFrom<JsValue> for JsValue {}
970
971// Loosely based on toInt32 in ecma-272 for abi semantics
972// with restriction that it only applies for numbers
973fn to_uint_32(v: &JsValue) -> Option<u32> {
974    v.as_f64().map(|n| {
975        if n.is_infinite() {
976            0
977        } else {
978            (n as i64) as u32
979        }
980    })
981}
982
983macro_rules! integers {
984    ($($n:ident)*) => ($(
985        impl PartialEq<$n> for JsValue {
986            #[inline]
987            fn eq(&self, other: &$n) -> bool {
988                self.as_f64() == Some(f64::from(*other))
989            }
990        }
991
992        impl From<$n> for JsValue {
993            #[inline]
994            fn from(n: $n) -> JsValue {
995                JsValue::from_f64(n.into())
996            }
997        }
998
999        // Follows semantics of https://www.w3.org/TR/wasm-js-api-2/#towebassemblyvalue
1000        impl TryFromJsValue for $n {
1001            #[inline]
1002            fn try_from_js_value_ref(val: &JsValue) -> Option<$n> {
1003                to_uint_32(val).map(|n| n as $n)
1004            }
1005        }
1006    )*)
1007}
1008
1009integers! { i8 u8 i16 u16 i32 u32 }
1010
1011macro_rules! floats {
1012    ($($n:ident)*) => ($(
1013        impl PartialEq<$n> for JsValue {
1014            #[inline]
1015            fn eq(&self, other: &$n) -> bool {
1016                self.as_f64() == Some(f64::from(*other))
1017            }
1018        }
1019
1020        impl From<$n> for JsValue {
1021            #[inline]
1022            fn from(n: $n) -> JsValue {
1023                JsValue::from_f64(n.into())
1024            }
1025        }
1026
1027        impl TryFromJsValue for $n {
1028            #[inline]
1029            fn try_from_js_value_ref(val: &JsValue) -> Option<$n> {
1030                val.as_f64().map(|n| n as $n)
1031            }
1032        }
1033    )*)
1034}
1035
1036floats! { f32 f64 }
1037
1038macro_rules! big_integers {
1039    ($($n:ident)*) => ($(
1040        impl PartialEq<$n> for JsValue {
1041            #[inline]
1042            fn eq(&self, other: &$n) -> bool {
1043                self == &JsValue::from(*other)
1044            }
1045        }
1046
1047        impl From<$n> for JsValue {
1048            #[inline]
1049            fn from(arg: $n) -> JsValue {
1050                wbg_cast(arg)
1051            }
1052        }
1053
1054        impl TryFrom<JsValue> for $n {
1055            type Error = JsValue;
1056
1057            #[inline]
1058            fn try_from(v: JsValue) -> Result<Self, JsValue> {
1059                Self::try_from_js_value(v)
1060            }
1061        }
1062
1063        impl TryFromJsValue for $n {
1064            #[inline]
1065            fn try_from_js_value_ref(val: &JsValue) -> Option<$n> {
1066                let as_i64 = __wbindgen_bigint_get_as_i64(&val)?;
1067                // Reinterpret bits; ABI-wise this is safe to do and allows us to avoid
1068                // having separate intrinsics per signed/unsigned types.
1069                let as_self = as_i64 as $n;
1070                // Double-check that we didn't truncate the bigint to 64 bits.
1071                if val == &as_self {
1072                    Some(as_self)
1073                } else {
1074                    None
1075                }
1076            }
1077        }
1078    )*)
1079}
1080
1081big_integers! { i64 u64 }
1082
1083macro_rules! num128 {
1084    ($ty:ty, $hi_ty:ty) => {
1085        impl PartialEq<$ty> for JsValue {
1086            #[inline]
1087            fn eq(&self, other: &$ty) -> bool {
1088                self == &JsValue::from(*other)
1089            }
1090        }
1091
1092        impl From<$ty> for JsValue {
1093            #[inline]
1094            fn from(arg: $ty) -> JsValue {
1095                wbg_cast(arg)
1096            }
1097        }
1098
1099        impl TryFrom<JsValue> for $ty {
1100            type Error = JsValue;
1101
1102            #[inline]
1103            fn try_from(v: JsValue) -> Result<Self, JsValue> {
1104                Self::try_from_js_value(v)
1105            }
1106        }
1107
1108        impl TryFromJsValue for $ty {
1109            // This is a non-standard Wasm bindgen conversion, supported equally
1110            fn try_from_js_value_ref(v: &JsValue) -> Option<$ty> {
1111                // Truncate the bigint to 64 bits, this will give us the lower part.
1112                // The lower part must be interpreted as unsigned in both i128 and u128.
1113                let lo = __wbindgen_bigint_get_as_i64(&v)? as u64;
1114                // Now we know it's a bigint, so we can safely use `>> 64n` without
1115                // worrying about a JS exception on type mismatch.
1116                let hi = v >> JsValue::from(64_u64);
1117                // The high part is the one we want checked against a 64-bit range.
1118                // If it fits, then our original number is in the 128-bit range.
1119                <$hi_ty>::try_from_js_value_ref(&hi).map(|hi| Self::from(hi) << 64 | Self::from(lo))
1120            }
1121        }
1122    };
1123}
1124
1125num128!(i128, i64);
1126
1127num128!(u128, u64);
1128
1129impl TryFromJsValue for () {
1130    fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
1131        if value.is_undefined() {
1132            Some(())
1133        } else {
1134            None
1135        }
1136    }
1137}
1138
1139impl<T: TryFromJsValue> TryFromJsValue for Option<T> {
1140    fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
1141        if value.is_undefined() {
1142            Some(None)
1143        } else {
1144            T::try_from_js_value_ref(value).map(Some)
1145        }
1146    }
1147}
1148
1149// `usize` and `isize` use the public pointer-sized JS number ABI, which is
1150// `u32`/`i32` on wasm32 and `f64` on wasm64.
1151impl PartialEq<usize> for JsValue {
1152    #[inline]
1153    fn eq(&self, other: &usize) -> bool {
1154        *self == (*other as crate::__rt::WasmWordRepr)
1155    }
1156}
1157
1158impl From<usize> for JsValue {
1159    #[inline]
1160    fn from(n: usize) -> Self {
1161        Self::from(n as crate::__rt::WasmWordRepr)
1162    }
1163}
1164
1165impl PartialEq<isize> for JsValue {
1166    #[inline]
1167    fn eq(&self, other: &isize) -> bool {
1168        *self == (*other as crate::__rt::WasmSignedWordRepr)
1169    }
1170}
1171
1172impl From<isize> for JsValue {
1173    #[inline]
1174    fn from(n: isize) -> Self {
1175        Self::from(n as crate::__rt::WasmSignedWordRepr)
1176    }
1177}
1178
1179// Follows semantics of https://www.w3.org/TR/wasm-js-api-2/#towebassemblyvalue
1180impl TryFromJsValue for isize {
1181    #[inline]
1182    fn try_from_js_value_ref(val: &JsValue) -> Option<isize> {
1183        val.as_f64().map(|n| n as isize)
1184    }
1185}
1186
1187// Follows semantics of https://www.w3.org/TR/wasm-js-api-2/#towebassemblyvalue
1188impl TryFromJsValue for usize {
1189    #[inline]
1190    fn try_from_js_value_ref(val: &JsValue) -> Option<usize> {
1191        val.as_f64().map(|n| n as usize)
1192    }
1193}
1194
1195// Intrinsics that are simply JS function bindings and can be self-hosted via the macro.
1196#[wasm_bindgen_macro::wasm_bindgen(wasm_bindgen = crate)]
1197extern "C" {
1198    #[wasm_bindgen(js_namespace = Array, js_name = isArray)]
1199    fn __wbindgen_is_array(v: &JsValue) -> bool;
1200
1201    #[wasm_bindgen(js_name = BigInt)]
1202    fn __wbindgen_bigint_from_str(s: &str) -> JsValue;
1203
1204    #[wasm_bindgen(js_name = Symbol)]
1205    fn __wbindgen_symbol_new(description: Option<&str>) -> JsValue;
1206
1207    #[wasm_bindgen(js_name = Error)]
1208    fn __wbindgen_error_new(msg: &str) -> JsValue;
1209
1210    #[wasm_bindgen(js_namespace = JSON, js_name = parse)]
1211    fn __wbindgen_json_parse(json: String) -> JsValue;
1212
1213    #[wasm_bindgen(js_namespace = JSON, js_name = stringify)]
1214    fn __wbindgen_json_serialize(v: &JsValue) -> Option<String>;
1215
1216    #[wasm_bindgen(js_name = Number)]
1217    fn __wbindgen_as_number(v: &JsValue) -> f64;
1218}
1219
1220// Intrinsics which are handled by cli-support but for which we can use
1221// standard wasm-bindgen ABI conversions.
1222#[wasm_bindgen_macro::wasm_bindgen(wasm_bindgen = crate, raw_module = "__wbindgen_placeholder__")]
1223extern "C" {
1224    #[cfg(not(wbg_reference_types))]
1225    fn __wbindgen_externref_heap_live_count() -> u32;
1226
1227    fn __wbindgen_is_null(js: &JsValue) -> bool;
1228    fn __wbindgen_is_undefined(js: &JsValue) -> bool;
1229    fn __wbindgen_is_null_or_undefined(js: &JsValue) -> bool;
1230    fn __wbindgen_is_symbol(js: &JsValue) -> bool;
1231    fn __wbindgen_is_object(js: &JsValue) -> bool;
1232    fn __wbindgen_is_function(js: &JsValue) -> bool;
1233    fn __wbindgen_is_string(js: &JsValue) -> bool;
1234    fn __wbindgen_is_bigint(js: &JsValue) -> bool;
1235    fn __wbindgen_typeof(js: &JsValue) -> JsValue;
1236
1237    fn __wbindgen_in(prop: &JsValue, obj: &JsValue) -> bool;
1238
1239    fn __wbindgen_is_falsy(js: &JsValue) -> bool;
1240    fn __wbindgen_try_into_number(js: &JsValue) -> JsValue;
1241    fn __wbindgen_neg(js: &JsValue) -> JsValue;
1242    fn __wbindgen_bit_and(a: &JsValue, b: &JsValue) -> JsValue;
1243    fn __wbindgen_bit_or(a: &JsValue, b: &JsValue) -> JsValue;
1244    fn __wbindgen_bit_xor(a: &JsValue, b: &JsValue) -> JsValue;
1245    fn __wbindgen_bit_not(js: &JsValue) -> JsValue;
1246    fn __wbindgen_shl(a: &JsValue, b: &JsValue) -> JsValue;
1247    fn __wbindgen_shr(a: &JsValue, b: &JsValue) -> JsValue;
1248    fn __wbindgen_unsigned_shr(a: &JsValue, b: &JsValue) -> u32;
1249    fn __wbindgen_add(a: &JsValue, b: &JsValue) -> JsValue;
1250    fn __wbindgen_sub(a: &JsValue, b: &JsValue) -> JsValue;
1251    fn __wbindgen_div(a: &JsValue, b: &JsValue) -> JsValue;
1252    fn __wbindgen_checked_div(a: &JsValue, b: &JsValue) -> JsValue;
1253    fn __wbindgen_mul(a: &JsValue, b: &JsValue) -> JsValue;
1254    fn __wbindgen_rem(a: &JsValue, b: &JsValue) -> JsValue;
1255    fn __wbindgen_pow(a: &JsValue, b: &JsValue) -> JsValue;
1256    fn __wbindgen_lt(a: &JsValue, b: &JsValue) -> bool;
1257    fn __wbindgen_le(a: &JsValue, b: &JsValue) -> bool;
1258    fn __wbindgen_ge(a: &JsValue, b: &JsValue) -> bool;
1259    fn __wbindgen_gt(a: &JsValue, b: &JsValue) -> bool;
1260
1261    fn __wbindgen_number_get(js: &JsValue) -> Option<f64>;
1262    fn __wbindgen_boolean_get(js: &JsValue) -> Option<bool>;
1263    fn __wbindgen_string_get(js: &JsValue) -> Option<String>;
1264    fn __wbindgen_bigint_get_as_i64(js: &JsValue) -> Option<i64>;
1265
1266    fn __wbindgen_debug_string(js: &JsValue) -> String;
1267
1268    fn __wbindgen_throw(msg: &str) /* -> ! */;
1269    fn __wbindgen_rethrow(js: JsValue) /* -> ! */;
1270
1271    fn __wbindgen_jsval_eq(a: &JsValue, b: &JsValue) -> bool;
1272    fn __wbindgen_jsval_loose_eq(a: &JsValue, b: &JsValue) -> bool;
1273
1274    fn __wbindgen_copy_to_typed_array(data: &[u8], js: &JsValue);
1275
1276    fn __wbindgen_init_externref_table();
1277
1278    fn __wbindgen_exports() -> JsValue;
1279    fn __wbindgen_memory() -> JsValue;
1280    fn __wbindgen_module() -> JsValue;
1281    fn __wbindgen_instance() -> JsValue;
1282    fn __wbindgen_function_table() -> JsValue;
1283
1284    fn __wbindgen_reinit();
1285}
1286
1287// Intrinsics that have to use raw imports because they're matched by other
1288// parts of the transform codebase instead of just generating JS.
1289externs! {
1290    #[link(wasm_import_module = "__wbindgen_placeholder__")]
1291    extern "C" {
1292        fn __wbindgen_object_clone_ref(idx: u32) -> u32;
1293        fn __wbindgen_object_drop_ref(idx: u32) -> ();
1294
1295        fn __wbindgen_describe(v: u32) -> ();
1296        fn __wbindgen_describe_cast(func: *const (), prims: *const ()) -> *const ();
1297    }
1298}
1299
1300impl Clone for JsValue {
1301    #[inline]
1302    fn clone(&self) -> JsValue {
1303        JsValue::_new(unsafe { __wbindgen_object_clone_ref(self.idx) })
1304    }
1305}
1306
1307impl core::fmt::Debug for JsValue {
1308    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1309        write!(f, "JsValue({})", self.as_debug_string())
1310    }
1311}
1312
1313impl Drop for JsValue {
1314    #[inline]
1315    fn drop(&mut self) {
1316        unsafe {
1317            // We definitely should never drop anything in the stack area
1318            debug_assert!(
1319                self.idx >= __rt::JSIDX_OFFSET,
1320                "free of stack slot {}",
1321                self.idx
1322            );
1323
1324            // Otherwise if we're not dropping one of our reserved values,
1325            // actually call the intrinsic. See #1054 for eventually removing
1326            // this branch.
1327            if self.idx >= __rt::JSIDX_RESERVED {
1328                __wbindgen_object_drop_ref(self.idx);
1329            }
1330        }
1331    }
1332}
1333
1334impl Default for JsValue {
1335    fn default() -> Self {
1336        Self::UNDEFINED
1337    }
1338}
1339
1340/// Wrapper type for imported statics.
1341///
1342/// This type is used whenever a `static` is imported from a JS module, for
1343/// example this import:
1344///
1345/// ```ignore
1346/// #[wasm_bindgen]
1347/// extern "C" {
1348///     static console: JsValue;
1349/// }
1350/// ```
1351///
1352/// will generate in Rust a value that looks like:
1353///
1354/// ```ignore
1355/// static console: JsStatic<JsValue> = ...;
1356/// ```
1357///
1358/// This type implements `Deref` to the inner type so it's typically used as if
1359/// it were `&T`.
1360#[cfg(feature = "std")]
1361#[deprecated = "use with `#[wasm_bindgen(thread_local_v2)]` instead"]
1362pub struct JsStatic<T: 'static> {
1363    #[doc(hidden)]
1364    pub __inner: &'static std::thread::LocalKey<T>,
1365}
1366
1367#[cfg(feature = "std")]
1368#[allow(deprecated)]
1369#[cfg(not(target_feature = "atomics"))]
1370impl<T: crate::convert::FromWasmAbi + 'static> Deref for JsStatic<T> {
1371    type Target = T;
1372    fn deref(&self) -> &T {
1373        unsafe { self.__inner.with(|ptr| &*(ptr as *const T)) }
1374    }
1375}
1376
1377/// Wrapper type for imported statics.
1378///
1379/// This type is used whenever a `static` is imported from a JS module, for
1380/// example this import:
1381///
1382/// ```ignore
1383/// #[wasm_bindgen]
1384/// extern "C" {
1385///     #[wasm_bindgen(thread_local_v2)]
1386///     static console: JsValue;
1387/// }
1388/// ```
1389///
1390/// will generate in Rust a value that looks like:
1391///
1392/// ```ignore
1393/// static console: JsThreadLocal<JsValue> = ...;
1394/// ```
1395pub struct JsThreadLocal<T: 'static> {
1396    #[doc(hidden)]
1397    #[cfg(not(target_feature = "atomics"))]
1398    pub __inner: &'static __rt::LazyCell<T>,
1399    #[doc(hidden)]
1400    #[cfg(target_feature = "atomics")]
1401    pub __inner: fn() -> *const T,
1402}
1403
1404impl<T> JsThreadLocal<T> {
1405    pub fn with<F, R>(&'static self, f: F) -> R
1406    where
1407        F: FnOnce(&T) -> R,
1408    {
1409        #[cfg(not(target_feature = "atomics"))]
1410        return f(self.__inner);
1411        #[cfg(target_feature = "atomics")]
1412        f(unsafe { &*(self.__inner)() })
1413    }
1414}
1415
1416#[cold]
1417#[inline(never)]
1418#[deprecated(note = "renamed to `throw_str`")]
1419#[doc(hidden)]
1420pub fn throw(s: &str) -> ! {
1421    throw_str(s)
1422}
1423
1424/// Throws a JS exception.
1425///
1426/// This function will throw a JS exception with the message provided. The
1427/// function will not return as the Wasm stack will be popped when the exception
1428/// is thrown.
1429///
1430/// Note that it is very easy to leak memory with this function because this
1431/// function, unlike `panic!` on other platforms, **will not run destructors**.
1432/// It's recommended to return a `Result` where possible to avoid the worry of
1433/// leaks.
1434///
1435/// If you need destructors to run, consider using `panic!` when building with
1436/// `-Cpanic=unwind`. If the `std` feature is used panics will be caught at the
1437/// JavaScript boundary and converted to JavaScript exceptions.
1438#[cold]
1439#[inline(never)]
1440pub fn throw_str(s: &str) -> ! {
1441    __wbindgen_throw(s);
1442    unsafe { core::hint::unreachable_unchecked() }
1443}
1444
1445/// Rethrow a JS exception
1446///
1447/// This function will throw a JS exception with the JS value provided. This
1448/// function will not return and the Wasm stack will be popped until the point
1449/// of entry of Wasm itself.
1450///
1451/// Note that it is very easy to leak memory with this function because this
1452/// function, unlike `panic!` on other platforms, **will not run destructors**.
1453/// It's recommended to return a `Result` where possible to avoid the worry of
1454/// leaks.
1455///
1456/// If you need destructors to run, consider using `panic!` when building with
1457/// `-Cpanic=unwind`. If the `std` feature is used panics will be caught at the
1458/// JavaScript boundary and converted to JavaScript exceptions.
1459#[cold]
1460#[inline(never)]
1461pub fn throw_val(s: JsValue) -> ! {
1462    __wbindgen_rethrow(s);
1463    unsafe { core::hint::unreachable_unchecked() }
1464}
1465
1466/// Get the count of live `externref`s / `JsValue`s in `wasm-bindgen`'s heap.
1467///
1468/// ## Usage
1469///
1470/// This is intended for debugging and writing tests.
1471///
1472/// To write a test that asserts against unnecessarily keeping `anref`s /
1473/// `JsValue`s alive:
1474///
1475/// * get an initial live count,
1476///
1477/// * perform some series of operations or function calls that should clean up
1478///   after themselves, and should not keep holding onto `externref`s / `JsValue`s
1479///   after completion,
1480///
1481/// * get the final live count,
1482///
1483/// * and assert that the initial and final counts are the same.
1484///
1485/// ## What is Counted
1486///
1487/// Note that this only counts the *owned* `externref`s / `JsValue`s that end up in
1488/// `wasm-bindgen`'s heap. It does not count borrowed `externref`s / `JsValue`s
1489/// that are on its stack.
1490///
1491/// For example, these `JsValue`s are accounted for:
1492///
1493/// ```ignore
1494/// #[wasm_bindgen]
1495/// pub fn my_function(this_is_counted: JsValue) {
1496///     let also_counted = JsValue::from_str("hi");
1497///     assert!(wasm_bindgen::externref_heap_live_count() >= 2);
1498/// }
1499/// ```
1500///
1501/// While this borrowed `JsValue` ends up on the stack, not the heap, and
1502/// therefore is not accounted for:
1503///
1504/// ```ignore
1505/// #[wasm_bindgen]
1506/// pub fn my_other_function(this_is_not_counted: &JsValue) {
1507///     // ...
1508/// }
1509/// ```
1510pub fn externref_heap_live_count() -> u32 {
1511    __wbindgen_externref_heap_live_count()
1512}
1513
1514#[doc(hidden)]
1515pub fn anyref_heap_live_count() -> u32 {
1516    externref_heap_live_count()
1517}
1518
1519/// An extension trait for `Option<T>` and `Result<T, E>` for unwrapping the `T`
1520/// value, or throwing a JS error if it is not available.
1521///
1522/// These methods should have a smaller code size footprint than the normal
1523/// `Option::unwrap` and `Option::expect` methods, but they are specific to
1524/// working with Wasm and JS.
1525///
1526/// On non-wasm32 targets, defaults to the normal unwrap/expect calls.
1527///
1528/// # Example
1529///
1530/// ```
1531/// use wasm_bindgen::prelude::*;
1532///
1533/// // If the value is `Option::Some` or `Result::Ok`, then we just get the
1534/// // contained `T` value.
1535/// let x = Some(42);
1536/// assert_eq!(x.unwrap_throw(), 42);
1537///
1538/// let y: Option<i32> = None;
1539///
1540/// // This call would throw an error to JS!
1541/// //
1542/// //     y.unwrap_throw()
1543/// //
1544/// // And this call would throw an error to JS with a custom error message!
1545/// //
1546/// //     y.expect_throw("woopsie daisy!")
1547/// ```
1548pub trait UnwrapThrowExt<T>: Sized {
1549    /// Unwrap this `Option` or `Result`, but instead of panicking on failure,
1550    /// throw an exception to JavaScript.
1551    #[cfg_attr(any(debug_assertions, not(target_family = "wasm")), track_caller)]
1552    fn unwrap_throw(self) -> T {
1553        if cfg!(all(debug_assertions, target_family = "wasm")) {
1554            let loc = core::panic::Location::caller();
1555            let msg = alloc::format!(
1556                "called `{}::unwrap_throw()` ({}:{}:{})",
1557                core::any::type_name::<Self>(),
1558                loc.file(),
1559                loc.line(),
1560                loc.column()
1561            );
1562            self.expect_throw(&msg)
1563        } else {
1564            self.expect_throw("called `unwrap_throw()`")
1565        }
1566    }
1567
1568    /// Unwrap this container's `T` value, or throw an error to JS with the
1569    /// given message if the `T` value is unavailable (e.g. an `Option<T>` is
1570    /// `None`).
1571    #[cfg_attr(any(debug_assertions, not(target_family = "wasm")), track_caller)]
1572    fn expect_throw(self, message: &str) -> T;
1573}
1574
1575impl<T> UnwrapThrowExt<T> for Option<T> {
1576    fn unwrap_throw(self) -> T {
1577        const MSG: &str = "called `Option::unwrap_throw()` on a `None` value";
1578
1579        if cfg!(target_family = "wasm") {
1580            if let Some(val) = self {
1581                val
1582            } else if cfg!(debug_assertions) {
1583                let loc = core::panic::Location::caller();
1584                let msg = alloc::format!("{MSG} ({}:{}:{})", loc.file(), loc.line(), loc.column(),);
1585
1586                throw_str(&msg)
1587            } else {
1588                throw_str(MSG)
1589            }
1590        } else {
1591            self.expect(MSG)
1592        }
1593    }
1594
1595    fn expect_throw(self, message: &str) -> T {
1596        if cfg!(target_family = "wasm") {
1597            if let Some(val) = self {
1598                val
1599            } else if cfg!(debug_assertions) {
1600                let loc = core::panic::Location::caller();
1601                let msg =
1602                    alloc::format!("{message} ({}:{}:{})", loc.file(), loc.line(), loc.column(),);
1603
1604                throw_str(&msg)
1605            } else {
1606                throw_str(message)
1607            }
1608        } else {
1609            self.expect(message)
1610        }
1611    }
1612}
1613
1614impl<T, E> UnwrapThrowExt<T> for Result<T, E>
1615where
1616    E: core::fmt::Debug,
1617{
1618    fn unwrap_throw(self) -> T {
1619        const MSG: &str = "called `Result::unwrap_throw()` on an `Err` value";
1620
1621        if cfg!(target_family = "wasm") {
1622            match self {
1623                Ok(val) => val,
1624                Err(err) => {
1625                    if cfg!(debug_assertions) {
1626                        let loc = core::panic::Location::caller();
1627                        let msg = alloc::format!(
1628                            "{MSG} ({}:{}:{}): {err:?}",
1629                            loc.file(),
1630                            loc.line(),
1631                            loc.column(),
1632                        );
1633
1634                        throw_str(&msg)
1635                    } else {
1636                        throw_str(MSG)
1637                    }
1638                }
1639            }
1640        } else {
1641            self.expect(MSG)
1642        }
1643    }
1644
1645    fn expect_throw(self, message: &str) -> T {
1646        if cfg!(target_family = "wasm") {
1647            match self {
1648                Ok(val) => val,
1649                Err(err) => {
1650                    if cfg!(debug_assertions) {
1651                        let loc = core::panic::Location::caller();
1652                        let msg = alloc::format!(
1653                            "{message} ({}:{}:{}): {err:?}",
1654                            loc.file(),
1655                            loc.line(),
1656                            loc.column(),
1657                        );
1658
1659                        throw_str(&msg)
1660                    } else {
1661                        throw_str(message)
1662                    }
1663                }
1664            }
1665        } else {
1666            self.expect(message)
1667        }
1668    }
1669}
1670
1671/// Returns a handle to this Wasm instance's `WebAssembly.Module`.
1672/// This is only available when the final Wasm app is built with
1673/// `--target no-modules`, `--target web`, `--target deno` or `--target nodejs`.
1674/// It is unavailable for `--target bundler`.
1675pub fn module() -> JsValue {
1676    __wbindgen_module()
1677}
1678
1679/// Returns a handle to this Wasm instance's `WebAssembly.Instance`.
1680/// This is only available when the final Wasm app is built with
1681/// `--target no-modules`, `--target web`, `--target deno` or `--target nodejs`.
1682/// It is unavailable for `--target bundler`.
1683pub fn instance() -> JsValue {
1684    __wbindgen_instance()
1685}
1686
1687// TODO: deprecate next major
1688/// Returns a handle to this Wasm instance's `WebAssembly.Instance.prototype.exports`
1689pub fn exports() -> JsValue {
1690    __wbindgen_exports()
1691}
1692
1693/// Returns a handle to this Wasm instance's `WebAssembly.Memory`
1694pub fn memory() -> JsValue {
1695    __wbindgen_memory()
1696}
1697
1698/// Returns a handle to this Wasm instance's `WebAssembly.Table` which is the
1699/// indirect function table used by Rust
1700pub fn function_table() -> JsValue {
1701    __wbindgen_function_table()
1702}
1703
1704/// A wrapper type around slices and vectors for binding the `Uint8ClampedArray`
1705/// array in JS.
1706///
1707/// If you need to invoke a JS API which must take `Uint8ClampedArray` array,
1708/// then you can define it as taking one of these types:
1709///
1710/// * `Clamped<&[u8]>`
1711/// * `Clamped<&mut [u8]>`
1712/// * `Clamped<Vec<u8>>`
1713///
1714/// All of these types will show up as `Uint8ClampedArray` in JS and will have
1715/// different forms of ownership in Rust.
1716#[derive(Copy, Clone, PartialEq, Debug, Eq)]
1717pub struct Clamped<T>(pub T);
1718
1719impl<T> Deref for Clamped<T> {
1720    type Target = T;
1721
1722    fn deref(&self) -> &T {
1723        &self.0
1724    }
1725}
1726
1727impl<T> DerefMut for Clamped<T> {
1728    fn deref_mut(&mut self) -> &mut T {
1729        &mut self.0
1730    }
1731}
1732
1733/// Convenience type for use on exported `fn() -> Result<T, JsError>` functions, where you wish to
1734/// throw a JavaScript `Error` object.
1735///
1736/// You can get wasm_bindgen to throw basic errors by simply returning
1737/// `Err(JsError::new("message"))` from such a function.
1738///
1739/// For more complex error handling, `JsError` implements `From<T> where T: std::error::Error` by
1740/// converting it to a string, so you can use it with `?`. Many Rust error types already do this,
1741/// and you can use [`thiserror`](https://crates.io/crates/thiserror) to derive Display
1742/// implementations easily or use any number of boxed error types that implement it already.
1743///
1744///
1745/// To allow JavaScript code to catch only your errors, you may wish to add a subclass of `Error`
1746/// in a JS module, and then implement `Into<JsValue>` directly on a type and instantiate that
1747/// subclass. In that case, you would not need `JsError` at all.
1748///
1749/// ### Basic example
1750///
1751/// ```rust,no_run
1752/// use wasm_bindgen::prelude::*;
1753///
1754/// #[wasm_bindgen]
1755/// pub fn throwing_function() -> Result<(), JsError> {
1756///     Err(JsError::new("message"))
1757/// }
1758/// ```
1759///
1760/// ### Complex Example
1761///
1762/// ```rust,no_run
1763/// use wasm_bindgen::prelude::*;
1764///
1765/// #[derive(Debug, Clone)]
1766/// enum MyErrorType {
1767///     SomeError,
1768/// }
1769///
1770/// use core::fmt;
1771/// impl std::error::Error for MyErrorType {}
1772/// impl fmt::Display for MyErrorType {
1773///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1774///         write!(f, "display implementation becomes the error message")
1775///     }
1776/// }
1777///
1778/// fn internal_api() -> Result<(), MyErrorType> {
1779///     Err(MyErrorType::SomeError)
1780/// }
1781///
1782/// #[wasm_bindgen]
1783/// pub fn throwing_function() -> Result<(), JsError> {
1784///     internal_api()?;
1785///     Ok(())
1786/// }
1787///
1788/// ```
1789#[derive(Clone, Debug)]
1790#[repr(transparent)]
1791pub struct JsError {
1792    value: JsValue,
1793}
1794
1795impl JsError {
1796    /// Construct a JavaScript `Error` object with a string message
1797    #[inline]
1798    pub fn new(s: &str) -> JsError {
1799        Self {
1800            value: __wbindgen_error_new(s),
1801        }
1802    }
1803}
1804
1805#[cfg(feature = "std")]
1806impl<E> From<E> for JsError
1807where
1808    E: std::error::Error,
1809{
1810    fn from(error: E) -> Self {
1811        use std::string::ToString;
1812
1813        JsError::new(&error.to_string())
1814    }
1815}
1816
1817impl From<JsError> for JsValue {
1818    fn from(error: JsError) -> Self {
1819        error.value
1820    }
1821}
1822
1823impl<T: VectorIntoWasmAbi> From<Box<[T]>> for JsValue {
1824    fn from(vector: Box<[T]>) -> Self {
1825        wbg_cast(vector)
1826    }
1827}
1828
1829impl<T: VectorIntoWasmAbi> From<Clamped<Box<[T]>>> for JsValue {
1830    fn from(vector: Clamped<Box<[T]>>) -> Self {
1831        wbg_cast(vector)
1832    }
1833}
1834
1835impl<T: VectorIntoWasmAbi> From<Vec<T>> for JsValue {
1836    fn from(vector: Vec<T>) -> Self {
1837        JsValue::from(vector.into_boxed_slice())
1838    }
1839}
1840
1841impl<T: VectorIntoWasmAbi> From<Clamped<Vec<T>>> for JsValue {
1842    fn from(vector: Clamped<Vec<T>>) -> Self {
1843        JsValue::from(Clamped(vector.0.into_boxed_slice()))
1844    }
1845}
1846
1847#[cfg(target_os = "emscripten")]
1848#[doc(hidden)]
1849#[used]
1850#[link_section = "__wasm_bindgen_emscripten_marker"]
1851/// A custom data section used to detect Emscripten.
1852pub static __WASM_BINDGEN_EMSCRIPTEN_MARKER: [u8; 1] = [1];