1#![no_std]
9#![cfg_attr(
10 wasm_bindgen_unstable_test_coverage,
11 feature(coverage_attribute, allow_internal_unstable),
12 allow(internal_features)
13)]
14#![cfg_attr(
15 all(not(feature = "std"), target_feature = "atomics"),
16 feature(thread_local, allow_internal_unstable),
17 allow(internal_features)
18)]
19#![allow(coherence_leak_check)]
20#![doc(html_root_url = "https://docs.rs/wasm-bindgen/0.2")]
21
22extern crate alloc;
23
24use alloc::boxed::Box;
25use alloc::string::String;
26use alloc::vec::Vec;
27use core::convert::TryFrom;
28use core::marker;
29use core::mem;
30use core::ops::{
31 Add, BitAnd, BitOr, BitXor, Deref, DerefMut, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub,
32};
33use core::ptr::NonNull;
34
35use crate::convert::{FromWasmAbi, TryFromJsValue, WasmRet, WasmSlice};
36
37macro_rules! if_std {
38 ($($i:item)*) => ($(
39 #[cfg(feature = "std")] $i
40 )*)
41}
42
43macro_rules! externs {
44 ($(#[$attr:meta])* extern "C" { $(fn $name:ident($($args:tt)*) -> $ret:ty;)* }) => (
45 #[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
46 $(#[$attr])*
47 extern "C" {
48 $(fn $name($($args)*) -> $ret;)*
49 }
50
51 $(
52 #[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
53 #[allow(unused_variables)]
54 unsafe extern fn $name($($args)*) -> $ret {
55 panic!("function not implemented on non-wasm32 targets")
56 }
57 )*
58 )
59}
60
61pub mod prelude {
67 pub use crate::closure::Closure;
68 pub use crate::JsCast;
69 pub use crate::JsValue;
70 pub use crate::UnwrapThrowExt;
71 #[doc(hidden)]
72 pub use wasm_bindgen_macro::__wasm_bindgen_class_marker;
73 pub use wasm_bindgen_macro::wasm_bindgen;
74
75 pub use crate::JsError;
76}
77
78pub use wasm_bindgen_macro::link_to;
79
80pub mod closure;
81pub mod convert;
82pub mod describe;
83mod externref;
84mod link;
85
86mod cast;
87pub use crate::cast::{JsCast, JsObject};
88
89if_std! {
90 extern crate std;
91 use std::prelude::v1::*;
92 mod cache;
93 pub use cache::intern::{intern, unintern};
94}
95
96pub struct JsValue {
103 idx: u32,
104 _marker: marker::PhantomData<*mut u8>, }
106
107const JSIDX_OFFSET: u32 = 128; const JSIDX_UNDEFINED: u32 = JSIDX_OFFSET;
109const JSIDX_NULL: u32 = JSIDX_OFFSET + 1;
110const JSIDX_TRUE: u32 = JSIDX_OFFSET + 2;
111const JSIDX_FALSE: u32 = JSIDX_OFFSET + 3;
112const JSIDX_RESERVED: u32 = JSIDX_OFFSET + 4;
113
114impl JsValue {
115 pub const NULL: JsValue = JsValue::_new(JSIDX_NULL);
117
118 pub const UNDEFINED: JsValue = JsValue::_new(JSIDX_UNDEFINED);
120
121 pub const TRUE: JsValue = JsValue::_new(JSIDX_TRUE);
123
124 pub const FALSE: JsValue = JsValue::_new(JSIDX_FALSE);
126
127 #[inline]
128 const fn _new(idx: u32) -> JsValue {
129 JsValue {
130 idx,
131 _marker: marker::PhantomData,
132 }
133 }
134
135 #[allow(clippy::should_implement_trait)] #[inline]
141 pub fn from_str(s: &str) -> JsValue {
142 unsafe { JsValue::_new(__wbindgen_string_new(s.as_ptr(), s.len())) }
143 }
144
145 #[inline]
150 pub fn from_f64(n: f64) -> JsValue {
151 unsafe { JsValue::_new(__wbindgen_number_new(n)) }
152 }
153
154 #[inline]
159 pub fn bigint_from_str(s: &str) -> JsValue {
160 unsafe { JsValue::_new(__wbindgen_bigint_from_str(s.as_ptr(), s.len())) }
161 }
162
163 #[inline]
168 pub const fn from_bool(b: bool) -> JsValue {
169 if b {
170 JsValue::TRUE
171 } else {
172 JsValue::FALSE
173 }
174 }
175
176 #[inline]
178 pub const fn undefined() -> JsValue {
179 JsValue::UNDEFINED
180 }
181
182 #[inline]
184 pub const fn null() -> JsValue {
185 JsValue::NULL
186 }
187
188 pub fn symbol(description: Option<&str>) -> JsValue {
193 unsafe {
194 match description {
195 Some(description) => JsValue::_new(__wbindgen_symbol_named_new(
196 description.as_ptr(),
197 description.len(),
198 )),
199 None => JsValue::_new(__wbindgen_symbol_anonymous_new()),
200 }
201 }
202 }
203
204 #[cfg(feature = "serde-serialize")]
228 #[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
229 pub fn from_serde<T>(t: &T) -> serde_json::Result<JsValue>
230 where
231 T: serde::ser::Serialize + ?Sized,
232 {
233 let s = serde_json::to_string(t)?;
234 unsafe { Ok(JsValue::_new(__wbindgen_json_parse(s.as_ptr(), s.len()))) }
235 }
236
237 #[cfg(feature = "serde-serialize")]
259 #[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
260 pub fn into_serde<T>(&self) -> serde_json::Result<T>
261 where
262 T: for<'a> serde::de::Deserialize<'a>,
263 {
264 unsafe {
265 let ret = __wbindgen_json_serialize(self.idx);
266 let s = String::from_abi(ret);
267 serde_json::from_str(&s)
268 }
269 }
270
271 #[inline]
277 pub fn as_f64(&self) -> Option<f64> {
278 unsafe { __wbindgen_number_get(self.idx).join() }
279 }
280
281 #[inline]
283 pub fn is_string(&self) -> bool {
284 unsafe { __wbindgen_is_string(self.idx) == 1 }
285 }
286
287 #[inline]
308 pub fn as_string(&self) -> Option<String> {
309 unsafe { FromWasmAbi::from_abi(__wbindgen_string_get(self.idx)) }
310 }
311
312 #[inline]
318 pub fn as_bool(&self) -> Option<bool> {
319 unsafe {
320 match __wbindgen_boolean_get(self.idx) {
321 0 => Some(false),
322 1 => Some(true),
323 _ => None,
324 }
325 }
326 }
327
328 #[inline]
330 pub fn is_null(&self) -> bool {
331 unsafe { __wbindgen_is_null(self.idx) == 1 }
332 }
333
334 #[inline]
336 pub fn is_undefined(&self) -> bool {
337 unsafe { __wbindgen_is_undefined(self.idx) == 1 }
338 }
339
340 #[inline]
342 pub fn is_symbol(&self) -> bool {
343 unsafe { __wbindgen_is_symbol(self.idx) == 1 }
344 }
345
346 #[inline]
348 pub fn is_object(&self) -> bool {
349 unsafe { __wbindgen_is_object(self.idx) == 1 }
350 }
351
352 #[inline]
354 pub fn is_array(&self) -> bool {
355 unsafe { __wbindgen_is_array(self.idx) == 1 }
356 }
357
358 #[inline]
360 pub fn is_function(&self) -> bool {
361 unsafe { __wbindgen_is_function(self.idx) == 1 }
362 }
363
364 #[inline]
366 pub fn is_bigint(&self) -> bool {
367 unsafe { __wbindgen_is_bigint(self.idx) == 1 }
368 }
369
370 #[inline]
374 pub fn js_typeof(&self) -> JsValue {
375 unsafe { JsValue::_new(__wbindgen_typeof(self.idx)) }
376 }
377
378 #[inline]
382 pub fn js_in(&self, obj: &JsValue) -> bool {
383 unsafe { __wbindgen_in(self.idx, obj.idx) == 1 }
384 }
385
386 #[inline]
390 pub fn is_truthy(&self) -> bool {
391 !self.is_falsy()
392 }
393
394 #[inline]
398 pub fn is_falsy(&self) -> bool {
399 unsafe { __wbindgen_is_falsy(self.idx) == 1 }
400 }
401
402 #[cfg(feature = "std")]
404 fn as_debug_string(&self) -> String {
405 unsafe {
406 let mut ret = [0; 2];
407 __wbindgen_debug_string(&mut ret, self.idx);
408 let data = Vec::from_raw_parts(ret[0] as *mut u8, ret[1], ret[1]);
409 String::from_utf8_unchecked(data)
410 }
411 }
412
413 #[inline]
417 pub fn loose_eq(&self, other: &Self) -> bool {
418 unsafe { __wbindgen_jsval_loose_eq(self.idx, other.idx) != 0 }
419 }
420
421 #[inline]
425 pub fn bit_not(&self) -> JsValue {
426 unsafe { JsValue::_new(__wbindgen_bit_not(self.idx)) }
427 }
428
429 #[inline]
433 pub fn unsigned_shr(&self, rhs: &Self) -> u32 {
434 unsafe { __wbindgen_unsigned_shr(self.idx, rhs.idx) }
435 }
436
437 #[inline]
441 pub fn checked_div(&self, rhs: &Self) -> Self {
442 unsafe { JsValue::_new(__wbindgen_checked_div(self.idx, rhs.idx)) }
443 }
444
445 #[inline]
449 pub fn pow(&self, rhs: &Self) -> Self {
450 unsafe { JsValue::_new(__wbindgen_pow(self.idx, rhs.idx)) }
451 }
452
453 #[inline]
457 pub fn lt(&self, other: &Self) -> bool {
458 unsafe { __wbindgen_lt(self.idx, other.idx) == 1 }
459 }
460
461 #[inline]
465 pub fn le(&self, other: &Self) -> bool {
466 unsafe { __wbindgen_le(self.idx, other.idx) == 1 }
467 }
468
469 #[inline]
473 pub fn ge(&self, other: &Self) -> bool {
474 unsafe { __wbindgen_ge(self.idx, other.idx) == 1 }
475 }
476
477 #[inline]
481 pub fn gt(&self, other: &Self) -> bool {
482 unsafe { __wbindgen_gt(self.idx, other.idx) == 1 }
483 }
484
485 #[inline]
489 pub fn unchecked_into_f64(&self) -> f64 {
490 unsafe { __wbindgen_as_number(self.idx) }
491 }
492}
493
494impl PartialEq for JsValue {
495 #[inline]
499 fn eq(&self, other: &Self) -> bool {
500 unsafe { __wbindgen_jsval_eq(self.idx, other.idx) != 0 }
501 }
502}
503
504impl PartialEq<bool> for JsValue {
505 #[inline]
506 fn eq(&self, other: &bool) -> bool {
507 self.as_bool() == Some(*other)
508 }
509}
510
511impl PartialEq<str> for JsValue {
512 #[inline]
513 fn eq(&self, other: &str) -> bool {
514 *self == JsValue::from_str(other)
515 }
516}
517
518impl<'a> PartialEq<&'a str> for JsValue {
519 #[inline]
520 fn eq(&self, other: &&'a str) -> bool {
521 <JsValue as PartialEq<str>>::eq(self, other)
522 }
523}
524
525impl PartialEq<String> for JsValue {
526 #[inline]
527 fn eq(&self, other: &String) -> bool {
528 <JsValue as PartialEq<str>>::eq(self, other)
529 }
530}
531impl<'a> PartialEq<&'a String> for JsValue {
532 #[inline]
533 fn eq(&self, other: &&'a String) -> bool {
534 <JsValue as PartialEq<str>>::eq(self, other)
535 }
536}
537
538macro_rules! forward_deref_unop {
539 (impl $imp:ident, $method:ident for $t:ty) => {
540 impl $imp for $t {
541 type Output = <&'static $t as $imp>::Output;
542
543 #[inline]
544 fn $method(self) -> <&'static $t as $imp>::Output {
545 $imp::$method(&self)
546 }
547 }
548 };
549}
550
551macro_rules! forward_deref_binop {
552 (impl $imp:ident, $method:ident for $t:ty) => {
553 impl<'a> $imp<$t> for &'a $t {
554 type Output = <&'static $t as $imp<&'static $t>>::Output;
555
556 #[inline]
557 fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
558 $imp::$method(self, &other)
559 }
560 }
561
562 impl $imp<&$t> for $t {
563 type Output = <&'static $t as $imp<&'static $t>>::Output;
564
565 #[inline]
566 fn $method(self, other: &$t) -> <&'static $t as $imp<&'static $t>>::Output {
567 $imp::$method(&self, other)
568 }
569 }
570
571 impl $imp<$t> for $t {
572 type Output = <&'static $t as $imp<&'static $t>>::Output;
573
574 #[inline]
575 fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
576 $imp::$method(&self, &other)
577 }
578 }
579 };
580}
581
582impl Not for &JsValue {
583 type Output = bool;
584
585 #[inline]
589 fn not(self) -> Self::Output {
590 JsValue::is_falsy(self)
591 }
592}
593
594forward_deref_unop!(impl Not, not for JsValue);
595
596impl TryFrom<JsValue> for f64 {
597 type Error = JsValue;
598
599 #[inline]
604 fn try_from(val: JsValue) -> Result<Self, Self::Error> {
605 f64::try_from(&val)
606 }
607}
608
609impl TryFrom<&JsValue> for f64 {
610 type Error = JsValue;
611
612 #[inline]
617 fn try_from(val: &JsValue) -> Result<Self, Self::Error> {
618 let jsval = unsafe { JsValue::_new(__wbindgen_try_into_number(val.idx)) };
619 match jsval.as_f64() {
620 Some(num) => Ok(num),
621 None => Err(jsval),
622 }
623 }
624}
625
626impl Neg for &JsValue {
627 type Output = JsValue;
628
629 #[inline]
633 fn neg(self) -> Self::Output {
634 unsafe { JsValue::_new(__wbindgen_neg(self.idx)) }
635 }
636}
637
638forward_deref_unop!(impl Neg, neg for JsValue);
639
640impl BitAnd for &JsValue {
641 type Output = JsValue;
642
643 #[inline]
647 fn bitand(self, rhs: Self) -> Self::Output {
648 unsafe { JsValue::_new(__wbindgen_bit_and(self.idx, rhs.idx)) }
649 }
650}
651
652forward_deref_binop!(impl BitAnd, bitand for JsValue);
653
654impl BitOr for &JsValue {
655 type Output = JsValue;
656
657 #[inline]
661 fn bitor(self, rhs: Self) -> Self::Output {
662 unsafe { JsValue::_new(__wbindgen_bit_or(self.idx, rhs.idx)) }
663 }
664}
665
666forward_deref_binop!(impl BitOr, bitor for JsValue);
667
668impl BitXor for &JsValue {
669 type Output = JsValue;
670
671 #[inline]
675 fn bitxor(self, rhs: Self) -> Self::Output {
676 unsafe { JsValue::_new(__wbindgen_bit_xor(self.idx, rhs.idx)) }
677 }
678}
679
680forward_deref_binop!(impl BitXor, bitxor for JsValue);
681
682impl Shl for &JsValue {
683 type Output = JsValue;
684
685 #[inline]
689 fn shl(self, rhs: Self) -> Self::Output {
690 unsafe { JsValue::_new(__wbindgen_shl(self.idx, rhs.idx)) }
691 }
692}
693
694forward_deref_binop!(impl Shl, shl for JsValue);
695
696impl Shr for &JsValue {
697 type Output = JsValue;
698
699 #[inline]
703 fn shr(self, rhs: Self) -> Self::Output {
704 unsafe { JsValue::_new(__wbindgen_shr(self.idx, rhs.idx)) }
705 }
706}
707
708forward_deref_binop!(impl Shr, shr for JsValue);
709
710impl Add for &JsValue {
711 type Output = JsValue;
712
713 #[inline]
717 fn add(self, rhs: Self) -> Self::Output {
718 unsafe { JsValue::_new(__wbindgen_add(self.idx, rhs.idx)) }
719 }
720}
721
722forward_deref_binop!(impl Add, add for JsValue);
723
724impl Sub for &JsValue {
725 type Output = JsValue;
726
727 #[inline]
731 fn sub(self, rhs: Self) -> Self::Output {
732 unsafe { JsValue::_new(__wbindgen_sub(self.idx, rhs.idx)) }
733 }
734}
735
736forward_deref_binop!(impl Sub, sub for JsValue);
737
738impl Div for &JsValue {
739 type Output = JsValue;
740
741 #[inline]
745 fn div(self, rhs: Self) -> Self::Output {
746 unsafe { JsValue::_new(__wbindgen_div(self.idx, rhs.idx)) }
747 }
748}
749
750forward_deref_binop!(impl Div, div for JsValue);
751
752impl Mul for &JsValue {
753 type Output = JsValue;
754
755 #[inline]
759 fn mul(self, rhs: Self) -> Self::Output {
760 unsafe { JsValue::_new(__wbindgen_mul(self.idx, rhs.idx)) }
761 }
762}
763
764forward_deref_binop!(impl Mul, mul for JsValue);
765
766impl Rem for &JsValue {
767 type Output = JsValue;
768
769 #[inline]
773 fn rem(self, rhs: Self) -> Self::Output {
774 unsafe { JsValue::_new(__wbindgen_rem(self.idx, rhs.idx)) }
775 }
776}
777
778forward_deref_binop!(impl Rem, rem for JsValue);
779
780impl<'a> From<&'a str> for JsValue {
781 #[inline]
782 fn from(s: &'a str) -> JsValue {
783 JsValue::from_str(s)
784 }
785}
786
787impl<T> From<*mut T> for JsValue {
788 #[inline]
789 fn from(s: *mut T) -> JsValue {
790 JsValue::from(s as usize)
791 }
792}
793
794impl<T> From<*const T> for JsValue {
795 #[inline]
796 fn from(s: *const T) -> JsValue {
797 JsValue::from(s as usize)
798 }
799}
800
801impl<T> From<NonNull<T>> for JsValue {
802 #[inline]
803 fn from(s: NonNull<T>) -> JsValue {
804 JsValue::from(s.as_ptr() as usize)
805 }
806}
807
808impl<'a> From<&'a String> for JsValue {
809 #[inline]
810 fn from(s: &'a String) -> JsValue {
811 JsValue::from_str(s)
812 }
813}
814
815impl From<String> for JsValue {
816 #[inline]
817 fn from(s: String) -> JsValue {
818 JsValue::from_str(&s)
819 }
820}
821
822impl TryFrom<JsValue> for String {
823 type Error = JsValue;
824
825 fn try_from(value: JsValue) -> Result<Self, Self::Error> {
826 match value.as_string() {
827 Some(s) => Ok(s),
828 None => Err(value),
829 }
830 }
831}
832
833impl TryFromJsValue for String {
834 type Error = JsValue;
835
836 fn try_from_js_value(value: JsValue) -> Result<Self, Self::Error> {
837 match value.as_string() {
838 Some(s) => Ok(s),
839 None => Err(value),
840 }
841 }
842}
843
844impl From<bool> for JsValue {
845 #[inline]
846 fn from(s: bool) -> JsValue {
847 JsValue::from_bool(s)
848 }
849}
850
851impl<'a, T> From<&'a T> for JsValue
852where
853 T: JsCast,
854{
855 #[inline]
856 fn from(s: &'a T) -> JsValue {
857 s.as_ref().clone()
858 }
859}
860
861impl<T> From<Option<T>> for JsValue
862where
863 JsValue: From<T>,
864{
865 #[inline]
866 fn from(s: Option<T>) -> JsValue {
867 match s {
868 Some(s) => s.into(),
869 None => JsValue::undefined(),
870 }
871 }
872}
873
874impl JsCast for JsValue {
875 #[inline]
877 fn instanceof(_val: &JsValue) -> bool {
878 true
879 }
880 #[inline]
881 fn unchecked_from_js(val: JsValue) -> Self {
882 val
883 }
884 #[inline]
885 fn unchecked_from_js_ref(val: &JsValue) -> &Self {
886 val
887 }
888}
889
890impl AsRef<JsValue> for JsValue {
891 #[inline]
892 fn as_ref(&self) -> &JsValue {
893 self
894 }
895}
896
897macro_rules! numbers {
898 ($($n:ident)*) => ($(
899 impl PartialEq<$n> for JsValue {
900 #[inline]
901 fn eq(&self, other: &$n) -> bool {
902 self.as_f64() == Some(f64::from(*other))
903 }
904 }
905
906 impl From<$n> for JsValue {
907 #[inline]
908 fn from(n: $n) -> JsValue {
909 JsValue::from_f64(n.into())
910 }
911 }
912 )*)
913}
914
915numbers! { i8 u8 i16 u16 i32 u32 f32 f64 }
916
917macro_rules! big_numbers {
918 (|$arg:ident|, $($n:ident = $handle:expr,)*) => ($(
919 impl PartialEq<$n> for JsValue {
920 #[inline]
921 fn eq(&self, other: &$n) -> bool {
922 self == &JsValue::from(*other)
923 }
924 }
925
926 impl From<$n> for JsValue {
927 #[inline]
928 fn from($arg: $n) -> JsValue {
929 unsafe { JsValue::_new($handle) }
930 }
931 }
932 )*)
933}
934
935fn bigint_get_as_i64(v: &JsValue) -> Option<i64> {
936 unsafe { __wbindgen_bigint_get_as_i64(v.idx).join() }
937}
938
939macro_rules! try_from_for_num64 {
940 ($ty:ty) => {
941 impl TryFrom<JsValue> for $ty {
942 type Error = JsValue;
943
944 #[inline]
945 fn try_from(v: JsValue) -> Result<Self, JsValue> {
946 bigint_get_as_i64(&v)
947 .map(|as_i64| as_i64 as Self)
950 .filter(|as_self| v == *as_self)
952 .ok_or(v)
954 }
955 }
956 };
957}
958
959try_from_for_num64!(i64);
960try_from_for_num64!(u64);
961
962macro_rules! try_from_for_num128 {
963 ($ty:ty, $hi_ty:ty) => {
964 impl TryFrom<JsValue> for $ty {
965 type Error = JsValue;
966
967 #[inline]
968 fn try_from(v: JsValue) -> Result<Self, JsValue> {
969 let lo = match bigint_get_as_i64(&v) {
971 Some(lo) => lo as u64,
973 None => return Err(v),
975 };
976 let hi = v >> JsValue::from(64_u64);
979 let hi = <$hi_ty>::try_from(hi)?;
982 Ok(Self::from(hi) << 64 | Self::from(lo))
983 }
984 }
985 };
986}
987
988try_from_for_num128!(i128, i64);
989try_from_for_num128!(u128, u64);
990
991big_numbers! {
992 |n|,
993 i64 = __wbindgen_bigint_from_i64(n),
994 u64 = __wbindgen_bigint_from_u64(n),
995 i128 = __wbindgen_bigint_from_i128((n >> 64) as i64, n as u64),
996 u128 = __wbindgen_bigint_from_u128((n >> 64) as u64, n as u64),
997}
998
999impl PartialEq<usize> for JsValue {
1003 #[inline]
1004 fn eq(&self, other: &usize) -> bool {
1005 *self == (*other as u32)
1006 }
1007}
1008
1009impl From<usize> for JsValue {
1010 #[inline]
1011 fn from(n: usize) -> Self {
1012 Self::from(n as u32)
1013 }
1014}
1015
1016impl PartialEq<isize> for JsValue {
1017 #[inline]
1018 fn eq(&self, other: &isize) -> bool {
1019 *self == (*other as i32)
1020 }
1021}
1022
1023impl From<isize> for JsValue {
1024 #[inline]
1025 fn from(n: isize) -> Self {
1026 Self::from(n as i32)
1027 }
1028}
1029
1030externs! {
1031 #[link(wasm_import_module = "__wbindgen_placeholder__")]
1032 extern "C" {
1033 fn __wbindgen_object_clone_ref(idx: u32) -> u32;
1034 fn __wbindgen_object_drop_ref(idx: u32) -> ();
1035
1036 fn __wbindgen_string_new(ptr: *const u8, len: usize) -> u32;
1037 fn __wbindgen_number_new(f: f64) -> u32;
1038 fn __wbindgen_bigint_from_str(ptr: *const u8, len: usize) -> u32;
1039 fn __wbindgen_bigint_from_i64(n: i64) -> u32;
1040 fn __wbindgen_bigint_from_u64(n: u64) -> u32;
1041 fn __wbindgen_bigint_from_i128(hi: i64, lo: u64) -> u32;
1042 fn __wbindgen_bigint_from_u128(hi: u64, lo: u64) -> u32;
1043 fn __wbindgen_symbol_named_new(ptr: *const u8, len: usize) -> u32;
1044 fn __wbindgen_symbol_anonymous_new() -> u32;
1045
1046 fn __wbindgen_externref_heap_live_count() -> u32;
1047
1048 fn __wbindgen_is_null(idx: u32) -> u32;
1049 fn __wbindgen_is_undefined(idx: u32) -> u32;
1050 fn __wbindgen_is_symbol(idx: u32) -> u32;
1051 fn __wbindgen_is_object(idx: u32) -> u32;
1052 fn __wbindgen_is_array(idx: u32) -> u32;
1053 fn __wbindgen_is_function(idx: u32) -> u32;
1054 fn __wbindgen_is_string(idx: u32) -> u32;
1055 fn __wbindgen_is_bigint(idx: u32) -> u32;
1056 fn __wbindgen_typeof(idx: u32) -> u32;
1057
1058 fn __wbindgen_in(prop: u32, obj: u32) -> u32;
1059
1060 fn __wbindgen_is_falsy(idx: u32) -> u32;
1061 fn __wbindgen_as_number(idx: u32) -> f64;
1062 fn __wbindgen_try_into_number(idx: u32) -> u32;
1063 fn __wbindgen_neg(idx: u32) -> u32;
1064 fn __wbindgen_bit_and(a: u32, b: u32) -> u32;
1065 fn __wbindgen_bit_or(a: u32, b: u32) -> u32;
1066 fn __wbindgen_bit_xor(a: u32, b: u32) -> u32;
1067 fn __wbindgen_bit_not(idx: u32) -> u32;
1068 fn __wbindgen_shl(a: u32, b: u32) -> u32;
1069 fn __wbindgen_shr(a: u32, b: u32) -> u32;
1070 fn __wbindgen_unsigned_shr(a: u32, b: u32) -> u32;
1071 fn __wbindgen_add(a: u32, b: u32) -> u32;
1072 fn __wbindgen_sub(a: u32, b: u32) -> u32;
1073 fn __wbindgen_div(a: u32, b: u32) -> u32;
1074 fn __wbindgen_checked_div(a: u32, b: u32) -> u32;
1075 fn __wbindgen_mul(a: u32, b: u32) -> u32;
1076 fn __wbindgen_rem(a: u32, b: u32) -> u32;
1077 fn __wbindgen_pow(a: u32, b: u32) -> u32;
1078 fn __wbindgen_lt(a: u32, b: u32) -> u32;
1079 fn __wbindgen_le(a: u32, b: u32) -> u32;
1080 fn __wbindgen_ge(a: u32, b: u32) -> u32;
1081 fn __wbindgen_gt(a: u32, b: u32) -> u32;
1082
1083 fn __wbindgen_number_get(idx: u32) -> WasmRet<Option<f64>>;
1084 fn __wbindgen_boolean_get(idx: u32) -> u32;
1085 fn __wbindgen_string_get(idx: u32) -> WasmSlice;
1086 fn __wbindgen_bigint_get_as_i64(idx: u32) -> WasmRet<Option<i64>>;
1087
1088 fn __wbindgen_debug_string(ret: *mut [usize; 2], idx: u32) -> ();
1089
1090 fn __wbindgen_throw(a: *const u8, b: usize) -> !;
1091 fn __wbindgen_rethrow(a: u32) -> !;
1092 fn __wbindgen_error_new(a: *const u8, b: usize) -> u32;
1093
1094 fn __wbindgen_cb_drop(idx: u32) -> u32;
1095
1096 fn __wbindgen_describe(v: u32) -> ();
1097 fn __wbindgen_describe_closure(a: u32, b: u32, c: u32) -> u32;
1098
1099 fn __wbindgen_json_parse(ptr: *const u8, len: usize) -> u32;
1100 fn __wbindgen_json_serialize(idx: u32) -> WasmSlice;
1101 fn __wbindgen_jsval_eq(a: u32, b: u32) -> u32;
1102 fn __wbindgen_jsval_loose_eq(a: u32, b: u32) -> u32;
1103
1104 fn __wbindgen_copy_to_typed_array(ptr: *const u8, len: usize, idx: u32) -> ();
1105
1106 fn __wbindgen_uint8_array_new(ptr: *mut u8, len: usize) -> u32;
1107 fn __wbindgen_uint8_clamped_array_new(ptr: *mut u8, len: usize) -> u32;
1108 fn __wbindgen_uint16_array_new(ptr: *mut u16, len: usize) -> u32;
1109 fn __wbindgen_uint32_array_new(ptr: *mut u32, len: usize) -> u32;
1110 fn __wbindgen_biguint64_array_new(ptr: *mut u64, len: usize) -> u32;
1111 fn __wbindgen_int8_array_new(ptr: *mut i8, len: usize) -> u32;
1112 fn __wbindgen_int16_array_new(ptr: *mut i16, len: usize) -> u32;
1113 fn __wbindgen_int32_array_new(ptr: *mut i32, len: usize) -> u32;
1114 fn __wbindgen_bigint64_array_new(ptr: *mut i64, len: usize) -> u32;
1115 fn __wbindgen_float32_array_new(ptr: *mut f32, len: usize) -> u32;
1116 fn __wbindgen_float64_array_new(ptr: *mut f64, len: usize) -> u32;
1117
1118 fn __wbindgen_array_new() -> u32;
1119 fn __wbindgen_array_push(array: u32, value: u32) -> ();
1120
1121 fn __wbindgen_not(idx: u32) -> u32;
1122
1123 fn __wbindgen_exports() -> u32;
1124 fn __wbindgen_memory() -> u32;
1125 fn __wbindgen_module() -> u32;
1126 fn __wbindgen_function_table() -> u32;
1127 }
1128}
1129
1130impl Clone for JsValue {
1131 #[inline]
1132 fn clone(&self) -> JsValue {
1133 unsafe {
1134 let idx = __wbindgen_object_clone_ref(self.idx);
1135 JsValue::_new(idx)
1136 }
1137 }
1138}
1139
1140#[cfg(feature = "std")]
1141impl core::fmt::Debug for JsValue {
1142 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1143 write!(f, "JsValue({})", self.as_debug_string())
1144 }
1145}
1146
1147#[cfg(not(feature = "std"))]
1148impl core::fmt::Debug for JsValue {
1149 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1150 f.write_str("JsValue")
1151 }
1152}
1153
1154impl Drop for JsValue {
1155 #[inline]
1156 fn drop(&mut self) {
1157 unsafe {
1158 debug_assert!(self.idx >= JSIDX_OFFSET, "free of stack slot {}", self.idx);
1160
1161 if self.idx >= JSIDX_RESERVED {
1165 __wbindgen_object_drop_ref(self.idx);
1166 }
1167 }
1168 }
1169}
1170
1171impl Default for JsValue {
1172 fn default() -> Self {
1173 Self::UNDEFINED
1174 }
1175}
1176
1177#[cfg(feature = "std")]
1198#[deprecated = "use with `#[wasm_bindgen(thread_local_v2)]` instead"]
1199pub struct JsStatic<T: 'static> {
1200 #[doc(hidden)]
1201 pub __inner: &'static std::thread::LocalKey<T>,
1202}
1203
1204#[cfg(feature = "std")]
1205#[allow(deprecated)]
1206#[cfg(not(target_feature = "atomics"))]
1207impl<T: FromWasmAbi + 'static> Deref for JsStatic<T> {
1208 type Target = T;
1209 fn deref(&self) -> &T {
1210 unsafe { self.__inner.with(|ptr| &*(ptr as *const T)) }
1211 }
1212}
1213
1214pub struct JsThreadLocal<T: 'static> {
1233 #[doc(hidden)]
1234 #[cfg(feature = "std")]
1235 pub __inner: &'static std::thread::LocalKey<T>,
1236 #[doc(hidden)]
1237 #[cfg(all(not(feature = "std"), not(target_feature = "atomics")))]
1238 pub __inner: &'static __rt::LazyCell<T>,
1239 #[doc(hidden)]
1240 #[cfg(all(not(feature = "std"), target_feature = "atomics"))]
1241 pub __inner: fn() -> *const T,
1242}
1243
1244impl<T> JsThreadLocal<T> {
1245 pub fn with<F, R>(&'static self, f: F) -> R
1246 where
1247 F: FnOnce(&T) -> R,
1248 {
1249 #[cfg(feature = "std")]
1250 return self.__inner.with(f);
1251 #[cfg(all(not(feature = "std"), not(target_feature = "atomics")))]
1252 return f(self.__inner);
1253 #[cfg(all(not(feature = "std"), target_feature = "atomics"))]
1254 f(unsafe { &*(self.__inner)() })
1255 }
1256}
1257
1258#[cold]
1259#[inline(never)]
1260#[deprecated(note = "renamed to `throw_str`")]
1261#[doc(hidden)]
1262pub fn throw(s: &str) -> ! {
1263 throw_str(s)
1264}
1265
1266#[cold]
1277#[inline(never)]
1278pub fn throw_str(s: &str) -> ! {
1279 unsafe {
1280 __wbindgen_throw(s.as_ptr(), s.len());
1281 }
1282}
1283
1284#[cold]
1295#[inline(never)]
1296pub fn throw_val(s: JsValue) -> ! {
1297 unsafe {
1298 let idx = s.idx;
1299 mem::forget(s);
1300 __wbindgen_rethrow(idx);
1301 }
1302}
1303
1304pub fn externref_heap_live_count() -> u32 {
1349 unsafe { __wbindgen_externref_heap_live_count() }
1350}
1351
1352#[doc(hidden)]
1353pub fn anyref_heap_live_count() -> u32 {
1354 externref_heap_live_count()
1355}
1356
1357pub trait UnwrapThrowExt<T>: Sized {
1387 #[cfg_attr(
1390 any(
1391 debug_assertions,
1392 not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))
1393 ),
1394 track_caller
1395 )]
1396 fn unwrap_throw(self) -> T {
1397 if cfg!(all(
1398 debug_assertions,
1399 all(
1400 target_arch = "wasm32",
1401 any(target_os = "unknown", target_os = "none")
1402 )
1403 )) {
1404 let loc = core::panic::Location::caller();
1405 let msg = alloc::format!(
1406 "called `{}::unwrap_throw()` ({}:{}:{})",
1407 core::any::type_name::<Self>(),
1408 loc.file(),
1409 loc.line(),
1410 loc.column()
1411 );
1412 self.expect_throw(&msg)
1413 } else {
1414 self.expect_throw("called `unwrap_throw()`")
1415 }
1416 }
1417
1418 #[cfg_attr(
1422 any(
1423 debug_assertions,
1424 not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))
1425 ),
1426 track_caller
1427 )]
1428 fn expect_throw(self, message: &str) -> T;
1429}
1430
1431impl<T> UnwrapThrowExt<T> for Option<T> {
1432 fn unwrap_throw(self) -> T {
1433 const MSG: &str = "called `Option::unwrap_throw()` on a `None` value";
1434
1435 if cfg!(all(
1436 target_arch = "wasm32",
1437 any(target_os = "unknown", target_os = "none")
1438 )) {
1439 if let Some(val) = self {
1440 val
1441 } else if cfg!(debug_assertions) {
1442 let loc = core::panic::Location::caller();
1443 let msg =
1444 alloc::format!("{} ({}:{}:{})", MSG, loc.file(), loc.line(), loc.column(),);
1445
1446 throw_str(&msg)
1447 } else {
1448 throw_str(MSG)
1449 }
1450 } else {
1451 self.expect(MSG)
1452 }
1453 }
1454
1455 fn expect_throw(self, message: &str) -> T {
1456 if cfg!(all(
1457 target_arch = "wasm32",
1458 any(target_os = "unknown", target_os = "none")
1459 )) {
1460 if let Some(val) = self {
1461 val
1462 } else if cfg!(debug_assertions) {
1463 let loc = core::panic::Location::caller();
1464 let msg = alloc::format!(
1465 "{} ({}:{}:{})",
1466 message,
1467 loc.file(),
1468 loc.line(),
1469 loc.column(),
1470 );
1471
1472 throw_str(&msg)
1473 } else {
1474 throw_str(message)
1475 }
1476 } else {
1477 self.expect(message)
1478 }
1479 }
1480}
1481
1482impl<T, E> UnwrapThrowExt<T> for Result<T, E>
1483where
1484 E: core::fmt::Debug,
1485{
1486 fn unwrap_throw(self) -> T {
1487 const MSG: &str = "called `Result::unwrap_throw()` on an `Err` value";
1488
1489 if cfg!(all(
1490 target_arch = "wasm32",
1491 any(target_os = "unknown", target_os = "none")
1492 )) {
1493 match self {
1494 Ok(val) => val,
1495 Err(err) => {
1496 if cfg!(debug_assertions) {
1497 let loc = core::panic::Location::caller();
1498 let msg = alloc::format!(
1499 "{} ({}:{}:{}): {:?}",
1500 MSG,
1501 loc.file(),
1502 loc.line(),
1503 loc.column(),
1504 err
1505 );
1506
1507 throw_str(&msg)
1508 } else {
1509 throw_str(MSG)
1510 }
1511 }
1512 }
1513 } else {
1514 self.expect(MSG)
1515 }
1516 }
1517
1518 fn expect_throw(self, message: &str) -> T {
1519 if cfg!(all(
1520 target_arch = "wasm32",
1521 any(target_os = "unknown", target_os = "none")
1522 )) {
1523 match self {
1524 Ok(val) => val,
1525 Err(err) => {
1526 if cfg!(debug_assertions) {
1527 let loc = core::panic::Location::caller();
1528 let msg = alloc::format!(
1529 "{} ({}:{}:{}): {:?}",
1530 message,
1531 loc.file(),
1532 loc.line(),
1533 loc.column(),
1534 err
1535 );
1536
1537 throw_str(&msg)
1538 } else {
1539 throw_str(message)
1540 }
1541 }
1542 }
1543 } else {
1544 self.expect(message)
1545 }
1546 }
1547}
1548
1549pub fn module() -> JsValue {
1553 unsafe { JsValue::_new(__wbindgen_module()) }
1554}
1555
1556pub fn exports() -> JsValue {
1558 unsafe { JsValue::_new(__wbindgen_exports()) }
1559}
1560
1561pub fn memory() -> JsValue {
1563 unsafe { JsValue::_new(__wbindgen_memory()) }
1564}
1565
1566pub fn function_table() -> JsValue {
1569 unsafe { JsValue::_new(__wbindgen_function_table()) }
1570}
1571
1572#[doc(hidden)]
1573pub mod __rt {
1574 use crate::JsValue;
1575 use core::borrow::{Borrow, BorrowMut};
1576 use core::cell::{Cell, UnsafeCell};
1577 use core::convert::Infallible;
1578 use core::mem;
1579 use core::ops::{Deref, DerefMut};
1580 #[cfg(all(target_feature = "atomics", not(feature = "std")))]
1581 use core::sync::atomic::{AtomicU8, Ordering};
1582
1583 pub extern crate alloc;
1584 pub extern crate core;
1585 #[cfg(feature = "std")]
1586 pub extern crate std;
1587
1588 use alloc::alloc::{alloc, dealloc, realloc, Layout};
1589 use alloc::boxed::Box;
1590 use alloc::rc::Rc;
1591
1592 #[cfg(not(feature = "std"))]
1595 pub struct LazyCell<T, F = fn() -> T>(::once_cell::unsync::Lazy<T, F>);
1596
1597 #[cfg(all(not(target_feature = "atomics"), not(feature = "std")))]
1598 unsafe impl<T, F> Sync for LazyCell<T, F> {}
1599
1600 #[cfg(all(not(target_feature = "atomics"), not(feature = "std")))]
1601 unsafe impl<T, F> Send for LazyCell<T, F> {}
1602
1603 #[cfg(not(feature = "std"))]
1604 impl<T, F> LazyCell<T, F> {
1605 pub const fn new(init: F) -> LazyCell<T, F> {
1606 Self(::once_cell::unsync::Lazy::new(init))
1607 }
1608 }
1609
1610 #[cfg(not(feature = "std"))]
1611 impl<T, F: FnOnce() -> T> LazyCell<T, F> {
1612 pub(crate) fn try_with<R>(
1613 &self,
1614 f: impl FnOnce(&T) -> R,
1615 ) -> Result<R, core::convert::Infallible> {
1616 Ok(f(&self.0))
1617 }
1618
1619 pub fn force(this: &Self) -> &T {
1620 &this.0
1621 }
1622 }
1623
1624 #[cfg(not(feature = "std"))]
1625 impl<T> Deref for LazyCell<T> {
1626 type Target = T;
1627
1628 fn deref(&self) -> &T {
1629 ::once_cell::unsync::Lazy::force(&self.0)
1630 }
1631 }
1632
1633 #[cfg(feature = "std")]
1634 pub use once_cell::sync::Lazy as LazyLock;
1635
1636 #[cfg(all(not(target_feature = "atomics"), not(feature = "std")))]
1637 pub use LazyCell as LazyLock;
1638
1639 #[cfg(all(target_feature = "atomics", not(feature = "std")))]
1640 pub struct LazyLock<T, F = fn() -> T> {
1641 state: AtomicU8,
1642 data: UnsafeCell<Data<T, F>>,
1643 }
1644
1645 #[cfg(all(target_feature = "atomics", not(feature = "std")))]
1646 enum Data<T, F> {
1647 Value(T),
1648 Init(F),
1649 }
1650
1651 #[cfg(all(target_feature = "atomics", not(feature = "std")))]
1652 unsafe impl<T, F> Sync for LazyLock<T, F> {}
1653
1654 #[cfg(all(target_feature = "atomics", not(feature = "std")))]
1655 unsafe impl<T, F> Send for LazyLock<T, F> {}
1656
1657 #[cfg(all(target_feature = "atomics", not(feature = "std")))]
1658 impl<T, F> LazyLock<T, F> {
1659 const STATE_UNINIT: u8 = 0;
1660 const STATE_INITIALIZING: u8 = 1;
1661 const STATE_INIT: u8 = 2;
1662
1663 pub const fn new(init: F) -> LazyLock<T, F> {
1664 Self {
1665 state: AtomicU8::new(Self::STATE_UNINIT),
1666 data: UnsafeCell::new(Data::Init(init)),
1667 }
1668 }
1669 }
1670
1671 #[cfg(all(target_feature = "atomics", not(feature = "std")))]
1672 impl<T> Deref for LazyLock<T> {
1673 type Target = T;
1674
1675 fn deref(&self) -> &T {
1676 let mut state = self.state.load(Ordering::Acquire);
1677
1678 loop {
1679 match state {
1680 Self::STATE_INIT => {
1681 let Data::Value(value) = (unsafe { &*self.data.get() }) else {
1682 unreachable!()
1683 };
1684 return value;
1685 }
1686 Self::STATE_UNINIT => {
1687 if let Err(new_state) = self.state.compare_exchange_weak(
1688 Self::STATE_UNINIT,
1689 Self::STATE_INITIALIZING,
1690 Ordering::Acquire,
1691 Ordering::Relaxed,
1692 ) {
1693 state = new_state;
1694 continue;
1695 }
1696
1697 let data = unsafe { &mut *self.data.get() };
1698 let Data::Init(init) = data else {
1699 unreachable!()
1700 };
1701 *data = Data::Value(init());
1702 self.state.store(Self::STATE_INIT, Ordering::Release);
1703 state = Self::STATE_INIT;
1704 }
1705 Self::STATE_INITIALIZING => {
1706 state = self.state.load(Ordering::Acquire);
1709 }
1710 _ => unreachable!(),
1711 }
1712 }
1713 }
1714 }
1715
1716 #[macro_export]
1717 #[doc(hidden)]
1718 #[cfg(all(not(feature = "std"), not(target_feature = "atomics")))]
1719 macro_rules! __wbindgen_thread_local {
1720 ($wasm_bindgen:tt, $actual_ty:ty) => {{
1721 static _VAL: $wasm_bindgen::__rt::LazyCell<$actual_ty> =
1722 $wasm_bindgen::__rt::LazyCell::new(init);
1723 $wasm_bindgen::JsThreadLocal { __inner: &_VAL }
1724 }};
1725 }
1726
1727 #[macro_export]
1728 #[doc(hidden)]
1729 #[cfg(all(not(feature = "std"), target_feature = "atomics"))]
1730 #[allow_internal_unstable(thread_local)]
1731 macro_rules! __wbindgen_thread_local {
1732 ($wasm_bindgen:tt, $actual_ty:ty) => {{
1733 #[thread_local]
1734 static _VAL: $wasm_bindgen::__rt::LazyCell<$actual_ty> =
1735 $wasm_bindgen::__rt::LazyCell::new(init);
1736 $wasm_bindgen::JsThreadLocal {
1737 __inner: || unsafe {
1738 $wasm_bindgen::__rt::LazyCell::force(&_VAL) as *const $actual_ty
1739 },
1740 }
1741 }};
1742 }
1743
1744 #[macro_export]
1745 #[doc(hidden)]
1746 #[cfg(not(wasm_bindgen_unstable_test_coverage))]
1747 macro_rules! __wbindgen_coverage {
1748 ($item:item) => {
1749 $item
1750 };
1751 }
1752
1753 #[macro_export]
1754 #[doc(hidden)]
1755 #[cfg(wasm_bindgen_unstable_test_coverage)]
1756 #[allow_internal_unstable(coverage_attribute)]
1757 macro_rules! __wbindgen_coverage {
1758 ($item:item) => {
1759 #[coverage(off)]
1760 $item
1761 };
1762 }
1763
1764 #[inline]
1765 pub fn assert_not_null<T>(s: *mut T) {
1766 if s.is_null() {
1767 throw_null();
1768 }
1769 }
1770
1771 #[cold]
1772 #[inline(never)]
1773 fn throw_null() -> ! {
1774 super::throw_str("null pointer passed to rust");
1775 }
1776
1777 pub struct WasmRefCell<T: ?Sized> {
1795 borrow: Cell<usize>,
1796 value: UnsafeCell<T>,
1797 }
1798
1799 impl<T: ?Sized> WasmRefCell<T> {
1800 pub fn new(value: T) -> WasmRefCell<T>
1801 where
1802 T: Sized,
1803 {
1804 WasmRefCell {
1805 value: UnsafeCell::new(value),
1806 borrow: Cell::new(0),
1807 }
1808 }
1809
1810 pub fn get_mut(&mut self) -> &mut T {
1811 unsafe { &mut *self.value.get() }
1812 }
1813
1814 pub fn borrow(&self) -> Ref<T> {
1815 unsafe {
1816 if self.borrow.get() == usize::MAX {
1817 borrow_fail();
1818 }
1819 self.borrow.set(self.borrow.get() + 1);
1820 Ref {
1821 value: &*self.value.get(),
1822 borrow: &self.borrow,
1823 }
1824 }
1825 }
1826
1827 pub fn borrow_mut(&self) -> RefMut<T> {
1828 unsafe {
1829 if self.borrow.get() != 0 {
1830 borrow_fail();
1831 }
1832 self.borrow.set(usize::MAX);
1833 RefMut {
1834 value: &mut *self.value.get(),
1835 borrow: &self.borrow,
1836 }
1837 }
1838 }
1839
1840 pub fn into_inner(self) -> T
1841 where
1842 T: Sized,
1843 {
1844 self.value.into_inner()
1845 }
1846 }
1847
1848 pub struct Ref<'b, T: ?Sized + 'b> {
1849 value: &'b T,
1850 borrow: &'b Cell<usize>,
1851 }
1852
1853 impl<T: ?Sized> Deref for Ref<'_, T> {
1854 type Target = T;
1855
1856 #[inline]
1857 fn deref(&self) -> &T {
1858 self.value
1859 }
1860 }
1861
1862 impl<T: ?Sized> Borrow<T> for Ref<'_, T> {
1863 #[inline]
1864 fn borrow(&self) -> &T {
1865 self.value
1866 }
1867 }
1868
1869 impl<T: ?Sized> Drop for Ref<'_, T> {
1870 fn drop(&mut self) {
1871 self.borrow.set(self.borrow.get() - 1);
1872 }
1873 }
1874
1875 pub struct RefMut<'b, T: ?Sized + 'b> {
1876 value: &'b mut T,
1877 borrow: &'b Cell<usize>,
1878 }
1879
1880 impl<T: ?Sized> Deref for RefMut<'_, T> {
1881 type Target = T;
1882
1883 #[inline]
1884 fn deref(&self) -> &T {
1885 self.value
1886 }
1887 }
1888
1889 impl<T: ?Sized> DerefMut for RefMut<'_, T> {
1890 #[inline]
1891 fn deref_mut(&mut self) -> &mut T {
1892 self.value
1893 }
1894 }
1895
1896 impl<T: ?Sized> Borrow<T> for RefMut<'_, T> {
1897 #[inline]
1898 fn borrow(&self) -> &T {
1899 self.value
1900 }
1901 }
1902
1903 impl<T: ?Sized> BorrowMut<T> for RefMut<'_, T> {
1904 #[inline]
1905 fn borrow_mut(&mut self) -> &mut T {
1906 self.value
1907 }
1908 }
1909
1910 impl<T: ?Sized> Drop for RefMut<'_, T> {
1911 fn drop(&mut self) {
1912 self.borrow.set(0);
1913 }
1914 }
1915
1916 fn borrow_fail() -> ! {
1917 super::throw_str(
1918 "recursive use of an object detected which would lead to \
1919 unsafe aliasing in rust",
1920 );
1921 }
1922
1923 pub struct RcRef<T: ?Sized + 'static> {
1929 ref_: Ref<'static, T>,
1938 _rc: Rc<WasmRefCell<T>>,
1939 }
1940
1941 impl<T: ?Sized> RcRef<T> {
1942 pub fn new(rc: Rc<WasmRefCell<T>>) -> Self {
1943 let ref_ = unsafe { (*Rc::as_ptr(&rc)).borrow() };
1944 Self { _rc: rc, ref_ }
1945 }
1946 }
1947
1948 impl<T: ?Sized> Deref for RcRef<T> {
1949 type Target = T;
1950
1951 #[inline]
1952 fn deref(&self) -> &T {
1953 &self.ref_
1954 }
1955 }
1956
1957 impl<T: ?Sized> Borrow<T> for RcRef<T> {
1958 #[inline]
1959 fn borrow(&self) -> &T {
1960 &self.ref_
1961 }
1962 }
1963
1964 pub struct RcRefMut<T: ?Sized + 'static> {
1970 ref_: RefMut<'static, T>,
1971 _rc: Rc<WasmRefCell<T>>,
1972 }
1973
1974 impl<T: ?Sized> RcRefMut<T> {
1975 pub fn new(rc: Rc<WasmRefCell<T>>) -> Self {
1976 let ref_ = unsafe { (*Rc::as_ptr(&rc)).borrow_mut() };
1977 Self { _rc: rc, ref_ }
1978 }
1979 }
1980
1981 impl<T: ?Sized> Deref for RcRefMut<T> {
1982 type Target = T;
1983
1984 #[inline]
1985 fn deref(&self) -> &T {
1986 &self.ref_
1987 }
1988 }
1989
1990 impl<T: ?Sized> DerefMut for RcRefMut<T> {
1991 #[inline]
1992 fn deref_mut(&mut self) -> &mut T {
1993 &mut self.ref_
1994 }
1995 }
1996
1997 impl<T: ?Sized> Borrow<T> for RcRefMut<T> {
1998 #[inline]
1999 fn borrow(&self) -> &T {
2000 &self.ref_
2001 }
2002 }
2003
2004 impl<T: ?Sized> BorrowMut<T> for RcRefMut<T> {
2005 #[inline]
2006 fn borrow_mut(&mut self) -> &mut T {
2007 &mut self.ref_
2008 }
2009 }
2010
2011 #[no_mangle]
2012 pub extern "C" fn __wbindgen_malloc(size: usize, align: usize) -> *mut u8 {
2013 if let Ok(layout) = Layout::from_size_align(size, align) {
2014 unsafe {
2015 if layout.size() > 0 {
2016 let ptr = alloc(layout);
2017 if !ptr.is_null() {
2018 return ptr;
2019 }
2020 } else {
2021 return align as *mut u8;
2022 }
2023 }
2024 }
2025
2026 malloc_failure();
2027 }
2028
2029 #[no_mangle]
2030 pub unsafe extern "C" fn __wbindgen_realloc(
2031 ptr: *mut u8,
2032 old_size: usize,
2033 new_size: usize,
2034 align: usize,
2035 ) -> *mut u8 {
2036 debug_assert!(old_size > 0);
2037 debug_assert!(new_size > 0);
2038 if let Ok(layout) = Layout::from_size_align(old_size, align) {
2039 let ptr = realloc(ptr, layout, new_size);
2040 if !ptr.is_null() {
2041 return ptr;
2042 }
2043 }
2044 malloc_failure();
2045 }
2046
2047 #[cold]
2048 fn malloc_failure() -> ! {
2049 cfg_if::cfg_if! {
2050 if #[cfg(debug_assertions)] {
2051 super::throw_str("invalid malloc request")
2052 } else if #[cfg(feature = "std")] {
2053 std::process::abort();
2054 } else if #[cfg(all(
2055 target_arch = "wasm32",
2056 any(target_os = "unknown", target_os = "none")
2057 ))] {
2058 core::arch::wasm32::unreachable();
2059 } else {
2060 unreachable!()
2061 }
2062 }
2063 }
2064
2065 #[no_mangle]
2066 pub unsafe extern "C" fn __wbindgen_free(ptr: *mut u8, size: usize, align: usize) {
2067 if size == 0 {
2070 return;
2071 }
2072 let layout = Layout::from_size_align_unchecked(size, align);
2073 dealloc(ptr, layout);
2074 }
2075
2076 #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
2110 pub fn link_mem_intrinsics() {
2111 crate::link::link_intrinsics();
2112 }
2113
2114 #[cfg(feature = "std")]
2115 std::thread_local! {
2116 static GLOBAL_EXNDATA: Cell<[u32; 2]> = Cell::new([0; 2]);
2117 }
2118 #[cfg(all(not(feature = "std"), not(target_feature = "atomics")))]
2119 static mut GLOBAL_EXNDATA: [u32; 2] = [0; 2];
2120 #[cfg(all(not(feature = "std"), target_feature = "atomics"))]
2121 #[thread_local]
2122 static GLOBAL_EXNDATA: Cell<[u32; 2]> = Cell::new([0; 2]);
2123
2124 struct GlobalExndata;
2125
2126 impl GlobalExndata {
2127 #[cfg(feature = "std")]
2128 fn get() -> [u32; 2] {
2129 GLOBAL_EXNDATA.with(Cell::get)
2130 }
2131
2132 #[cfg(all(not(feature = "std"), not(target_feature = "atomics")))]
2133 fn get() -> [u32; 2] {
2134 unsafe { GLOBAL_EXNDATA }
2135 }
2136
2137 #[cfg(all(not(feature = "std"), target_feature = "atomics"))]
2138 fn get() -> [u32; 2] {
2139 GLOBAL_EXNDATA.get()
2140 }
2141
2142 #[cfg(feature = "std")]
2143 fn set(data: [u32; 2]) {
2144 GLOBAL_EXNDATA.with(|d| d.set(data))
2145 }
2146
2147 #[cfg(all(not(feature = "std"), not(target_feature = "atomics")))]
2148 fn set(data: [u32; 2]) {
2149 unsafe { GLOBAL_EXNDATA = data };
2150 }
2151
2152 #[cfg(all(not(feature = "std"), target_feature = "atomics"))]
2153 fn set(data: [u32; 2]) {
2154 GLOBAL_EXNDATA.set(data);
2155 }
2156 }
2157
2158 #[no_mangle]
2159 pub unsafe extern "C" fn __wbindgen_exn_store(idx: u32) {
2160 debug_assert_eq!(GlobalExndata::get()[0], 0);
2161 GlobalExndata::set([1, idx]);
2162 }
2163
2164 pub fn take_last_exception() -> Result<(), super::JsValue> {
2165 let ret = if GlobalExndata::get()[0] == 1 {
2166 Err(super::JsValue::_new(GlobalExndata::get()[1]))
2167 } else {
2168 Ok(())
2169 };
2170 GlobalExndata::set([0, 0]);
2171 ret
2172 }
2173
2174 pub trait IntoJsResult {
2179 fn into_js_result(self) -> Result<JsValue, JsValue>;
2180 }
2181
2182 impl IntoJsResult for () {
2183 fn into_js_result(self) -> Result<JsValue, JsValue> {
2184 Ok(JsValue::undefined())
2185 }
2186 }
2187
2188 impl<T: Into<JsValue>> IntoJsResult for T {
2189 fn into_js_result(self) -> Result<JsValue, JsValue> {
2190 Ok(self.into())
2191 }
2192 }
2193
2194 impl<T: Into<JsValue>, E: Into<JsValue>> IntoJsResult for Result<T, E> {
2195 fn into_js_result(self) -> Result<JsValue, JsValue> {
2196 match self {
2197 Ok(e) => Ok(e.into()),
2198 Err(e) => Err(e.into()),
2199 }
2200 }
2201 }
2202
2203 impl<E: Into<JsValue>> IntoJsResult for Result<(), E> {
2204 fn into_js_result(self) -> Result<JsValue, JsValue> {
2205 match self {
2206 Ok(()) => Ok(JsValue::undefined()),
2207 Err(e) => Err(e.into()),
2208 }
2209 }
2210 }
2211
2212 pub trait Start {
2215 fn start(self);
2216 }
2217
2218 impl Start for () {
2219 #[inline]
2220 fn start(self) {}
2221 }
2222
2223 impl<E: Into<JsValue>> Start for Result<(), E> {
2224 #[inline]
2225 fn start(self) {
2226 if let Err(e) = self {
2227 crate::throw_val(e.into());
2228 }
2229 }
2230 }
2231
2232 pub struct MainWrapper<T>(pub Option<T>);
2235
2236 pub trait Main {
2237 fn __wasm_bindgen_main(&mut self);
2238 }
2239
2240 impl Main for &mut &mut MainWrapper<()> {
2241 #[inline]
2242 fn __wasm_bindgen_main(&mut self) {}
2243 }
2244
2245 impl Main for &mut &mut MainWrapper<Infallible> {
2246 #[inline]
2247 fn __wasm_bindgen_main(&mut self) {}
2248 }
2249
2250 impl<E: Into<JsValue>> Main for &mut &mut MainWrapper<Result<(), E>> {
2251 #[inline]
2252 fn __wasm_bindgen_main(&mut self) {
2253 if let Err(e) = self.0.take().unwrap() {
2254 crate::throw_val(e.into());
2255 }
2256 }
2257 }
2258
2259 impl<E: core::fmt::Debug> Main for &mut MainWrapper<Result<(), E>> {
2260 #[inline]
2261 fn __wasm_bindgen_main(&mut self) {
2262 if let Err(e) = self.0.take().unwrap() {
2263 crate::throw_str(&alloc::format!("{:?}", e));
2264 }
2265 }
2266 }
2267
2268 pub const fn flat_len<T, const SIZE: usize>(slices: [&[T]; SIZE]) -> usize {
2269 let mut len = 0;
2270 let mut i = 0;
2271 while i < slices.len() {
2272 len += slices[i].len();
2273 i += 1;
2274 }
2275 len
2276 }
2277
2278 pub const fn flat_byte_slices<const RESULT_LEN: usize, const SIZE: usize>(
2279 slices: [&[u8]; SIZE],
2280 ) -> [u8; RESULT_LEN] {
2281 let mut result = [0; RESULT_LEN];
2282
2283 let mut slice_index = 0;
2284 let mut result_offset = 0;
2285
2286 while slice_index < slices.len() {
2287 let mut i = 0;
2288 let slice = slices[slice_index];
2289 while i < slice.len() {
2290 result[result_offset] = slice[i];
2291 i += 1;
2292 result_offset += 1;
2293 }
2294 slice_index += 1;
2295 }
2296
2297 result
2298 }
2299
2300 pub const fn encode_u32_to_fixed_len_bytes(value: u32) -> [u8; 5] {
2304 let mut result: [u8; 5] = [0; 5];
2305 let mut i = 0;
2306 while i < 4 {
2307 result[i] = ((value >> (7 * i)) | 0x80) as u8;
2308 i += 1;
2309 }
2310 result[4] = (value >> (7 * 4)) as u8;
2311 result
2312 }
2313
2314 pub trait VectorIntoJsValue: Sized {
2317 fn vector_into_jsvalue(vector: Box<[Self]>) -> JsValue;
2318 }
2319
2320 impl<T: VectorIntoJsValue> From<Box<[T]>> for JsValue {
2321 fn from(vector: Box<[T]>) -> Self {
2322 T::vector_into_jsvalue(vector)
2323 }
2324 }
2325
2326 pub fn js_value_vector_into_jsvalue<T: Into<JsValue>>(vector: Box<[T]>) -> JsValue {
2327 let result = unsafe { JsValue::_new(super::__wbindgen_array_new()) };
2328 for value in vector.into_vec() {
2329 let js: JsValue = value.into();
2330 unsafe { super::__wbindgen_array_push(result.idx, js.idx) }
2331 mem::forget(js);
2334 }
2335 result
2336 }
2337}
2338
2339#[derive(Copy, Clone, PartialEq, Debug, Eq)]
2352pub struct Clamped<T>(pub T);
2353
2354impl<T> Deref for Clamped<T> {
2355 type Target = T;
2356
2357 fn deref(&self) -> &T {
2358 &self.0
2359 }
2360}
2361
2362impl<T> DerefMut for Clamped<T> {
2363 fn deref_mut(&mut self) -> &mut T {
2364 &mut self.0
2365 }
2366}
2367
2368#[derive(Clone, Debug)]
2425pub struct JsError {
2426 value: JsValue,
2427}
2428
2429impl JsError {
2430 #[inline]
2432 pub fn new(s: &str) -> JsError {
2433 Self {
2434 value: unsafe { JsValue::_new(crate::__wbindgen_error_new(s.as_ptr(), s.len())) },
2435 }
2436 }
2437}
2438
2439if_std! {
2440 impl<E> From<E> for JsError
2441 where
2442 E: std::error::Error,
2443 {
2444 fn from(error: E) -> Self {
2445 JsError::new(&error.to_string())
2446 }
2447 }
2448}
2449
2450impl From<JsError> for JsValue {
2451 fn from(error: JsError) -> Self {
2452 error.value
2453 }
2454}
2455
2456macro_rules! typed_arrays {
2457 ($($ty:ident $ctor:ident $clamped_ctor:ident,)*) => {
2458 $(
2459 impl From<Box<[$ty]>> for JsValue {
2460 fn from(mut vector: Box<[$ty]>) -> Self {
2461 let result = unsafe { JsValue::_new($ctor(vector.as_mut_ptr(), vector.len())) };
2462 mem::forget(vector);
2463 result
2464 }
2465 }
2466
2467 impl From<Clamped<Box<[$ty]>>> for JsValue {
2468 fn from(mut vector: Clamped<Box<[$ty]>>) -> Self {
2469 let result = unsafe { JsValue::_new($clamped_ctor(vector.as_mut_ptr(), vector.len())) };
2470 mem::forget(vector);
2471 result
2472 }
2473 }
2474 )*
2475 };
2476 }
2477
2478typed_arrays! {
2479 u8 __wbindgen_uint8_array_new __wbindgen_uint8_clamped_array_new,
2480 u16 __wbindgen_uint16_array_new __wbindgen_uint16_array_new,
2481 u32 __wbindgen_uint32_array_new __wbindgen_uint32_array_new,
2482 u64 __wbindgen_biguint64_array_new __wbindgen_biguint64_array_new,
2483 i8 __wbindgen_int8_array_new __wbindgen_int8_array_new,
2484 i16 __wbindgen_int16_array_new __wbindgen_int16_array_new,
2485 i32 __wbindgen_int32_array_new __wbindgen_int32_array_new,
2486 i64 __wbindgen_bigint64_array_new __wbindgen_bigint64_array_new,
2487 f32 __wbindgen_float32_array_new __wbindgen_float32_array_new,
2488 f64 __wbindgen_float64_array_new __wbindgen_float64_array_new,
2489}
2490
2491impl __rt::VectorIntoJsValue for JsValue {
2492 fn vector_into_jsvalue(vector: Box<[JsValue]>) -> JsValue {
2493 __rt::js_value_vector_into_jsvalue::<JsValue>(vector)
2494 }
2495}
2496
2497impl<T: JsObject> __rt::VectorIntoJsValue for T {
2498 fn vector_into_jsvalue(vector: Box<[T]>) -> JsValue {
2499 __rt::js_value_vector_into_jsvalue::<T>(vector)
2500 }
2501}
2502
2503impl __rt::VectorIntoJsValue for String {
2504 fn vector_into_jsvalue(vector: Box<[String]>) -> JsValue {
2505 __rt::js_value_vector_into_jsvalue::<String>(vector)
2506 }
2507}
2508
2509impl<T> From<Vec<T>> for JsValue
2510where
2511 JsValue: From<Box<[T]>>,
2512{
2513 fn from(vector: Vec<T>) -> Self {
2514 JsValue::from(vector.into_boxed_slice())
2515 }
2516}
2517
2518impl<T> From<Clamped<Vec<T>>> for JsValue
2519where
2520 JsValue: From<Clamped<Box<[T]>>>,
2521{
2522 fn from(vector: Clamped<Vec<T>>) -> Self {
2523 JsValue::from(Clamped(vector.0.into_boxed_slice()))
2524 }
2525}