|
| 1 | +use std::marker::PhantomData; |
| 2 | +use std::ops::{Deref, DerefMut}; |
| 3 | +use std::ptr::NonNull; |
| 4 | + |
| 5 | +use crate::jsapi::JS::Realm; |
| 6 | +use crate::jsapi::{JSAutoRealm, JSObject}; |
| 7 | + |
| 8 | +use crate::context::JSContext; |
| 9 | +use crate::gc::Handle; |
| 10 | +use crate::rust::wrappers2::{CurrentGlobalOrNull, GetCurrentRealmOrNull}; |
| 11 | + |
| 12 | +/// Safe wrapper around [JSAutoRealm]. |
| 13 | +/// |
| 14 | +/// On creation it enters the realm of the target object, |
| 15 | +/// realm becomes current (it's on top of the realm stack). |
| 16 | +/// Drop exits realm. |
| 17 | +/// |
| 18 | +/// While creating [AutoRealm] will not trigger GC, |
| 19 | +/// it still takes `&mut JSContext`, because it can be used in place of [JSContext] (by [Deref]/[DerefMut]). |
| 20 | +/// with additional information of entered/current realm: |
| 21 | +/// ```compile_fail |
| 22 | +/// use mozjs::context::JSContext; |
| 23 | +/// use mozjs::jsapi::JSObject; |
| 24 | +/// use mozjs::realm::AutoRealm; |
| 25 | +/// use std::ptr::NonNull; |
| 26 | +/// |
| 27 | +/// fn f(cx: &mut JSContext, target: NonNull<JSObject>) { |
| 28 | +/// let realm = AutoRealm::new(cx, target); |
| 29 | +/// f(cx, target); // one cannot use JSContext here, |
| 30 | +/// // because that could allow out of order realm drops. |
| 31 | +/// } |
| 32 | +/// ``` |
| 33 | +/// instead do this: |
| 34 | +/// ``` |
| 35 | +/// use mozjs::context::JSContext; |
| 36 | +/// use mozjs::jsapi::JSObject; |
| 37 | +/// use mozjs::realm::AutoRealm; |
| 38 | +/// use std::ptr::NonNull; |
| 39 | +/// |
| 40 | +/// fn f(cx: &mut JSContext, target: NonNull<JSObject>) { |
| 41 | +/// let mut realm = AutoRealm::new(cx, target); |
| 42 | +/// let cx = &mut realm; // this JSContext is bounded to AutoRealm |
| 43 | +/// // which in turn is bounded to original JSContext |
| 44 | +/// f(cx, target); |
| 45 | +/// } |
| 46 | +/// ``` |
| 47 | +/// |
| 48 | +/// This also enforces LIFO entering/exiting realms, which is not enforced by [JSAutoRealm]: |
| 49 | +/// ```compile_fail |
| 50 | +/// use mozjs::context::JSContext; |
| 51 | +/// use mozjs::jsapi::JSObject; |
| 52 | +/// use mozjs::realm::AutoRealm; |
| 53 | +/// use std::ptr::NonNull; |
| 54 | +/// |
| 55 | +/// fn f(cx: &mut JSContext, t1: NonNull<JSObject>, t2: NonNull<JSObject>) { |
| 56 | +/// let mut realm1 = AutoRealm::new(cx, t1); |
| 57 | +/// let cx = &mut realm1; |
| 58 | +/// let realm2 = AutoRealm::new(cx, t2); |
| 59 | +/// drop(realm1); // it's not possible to drop realm1 before realm2 |
| 60 | +/// } |
| 61 | +/// ``` |
| 62 | +pub struct AutoRealm<'cx> { |
| 63 | + cx: JSContext, |
| 64 | + realm: JSAutoRealm, |
| 65 | + phantom: PhantomData<&'cx mut ()>, |
| 66 | +} |
| 67 | + |
| 68 | +impl<'cx> AutoRealm<'cx> { |
| 69 | + /// Enters the realm of the given target object. |
| 70 | + /// The realm becomes the current realm (it's on top of the realm stack). |
| 71 | + /// The realm is exited when the [AutoRealm] is dropped. |
| 72 | + /// |
| 73 | + /// While this function will not trigger GC (it will in fact root the object) |
| 74 | + /// but because [AutoRealm] can act as a [JSContext] we need to take `&mut JSContext`. |
| 75 | + pub fn new(cx: &'cx mut JSContext, target: NonNull<JSObject>) -> AutoRealm<'cx> { |
| 76 | + let realm = JSAutoRealm::new(unsafe { cx.raw_cx_no_gc() }, target.as_ptr()); |
| 77 | + AutoRealm { |
| 78 | + cx: unsafe { JSContext::from_ptr(NonNull::new_unchecked(cx.raw_cx())) }, |
| 79 | + realm, |
| 80 | + phantom: PhantomData, |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// Enters the realm of the given target object. |
| 85 | + /// The realm becomes the current realm (it's on top of the realm stack). |
| 86 | + /// The realm is exited when the [AutoRealm] is dropped. |
| 87 | + /// |
| 88 | + /// While this function will not trigger GC (it will in fact root the object) |
| 89 | + /// but because [AutoRealm] can act as a [JSContext] we need to take `&mut JSContext`. |
| 90 | + pub fn new_from_handle( |
| 91 | + cx: &'cx mut JSContext, |
| 92 | + target: Handle<*mut JSObject>, |
| 93 | + ) -> AutoRealm<'cx> { |
| 94 | + Self::new(cx, NonNull::new(target.get()).unwrap()) |
| 95 | + } |
| 96 | + |
| 97 | + /// If we can get &mut AutoRealm then we are current realm, |
| 98 | + /// because if there existed other current realm, we couldn't get &mut AutoRealm. |
| 99 | + pub fn current_realm(&'cx mut self) -> CurrentRealm<'cx> { |
| 100 | + CurrentRealm::assert(self) |
| 101 | + } |
| 102 | + |
| 103 | + /// Obtain the handle to the global object of the current realm. |
| 104 | + pub fn global(&'_ self) -> Handle<'_, *mut JSObject> { |
| 105 | + // SAFETY: object is rooted by realm |
| 106 | + unsafe { Handle::from_marked_location(CurrentGlobalOrNull(&*self) as _) } |
| 107 | + } |
| 108 | + |
| 109 | + /// Erase the lifetime of this [AutoRealm]. |
| 110 | + /// |
| 111 | + /// # Safety |
| 112 | + /// - The caller must ensure that the [AutoRealm] does not outlive the [JSContext] it was created with. |
| 113 | + pub unsafe fn erase_lifetime(self) -> AutoRealm<'static> { |
| 114 | + std::mem::transmute(self) |
| 115 | + } |
| 116 | + |
| 117 | + pub fn realm(&self) -> &JSAutoRealm { |
| 118 | + &self.realm |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl<'cx> Deref for AutoRealm<'cx> { |
| 123 | + type Target = JSContext; |
| 124 | + |
| 125 | + fn deref(&'_ self) -> &'_ Self::Target { |
| 126 | + &self.cx |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl<'cx> DerefMut for AutoRealm<'cx> { |
| 131 | + fn deref_mut(&'_ mut self) -> &'_ mut Self::Target { |
| 132 | + &mut self.cx |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl<'cx> Drop for AutoRealm<'cx> { |
| 137 | + // if we do not implement this rust can shorten lifetime of cx, |
| 138 | + // without effecting JSAutoRealm (realm drops after we lose lifetime of cx) |
| 139 | + fn drop(&mut self) {} |
| 140 | +} |
| 141 | + |
| 142 | +/// Represents the current realm of [JSContext] (top realm on realm stack). |
| 143 | +/// |
| 144 | +/// Similarly to [AutoRealm], while you can access this type via `&mut`/`&mut` |
| 145 | +/// we know that this realm is current (on top of realm stack). |
| 146 | +/// |
| 147 | +/// ```compile_fail |
| 148 | +/// use mozjs::context::JSContext; |
| 149 | +/// use mozjs::jsapi::JSObject; |
| 150 | +/// use mozjs::realm::{AutoRealm, CurrentRealm}; |
| 151 | +/// use std::ptr::NonNull; |
| 152 | +/// |
| 153 | +/// fn f(current_realm: &mut CurrentRealm, target: NonNull<JSObject>) { |
| 154 | +/// let mut realm = AutoRealm::new(current_realm, target); |
| 155 | +/// let cx: &mut JSContext = &mut *current_realm; // we cannot use current realm while it's not current |
| 156 | +/// } |
| 157 | +/// ``` |
| 158 | +pub struct CurrentRealm<'cx> { |
| 159 | + cx: &'cx mut JSContext, |
| 160 | + realm: NonNull<Realm>, |
| 161 | +} |
| 162 | + |
| 163 | +impl<'cx> CurrentRealm<'cx> { |
| 164 | + /// Asserts that the current realm is valid and returns it. |
| 165 | + pub fn assert(cx: &'cx mut JSContext) -> CurrentRealm<'cx> { |
| 166 | + let realm = unsafe { GetCurrentRealmOrNull(cx) }; |
| 167 | + CurrentRealm { |
| 168 | + cx, |
| 169 | + realm: NonNull::new(realm).unwrap(), |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /// Obtain the handle to the global object of the current realm. |
| 174 | + pub fn global(&'_ self) -> Handle<'_, *mut JSObject> { |
| 175 | + // SAFETY: object is rooted by realm |
| 176 | + unsafe { Handle::from_marked_location(CurrentGlobalOrNull(&*self) as _) } |
| 177 | + } |
| 178 | + |
| 179 | + pub fn realm(&self) -> &NonNull<Realm> { |
| 180 | + &self.realm |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +impl<'cx> Deref for CurrentRealm<'cx> { |
| 185 | + type Target = JSContext; |
| 186 | + |
| 187 | + fn deref(&'_ self) -> &'_ Self::Target { |
| 188 | + &self.cx |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +impl<'cx> DerefMut for CurrentRealm<'cx> { |
| 193 | + fn deref_mut(&'_ mut self) -> &'_ mut Self::Target { |
| 194 | + &mut self.cx |
| 195 | + } |
| 196 | +} |
0 commit comments