Skip to content

Commit 541a913

Browse files
committed
more docs
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
1 parent 15ea48e commit 541a913

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

mozjs/src/realm.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,27 @@ impl<'cx> AutoRealm<'cx> {
127127
}
128128

129129
impl<'cx> Drop for AutoRealm<'cx> {
130-
// this is not trivially dropped type
131-
// not sure why we need to do this manually
130+
// if we do not implement this rust can shorten lifetime of cx,
131+
// without effecting JSAutoRealm (realm drops after we lose lifetime of cx)
132132
fn drop(&mut self) {}
133133
}
134134

135135
/// Represents the current realm of [JSContext] (top realm on realm stack).
136+
///
137+
/// Similarly to [AutoRealm], while you can access this type via `&mut`/`&mut`
138+
/// we know that this realm is current (on top of realm stack).
139+
///
140+
/// ```compile_fail
141+
/// use mozjs::context::JSContext;
142+
/// use mozjs::jsapi::JSObject;
143+
/// use mozjs::realm::{AutoRealm, CurrentRealm};
144+
/// use std::ptr::NonNull;
145+
///
146+
/// fn f(current_realm: CurrentRealm, target: NonNull<JSObject>) {
147+
/// let mut realm = AutoRealm::new(current_realm.cx(), target);
148+
/// let cx = current_realm.cx(); // we cannot use current realm while it's not current
149+
/// }
150+
/// ```
136151
pub struct CurrentRealm<'cx> {
137152
cx: &'cx mut JSContext,
138153
realm: NonNull<Realm>,

0 commit comments

Comments
 (0)