rquickjs_core/js_lifetime.rs
1use crate::{
2 atom, value::Constructor, Array, Atom, BigInt, CString, CStringEncoding, Exception, Function,
3 Module, Object, Promise, Proxy, String, Symbol, Value,
4};
5
6/// The trait which signifies a type using the rquickjs `'js` lifetime trick for maintaining safety around Javascript values.
7///
8/// # Safety
9///
10/// This trait can only be implemented for types which derive a `'js` lifetime from a Javascript
11/// value, directly or indirectly.
12///
13/// All of the base Javascript types used in rquickjs like [`Value`] have a `'js` lifetime. If a
14/// type wants to contains one of those types it must define a lifetime generic. This trait is for
15/// indicating that that lifetime is one derived from a Javascript value. Rquickjs needs to know
16/// about this lifetime so that it is able to ensure safe use of types.
17///
18/// This trait can be derived with `#[derive(JsLifetime)]` in most cases, however sometimes a manual
19/// implementation is required.
20///
21/// This trait must be implemented correctly, failing to do so will make it possible to create
22/// unsound behavior. Correct implementions have the `'js` lifetime in `JsLifetime<'js>` be the
23/// same as the lifetime on the container, furthermore the associated type `Changed<'to>` is
24/// defined as the exact same type with the only difference being that the `'js` lifetime is now
25/// `'to`.
26///
27/// The following is a correct implementation of the [`JsLifetime`] trait.
28/// ```
29/// # use rquickjs::JsLifetime;
30/// struct Container<'js>(rquickjs::Object<'js>);
31///
32/// unsafe impl<'js> JsLifetime<'js> for Container<'js>{
33/// type Changed<'to> = Container<'to>;
34/// }
35/// ```
36///
37/// If a type does not have any lifetimes associated with it or all the lifetimes are `'static`
38/// then if is always save to implement `JsLifetime`.
39///
40/// See correct example for a static type below.
41/// ```
42/// # use rquickjs::JsLifetime;
43/// struct Bytes(Vec<u8>);
44///
45/// unsafe impl<'js> JsLifetime<'js> for Bytes{
46/// type Changed<'to> = Bytes;
47/// }
48///
49/// ```
50///
51///
52/// ## Incorrect examples
53///
54/// For example the following is unsound!
55/// ```no_run
56/// # use rquickjs::JsLifetime;
57/// struct Container<'js>(rquickjs::Object<'js>);
58///
59/// unsafe impl<'a,'js> JsLifetime<'js> for Container<'a>{ // WRONG LIFETIME!
60/// type Changed<'to> = Container<'to>;
61/// }
62/// ```
63/// `Container` here is defined as having a `'a` lifetime where it should be `'js`.
64///
65/// The following is also incorrect
66///
67/// ```no_run
68/// # use rquickjs::JsLifetime;
69/// // Her 'a is not derived from an Javascript value type, but instead the lifetime of a reference.
70/// struct Container<'a,'js>(&'a rquickjs::Object<'js>);
71///
72/// // Non 'js lifetime marked as a 'js lifetime. Unsound!
73/// unsafe impl<'js> JsLifetime<'js> for Container<'js, 'js>{
74/// type Changed<'to> = Container<'to,'to>;
75/// }
76/// ```
77/// The lifetime marked must be from an rquickjs type with a defined `<'js>` lifetime, it cannot be a
78/// the lifetime of reference!
79///
80pub unsafe trait JsLifetime<'js> {
81 /// The target which has the same type as a `Self` but with another lifetime `'t`
82 type Changed<'to>: 'to;
83}
84
85macro_rules! outlive_impls {
86 ($($type:ident,)*) => {
87 $(
88 unsafe impl<'js> JsLifetime<'js> for $type<'js> {
89 type Changed<'to> = $type<'to>;
90 //type Static = $type<'static>;
91 }
92 )*
93 };
94}
95
96outlive_impls! {
97 Value,
98 Symbol,
99 String,
100 Object,
101 Array,
102 BigInt,
103 Function,
104 Constructor,
105 Promise,
106 Proxy,
107 Exception,
108 Atom,
109}
110
111unsafe impl<'js, E: CStringEncoding> JsLifetime<'js> for CString<'js, E> {
112 type Changed<'to> = CString<'to, E>;
113}
114
115macro_rules! impl_outlive{
116 ($($($ty:ident)::+$(<$($g:ident),+>)*),*$(,)?) => {
117 $(
118 unsafe impl<'js,$($($g,)*)*> JsLifetime<'js> for $($ty)::*$(<$($g,)*>)*
119 where
120 $($($g: JsLifetime<'js>,)*)*
121 {
122 type Changed<'to> = $($ty)::*$(<$($g::Changed<'to>,)*>)*;
123 //type Static = $($ty)::*$(<$($g::Static,)*>)*;
124 }
125 )*
126 };
127}
128
129impl_outlive!(
130 u8,
131 u16,
132 u32,
133 u64,
134 usize,
135 u128,
136 i8,
137 i16,
138 i32,
139 i64,
140 isize,
141 i128,
142 char,
143 alloc::string::String,
144 alloc::vec::Vec<T>,
145 alloc::boxed::Box<T>,
146 core::option::Option<T>,
147 core::result::Result<T,E>,
148 core::cell::Cell<T>,
149 core::cell::RefCell<T>,
150 core::cell::UnsafeCell<T>,
151 alloc::collections::BTreeMap<K,V>,
152 alloc::collections::BTreeSet<K>,
153 alloc::collections::BinaryHeap<K>,
154 alloc::collections::LinkedList<T>,
155 alloc::collections::VecDeque<T>,
156 alloc::ffi::CString,
157 core::ops::Range<T>,
158 core::ops::RangeFrom<T>,
159 core::ops::RangeFull,
160 core::ops::RangeInclusive<T>,
161 core::ops::RangeTo<T>,
162 core::ops::RangeToInclusive<T>,
163 core::ops::Bound<T>,
164 core::ops::ControlFlow<B,C>,
165 alloc::rc::Rc<T>,
166 alloc::sync::Arc<T>,
167 atom::PredefinedAtom,
168);
169
170#[cfg(feature = "std")]
171impl_outlive!(
172 std::backtrace::Backtrace,
173 std::collections::HashMap<K,V>,
174 std::collections::HashSet<K>,
175 std::ffi::OsString,
176 std::process::Child,
177 std::process::Command,
178 std::process::ExitCode,
179 std::process::ExitStatus,
180 std::process::Output,
181 std::process::Stdio,
182 std::path::PathBuf,
183 std::sync::Mutex<T>,
184 std::sync::RwLock<T>,
185);
186
187unsafe impl<'js, T: JsLifetime<'js>> JsLifetime<'js> for Module<'js, T> {
188 type Changed<'to> = Module<'to, T::Changed<'to>>;
189}
190
191unsafe impl<'js> JsLifetime<'js> for () {
192 type Changed<'to> = ();
193}