|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +use jsapi::JS; |
| 6 | +use jsapi::jsid; |
| 7 | +use jsapi::JSFlatString; |
| 8 | +use jsapi::JSFunction; |
| 9 | +use jsapi::JSID_VOID; |
| 10 | +use jsapi::JSObject; |
| 11 | +use jsapi::JSScript; |
| 12 | +use jsapi::JSString; |
| 13 | + |
| 14 | +use std::cell::UnsafeCell; |
| 15 | +use std::mem; |
| 16 | +use std::ptr; |
| 17 | + |
| 18 | +// Trait for rooting |
| 19 | + |
| 20 | +pub trait RootKind { |
| 21 | + #[allow(non_snake_case)] |
| 22 | + #[inline(always)] |
| 23 | + fn rootKind() -> JS::RootKind; |
| 24 | +} |
| 25 | + |
| 26 | +impl RootKind for *mut JSObject { |
| 27 | + #[inline(always)] |
| 28 | + fn rootKind() -> JS::RootKind { JS::RootKind::Object } |
| 29 | +} |
| 30 | + |
| 31 | +impl RootKind for *mut JSFlatString { |
| 32 | + #[inline(always)] |
| 33 | + fn rootKind() -> JS::RootKind { JS::RootKind::String } |
| 34 | +} |
| 35 | + |
| 36 | +impl RootKind for *mut JSFunction { |
| 37 | + #[inline(always)] |
| 38 | + fn rootKind() -> JS::RootKind { JS::RootKind::Object } |
| 39 | +} |
| 40 | + |
| 41 | +impl RootKind for *mut JSString { |
| 42 | + #[inline(always)] |
| 43 | + fn rootKind() -> JS::RootKind { JS::RootKind::String } |
| 44 | +} |
| 45 | + |
| 46 | +impl RootKind for *mut JS::Symbol { |
| 47 | + #[inline(always)] |
| 48 | + fn rootKind() -> JS::RootKind { JS::RootKind::Symbol } |
| 49 | +} |
| 50 | + |
| 51 | +impl RootKind for *mut JSScript { |
| 52 | + #[inline(always)] |
| 53 | + fn rootKind() -> JS::RootKind { JS::RootKind::Script } |
| 54 | +} |
| 55 | + |
| 56 | +impl RootKind for jsid { |
| 57 | + #[inline(always)] |
| 58 | + fn rootKind() -> JS::RootKind { JS::RootKind::Id } |
| 59 | +} |
| 60 | + |
| 61 | +impl RootKind for JS::Value { |
| 62 | + #[inline(always)] |
| 63 | + fn rootKind() -> JS::RootKind { JS::RootKind::Value } |
| 64 | +} |
| 65 | + |
| 66 | +impl RootKind for JS::PropertyDescriptor { |
| 67 | + #[inline(always)] |
| 68 | + fn rootKind() -> JS::RootKind { JS::RootKind::Traceable } |
| 69 | +} |
| 70 | + |
| 71 | +// Trait for GC |
| 72 | + |
| 73 | +pub trait GCMethods { |
| 74 | + unsafe fn initial() -> Self; |
| 75 | + unsafe fn post_barrier(v: *mut Self, prev: Self, next: Self); |
| 76 | +} |
| 77 | + |
| 78 | +impl GCMethods for jsid { |
| 79 | + unsafe fn initial() -> jsid { JSID_VOID } |
| 80 | + unsafe fn post_barrier(_: *mut jsid, _: jsid, _: jsid) {} |
| 81 | +} |
| 82 | + |
| 83 | +impl GCMethods for *mut JSObject { |
| 84 | + unsafe fn initial() -> *mut JSObject { ptr::null_mut() } |
| 85 | + unsafe fn post_barrier(v: *mut *mut JSObject, |
| 86 | + prev: *mut JSObject, next: *mut JSObject) { |
| 87 | + JS::HeapObjectPostBarrier(v, prev, next); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl GCMethods for *mut JSString { |
| 92 | + unsafe fn initial() -> *mut JSString { ptr::null_mut() } |
| 93 | + unsafe fn post_barrier(_: *mut *mut JSString, _: *mut JSString, _: *mut JSString) {} |
| 94 | +} |
| 95 | + |
| 96 | +impl GCMethods for *mut JSScript { |
| 97 | + unsafe fn initial() -> *mut JSScript { ptr::null_mut() } |
| 98 | + unsafe fn post_barrier(_: *mut *mut JSScript, _: *mut JSScript, _: *mut JSScript) { } |
| 99 | +} |
| 100 | + |
| 101 | +impl GCMethods for *mut JSFunction { |
| 102 | + unsafe fn initial() -> *mut JSFunction { ptr::null_mut() } |
| 103 | + unsafe fn post_barrier(v: *mut *mut JSFunction, |
| 104 | + prev: *mut JSFunction, next: *mut JSFunction) { |
| 105 | + JS::HeapObjectPostBarrier(mem::transmute(v), |
| 106 | + mem::transmute(prev), mem::transmute(next)); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl GCMethods for JS::Value { |
| 111 | + unsafe fn initial() -> JS::Value { JS::Value::default() } |
| 112 | + unsafe fn post_barrier(v: *mut JS::Value, prev: JS::Value, next: JS::Value) { |
| 113 | + JS::HeapValuePostBarrier(v, &prev, &next); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl GCMethods for JS::PropertyDescriptor { |
| 118 | + unsafe fn initial() -> JS::PropertyDescriptor { JS::PropertyDescriptor::default() } |
| 119 | + unsafe fn post_barrier(_ : *mut JS::PropertyDescriptor, _ : JS::PropertyDescriptor, _ : JS::PropertyDescriptor) {} |
| 120 | +} |
| 121 | + |
| 122 | +/// Heap values encapsulate GC concerns of an on-heap reference to a JS |
| 123 | +/// object. This means that every reference to a JS object on heap must |
| 124 | +/// be realized through this structure. |
| 125 | +/// |
| 126 | +/// # Safety |
| 127 | +/// For garbage collection to work correctly in SpiderMonkey, modifying the |
| 128 | +/// wrapped value triggers a GC barrier, pointing to the underlying object. |
| 129 | +/// |
| 130 | +/// This means that after calling the `set()` function with a non-null or |
| 131 | +/// non-undefined value, the `Heap` wrapper *must not* be moved, since doing |
| 132 | +/// so will invalidate the local reference to wrapped value, still held by |
| 133 | +/// SpiderMonkey. |
| 134 | +/// |
| 135 | +/// For safe `Heap` construction with value see `Heap::boxed` function. |
| 136 | +#[repr(C)] |
| 137 | +#[derive(Debug)] |
| 138 | +pub struct Heap<T: GCMethods + Copy> { |
| 139 | + pub ptr: UnsafeCell<T>, |
| 140 | +} |
| 141 | + |
| 142 | +impl<T: GCMethods + Copy> Heap<T> { |
| 143 | + /// This creates a `Box`-wrapped Heap value. Setting a value inside Heap |
| 144 | + /// object triggers a barrier, referring to the Heap object location, |
| 145 | + /// hence why it is not safe to construct a temporary Heap value, assign |
| 146 | + /// a non-null value and move it (e.g. typical object construction). |
| 147 | + /// |
| 148 | + /// Using boxed Heap value guarantees that the underlying Heap value will |
| 149 | + /// not be moved when constructed. |
| 150 | + pub fn boxed(v: T) -> Box<Heap<T>> |
| 151 | + where Heap<T>: Default |
| 152 | + { |
| 153 | + let boxed = Box::new(Heap::default()); |
| 154 | + boxed.set(v); |
| 155 | + boxed |
| 156 | + } |
| 157 | + |
| 158 | + pub fn set(&self, v: T) { |
| 159 | + unsafe { |
| 160 | + let ptr = self.ptr.get(); |
| 161 | + let prev = *ptr; |
| 162 | + *ptr = v; |
| 163 | + T::post_barrier(ptr, prev, v); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + pub fn get(&self) -> T { |
| 168 | + unsafe { *self.ptr.get() } |
| 169 | + } |
| 170 | + |
| 171 | + pub fn get_unsafe(&self) -> *mut T { |
| 172 | + self.ptr.get() |
| 173 | + } |
| 174 | + |
| 175 | + pub fn handle_mut(&self) -> JS::MutableHandle<T> { |
| 176 | + unsafe { |
| 177 | + JS::MutableHandle::from_marked_location(self.ptr.get()) |
| 178 | + } |
| 179 | + } |
| 180 | + /// Retrieves a Handle to the underlying value. |
| 181 | + /// |
| 182 | + /// # Safety |
| 183 | + /// |
| 184 | + /// This is only safe to do on a rooted object (which Heap is not, it needs |
| 185 | + /// to be additionally rooted), like RootedGuard, so use this only if you |
| 186 | + /// know what you're doing. |
| 187 | + /// |
| 188 | + /// # Notes |
| 189 | + /// |
| 190 | + /// Since Heap values need to be informed when a change to underlying |
| 191 | + /// value is made (e.g. via `get()`), this does not allow to create |
| 192 | + /// MutableHandle objects, which can bypass this and lead to crashes. |
| 193 | + pub unsafe fn handle(&self) -> JS::Handle<T> { |
| 194 | + JS::Handle::from_marked_location(self.ptr.get() as *const _) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +impl<T> Default for Heap<*mut T> |
| 199 | + where *mut T: GCMethods + Copy |
| 200 | +{ |
| 201 | + fn default() -> Heap<*mut T> { |
| 202 | + Heap { |
| 203 | + ptr: UnsafeCell::new(ptr::null_mut()) |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +impl Default for Heap<JS::Value> { |
| 209 | + fn default() -> Heap<JS::Value> { |
| 210 | + Heap { |
| 211 | + ptr: UnsafeCell::new(JS::Value::default()) |
| 212 | + } |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +impl<T: GCMethods + Copy> Drop for Heap<T> { |
| 217 | + fn drop(&mut self) { |
| 218 | + unsafe { |
| 219 | + let ptr = self.ptr.get(); |
| 220 | + T::post_barrier(ptr, *ptr, T::initial()); |
| 221 | + } |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +impl<T: GCMethods + Copy + PartialEq> PartialEq for Heap<T> { |
| 226 | + fn eq(&self, other: &Self) -> bool { |
| 227 | + self.get() == other.get() |
| 228 | + } |
| 229 | +} |
0 commit comments