1#![no_std]
42#![cfg_attr(wasm_bindgen_unstable_test_coverage, feature(coverage_attribute))]
43#![cfg_attr(
44 wasm_bindgen_unstable_try_trait,
45 feature(try_trait_v2, try_trait_v2_residual)
46)]
47#![cfg_attr(target_feature = "atomics", feature(thread_local))]
48#![cfg_attr(
49 any(target_feature = "atomics", wasm_bindgen_unstable_test_coverage),
50 feature(allow_internal_unstable),
51 allow(internal_features)
52)]
53#![cfg_attr(
54 all(not(debug_assertions), not(feature = "std"), target_arch = "wasm64"),
55 feature(simd_wasm64)
56)]
57#![doc(html_root_url = "https://docs.rs/wasm-bindgen/0.2")]
58
59extern crate alloc;
60#[cfg(feature = "std")]
61extern crate std;
62
63use crate::convert::{TryFromJsValue, UpcastFrom, VectorIntoWasmAbi};
64use crate::sys::Promising;
65use alloc::boxed::Box;
66use alloc::string::String;
67use alloc::vec::Vec;
68use core::convert::TryFrom;
69use core::marker::PhantomData;
70use core::ops::{
71 Add, BitAnd, BitOr, BitXor, Deref, DerefMut, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub,
72};
73use core::ptr::NonNull;
74
75const _: () = {
76 #[no_mangle]
94 pub extern "C" fn __wbindgen_skip_interpret_calls() {}
95
96 #[cfg(target_os = "emscripten")]
98 #[link_section = "__wasm_bindgen_emscripten_marker"]
99 static __WASM_BINDGEN_EMSCRIPTEN_MARKER: [u8; 1] = [1];
100
101 #[cfg(all(target_os = "emscripten", wasm_bindgen_unstable_jspi))]
105 #[link_section = "__wasm_bindgen_emscripten_jspi_marker"]
106 static __WASM_BINDGEN_EMSCRIPTEN_JSPI_MARKER: [u8; 1] = [1];
107
108 #[cfg(all(target_os = "emscripten", panic = "abort"))]
112 #[link_section = "__wasm_bindgen_emscripten_panic_abort_marker"]
113 static __WASM_BINDGEN_EMSCRIPTEN_PANIC_ABORT_MARKER: [u8; 1] = [1];
114};
115
116macro_rules! externs {
117 ($(#[$attr:meta])* extern "C" { $(fn $name:ident($($args:tt)*) -> $ret:ty;)* }) => (
118 #[cfg(all(target_family = "wasm", not(target_os = "wasi")))]
119 $(#[$attr])*
120 extern "C" {
121 $(fn $name($($args)*) -> $ret;)*
122 }
123
124 $(
125 #[cfg(not(all(target_family = "wasm", not(target_os = "wasi"))))]
126 #[allow(unused_variables)]
127 unsafe extern "C" fn $name($($args)*) -> $ret {
128 panic!("function not implemented on non-wasm32 targets")
129 }
130 )*
131 )
132}
133
134pub mod prelude {
140 pub use crate::closure::{Closure, ScopedClosure};
141 pub use crate::convert::Upcast; pub use crate::JsCast;
143 pub use crate::JsValue;
144 pub use crate::Nullable;
145 pub use crate::UnwrapThrowExt;
146 #[doc(hidden)]
147 pub use wasm_bindgen_macro::__wasm_bindgen_class_marker;
148 pub use wasm_bindgen_macro::wasm_bindgen;
149
150 pub use crate::JsError;
151}
152
153pub use wasm_bindgen_macro::link_to;
154
155pub mod closure;
156pub mod convert;
157pub mod describe;
158mod link;
159mod nullable;
160mod safe_integer;
161pub mod sys;
162
163pub use crate::nullable::Nullable;
164pub use crate::safe_integer::{SafeI64, SafeInteger, SafeIntegerPrimitive, SafeU64};
165
166#[cfg(wbg_reference_types)]
167mod externref;
168#[cfg(wbg_reference_types)]
169use externref::__wbindgen_externref_heap_live_count;
170
171pub use crate::__rt::marker::ErasableGeneric;
172pub use crate::convert::{IntoJsGeneric, JsGeneric, JsStringLike};
173
174#[doc(hidden)]
175pub mod handler;
176
177mod cast;
178pub use crate::cast::JsCast;
179
180mod parent;
181pub use crate::parent::Parent;
182
183mod cache;
184pub use cache::intern::{intern, unintern};
185
186#[doc(hidden)]
187#[path = "rt/mod.rs"]
188pub mod __rt;
189use __rt::wbg_cast;
190
191pub struct JsValue {
198 idx: u32,
199 _marker: PhantomData<*mut u8>, }
201
202#[cfg(not(target_feature = "atomics"))]
203unsafe impl Send for JsValue {}
204#[cfg(not(target_feature = "atomics"))]
205unsafe impl Sync for JsValue {}
206
207unsafe impl ErasableGeneric for JsValue {
208 type Repr = JsValue;
209}
210
211impl Promising for JsValue {
212 type Resolution = JsValue;
213}
214
215impl JsValue {
216 pub const NULL: JsValue = JsValue::_new(__rt::JSIDX_NULL);
218
219 pub const UNDEFINED: JsValue = JsValue::_new(__rt::JSIDX_UNDEFINED);
221
222 pub const TRUE: JsValue = JsValue::_new(__rt::JSIDX_TRUE);
224
225 pub const FALSE: JsValue = JsValue::_new(__rt::JSIDX_FALSE);
227
228 #[inline]
229 const fn _new(idx: u32) -> JsValue {
230 JsValue {
231 idx,
232 _marker: PhantomData,
233 }
234 }
235
236 #[allow(clippy::should_implement_trait)] #[inline]
242 pub fn from_str(s: &str) -> JsValue {
243 wbg_cast(s)
244 }
245
246 #[inline]
253 pub fn from_utf8_lossy(bytes: &[u8]) -> JsValue {
254 #[cfg(feature = "enable-interning")]
255 if let Some(value) = cache::intern::get_bytes(bytes) {
256 return value;
257 }
258 __wbindgen_string_from_utf8_lossy(bytes)
259 }
260
261 #[inline]
266 pub fn from_f64(n: f64) -> JsValue {
267 wbg_cast(n)
268 }
269
270 #[inline]
275 pub fn bigint_from_str(s: &str) -> JsValue {
276 __wbindgen_bigint_from_str(s)
277 }
278
279 #[inline]
284 pub const fn from_bool(b: bool) -> JsValue {
285 if b {
286 JsValue::TRUE
287 } else {
288 JsValue::FALSE
289 }
290 }
291
292 #[inline]
294 pub const fn undefined() -> JsValue {
295 JsValue::UNDEFINED
296 }
297
298 #[inline]
300 pub const fn null() -> JsValue {
301 JsValue::NULL
302 }
303
304 pub fn symbol(description: Option<&str>) -> JsValue {
309 __wbindgen_symbol_new(description)
310 }
311
312 #[cfg(feature = "serde-serialize")]
336 #[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
337 pub fn from_serde<T>(t: &T) -> serde_json::Result<JsValue>
338 where
339 T: serde::ser::Serialize + ?Sized,
340 {
341 let s = serde_json::to_string(t)?;
342 Ok(__wbindgen_json_parse(s))
343 }
344
345 #[cfg(feature = "serde-serialize")]
367 #[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
368 pub fn into_serde<T>(&self) -> serde_json::Result<T>
369 where
370 T: for<'a> serde::de::Deserialize<'a>,
371 {
372 let s = __wbindgen_json_serialize(self);
373 serde_json::from_str(s.as_deref().unwrap_or("null"))
377 }
378
379 #[inline]
385 pub fn as_f64(&self) -> Option<f64> {
386 __wbindgen_number_get(self)
387 }
388
389 #[inline]
391 pub fn is_string(&self) -> bool {
392 __wbindgen_is_string(self)
393 }
394
395 #[inline]
416 pub fn as_string(&self) -> Option<String> {
417 __wbindgen_string_get(self)
418 }
419
420 #[inline]
426 pub fn as_bool(&self) -> Option<bool> {
427 __wbindgen_boolean_get(self)
428 }
429
430 #[inline]
432 pub fn is_null(&self) -> bool {
433 __wbindgen_is_null(self)
434 }
435
436 #[inline]
438 pub fn is_undefined(&self) -> bool {
439 __wbindgen_is_undefined(self)
440 }
441
442 #[inline]
444 pub fn is_null_or_undefined(&self) -> bool {
445 __wbindgen_is_null_or_undefined(self)
446 }
447
448 #[inline]
450 pub fn is_symbol(&self) -> bool {
451 __wbindgen_is_symbol(self)
452 }
453
454 #[inline]
456 pub fn is_object(&self) -> bool {
457 __wbindgen_is_object(self)
458 }
459
460 #[inline]
462 pub fn is_array(&self) -> bool {
463 __wbindgen_is_array(self)
464 }
465
466 #[inline]
468 pub fn is_function(&self) -> bool {
469 __wbindgen_is_function(self)
470 }
471
472 #[inline]
474 pub fn is_bigint(&self) -> bool {
475 __wbindgen_is_bigint(self)
476 }
477
478 #[inline]
482 pub fn js_typeof(&self) -> JsValue {
483 __wbindgen_typeof(self)
484 }
485
486 #[inline]
490 pub fn js_in(&self, obj: &JsValue) -> bool {
491 __wbindgen_in(self, obj)
492 }
493
494 #[inline]
498 pub fn is_truthy(&self) -> bool {
499 !self.is_falsy()
500 }
501
502 #[inline]
506 pub fn is_falsy(&self) -> bool {
507 __wbindgen_is_falsy(self)
508 }
509
510 fn as_debug_string(&self) -> String {
512 __wbindgen_debug_string(self)
513 }
514
515 #[inline]
519 pub fn loose_eq(&self, other: &Self) -> bool {
520 __wbindgen_jsval_loose_eq(self, other)
521 }
522
523 #[inline]
527 pub fn bit_not(&self) -> JsValue {
528 __wbindgen_bit_not(self)
529 }
530
531 #[inline]
535 pub fn unsigned_shr(&self, rhs: &Self) -> u32 {
536 __wbindgen_unsigned_shr(self, rhs)
537 }
538
539 #[inline]
543 pub fn checked_div(&self, rhs: &Self) -> Self {
544 __wbindgen_checked_div(self, rhs)
545 }
546
547 #[inline]
551 pub fn pow(&self, rhs: &Self) -> Self {
552 __wbindgen_pow(self, rhs)
553 }
554
555 #[inline]
559 pub fn lt(&self, other: &Self) -> bool {
560 __wbindgen_lt(self, other)
561 }
562
563 #[inline]
567 pub fn le(&self, other: &Self) -> bool {
568 __wbindgen_le(self, other)
569 }
570
571 #[inline]
575 pub fn ge(&self, other: &Self) -> bool {
576 __wbindgen_ge(self, other)
577 }
578
579 #[inline]
583 pub fn gt(&self, other: &Self) -> bool {
584 __wbindgen_gt(self, other)
585 }
586
587 #[inline]
591 pub fn unchecked_into_f64(&self) -> f64 {
592 __wbindgen_as_number(self)
595 }
596}
597
598impl PartialEq for JsValue {
599 #[inline]
603 fn eq(&self, other: &Self) -> bool {
604 __wbindgen_jsval_eq(self, other)
605 }
606}
607
608impl PartialEq<bool> for JsValue {
609 #[inline]
610 fn eq(&self, other: &bool) -> bool {
611 self.as_bool() == Some(*other)
612 }
613}
614
615impl PartialEq<str> for JsValue {
616 #[inline]
617 fn eq(&self, other: &str) -> bool {
618 *self == JsValue::from_str(other)
619 }
620}
621
622impl<'a> PartialEq<&'a str> for JsValue {
623 #[inline]
624 fn eq(&self, other: &&'a str) -> bool {
625 <JsValue as PartialEq<str>>::eq(self, other)
626 }
627}
628
629impl PartialEq<String> for JsValue {
630 #[inline]
631 fn eq(&self, other: &String) -> bool {
632 <JsValue as PartialEq<str>>::eq(self, other)
633 }
634}
635impl<'a> PartialEq<&'a String> for JsValue {
636 #[inline]
637 fn eq(&self, other: &&'a String) -> bool {
638 <JsValue as PartialEq<str>>::eq(self, other)
639 }
640}
641
642macro_rules! forward_deref_unop {
643 (impl $imp:ident, $method:ident for $t:ty) => {
644 impl $imp for $t {
645 type Output = <&'static $t as $imp>::Output;
646
647 #[inline]
648 fn $method(self) -> <&'static $t as $imp>::Output {
649 $imp::$method(&self)
650 }
651 }
652 };
653}
654
655macro_rules! forward_deref_binop {
656 (impl $imp:ident, $method:ident for $t:ty) => {
657 impl<'a> $imp<$t> for &'a $t {
658 type Output = <&'static $t as $imp<&'static $t>>::Output;
659
660 #[inline]
661 fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
662 $imp::$method(self, &other)
663 }
664 }
665
666 impl $imp<&$t> for $t {
667 type Output = <&'static $t as $imp<&'static $t>>::Output;
668
669 #[inline]
670 fn $method(self, other: &$t) -> <&'static $t as $imp<&'static $t>>::Output {
671 $imp::$method(&self, other)
672 }
673 }
674
675 impl $imp<$t> for $t {
676 type Output = <&'static $t as $imp<&'static $t>>::Output;
677
678 #[inline]
679 fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
680 $imp::$method(&self, &other)
681 }
682 }
683 };
684}
685
686impl Not for &JsValue {
687 type Output = bool;
688
689 #[inline]
693 fn not(self) -> Self::Output {
694 JsValue::is_falsy(self)
695 }
696}
697
698forward_deref_unop!(impl Not, not for JsValue);
699
700impl TryFrom<JsValue> for f64 {
701 type Error = JsValue;
702
703 #[inline]
708 fn try_from(val: JsValue) -> Result<Self, Self::Error> {
709 f64::try_from(&val)
710 }
711}
712
713impl TryFrom<&JsValue> for f64 {
714 type Error = JsValue;
715
716 #[inline]
721 fn try_from(val: &JsValue) -> Result<Self, Self::Error> {
722 let jsval = __wbindgen_try_into_number(val);
723 match jsval.as_f64() {
724 Some(num) => Ok(num),
725 None => Err(jsval),
726 }
727 }
728}
729
730impl Neg for &JsValue {
731 type Output = JsValue;
732
733 #[inline]
737 fn neg(self) -> Self::Output {
738 __wbindgen_neg(self)
739 }
740}
741
742forward_deref_unop!(impl Neg, neg for JsValue);
743
744impl BitAnd for &JsValue {
745 type Output = JsValue;
746
747 #[inline]
751 fn bitand(self, rhs: Self) -> Self::Output {
752 __wbindgen_bit_and(self, rhs)
753 }
754}
755
756forward_deref_binop!(impl BitAnd, bitand for JsValue);
757
758impl BitOr for &JsValue {
759 type Output = JsValue;
760
761 #[inline]
765 fn bitor(self, rhs: Self) -> Self::Output {
766 __wbindgen_bit_or(self, rhs)
767 }
768}
769
770forward_deref_binop!(impl BitOr, bitor for JsValue);
771
772impl BitXor for &JsValue {
773 type Output = JsValue;
774
775 #[inline]
779 fn bitxor(self, rhs: Self) -> Self::Output {
780 __wbindgen_bit_xor(self, rhs)
781 }
782}
783
784forward_deref_binop!(impl BitXor, bitxor for JsValue);
785
786impl Shl for &JsValue {
787 type Output = JsValue;
788
789 #[inline]
793 fn shl(self, rhs: Self) -> Self::Output {
794 __wbindgen_shl(self, rhs)
795 }
796}
797
798forward_deref_binop!(impl Shl, shl for JsValue);
799
800impl Shr for &JsValue {
801 type Output = JsValue;
802
803 #[inline]
807 fn shr(self, rhs: Self) -> Self::Output {
808 __wbindgen_shr(self, rhs)
809 }
810}
811
812forward_deref_binop!(impl Shr, shr for JsValue);
813
814impl Add for &JsValue {
815 type Output = JsValue;
816
817 #[inline]
821 fn add(self, rhs: Self) -> Self::Output {
822 __wbindgen_add(self, rhs)
823 }
824}
825
826forward_deref_binop!(impl Add, add for JsValue);
827
828impl Sub for &JsValue {
829 type Output = JsValue;
830
831 #[inline]
835 fn sub(self, rhs: Self) -> Self::Output {
836 __wbindgen_sub(self, rhs)
837 }
838}
839
840forward_deref_binop!(impl Sub, sub for JsValue);
841
842impl Div for &JsValue {
843 type Output = JsValue;
844
845 #[inline]
849 fn div(self, rhs: Self) -> Self::Output {
850 __wbindgen_div(self, rhs)
851 }
852}
853
854forward_deref_binop!(impl Div, div for JsValue);
855
856impl Mul for &JsValue {
857 type Output = JsValue;
858
859 #[inline]
863 fn mul(self, rhs: Self) -> Self::Output {
864 __wbindgen_mul(self, rhs)
865 }
866}
867
868forward_deref_binop!(impl Mul, mul for JsValue);
869
870impl Rem for &JsValue {
871 type Output = JsValue;
872
873 #[inline]
877 fn rem(self, rhs: Self) -> Self::Output {
878 __wbindgen_rem(self, rhs)
879 }
880}
881
882forward_deref_binop!(impl Rem, rem for JsValue);
883
884impl<'a> From<&'a str> for JsValue {
885 #[inline]
886 fn from(s: &'a str) -> JsValue {
887 JsValue::from_str(s)
888 }
889}
890
891impl<T> From<*mut T> for JsValue {
892 #[inline]
893 fn from(s: *mut T) -> JsValue {
894 JsValue::from(s as usize)
895 }
896}
897
898impl<T> From<*const T> for JsValue {
899 #[inline]
900 fn from(s: *const T) -> JsValue {
901 JsValue::from(s as usize)
902 }
903}
904
905impl<T> From<NonNull<T>> for JsValue {
906 #[inline]
907 fn from(s: NonNull<T>) -> JsValue {
908 JsValue::from(s.as_ptr() as usize)
909 }
910}
911
912impl<'a> From<&'a String> for JsValue {
913 #[inline]
914 fn from(s: &'a String) -> JsValue {
915 JsValue::from_str(s)
916 }
917}
918
919impl From<String> for JsValue {
920 #[inline]
921 fn from(s: String) -> JsValue {
922 JsValue::from_str(&s)
923 }
924}
925
926impl TryFrom<JsValue> for String {
927 type Error = JsValue;
928
929 fn try_from(value: JsValue) -> Result<Self, Self::Error> {
930 match value.as_string() {
931 Some(s) => Ok(s),
932 None => Err(value),
933 }
934 }
935}
936
937impl TryFromJsValue for String {
938 fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
939 value.as_string()
940 }
941}
942
943impl From<bool> for JsValue {
944 #[inline]
945 fn from(s: bool) -> JsValue {
946 JsValue::from_bool(s)
947 }
948}
949
950impl TryFromJsValue for bool {
951 fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
952 value.as_bool()
953 }
954}
955
956impl TryFromJsValue for char {
957 fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
958 let s = value.as_string()?;
959 if s.len() == 1 {
960 Some(s.chars().nth(0).unwrap())
961 } else {
962 None
963 }
964 }
965}
966
967impl<'a, T> From<&'a T> for JsValue
968where
969 T: JsCast,
970{
971 #[inline]
972 fn from(s: &'a T) -> JsValue {
973 s.as_ref().clone()
974 }
975}
976
977impl<T> From<Option<T>> for JsValue
978where
979 JsValue: From<T>,
980{
981 #[inline]
982 fn from(s: Option<T>) -> JsValue {
983 match s {
984 Some(s) => s.into(),
985 None => JsValue::undefined(),
986 }
987 }
988}
989
990impl JsCast for JsValue {
992 #[inline]
993 fn instanceof(_val: &JsValue) -> bool {
994 true
995 }
996 #[inline]
997 fn unchecked_from_js(val: JsValue) -> Self {
998 val
999 }
1000 #[inline]
1001 fn unchecked_from_js_ref(val: &JsValue) -> &Self {
1002 val
1003 }
1004}
1005
1006impl AsRef<JsValue> for JsValue {
1007 #[inline]
1008 fn as_ref(&self) -> &JsValue {
1009 self
1010 }
1011}
1012
1013impl UpcastFrom<JsValue> for JsValue {}
1014impl<Target: UpcastFrom<JsValue>> UpcastFrom<JsValue> for Option<Target> {}
1015impl<Target: UpcastFrom<JsValue>> UpcastFrom<JsValue> for Nullable<Target> {}
1016
1017fn to_uint_32(v: &JsValue) -> Option<u32> {
1020 v.as_f64().map(|n| {
1021 if n.is_infinite() {
1022 0
1023 } else {
1024 (n as i64) as u32
1025 }
1026 })
1027}
1028
1029macro_rules! integers {
1030 ($($n:ident)*) => ($(
1031 impl PartialEq<$n> for JsValue {
1032 #[inline]
1033 fn eq(&self, other: &$n) -> bool {
1034 self.as_f64() == Some(f64::from(*other))
1035 }
1036 }
1037
1038 impl From<$n> for JsValue {
1039 #[inline]
1040 fn from(n: $n) -> JsValue {
1041 JsValue::from_f64(n.into())
1042 }
1043 }
1044
1045 impl TryFromJsValue for $n {
1047 #[inline]
1048 fn try_from_js_value_ref(val: &JsValue) -> Option<$n> {
1049 to_uint_32(val).map(|n| n as $n)
1050 }
1051 }
1052 )*)
1053}
1054
1055integers! { i8 u8 i16 u16 i32 u32 }
1056
1057macro_rules! floats {
1058 ($($n:ident)*) => ($(
1059 impl PartialEq<$n> for JsValue {
1060 #[inline]
1061 fn eq(&self, other: &$n) -> bool {
1062 self.as_f64() == Some(f64::from(*other))
1063 }
1064 }
1065
1066 impl From<$n> for JsValue {
1067 #[inline]
1068 fn from(n: $n) -> JsValue {
1069 JsValue::from_f64(n.into())
1070 }
1071 }
1072
1073 impl TryFromJsValue for $n {
1074 #[inline]
1075 fn try_from_js_value_ref(val: &JsValue) -> Option<$n> {
1076 val.as_f64().map(|n| n as $n)
1077 }
1078 }
1079 )*)
1080}
1081
1082floats! { f32 f64 }
1083
1084macro_rules! big_integers {
1085 ($($n:ident)*) => ($(
1086 impl PartialEq<$n> for JsValue {
1087 #[inline]
1088 fn eq(&self, other: &$n) -> bool {
1089 self == &JsValue::from(*other)
1090 }
1091 }
1092
1093 impl From<$n> for JsValue {
1094 #[inline]
1095 fn from(arg: $n) -> JsValue {
1096 wbg_cast(arg)
1097 }
1098 }
1099
1100 impl TryFrom<JsValue> for $n {
1101 type Error = JsValue;
1102
1103 #[inline]
1104 fn try_from(v: JsValue) -> Result<Self, JsValue> {
1105 Self::try_from_js_value(v)
1106 }
1107 }
1108
1109 impl TryFromJsValue for $n {
1110 #[inline]
1111 fn try_from_js_value_ref(val: &JsValue) -> Option<$n> {
1112 let as_i64 = __wbindgen_bigint_get_as_i64(&val)?;
1113 let as_self = as_i64 as $n;
1116 if val == &as_self {
1118 Some(as_self)
1119 } else {
1120 None
1121 }
1122 }
1123 }
1124 )*)
1125}
1126
1127big_integers! { i64 u64 }
1128
1129macro_rules! num128 {
1130 ($ty:ty, $hi_ty:ty) => {
1131 impl PartialEq<$ty> for JsValue {
1132 #[inline]
1133 fn eq(&self, other: &$ty) -> bool {
1134 self == &JsValue::from(*other)
1135 }
1136 }
1137
1138 impl From<$ty> for JsValue {
1139 #[inline]
1140 fn from(arg: $ty) -> JsValue {
1141 wbg_cast(arg)
1142 }
1143 }
1144
1145 impl TryFrom<JsValue> for $ty {
1146 type Error = JsValue;
1147
1148 #[inline]
1149 fn try_from(v: JsValue) -> Result<Self, JsValue> {
1150 Self::try_from_js_value(v)
1151 }
1152 }
1153
1154 impl TryFromJsValue for $ty {
1155 fn try_from_js_value_ref(v: &JsValue) -> Option<$ty> {
1157 let lo = __wbindgen_bigint_get_as_i64(&v)? as u64;
1160 let hi = v >> JsValue::from(64_u64);
1163 <$hi_ty>::try_from_js_value_ref(&hi).map(|hi| Self::from(hi) << 64 | Self::from(lo))
1166 }
1167 }
1168 };
1169}
1170
1171num128!(i128, i64);
1172
1173num128!(u128, u64);
1174
1175impl TryFromJsValue for () {
1176 fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
1177 if value.is_undefined() {
1178 Some(())
1179 } else {
1180 None
1181 }
1182 }
1183}
1184
1185impl<T: TryFromJsValue> TryFromJsValue for Option<T> {
1186 fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
1187 if value.is_null_or_undefined() {
1188 Some(None)
1189 } else {
1190 T::try_from_js_value_ref(value).map(Some)
1191 }
1192 }
1193}
1194
1195impl<T: TryFromJsValue> TryFromJsValue for Nullable<T> {
1196 fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
1197 if value.is_null() {
1198 Some(Nullable::Null)
1199 } else {
1200 T::try_from_js_value_ref(value).map(Nullable::Value)
1201 }
1202 }
1203}
1204
1205impl<T: TryFromJsValue> TryFromJsValue for Vec<T> {
1210 fn try_from_js_value_ref(value: &JsValue) -> Option<Self> {
1211 if !__wbindgen_is_array(value) {
1212 return None;
1213 }
1214 let len = __wbindgen_reflect_get(value, &JsValue::from_str("length")).as_f64()? as u32;
1215 let mut out = Vec::with_capacity(len as usize);
1216 for i in 0..len {
1217 let elem = __wbindgen_reflect_get(value, &JsValue::from_f64(i as f64));
1218 out.push(T::try_from_js_value(elem).ok()?);
1219 }
1220 Some(out)
1221 }
1222}
1223
1224impl PartialEq<usize> for JsValue {
1227 #[inline]
1228 fn eq(&self, other: &usize) -> bool {
1229 *self == (*other as crate::__rt::WasmWordRepr)
1230 }
1231}
1232
1233impl From<usize> for JsValue {
1234 #[inline]
1235 fn from(n: usize) -> Self {
1236 Self::from(n as crate::__rt::WasmWordRepr)
1237 }
1238}
1239
1240impl PartialEq<isize> for JsValue {
1241 #[inline]
1242 fn eq(&self, other: &isize) -> bool {
1243 *self == (*other as crate::__rt::WasmSignedWordRepr)
1244 }
1245}
1246
1247impl From<isize> for JsValue {
1248 #[inline]
1249 fn from(n: isize) -> Self {
1250 Self::from(n as crate::__rt::WasmSignedWordRepr)
1251 }
1252}
1253
1254impl TryFromJsValue for isize {
1256 #[inline]
1257 fn try_from_js_value_ref(val: &JsValue) -> Option<isize> {
1258 val.as_f64().map(|n| n as isize)
1259 }
1260}
1261
1262impl TryFromJsValue for usize {
1264 #[inline]
1265 fn try_from_js_value_ref(val: &JsValue) -> Option<usize> {
1266 val.as_f64().map(|n| n as usize)
1267 }
1268}
1269
1270#[wasm_bindgen_macro::wasm_bindgen(wasm_bindgen = crate)]
1272extern "C" {
1273 #[wasm_bindgen(js_namespace = Array, js_name = isArray)]
1274 fn __wbindgen_is_array(v: &JsValue) -> bool;
1275
1276 #[wasm_bindgen(js_namespace = Reflect, js_name = get)]
1277 fn __wbindgen_reflect_get(target: &JsValue, key: &JsValue) -> JsValue;
1278
1279 #[wasm_bindgen(js_name = BigInt)]
1280 fn __wbindgen_bigint_from_str(s: &str) -> JsValue;
1281
1282 #[wasm_bindgen(js_name = Symbol)]
1283 fn __wbindgen_symbol_new(description: Option<&str>) -> JsValue;
1284
1285 #[wasm_bindgen(js_name = Error)]
1286 fn __wbindgen_error_new(msg: &str) -> JsValue;
1287
1288 #[wasm_bindgen(js_namespace = JSON, js_name = parse)]
1289 fn __wbindgen_json_parse(json: String) -> JsValue;
1290
1291 #[wasm_bindgen(js_namespace = JSON, js_name = stringify)]
1292 fn __wbindgen_json_serialize(v: &JsValue) -> Option<String>;
1293
1294 #[wasm_bindgen(js_name = Number)]
1295 fn __wbindgen_as_number(v: &JsValue) -> f64;
1296}
1297
1298#[wasm_bindgen_macro::wasm_bindgen(wasm_bindgen = crate, raw_module = "__wbindgen_placeholder__")]
1301extern "C" {
1302 #[cfg(not(wbg_reference_types))]
1303 fn __wbindgen_externref_heap_live_count() -> u32;
1304
1305 fn __wbindgen_is_null(js: &JsValue) -> bool;
1306 fn __wbindgen_is_undefined(js: &JsValue) -> bool;
1307 fn __wbindgen_is_null_or_undefined(js: &JsValue) -> bool;
1308 fn __wbindgen_is_symbol(js: &JsValue) -> bool;
1309 fn __wbindgen_is_object(js: &JsValue) -> bool;
1310 fn __wbindgen_is_function(js: &JsValue) -> bool;
1311 fn __wbindgen_is_string(js: &JsValue) -> bool;
1312 fn __wbindgen_is_bigint(js: &JsValue) -> bool;
1313 fn __wbindgen_typeof(js: &JsValue) -> JsValue;
1314
1315 fn __wbindgen_in(prop: &JsValue, obj: &JsValue) -> bool;
1316
1317 fn __wbindgen_is_falsy(js: &JsValue) -> bool;
1318 fn __wbindgen_try_into_number(js: &JsValue) -> JsValue;
1319 fn __wbindgen_neg(js: &JsValue) -> JsValue;
1320 fn __wbindgen_bit_and(a: &JsValue, b: &JsValue) -> JsValue;
1321 fn __wbindgen_bit_or(a: &JsValue, b: &JsValue) -> JsValue;
1322 fn __wbindgen_bit_xor(a: &JsValue, b: &JsValue) -> JsValue;
1323 fn __wbindgen_bit_not(js: &JsValue) -> JsValue;
1324 fn __wbindgen_shl(a: &JsValue, b: &JsValue) -> JsValue;
1325 fn __wbindgen_shr(a: &JsValue, b: &JsValue) -> JsValue;
1326 fn __wbindgen_unsigned_shr(a: &JsValue, b: &JsValue) -> u32;
1327 fn __wbindgen_add(a: &JsValue, b: &JsValue) -> JsValue;
1328 fn __wbindgen_sub(a: &JsValue, b: &JsValue) -> JsValue;
1329 fn __wbindgen_div(a: &JsValue, b: &JsValue) -> JsValue;
1330 fn __wbindgen_checked_div(a: &JsValue, b: &JsValue) -> JsValue;
1331 fn __wbindgen_mul(a: &JsValue, b: &JsValue) -> JsValue;
1332 fn __wbindgen_rem(a: &JsValue, b: &JsValue) -> JsValue;
1333 fn __wbindgen_pow(a: &JsValue, b: &JsValue) -> JsValue;
1334 fn __wbindgen_lt(a: &JsValue, b: &JsValue) -> bool;
1335 fn __wbindgen_le(a: &JsValue, b: &JsValue) -> bool;
1336 fn __wbindgen_ge(a: &JsValue, b: &JsValue) -> bool;
1337 fn __wbindgen_gt(a: &JsValue, b: &JsValue) -> bool;
1338
1339 fn __wbindgen_number_get(js: &JsValue) -> Option<f64>;
1340 fn __wbindgen_boolean_get(js: &JsValue) -> Option<bool>;
1341 fn __wbindgen_string_get(js: &JsValue) -> Option<String>;
1342 fn __wbindgen_string_from_utf8_lossy(bytes: &[u8]) -> JsValue;
1343 fn __wbindgen_bigint_get_as_i64(js: &JsValue) -> Option<i64>;
1344
1345 fn __wbindgen_debug_string(js: &JsValue) -> String;
1346
1347 fn __wbindgen_throw(msg: &str) ;
1348 fn __wbindgen_rethrow(js: JsValue) ;
1349
1350 fn __wbindgen_jsval_eq(a: &JsValue, b: &JsValue) -> bool;
1351 fn __wbindgen_jsval_loose_eq(a: &JsValue, b: &JsValue) -> bool;
1352
1353 fn __wbindgen_copy_to_typed_array(data: &[u8], js: &JsValue);
1354
1355 fn __wbindgen_init_externref_table();
1356
1357 fn __wbindgen_exports() -> JsValue;
1358 fn __wbindgen_memory() -> JsValue;
1359 fn __wbindgen_module() -> JsValue;
1360 fn __wbindgen_instance() -> JsValue;
1361 fn __wbindgen_function_table() -> JsValue;
1362
1363 fn __wbindgen_reinit();
1364}
1365
1366externs! {
1369 #[link(wasm_import_module = "__wbindgen_placeholder__")]
1370 extern "C" {
1371 fn __wbindgen_object_clone_ref(idx: u32) -> u32;
1372 fn __wbindgen_object_drop_ref(idx: u32) -> ();
1373
1374 fn __wbindgen_describe(v: u32) -> ();
1375 fn __wbindgen_describe_generic_import(func: *const (), prims: *const ()) -> *const ();
1384 }
1385}
1386
1387#[inline]
1391fn externref_statics_end() -> u32 {
1392 #[cfg(wbg_reference_types)]
1393 return externref::statics_end();
1394 #[cfg(not(wbg_reference_types))]
1395 __rt::JSIDX_RESERVED
1396}
1397
1398impl Clone for JsValue {
1399 #[inline]
1400 fn clone(&self) -> JsValue {
1401 if (__rt::JSIDX_OFFSET..externref_statics_end()).contains(&self.idx) {
1402 JsValue::_new(self.idx)
1403 } else {
1404 JsValue::_new(unsafe { __wbindgen_object_clone_ref(self.idx) })
1405 }
1406 }
1407}
1408
1409impl core::fmt::Debug for JsValue {
1410 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1411 write!(f, "JsValue({})", self.as_debug_string())
1412 }
1413}
1414
1415impl Drop for JsValue {
1416 #[inline]
1417 fn drop(&mut self) {
1418 unsafe {
1419 debug_assert!(
1421 self.idx >= __rt::JSIDX_OFFSET,
1422 "free of stack slot {}",
1423 self.idx
1424 );
1425
1426 if self.idx >= externref_statics_end() {
1430 __wbindgen_object_drop_ref(self.idx);
1431 }
1432 }
1433 }
1434}
1435
1436impl Default for JsValue {
1437 fn default() -> Self {
1438 Self::UNDEFINED
1439 }
1440}
1441
1442#[cfg(feature = "std")]
1463#[deprecated = "use with `#[wasm_bindgen(thread_local_v2)]` instead"]
1464pub struct JsStatic<T: 'static> {
1465 #[doc(hidden)]
1466 pub __inner: &'static std::thread::LocalKey<T>,
1467}
1468
1469#[cfg(feature = "std")]
1470#[allow(deprecated)]
1471#[cfg(not(target_feature = "atomics"))]
1472impl<T: crate::convert::FromWasmAbi + 'static> Deref for JsStatic<T> {
1473 type Target = T;
1474 fn deref(&self) -> &T {
1475 unsafe { self.__inner.with(|ptr| &*(ptr as *const T)) }
1476 }
1477}
1478
1479pub struct JsThreadLocal<T: 'static> {
1498 #[doc(hidden)]
1499 #[cfg(not(target_feature = "atomics"))]
1500 pub __inner: &'static __rt::LazyCell<T>,
1501 #[doc(hidden)]
1502 #[cfg(target_feature = "atomics")]
1503 pub __inner: fn() -> *const T,
1504}
1505
1506impl<T> JsThreadLocal<T> {
1507 pub fn with<F, R>(&'static self, f: F) -> R
1508 where
1509 F: FnOnce(&T) -> R,
1510 {
1511 #[cfg(not(target_feature = "atomics"))]
1512 return f(self.__inner);
1513 #[cfg(target_feature = "atomics")]
1514 f(unsafe { &*(self.__inner)() })
1515 }
1516}
1517
1518#[cold]
1519#[inline(never)]
1520#[deprecated(note = "renamed to `throw_str`")]
1521#[doc(hidden)]
1522pub fn throw(s: &str) -> ! {
1523 throw_str(s)
1524}
1525
1526#[cold]
1541#[inline(never)]
1542pub fn throw_str(s: &str) -> ! {
1543 __wbindgen_throw(s);
1544 unsafe { core::hint::unreachable_unchecked() }
1545}
1546
1547#[cold]
1562#[inline(never)]
1563pub fn throw_val(s: JsValue) -> ! {
1564 __wbindgen_rethrow(s);
1565 unsafe { core::hint::unreachable_unchecked() }
1566}
1567
1568pub fn externref_heap_live_count() -> u32 {
1613 __wbindgen_externref_heap_live_count()
1614}
1615
1616#[doc(hidden)]
1617pub fn anyref_heap_live_count() -> u32 {
1618 externref_heap_live_count()
1619}
1620
1621pub trait UnwrapThrowExt<T>: Sized {
1651 #[cfg_attr(
1654 any(
1655 debug_assertions,
1656 not(all(target_family = "wasm", not(target_os = "wasi")))
1657 ),
1658 track_caller
1659 )]
1660 fn unwrap_throw(self) -> T {
1661 if cfg!(all(
1662 debug_assertions,
1663 all(target_family = "wasm", not(target_os = "wasi"))
1664 )) {
1665 let loc = core::panic::Location::caller();
1666 let msg = alloc::format!(
1667 "called `{}::unwrap_throw()` ({}:{}:{})",
1668 core::any::type_name::<Self>(),
1669 loc.file(),
1670 loc.line(),
1671 loc.column()
1672 );
1673 self.expect_throw(&msg)
1674 } else {
1675 self.expect_throw("called `unwrap_throw()`")
1676 }
1677 }
1678
1679 #[cfg_attr(
1683 any(
1684 debug_assertions,
1685 not(all(target_family = "wasm", not(target_os = "wasi")))
1686 ),
1687 track_caller
1688 )]
1689 fn expect_throw(self, message: &str) -> T;
1690}
1691
1692impl<T> UnwrapThrowExt<T> for Option<T> {
1693 fn unwrap_throw(self) -> T {
1694 const MSG: &str = "called `Option::unwrap_throw()` on a `None` value";
1695
1696 if cfg!(all(target_family = "wasm", not(target_os = "wasi"))) {
1697 if let Some(val) = self {
1698 val
1699 } else if cfg!(debug_assertions) {
1700 let loc = core::panic::Location::caller();
1701 let msg = alloc::format!("{MSG} ({}:{}:{})", loc.file(), loc.line(), loc.column(),);
1702
1703 throw_str(&msg)
1704 } else {
1705 throw_str(MSG)
1706 }
1707 } else {
1708 self.expect(MSG)
1709 }
1710 }
1711
1712 fn expect_throw(self, message: &str) -> T {
1713 if cfg!(all(target_family = "wasm", not(target_os = "wasi"))) {
1714 if let Some(val) = self {
1715 val
1716 } else if cfg!(debug_assertions) {
1717 let loc = core::panic::Location::caller();
1718 let msg =
1719 alloc::format!("{message} ({}:{}:{})", loc.file(), loc.line(), loc.column(),);
1720
1721 throw_str(&msg)
1722 } else {
1723 throw_str(message)
1724 }
1725 } else {
1726 self.expect(message)
1727 }
1728 }
1729}
1730
1731impl<T, E> UnwrapThrowExt<T> for Result<T, E>
1732where
1733 E: core::fmt::Debug,
1734{
1735 fn unwrap_throw(self) -> T {
1736 const MSG: &str = "called `Result::unwrap_throw()` on an `Err` value";
1737
1738 if cfg!(all(target_family = "wasm", not(target_os = "wasi"))) {
1739 match self {
1740 Ok(val) => val,
1741 Err(err) => {
1742 if cfg!(debug_assertions) {
1743 let loc = core::panic::Location::caller();
1744 let msg = alloc::format!(
1745 "{MSG} ({}:{}:{}): {err:?}",
1746 loc.file(),
1747 loc.line(),
1748 loc.column(),
1749 );
1750
1751 throw_str(&msg)
1752 } else {
1753 throw_str(MSG)
1754 }
1755 }
1756 }
1757 } else {
1758 self.expect(MSG)
1759 }
1760 }
1761
1762 fn expect_throw(self, message: &str) -> T {
1763 if cfg!(all(target_family = "wasm", not(target_os = "wasi"))) {
1764 match self {
1765 Ok(val) => val,
1766 Err(err) => {
1767 if cfg!(debug_assertions) {
1768 let loc = core::panic::Location::caller();
1769 let msg = alloc::format!(
1770 "{message} ({}:{}:{}): {err:?}",
1771 loc.file(),
1772 loc.line(),
1773 loc.column(),
1774 );
1775
1776 throw_str(&msg)
1777 } else {
1778 throw_str(message)
1779 }
1780 }
1781 }
1782 } else {
1783 self.expect(message)
1784 }
1785 }
1786}
1787
1788pub fn module() -> JsValue {
1793 __wbindgen_module()
1794}
1795
1796pub fn instance() -> JsValue {
1801 __wbindgen_instance()
1802}
1803
1804pub fn exports() -> JsValue {
1807 __wbindgen_exports()
1808}
1809
1810pub fn memory() -> JsValue {
1812 __wbindgen_memory()
1813}
1814
1815pub fn function_table() -> JsValue {
1818 __wbindgen_function_table()
1819}
1820
1821#[derive(Copy, Clone, PartialEq, Debug, Eq)]
1834pub struct Clamped<T>(pub T);
1835
1836impl<T> Deref for Clamped<T> {
1837 type Target = T;
1838
1839 fn deref(&self) -> &T {
1840 &self.0
1841 }
1842}
1843
1844impl<T> DerefMut for Clamped<T> {
1845 fn deref_mut(&mut self) -> &mut T {
1846 &mut self.0
1847 }
1848}
1849
1850#[derive(Clone, Debug)]
1907#[repr(transparent)]
1908pub struct JsError {
1909 value: JsValue,
1910}
1911
1912impl JsError {
1913 #[inline]
1915 pub fn new(s: &str) -> JsError {
1916 Self {
1917 value: __wbindgen_error_new(s),
1918 }
1919 }
1920}
1921
1922impl<E> From<E> for JsError
1923where
1924 E: core::error::Error,
1925{
1926 fn from(error: E) -> Self {
1927 use alloc::string::ToString;
1928
1929 JsError::new(&error.to_string())
1930 }
1931}
1932
1933impl From<JsError> for JsValue {
1934 fn from(error: JsError) -> Self {
1935 error.value
1936 }
1937}
1938
1939impl<T: VectorIntoWasmAbi> From<Box<[T]>> for JsValue {
1940 fn from(vector: Box<[T]>) -> Self {
1941 wbg_cast(vector)
1942 }
1943}
1944
1945impl<T: VectorIntoWasmAbi> From<Clamped<Box<[T]>>> for JsValue {
1946 fn from(vector: Clamped<Box<[T]>>) -> Self {
1947 wbg_cast(vector)
1948 }
1949}
1950
1951impl<T: VectorIntoWasmAbi> From<Vec<T>> for JsValue {
1952 fn from(vector: Vec<T>) -> Self {
1953 JsValue::from(vector.into_boxed_slice())
1954 }
1955}
1956
1957impl<T: VectorIntoWasmAbi> From<Clamped<Vec<T>>> for JsValue {
1958 fn from(vector: Clamped<Vec<T>>) -> Self {
1959 JsValue::from(Clamped(vector.0.into_boxed_slice()))
1960 }
1961}