1#![no_std]
50#![cfg_attr(wasm_bindgen_unstable_test_coverage, feature(coverage_attribute))]
51#![cfg_attr(target_feature = "atomics", feature(thread_local))]
52#![cfg_attr(
53 any(target_feature = "atomics", wasm_bindgen_unstable_test_coverage),
54 feature(allow_internal_unstable),
55 allow(internal_features)
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 alloc::boxed::Box;
64use alloc::string::String;
65use alloc::vec::Vec;
66use core::convert::TryFrom;
67use core::marker::PhantomData;
68use core::mem;
69use core::ops::{
70 Add, BitAnd, BitOr, BitXor, Deref, DerefMut, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub,
71};
72use core::ptr::NonNull;
73
74use crate::convert::{FromWasmAbi, TryFromJsValue, WasmRet, WasmSlice};
75
76const _: () = {
77 #[no_mangle]
95 pub extern "C" fn __wbindgen_skip_interpret_calls() {}
96};
97
98macro_rules! externs {
99 ($(#[$attr:meta])* extern "C" { $(fn $name:ident($($args:tt)*) -> $ret:ty;)* }) => (
100 #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
101 $(#[$attr])*
102 extern "C" {
103 $(fn $name($($args)*) -> $ret;)*
104 }
105
106 $(
107 #[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
108 #[allow(unused_variables)]
109 unsafe extern "C" fn $name($($args)*) -> $ret {
110 panic!("function not implemented on non-wasm32 targets")
111 }
112 )*
113 )
114}
115
116pub mod prelude {
122 pub use crate::closure::Closure;
123 pub use crate::JsCast;
124 pub use crate::JsValue;
125 pub use crate::UnwrapThrowExt;
126 #[doc(hidden)]
127 pub use wasm_bindgen_macro::__wasm_bindgen_class_marker;
128 pub use wasm_bindgen_macro::wasm_bindgen;
129
130 pub use crate::JsError;
131}
132
133pub use wasm_bindgen_macro::link_to;
134
135pub mod closure;
136pub mod convert;
137pub mod describe;
138mod externref;
139mod link;
140
141mod cast;
142pub use crate::cast::{JsCast, JsObject};
143
144mod cache;
145pub use cache::intern::{intern, unintern};
146
147#[doc(hidden)]
148#[path = "rt/mod.rs"]
149pub mod __rt;
150
151pub struct JsValue {
158 idx: u32,
159 _marker: PhantomData<*mut u8>, }
161
162const JSIDX_OFFSET: u32 = 128; const JSIDX_UNDEFINED: u32 = JSIDX_OFFSET;
164const JSIDX_NULL: u32 = JSIDX_OFFSET + 1;
165const JSIDX_TRUE: u32 = JSIDX_OFFSET + 2;
166const JSIDX_FALSE: u32 = JSIDX_OFFSET + 3;
167const JSIDX_RESERVED: u32 = JSIDX_OFFSET + 4;
168
169impl JsValue {
170 pub const NULL: JsValue = JsValue::_new(JSIDX_NULL);
172
173 pub const UNDEFINED: JsValue = JsValue::_new(JSIDX_UNDEFINED);
175
176 pub const TRUE: JsValue = JsValue::_new(JSIDX_TRUE);
178
179 pub const FALSE: JsValue = JsValue::_new(JSIDX_FALSE);
181
182 #[inline]
183 const fn _new(idx: u32) -> JsValue {
184 JsValue {
185 idx,
186 _marker: PhantomData,
187 }
188 }
189
190 #[allow(clippy::should_implement_trait)] #[inline]
196 pub fn from_str(s: &str) -> JsValue {
197 unsafe { JsValue::_new(__wbindgen_string_new(s.as_ptr(), s.len())) }
198 }
199
200 #[inline]
205 pub fn from_f64(n: f64) -> JsValue {
206 unsafe { JsValue::_new(__wbindgen_number_new(n)) }
207 }
208
209 #[inline]
214 pub fn bigint_from_str(s: &str) -> JsValue {
215 __wbindgen_bigint_from_str(s)
216 }
217
218 #[inline]
223 pub const fn from_bool(b: bool) -> JsValue {
224 if b {
225 JsValue::TRUE
226 } else {
227 JsValue::FALSE
228 }
229 }
230
231 #[inline]
233 pub const fn undefined() -> JsValue {
234 JsValue::UNDEFINED
235 }
236
237 #[inline]
239 pub const fn null() -> JsValue {
240 JsValue::NULL
241 }
242
243 pub fn symbol(description: Option<&str>) -> JsValue {
248 __wbindgen_symbol_new(description)
249 }
250
251 #[cfg(feature = "serde-serialize")]
275 #[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
276 pub fn from_serde<T>(t: &T) -> serde_json::Result<JsValue>
277 where
278 T: serde::ser::Serialize + ?Sized,
279 {
280 let s = serde_json::to_string(t)?;
281 Ok(__wbindgen_json_parse(s))
282 }
283
284 #[cfg(feature = "serde-serialize")]
306 #[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
307 pub fn into_serde<T>(&self) -> serde_json::Result<T>
308 where
309 T: for<'a> serde::de::Deserialize<'a>,
310 {
311 let s = __wbindgen_json_serialize(self);
312 serde_json::from_str(s.as_deref().unwrap_or("null"))
316 }
317
318 #[inline]
324 pub fn as_f64(&self) -> Option<f64> {
325 unsafe { __wbindgen_number_get(self.idx).join() }
326 }
327
328 #[inline]
330 pub fn is_string(&self) -> bool {
331 unsafe { __wbindgen_is_string(self.idx) == 1 }
332 }
333
334 #[inline]
355 pub fn as_string(&self) -> Option<String> {
356 unsafe { FromWasmAbi::from_abi(__wbindgen_string_get(self.idx)) }
357 }
358
359 #[inline]
365 pub fn as_bool(&self) -> Option<bool> {
366 unsafe {
367 match __wbindgen_boolean_get(self.idx) {
368 0 => Some(false),
369 1 => Some(true),
370 _ => None,
371 }
372 }
373 }
374
375 #[inline]
377 pub fn is_null(&self) -> bool {
378 unsafe { __wbindgen_is_null(self.idx) == 1 }
379 }
380
381 #[inline]
383 pub fn is_undefined(&self) -> bool {
384 unsafe { __wbindgen_is_undefined(self.idx) == 1 }
385 }
386
387 #[inline]
389 pub fn is_symbol(&self) -> bool {
390 unsafe { __wbindgen_is_symbol(self.idx) == 1 }
391 }
392
393 #[inline]
395 pub fn is_object(&self) -> bool {
396 unsafe { __wbindgen_is_object(self.idx) == 1 }
397 }
398
399 #[inline]
401 pub fn is_array(&self) -> bool {
402 __wbindgen_is_array(self)
403 }
404
405 #[inline]
407 pub fn is_function(&self) -> bool {
408 unsafe { __wbindgen_is_function(self.idx) == 1 }
409 }
410
411 #[inline]
413 pub fn is_bigint(&self) -> bool {
414 unsafe { __wbindgen_is_bigint(self.idx) == 1 }
415 }
416
417 #[inline]
421 pub fn js_typeof(&self) -> JsValue {
422 unsafe { JsValue::_new(__wbindgen_typeof(self.idx)) }
423 }
424
425 #[inline]
429 pub fn js_in(&self, obj: &JsValue) -> bool {
430 unsafe { __wbindgen_in(self.idx, obj.idx) == 1 }
431 }
432
433 #[inline]
437 pub fn is_truthy(&self) -> bool {
438 !self.is_falsy()
439 }
440
441 #[inline]
445 pub fn is_falsy(&self) -> bool {
446 unsafe { __wbindgen_is_falsy(self.idx) == 1 }
447 }
448
449 fn as_debug_string(&self) -> String {
451 unsafe {
452 let mut ret = [0; 2];
453 __wbindgen_debug_string(&mut ret, self.idx);
454 let data = Vec::from_raw_parts(ret[0] as *mut u8, ret[1], ret[1]);
455 String::from_utf8_unchecked(data)
456 }
457 }
458
459 #[inline]
463 pub fn loose_eq(&self, other: &Self) -> bool {
464 unsafe { __wbindgen_jsval_loose_eq(self.idx, other.idx) != 0 }
465 }
466
467 #[inline]
471 pub fn bit_not(&self) -> JsValue {
472 unsafe { JsValue::_new(__wbindgen_bit_not(self.idx)) }
473 }
474
475 #[inline]
479 pub fn unsigned_shr(&self, rhs: &Self) -> u32 {
480 unsafe { __wbindgen_unsigned_shr(self.idx, rhs.idx) }
481 }
482
483 #[inline]
487 pub fn checked_div(&self, rhs: &Self) -> Self {
488 unsafe { JsValue::_new(__wbindgen_checked_div(self.idx, rhs.idx)) }
489 }
490
491 #[inline]
495 pub fn pow(&self, rhs: &Self) -> Self {
496 unsafe { JsValue::_new(__wbindgen_pow(self.idx, rhs.idx)) }
497 }
498
499 #[inline]
503 pub fn lt(&self, other: &Self) -> bool {
504 unsafe { __wbindgen_lt(self.idx, other.idx) == 1 }
505 }
506
507 #[inline]
511 pub fn le(&self, other: &Self) -> bool {
512 unsafe { __wbindgen_le(self.idx, other.idx) == 1 }
513 }
514
515 #[inline]
519 pub fn ge(&self, other: &Self) -> bool {
520 unsafe { __wbindgen_ge(self.idx, other.idx) == 1 }
521 }
522
523 #[inline]
527 pub fn gt(&self, other: &Self) -> bool {
528 unsafe { __wbindgen_gt(self.idx, other.idx) == 1 }
529 }
530
531 #[inline]
535 pub fn unchecked_into_f64(&self) -> f64 {
536 unsafe { __wbindgen_as_number(self.idx) }
537 }
538}
539
540impl PartialEq for JsValue {
541 #[inline]
545 fn eq(&self, other: &Self) -> bool {
546 unsafe { __wbindgen_jsval_eq(self.idx, other.idx) != 0 }
547 }
548}
549
550impl PartialEq<bool> for JsValue {
551 #[inline]
552 fn eq(&self, other: &bool) -> bool {
553 self.as_bool() == Some(*other)
554 }
555}
556
557impl PartialEq<str> for JsValue {
558 #[inline]
559 fn eq(&self, other: &str) -> bool {
560 *self == JsValue::from_str(other)
561 }
562}
563
564impl<'a> PartialEq<&'a str> for JsValue {
565 #[inline]
566 fn eq(&self, other: &&'a str) -> bool {
567 <JsValue as PartialEq<str>>::eq(self, other)
568 }
569}
570
571impl PartialEq<String> for JsValue {
572 #[inline]
573 fn eq(&self, other: &String) -> bool {
574 <JsValue as PartialEq<str>>::eq(self, other)
575 }
576}
577impl<'a> PartialEq<&'a String> for JsValue {
578 #[inline]
579 fn eq(&self, other: &&'a String) -> bool {
580 <JsValue as PartialEq<str>>::eq(self, other)
581 }
582}
583
584macro_rules! forward_deref_unop {
585 (impl $imp:ident, $method:ident for $t:ty) => {
586 impl $imp for $t {
587 type Output = <&'static $t as $imp>::Output;
588
589 #[inline]
590 fn $method(self) -> <&'static $t as $imp>::Output {
591 $imp::$method(&self)
592 }
593 }
594 };
595}
596
597macro_rules! forward_deref_binop {
598 (impl $imp:ident, $method:ident for $t:ty) => {
599 impl<'a> $imp<$t> for &'a $t {
600 type Output = <&'static $t as $imp<&'static $t>>::Output;
601
602 #[inline]
603 fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
604 $imp::$method(self, &other)
605 }
606 }
607
608 impl $imp<&$t> for $t {
609 type Output = <&'static $t as $imp<&'static $t>>::Output;
610
611 #[inline]
612 fn $method(self, other: &$t) -> <&'static $t as $imp<&'static $t>>::Output {
613 $imp::$method(&self, other)
614 }
615 }
616
617 impl $imp<$t> for $t {
618 type Output = <&'static $t as $imp<&'static $t>>::Output;
619
620 #[inline]
621 fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
622 $imp::$method(&self, &other)
623 }
624 }
625 };
626}
627
628impl Not for &JsValue {
629 type Output = bool;
630
631 #[inline]
635 fn not(self) -> Self::Output {
636 JsValue::is_falsy(self)
637 }
638}
639
640forward_deref_unop!(impl Not, not for JsValue);
641
642impl TryFrom<JsValue> for f64 {
643 type Error = JsValue;
644
645 #[inline]
650 fn try_from(val: JsValue) -> Result<Self, Self::Error> {
651 f64::try_from(&val)
652 }
653}
654
655impl TryFrom<&JsValue> for f64 {
656 type Error = JsValue;
657
658 #[inline]
663 fn try_from(val: &JsValue) -> Result<Self, Self::Error> {
664 let jsval = unsafe { JsValue::_new(__wbindgen_try_into_number(val.idx)) };
665 match jsval.as_f64() {
666 Some(num) => Ok(num),
667 None => Err(jsval),
668 }
669 }
670}
671
672impl Neg for &JsValue {
673 type Output = JsValue;
674
675 #[inline]
679 fn neg(self) -> Self::Output {
680 unsafe { JsValue::_new(__wbindgen_neg(self.idx)) }
681 }
682}
683
684forward_deref_unop!(impl Neg, neg for JsValue);
685
686impl BitAnd for &JsValue {
687 type Output = JsValue;
688
689 #[inline]
693 fn bitand(self, rhs: Self) -> Self::Output {
694 unsafe { JsValue::_new(__wbindgen_bit_and(self.idx, rhs.idx)) }
695 }
696}
697
698forward_deref_binop!(impl BitAnd, bitand for JsValue);
699
700impl BitOr for &JsValue {
701 type Output = JsValue;
702
703 #[inline]
707 fn bitor(self, rhs: Self) -> Self::Output {
708 unsafe { JsValue::_new(__wbindgen_bit_or(self.idx, rhs.idx)) }
709 }
710}
711
712forward_deref_binop!(impl BitOr, bitor for JsValue);
713
714impl BitXor for &JsValue {
715 type Output = JsValue;
716
717 #[inline]
721 fn bitxor(self, rhs: Self) -> Self::Output {
722 unsafe { JsValue::_new(__wbindgen_bit_xor(self.idx, rhs.idx)) }
723 }
724}
725
726forward_deref_binop!(impl BitXor, bitxor for JsValue);
727
728impl Shl for &JsValue {
729 type Output = JsValue;
730
731 #[inline]
735 fn shl(self, rhs: Self) -> Self::Output {
736 unsafe { JsValue::_new(__wbindgen_shl(self.idx, rhs.idx)) }
737 }
738}
739
740forward_deref_binop!(impl Shl, shl for JsValue);
741
742impl Shr for &JsValue {
743 type Output = JsValue;
744
745 #[inline]
749 fn shr(self, rhs: Self) -> Self::Output {
750 unsafe { JsValue::_new(__wbindgen_shr(self.idx, rhs.idx)) }
751 }
752}
753
754forward_deref_binop!(impl Shr, shr for JsValue);
755
756impl Add for &JsValue {
757 type Output = JsValue;
758
759 #[inline]
763 fn add(self, rhs: Self) -> Self::Output {
764 unsafe { JsValue::_new(__wbindgen_add(self.idx, rhs.idx)) }
765 }
766}
767
768forward_deref_binop!(impl Add, add for JsValue);
769
770impl Sub for &JsValue {
771 type Output = JsValue;
772
773 #[inline]
777 fn sub(self, rhs: Self) -> Self::Output {
778 unsafe { JsValue::_new(__wbindgen_sub(self.idx, rhs.idx)) }
779 }
780}
781
782forward_deref_binop!(impl Sub, sub for JsValue);
783
784impl Div for &JsValue {
785 type Output = JsValue;
786
787 #[inline]
791 fn div(self, rhs: Self) -> Self::Output {
792 unsafe { JsValue::_new(__wbindgen_div(self.idx, rhs.idx)) }
793 }
794}
795
796forward_deref_binop!(impl Div, div for JsValue);
797
798impl Mul for &JsValue {
799 type Output = JsValue;
800
801 #[inline]
805 fn mul(self, rhs: Self) -> Self::Output {
806 unsafe { JsValue::_new(__wbindgen_mul(self.idx, rhs.idx)) }
807 }
808}
809
810forward_deref_binop!(impl Mul, mul for JsValue);
811
812impl Rem for &JsValue {
813 type Output = JsValue;
814
815 #[inline]
819 fn rem(self, rhs: Self) -> Self::Output {
820 unsafe { JsValue::_new(__wbindgen_rem(self.idx, rhs.idx)) }
821 }
822}
823
824forward_deref_binop!(impl Rem, rem for JsValue);
825
826impl<'a> From<&'a str> for JsValue {
827 #[inline]
828 fn from(s: &'a str) -> JsValue {
829 JsValue::from_str(s)
830 }
831}
832
833impl<T> From<*mut T> for JsValue {
834 #[inline]
835 fn from(s: *mut T) -> JsValue {
836 JsValue::from(s as usize)
837 }
838}
839
840impl<T> From<*const T> for JsValue {
841 #[inline]
842 fn from(s: *const T) -> JsValue {
843 JsValue::from(s as usize)
844 }
845}
846
847impl<T> From<NonNull<T>> for JsValue {
848 #[inline]
849 fn from(s: NonNull<T>) -> JsValue {
850 JsValue::from(s.as_ptr() as usize)
851 }
852}
853
854impl<'a> From<&'a String> for JsValue {
855 #[inline]
856 fn from(s: &'a String) -> JsValue {
857 JsValue::from_str(s)
858 }
859}
860
861impl From<String> for JsValue {
862 #[inline]
863 fn from(s: String) -> JsValue {
864 JsValue::from_str(&s)
865 }
866}
867
868impl TryFrom<JsValue> for String {
869 type Error = JsValue;
870
871 fn try_from(value: JsValue) -> Result<Self, Self::Error> {
872 match value.as_string() {
873 Some(s) => Ok(s),
874 None => Err(value),
875 }
876 }
877}
878
879impl TryFromJsValue for String {
880 type Error = JsValue;
881
882 fn try_from_js_value(value: JsValue) -> Result<Self, Self::Error> {
883 match value.as_string() {
884 Some(s) => Ok(s),
885 None => Err(value),
886 }
887 }
888}
889
890impl From<bool> for JsValue {
891 #[inline]
892 fn from(s: bool) -> JsValue {
893 JsValue::from_bool(s)
894 }
895}
896
897impl<'a, T> From<&'a T> for JsValue
898where
899 T: JsCast,
900{
901 #[inline]
902 fn from(s: &'a T) -> JsValue {
903 s.as_ref().clone()
904 }
905}
906
907impl<T> From<Option<T>> for JsValue
908where
909 JsValue: From<T>,
910{
911 #[inline]
912 fn from(s: Option<T>) -> JsValue {
913 match s {
914 Some(s) => s.into(),
915 None => JsValue::undefined(),
916 }
917 }
918}
919
920impl JsCast for JsValue {
921 #[inline]
923 fn instanceof(_val: &JsValue) -> bool {
924 true
925 }
926 #[inline]
927 fn unchecked_from_js(val: JsValue) -> Self {
928 val
929 }
930 #[inline]
931 fn unchecked_from_js_ref(val: &JsValue) -> &Self {
932 val
933 }
934}
935
936impl AsRef<JsValue> for JsValue {
937 #[inline]
938 fn as_ref(&self) -> &JsValue {
939 self
940 }
941}
942
943macro_rules! numbers {
944 ($($n:ident)*) => ($(
945 impl PartialEq<$n> for JsValue {
946 #[inline]
947 fn eq(&self, other: &$n) -> bool {
948 self.as_f64() == Some(f64::from(*other))
949 }
950 }
951
952 impl From<$n> for JsValue {
953 #[inline]
954 fn from(n: $n) -> JsValue {
955 JsValue::from_f64(n.into())
956 }
957 }
958 )*)
959}
960
961numbers! { i8 u8 i16 u16 i32 u32 f32 f64 }
962
963macro_rules! big_numbers {
964 (|$arg:ident|, $($n:ident = $handle:expr,)*) => ($(
965 impl PartialEq<$n> for JsValue {
966 #[inline]
967 fn eq(&self, other: &$n) -> bool {
968 self == &JsValue::from(*other)
969 }
970 }
971
972 impl From<$n> for JsValue {
973 #[inline]
974 fn from($arg: $n) -> JsValue {
975 unsafe { JsValue::_new($handle) }
976 }
977 }
978 )*)
979}
980
981fn bigint_get_as_i64(v: &JsValue) -> Option<i64> {
982 unsafe { __wbindgen_bigint_get_as_i64(v.idx).join() }
983}
984
985macro_rules! try_from_for_num64 {
986 ($ty:ty) => {
987 impl TryFrom<JsValue> for $ty {
988 type Error = JsValue;
989
990 #[inline]
991 fn try_from(v: JsValue) -> Result<Self, JsValue> {
992 bigint_get_as_i64(&v)
993 .map(|as_i64| as_i64 as Self)
996 .filter(|as_self| v == *as_self)
998 .ok_or(v)
1000 }
1001 }
1002 };
1003}
1004
1005try_from_for_num64!(i64);
1006try_from_for_num64!(u64);
1007
1008macro_rules! try_from_for_num128 {
1009 ($ty:ty, $hi_ty:ty) => {
1010 impl TryFrom<JsValue> for $ty {
1011 type Error = JsValue;
1012
1013 #[inline]
1014 fn try_from(v: JsValue) -> Result<Self, JsValue> {
1015 let lo = match bigint_get_as_i64(&v) {
1017 Some(lo) => lo as u64,
1019 None => return Err(v),
1021 };
1022 let hi = v >> JsValue::from(64_u64);
1025 let hi = <$hi_ty>::try_from(hi)?;
1028 Ok(Self::from(hi) << 64 | Self::from(lo))
1029 }
1030 }
1031 };
1032}
1033
1034try_from_for_num128!(i128, i64);
1035try_from_for_num128!(u128, u64);
1036
1037big_numbers! {
1038 |n|,
1039 i64 = __wbindgen_bigint_from_i64(n),
1040 u64 = __wbindgen_bigint_from_u64(n),
1041 i128 = __wbindgen_bigint_from_i128((n >> 64) as i64, n as u64),
1042 u128 = __wbindgen_bigint_from_u128((n >> 64) as u64, n as u64),
1043}
1044
1045impl PartialEq<usize> for JsValue {
1049 #[inline]
1050 fn eq(&self, other: &usize) -> bool {
1051 *self == (*other as u32)
1052 }
1053}
1054
1055impl From<usize> for JsValue {
1056 #[inline]
1057 fn from(n: usize) -> Self {
1058 Self::from(n as u32)
1059 }
1060}
1061
1062impl PartialEq<isize> for JsValue {
1063 #[inline]
1064 fn eq(&self, other: &isize) -> bool {
1065 *self == (*other as i32)
1066 }
1067}
1068
1069impl From<isize> for JsValue {
1070 #[inline]
1071 fn from(n: isize) -> Self {
1072 Self::from(n as i32)
1073 }
1074}
1075
1076#[wasm_bindgen_macro::wasm_bindgen(wasm_bindgen = crate)]
1078extern "C" {
1079 #[wasm_bindgen(js_namespace = Array, js_name = isArray)]
1080 fn __wbindgen_is_array(v: &JsValue) -> bool;
1081
1082 #[wasm_bindgen(js_name = BigInt)]
1083 fn __wbindgen_bigint_from_str(s: &str) -> JsValue;
1084
1085 #[wasm_bindgen(js_name = Symbol)]
1086 fn __wbindgen_symbol_new(description: Option<&str>) -> JsValue;
1087
1088 #[wasm_bindgen(js_name = Error)]
1089 fn __wbindgen_error_new(msg: &str) -> JsValue;
1090
1091 #[wasm_bindgen(js_namespace = JSON, js_name = parse)]
1092 fn __wbindgen_json_parse(json: String) -> JsValue;
1093
1094 #[wasm_bindgen(js_namespace = JSON, js_name = stringify)]
1095 fn __wbindgen_json_serialize(v: &JsValue) -> Option<String>;
1096}
1097
1098externs! {
1099 #[link(wasm_import_module = "__wbindgen_placeholder__")]
1100 extern "C" {
1101 fn __wbindgen_object_clone_ref(idx: u32) -> u32;
1102 fn __wbindgen_object_drop_ref(idx: u32) -> ();
1103
1104 fn __wbindgen_string_new(ptr: *const u8, len: usize) -> u32;
1105 fn __wbindgen_number_new(f: f64) -> u32;
1106 fn __wbindgen_bigint_from_i64(n: i64) -> u32;
1107 fn __wbindgen_bigint_from_u64(n: u64) -> u32;
1108 fn __wbindgen_bigint_from_i128(hi: i64, lo: u64) -> u32;
1109 fn __wbindgen_bigint_from_u128(hi: u64, lo: u64) -> u32;
1110
1111 fn __wbindgen_externref_heap_live_count() -> u32;
1112
1113 fn __wbindgen_is_null(idx: u32) -> u32;
1114 fn __wbindgen_is_undefined(idx: u32) -> u32;
1115 fn __wbindgen_is_symbol(idx: u32) -> u32;
1116 fn __wbindgen_is_object(idx: u32) -> u32;
1117 fn __wbindgen_is_function(idx: u32) -> u32;
1118 fn __wbindgen_is_string(idx: u32) -> u32;
1119 fn __wbindgen_is_bigint(idx: u32) -> u32;
1120 fn __wbindgen_typeof(idx: u32) -> u32;
1121
1122 fn __wbindgen_in(prop: u32, obj: u32) -> u32;
1123
1124 fn __wbindgen_is_falsy(idx: u32) -> u32;
1125 fn __wbindgen_as_number(idx: u32) -> f64;
1126 fn __wbindgen_try_into_number(idx: u32) -> u32;
1127 fn __wbindgen_neg(idx: u32) -> u32;
1128 fn __wbindgen_bit_and(a: u32, b: u32) -> u32;
1129 fn __wbindgen_bit_or(a: u32, b: u32) -> u32;
1130 fn __wbindgen_bit_xor(a: u32, b: u32) -> u32;
1131 fn __wbindgen_bit_not(idx: u32) -> u32;
1132 fn __wbindgen_shl(a: u32, b: u32) -> u32;
1133 fn __wbindgen_shr(a: u32, b: u32) -> u32;
1134 fn __wbindgen_unsigned_shr(a: u32, b: u32) -> u32;
1135 fn __wbindgen_add(a: u32, b: u32) -> u32;
1136 fn __wbindgen_sub(a: u32, b: u32) -> u32;
1137 fn __wbindgen_div(a: u32, b: u32) -> u32;
1138 fn __wbindgen_checked_div(a: u32, b: u32) -> u32;
1139 fn __wbindgen_mul(a: u32, b: u32) -> u32;
1140 fn __wbindgen_rem(a: u32, b: u32) -> u32;
1141 fn __wbindgen_pow(a: u32, b: u32) -> u32;
1142 fn __wbindgen_lt(a: u32, b: u32) -> u32;
1143 fn __wbindgen_le(a: u32, b: u32) -> u32;
1144 fn __wbindgen_ge(a: u32, b: u32) -> u32;
1145 fn __wbindgen_gt(a: u32, b: u32) -> u32;
1146
1147 fn __wbindgen_number_get(idx: u32) -> WasmRet<Option<f64>>;
1148 fn __wbindgen_boolean_get(idx: u32) -> u32;
1149 fn __wbindgen_string_get(idx: u32) -> WasmSlice;
1150 fn __wbindgen_bigint_get_as_i64(idx: u32) -> WasmRet<Option<i64>>;
1151
1152 fn __wbindgen_debug_string(ret: *mut [usize; 2], idx: u32) -> ();
1153
1154 fn __wbindgen_throw(a: *const u8, b: usize) -> !;
1155 fn __wbindgen_rethrow(a: u32) -> !;
1156
1157 fn __wbindgen_cb_drop(idx: u32) -> u32;
1158
1159 fn __wbindgen_describe(v: u32) -> ();
1160 fn __wbindgen_describe_closure(a: u32, b: u32, c: u32) -> u32;
1161
1162 fn __wbindgen_jsval_eq(a: u32, b: u32) -> u32;
1163 fn __wbindgen_jsval_loose_eq(a: u32, b: u32) -> u32;
1164
1165 fn __wbindgen_copy_to_typed_array(ptr: *const u8, len: usize, idx: u32) -> ();
1166
1167 fn __wbindgen_uint8_array_new(ptr: *mut u8, len: usize) -> u32;
1168 fn __wbindgen_uint8_clamped_array_new(ptr: *mut u8, len: usize) -> u32;
1169 fn __wbindgen_uint16_array_new(ptr: *mut u16, len: usize) -> u32;
1170 fn __wbindgen_uint32_array_new(ptr: *mut u32, len: usize) -> u32;
1171 fn __wbindgen_biguint64_array_new(ptr: *mut u64, len: usize) -> u32;
1172 fn __wbindgen_int8_array_new(ptr: *mut i8, len: usize) -> u32;
1173 fn __wbindgen_int16_array_new(ptr: *mut i16, len: usize) -> u32;
1174 fn __wbindgen_int32_array_new(ptr: *mut i32, len: usize) -> u32;
1175 fn __wbindgen_bigint64_array_new(ptr: *mut i64, len: usize) -> u32;
1176 fn __wbindgen_float32_array_new(ptr: *mut f32, len: usize) -> u32;
1177 fn __wbindgen_float64_array_new(ptr: *mut f64, len: usize) -> u32;
1178
1179 fn __wbindgen_array_new() -> u32;
1180 fn __wbindgen_array_push(array: u32, value: u32) -> ();
1181
1182 fn __wbindgen_not(idx: u32) -> u32;
1183
1184 fn __wbindgen_exports() -> u32;
1185 fn __wbindgen_memory() -> u32;
1186 fn __wbindgen_module() -> u32;
1187 fn __wbindgen_function_table() -> u32;
1188 }
1189}
1190
1191impl Clone for JsValue {
1192 #[inline]
1193 fn clone(&self) -> JsValue {
1194 unsafe {
1195 let idx = __wbindgen_object_clone_ref(self.idx);
1196 JsValue::_new(idx)
1197 }
1198 }
1199}
1200
1201impl core::fmt::Debug for JsValue {
1202 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1203 write!(f, "JsValue({})", self.as_debug_string())
1204 }
1205}
1206
1207impl Drop for JsValue {
1208 #[inline]
1209 fn drop(&mut self) {
1210 unsafe {
1211 debug_assert!(self.idx >= JSIDX_OFFSET, "free of stack slot {}", self.idx);
1213
1214 if self.idx >= JSIDX_RESERVED {
1218 __wbindgen_object_drop_ref(self.idx);
1219 }
1220 }
1221 }
1222}
1223
1224impl Default for JsValue {
1225 fn default() -> Self {
1226 Self::UNDEFINED
1227 }
1228}
1229
1230#[cfg(feature = "std")]
1251#[deprecated = "use with `#[wasm_bindgen(thread_local_v2)]` instead"]
1252pub struct JsStatic<T: 'static> {
1253 #[doc(hidden)]
1254 pub __inner: &'static std::thread::LocalKey<T>,
1255}
1256
1257#[cfg(feature = "std")]
1258#[allow(deprecated)]
1259#[cfg(not(target_feature = "atomics"))]
1260impl<T: FromWasmAbi + 'static> Deref for JsStatic<T> {
1261 type Target = T;
1262 fn deref(&self) -> &T {
1263 unsafe { self.__inner.with(|ptr| &*(ptr as *const T)) }
1264 }
1265}
1266
1267pub struct JsThreadLocal<T: 'static> {
1286 #[doc(hidden)]
1287 #[cfg(not(target_feature = "atomics"))]
1288 pub __inner: &'static __rt::LazyCell<T>,
1289 #[doc(hidden)]
1290 #[cfg(target_feature = "atomics")]
1291 pub __inner: fn() -> *const T,
1292}
1293
1294impl<T> JsThreadLocal<T> {
1295 pub fn with<F, R>(&'static self, f: F) -> R
1296 where
1297 F: FnOnce(&T) -> R,
1298 {
1299 #[cfg(not(target_feature = "atomics"))]
1300 return f(self.__inner);
1301 #[cfg(target_feature = "atomics")]
1302 f(unsafe { &*(self.__inner)() })
1303 }
1304}
1305
1306#[cold]
1307#[inline(never)]
1308#[deprecated(note = "renamed to `throw_str`")]
1309#[doc(hidden)]
1310pub fn throw(s: &str) -> ! {
1311 throw_str(s)
1312}
1313
1314#[cold]
1325#[inline(never)]
1326pub fn throw_str(s: &str) -> ! {
1327 unsafe {
1328 __wbindgen_throw(s.as_ptr(), s.len());
1329 }
1330}
1331
1332#[cold]
1343#[inline(never)]
1344pub fn throw_val(s: JsValue) -> ! {
1345 unsafe {
1346 let idx = s.idx;
1347 mem::forget(s);
1348 __wbindgen_rethrow(idx);
1349 }
1350}
1351
1352pub fn externref_heap_live_count() -> u32 {
1397 unsafe { __wbindgen_externref_heap_live_count() }
1398}
1399
1400#[doc(hidden)]
1401pub fn anyref_heap_live_count() -> u32 {
1402 externref_heap_live_count()
1403}
1404
1405pub trait UnwrapThrowExt<T>: Sized {
1435 #[cfg_attr(
1438 any(
1439 debug_assertions,
1440 not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))
1441 ),
1442 track_caller
1443 )]
1444 fn unwrap_throw(self) -> T {
1445 if cfg!(all(
1446 debug_assertions,
1447 all(
1448 target_arch = "wasm32",
1449 any(target_os = "unknown", target_os = "none")
1450 )
1451 )) {
1452 let loc = core::panic::Location::caller();
1453 let msg = alloc::format!(
1454 "called `{}::unwrap_throw()` ({}:{}:{})",
1455 core::any::type_name::<Self>(),
1456 loc.file(),
1457 loc.line(),
1458 loc.column()
1459 );
1460 self.expect_throw(&msg)
1461 } else {
1462 self.expect_throw("called `unwrap_throw()`")
1463 }
1464 }
1465
1466 #[cfg_attr(
1470 any(
1471 debug_assertions,
1472 not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))
1473 ),
1474 track_caller
1475 )]
1476 fn expect_throw(self, message: &str) -> T;
1477}
1478
1479impl<T> UnwrapThrowExt<T> for Option<T> {
1480 fn unwrap_throw(self) -> T {
1481 const MSG: &str = "called `Option::unwrap_throw()` on a `None` value";
1482
1483 if cfg!(all(
1484 target_arch = "wasm32",
1485 any(target_os = "unknown", target_os = "none")
1486 )) {
1487 if let Some(val) = self {
1488 val
1489 } else if cfg!(debug_assertions) {
1490 let loc = core::panic::Location::caller();
1491 let msg =
1492 alloc::format!("{} ({}:{}:{})", MSG, loc.file(), loc.line(), loc.column(),);
1493
1494 throw_str(&msg)
1495 } else {
1496 throw_str(MSG)
1497 }
1498 } else {
1499 self.expect(MSG)
1500 }
1501 }
1502
1503 fn expect_throw(self, message: &str) -> T {
1504 if cfg!(all(
1505 target_arch = "wasm32",
1506 any(target_os = "unknown", target_os = "none")
1507 )) {
1508 if let Some(val) = self {
1509 val
1510 } else if cfg!(debug_assertions) {
1511 let loc = core::panic::Location::caller();
1512 let msg = alloc::format!(
1513 "{} ({}:{}:{})",
1514 message,
1515 loc.file(),
1516 loc.line(),
1517 loc.column(),
1518 );
1519
1520 throw_str(&msg)
1521 } else {
1522 throw_str(message)
1523 }
1524 } else {
1525 self.expect(message)
1526 }
1527 }
1528}
1529
1530impl<T, E> UnwrapThrowExt<T> for Result<T, E>
1531where
1532 E: core::fmt::Debug,
1533{
1534 fn unwrap_throw(self) -> T {
1535 const MSG: &str = "called `Result::unwrap_throw()` on an `Err` value";
1536
1537 if cfg!(all(
1538 target_arch = "wasm32",
1539 any(target_os = "unknown", target_os = "none")
1540 )) {
1541 match self {
1542 Ok(val) => val,
1543 Err(err) => {
1544 if cfg!(debug_assertions) {
1545 let loc = core::panic::Location::caller();
1546 let msg = alloc::format!(
1547 "{} ({}:{}:{}): {:?}",
1548 MSG,
1549 loc.file(),
1550 loc.line(),
1551 loc.column(),
1552 err
1553 );
1554
1555 throw_str(&msg)
1556 } else {
1557 throw_str(MSG)
1558 }
1559 }
1560 }
1561 } else {
1562 self.expect(MSG)
1563 }
1564 }
1565
1566 fn expect_throw(self, message: &str) -> T {
1567 if cfg!(all(
1568 target_arch = "wasm32",
1569 any(target_os = "unknown", target_os = "none")
1570 )) {
1571 match self {
1572 Ok(val) => val,
1573 Err(err) => {
1574 if cfg!(debug_assertions) {
1575 let loc = core::panic::Location::caller();
1576 let msg = alloc::format!(
1577 "{} ({}:{}:{}): {:?}",
1578 message,
1579 loc.file(),
1580 loc.line(),
1581 loc.column(),
1582 err
1583 );
1584
1585 throw_str(&msg)
1586 } else {
1587 throw_str(message)
1588 }
1589 }
1590 }
1591 } else {
1592 self.expect(message)
1593 }
1594 }
1595}
1596
1597pub fn module() -> JsValue {
1601 unsafe { JsValue::_new(__wbindgen_module()) }
1602}
1603
1604pub fn exports() -> JsValue {
1606 unsafe { JsValue::_new(__wbindgen_exports()) }
1607}
1608
1609pub fn memory() -> JsValue {
1611 unsafe { JsValue::_new(__wbindgen_memory()) }
1612}
1613
1614pub fn function_table() -> JsValue {
1617 unsafe { JsValue::_new(__wbindgen_function_table()) }
1618}
1619
1620#[derive(Copy, Clone, PartialEq, Debug, Eq)]
1633pub struct Clamped<T>(pub T);
1634
1635impl<T> Deref for Clamped<T> {
1636 type Target = T;
1637
1638 fn deref(&self) -> &T {
1639 &self.0
1640 }
1641}
1642
1643impl<T> DerefMut for Clamped<T> {
1644 fn deref_mut(&mut self) -> &mut T {
1645 &mut self.0
1646 }
1647}
1648
1649#[derive(Clone, Debug)]
1706pub struct JsError {
1707 value: JsValue,
1708}
1709
1710impl JsError {
1711 #[inline]
1713 pub fn new(s: &str) -> JsError {
1714 Self {
1715 value: __wbindgen_error_new(s),
1716 }
1717 }
1718}
1719
1720#[cfg(feature = "std")]
1721impl<E> From<E> for JsError
1722where
1723 E: std::error::Error,
1724{
1725 fn from(error: E) -> Self {
1726 use std::string::ToString;
1727
1728 JsError::new(&error.to_string())
1729 }
1730}
1731
1732impl From<JsError> for JsValue {
1733 fn from(error: JsError) -> Self {
1734 error.value
1735 }
1736}
1737
1738macro_rules! typed_arrays {
1739 ($($ty:ident $ctor:ident $clamped_ctor:ident,)*) => {
1740 $(
1741 impl From<Box<[$ty]>> for JsValue {
1742 fn from(mut vector: Box<[$ty]>) -> Self {
1743 let result = unsafe { JsValue::_new($ctor(vector.as_mut_ptr(), vector.len())) };
1744 mem::forget(vector);
1745 result
1746 }
1747 }
1748
1749 impl From<Clamped<Box<[$ty]>>> for JsValue {
1750 fn from(mut vector: Clamped<Box<[$ty]>>) -> Self {
1751 let result = unsafe { JsValue::_new($clamped_ctor(vector.as_mut_ptr(), vector.len())) };
1752 mem::forget(vector);
1753 result
1754 }
1755 }
1756 )*
1757 };
1758 }
1759
1760typed_arrays! {
1761 u8 __wbindgen_uint8_array_new __wbindgen_uint8_clamped_array_new,
1762 u16 __wbindgen_uint16_array_new __wbindgen_uint16_array_new,
1763 u32 __wbindgen_uint32_array_new __wbindgen_uint32_array_new,
1764 u64 __wbindgen_biguint64_array_new __wbindgen_biguint64_array_new,
1765 i8 __wbindgen_int8_array_new __wbindgen_int8_array_new,
1766 i16 __wbindgen_int16_array_new __wbindgen_int16_array_new,
1767 i32 __wbindgen_int32_array_new __wbindgen_int32_array_new,
1768 i64 __wbindgen_bigint64_array_new __wbindgen_bigint64_array_new,
1769 f32 __wbindgen_float32_array_new __wbindgen_float32_array_new,
1770 f64 __wbindgen_float64_array_new __wbindgen_float64_array_new,
1771}
1772
1773impl __rt::VectorIntoJsValue for JsValue {
1774 fn vector_into_jsvalue(vector: Box<[JsValue]>) -> JsValue {
1775 __rt::js_value_vector_into_jsvalue::<JsValue>(vector)
1776 }
1777}
1778
1779impl<T: JsObject> __rt::VectorIntoJsValue for T {
1780 fn vector_into_jsvalue(vector: Box<[T]>) -> JsValue {
1781 __rt::js_value_vector_into_jsvalue::<T>(vector)
1782 }
1783}
1784
1785impl __rt::VectorIntoJsValue for String {
1786 fn vector_into_jsvalue(vector: Box<[String]>) -> JsValue {
1787 __rt::js_value_vector_into_jsvalue::<String>(vector)
1788 }
1789}
1790
1791impl<T> From<Vec<T>> for JsValue
1792where
1793 JsValue: From<Box<[T]>>,
1794{
1795 fn from(vector: Vec<T>) -> Self {
1796 JsValue::from(vector.into_boxed_slice())
1797 }
1798}
1799
1800impl<T> From<Clamped<Vec<T>>> for JsValue
1801where
1802 JsValue: From<Clamped<Box<[T]>>>,
1803{
1804 fn from(vector: Clamped<Vec<T>>) -> Self {
1805 JsValue::from(Clamped(vector.0.into_boxed_slice()))
1806 }
1807}