1#![no_std]
16
17#[doc(hidden)]
18pub extern crate alloc;
19#[macro_use]
20extern crate std;
21
22pub mod batch;
23mod cast;
24mod clamped;
25pub mod closure;
26pub mod convert;
27mod encode;
28mod erasure;
29mod function;
30mod function_registry;
31mod id_allocator;
32mod intern;
33pub(crate) mod ipc;
34#[macro_use]
35mod wire;
36#[doc(hidden)]
37#[path = "rt.rs"]
38pub mod __rt;
39mod js_error;
40mod js_helpers;
41mod lazy;
42mod object_store;
43mod parent;
44mod runtime;
45pub mod sys;
46mod try_from_js;
47mod type_cache;
48mod value;
49pub mod wry;
50
51pub use intern::*;
52
53pub use crate::__rt::marker::ErasableGeneric;
55pub use cast::JsCast;
56pub use clamped::Clamped;
57pub use closure::{
58 Closure, IntoWasmClosure, IntoWasmClosureRef, IntoWasmClosureRefMut, MaybeUnwindSafe,
59 ScopedClosure, WasmClosure, WasmClosureFnOnce, WasmClosureFnOnceAbort, WryWasmClosure,
60};
61pub use js_error::JsError;
62pub use lazy::JsThreadLocal;
63pub use value::JsValue;
64
65pub use crate::__rt::{Ref, RefMut};
66pub use parent::Parent;
67
68pub use convert::{IntoJsGeneric, JsGeneric};
69pub use encode::{BatchableResult, BinaryDecode, BinaryEncode, EncodeTypeDef};
70pub use function::JSFunction;
71pub use ipc::{DecodeError, DecodedData, EncodedData};
72pub use sys::{JsOption, Null, Promising, Undefined};
73
74pub use wry_bindgen_macro::link_to;
76pub use wry_bindgen_macro::wasm_bindgen;
77
78#[macro_export]
91#[doc(hidden)]
92macro_rules! __wry_call_js_function {
93 ($js_code:expr, $fn_type:ty, ($($args:expr),*)) => {{
94 static __FUNC: $crate::__rt::LazyJsFunction<$fn_type> =
95 $crate::__wry_submit_js_function!($js_code);
96
97 __FUNC.call($($args),*)
98 }};
99}
100
101#[macro_export]
113#[doc(hidden)]
114macro_rules! __wry_submit_js_function {
115 ($js_code:expr) => {{
116 static __SPEC: $crate::__rt::JsFunctionSpec =
117 $crate::__rt::JsFunctionSpec::new(|| $crate::alloc::format!($js_code));
118
119 $crate::__rt::inventory::submit! {
120 __SPEC
121 }
122
123 __SPEC.resolve_as()
124 }};
125}
126
127pub trait UnwrapThrowExt<T>: Sized {
130 #[cfg_attr(any(debug_assertions, not(target_family = "wasm")), track_caller)]
135 fn unwrap_throw(self) -> T {
136 if cfg!(all(debug_assertions, target_family = "wasm")) {
137 let loc = core::panic::Location::caller();
138 let msg = alloc::format!(
139 "called `{}::unwrap_throw()` ({}:{}:{})",
140 core::any::type_name::<Self>(),
141 loc.file(),
142 loc.line(),
143 loc.column()
144 );
145 self.expect_throw(&msg)
146 } else {
147 self.expect_throw("called `unwrap_throw()`")
148 }
149 }
150
151 fn expect_throw(self, message: &str) -> T;
153}
154
155impl<T> UnwrapThrowExt<T> for Option<T> {
156 fn unwrap_throw(self) -> T {
157 self.expect("called `Option::unwrap_throw()` on a `None` value")
158 }
159
160 fn expect_throw(self, message: &str) -> T {
161 self.expect(message)
162 }
163}
164
165impl<T, E> UnwrapThrowExt<T> for Result<T, E>
166where
167 E: core::fmt::Debug,
168{
169 fn unwrap_throw(self) -> T {
170 self.expect("called `Result::unwrap_throw()` on an `Err` value")
171 }
172
173 fn expect_throw(self, message: &str) -> T {
174 self.expect(message)
175 }
176}
177
178#[cold]
179#[inline(never)]
180pub fn throw_val(s: JsValue) -> ! {
181 panic!("{s:?}");
182}
183
184#[cold]
189#[inline(never)]
190pub fn throw_str(s: &str) -> ! {
191 panic!("cannot throw JS exception when running outside of wasm: {s}");
192}
193
194#[cold]
196#[inline(never)]
197#[deprecated(note = "renamed to `throw_str`")]
198#[doc(hidden)]
199pub fn throw(s: &str) -> ! {
200 throw_str(s)
201}
202
203pub fn externref_heap_live_count() -> u32 {
208 panic!("cannot introspect wasm memory when running outside of wasm")
209}
210
211pub fn module() -> JsValue {
216 panic!("cannot introspect wasm memory when running outside of wasm")
217}
218
219pub fn instance() -> JsValue {
224 panic!("cannot introspect wasm memory when running outside of wasm")
225}
226
227pub fn exports() -> JsValue {
232 panic!("cannot introspect wasm memory when running outside of wasm")
233}
234
235pub fn memory() -> JsValue {
240 panic!("cannot introspect wasm memory when running outside of wasm")
241}
242
243pub fn function_table() -> JsValue {
248 panic!("cannot introspect wasm memory when running outside of wasm")
249}
250
251#[deprecated = "use with `#[wasm_bindgen(thread_local_v2)]` instead"]
256pub struct JsStatic<T: 'static> {
257 #[doc(hidden)]
258 pub __inner: &'static std::thread::LocalKey<T>,
259}
260
261#[allow(deprecated)]
262impl<T: 'static> core::ops::Deref for JsStatic<T> {
263 type Target = T;
264 fn deref(&self) -> &T {
265 unsafe { self.__inner.with(|ptr| &*(ptr as *const T)) }
266 }
267}
268
269pub mod prelude {
271 pub use crate::Clamped;
272 pub use crate::JsCast;
273 pub use crate::JsError;
274 pub use crate::JsValue;
275 pub use crate::UnwrapThrowExt;
276 pub use crate::WasmClosure;
277 pub use crate::closure::{Closure, ScopedClosure};
278 pub use crate::convert::Upcast;
279 pub use crate::convert::{IntoJsGeneric, JsGeneric, UpcastFrom};
280 pub use crate::wasm_bindgen;
281 pub use crate::{BatchableResult, BinaryDecode, BinaryEncode, EncodeTypeDef};
282 pub use crate::{JSFunction, JsThreadLocal};
283 pub use crate::{JsOption, Null, Promising, Undefined};
284}