josephine 0.2.0

Josephine: using JavaScript to safely manage the lifetimes of Rust data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use super::Compartment;
use super::JSInitializable;
use super::JSManaged;
use super::JSRoot;
use super::JSLifetime;
use super::JSTraceable;
use super::JSCompartmental;
use super::compartment::BOUND;
use super::ffi::JSEvaluateErr;
use super::ffi::JSInitializer;
use super::managed::JSManageable;
use super::root::trace_thread_local_roots;

use js::glue::NewCompileOptions;

use js::jsapi;
use js::jsapi::JS::Evaluate2;
use js::jsapi::JS::Handle;
use js::heap::Heap;
use js::jsapi::JSAutoCompartment;
use js::jsapi::JSObject;
use js::jsapi::JS_AddExtraGCRootsTracer;
use js::jsapi::JS_ClearPendingException;
use js::jsapi::JS_DefineFunctions;
use js::jsapi::JS_DefineProperties;
use js::jsapi::JS_GC;
use js::jsapi::JS_GetPendingException;
use js::jsapi::JS_GetReservedSlot;
use js::jsapi::JS_GetRuntime;
use js::jsapi::JS_IsExceptionPending;
use js::jsapi::JS_NewGlobalObject;
use js::jsapi::JS_SetReservedSlot;
use js::jsapi::JS::MutableHandle;
use js::jsapi::JS::Value;

use js::jsval::PrivateValue;
use js::jsval::UndefinedValue;

use js::rust::Runtime;

use libc;

use std::any::TypeId;
use std::cell::RefCell;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::mem;
use std::ptr;
use std::str;

/// The type for JS contexts whose current state is `S`.
pub struct JSContext<S> {
    jsapi_context: *mut jsapi::JSContext,
    global_js_object: *mut Heap<*mut JSObject>,
    global_raw: *mut (),
    auto_compartment: Option<JSAutoCompartment>,
    runtime: Option<Runtime>,
    marker: PhantomData<S>,
}

/// A context state in an initialized compartment `C` with lifetime `'a` and global type `T`.
pub struct Initialized<'a, C, T> (PhantomData<(&'a(), C, T)>);

/// A context state in the middTle of initializing a compartment `C` with lifetime `'a` and global type `T`.
pub struct Initializing<'a, C, T> (PhantomData<(&'a(), C, T)>);

/// A context state that has entered compartment `C` via an object with lifetime `'a` and global type `T`.
/// The previous context state was `S`.
pub struct Entered<'a, C, T, S> (PhantomData<(&'a(), C, T, S)>);

/// A context state for JS contexts owned by Rust.
pub struct Owned (());

/// A context state for callbacks from JS,
pub struct FromJS (());

/// A context state in snapshotted compartment in underlying state `S`,
/// which guarantees that no GC will happen during the lifetime `'a`.
pub struct Snapshotted<'a, S> (PhantomData<(&'a(), S)>);

/// A marker trait for JS contexts in compartment `C`
pub trait InCompartment<C> {}
impl<'a, C, T> InCompartment<C> for Initializing<'a, C, T> {}
impl<'a, C, T> InCompartment<C> for Initialized<'a, C, T> {}
impl<'a, C, T, S> InCompartment<C> for Entered<'a, C, T, S> {}
impl<'a, C, S> InCompartment<C> for Snapshotted<'a, S> where S: InCompartment<C> {}

/// A marker trait for JS contexts that can access native state
pub trait CanAccess {}
impl<'a, C, T> CanAccess for Initialized<'a, C, T> {}
impl CanAccess for FromJS {}
impl CanAccess for Owned {}
impl<'a, C, T, S> CanAccess for Entered<'a, C, T, S> where S: CanAccess {}
impl<'a, S> CanAccess for Snapshotted<'a, S> where S: CanAccess {}

/// A marker trait for JS contexts that are snapshots
pub trait IsSnapshot<'a> {}
impl<'a, S> IsSnapshot<'a> for Snapshotted<'a, S> {}
impl<'a, 'b, C, T, S> IsSnapshot<'a> for Entered<'b, C, T, S> where S: IsSnapshot<'a> {}

/// A marker trait for JS contexts that can (de)allocate objects
pub trait CanAlloc {}
impl<'a, C, T> CanAlloc for Initialized<'a, C, T> {}
impl<'a, C, T> CanAlloc for Initializing<'a, C, T> {}
impl CanAlloc for FromJS {}
impl CanAlloc for Owned {}
impl<'a, C, T, S> CanAlloc for Entered<'a, C, T, S> where S: CanAlloc {}

/// A marker trait for JS contexts that are in the middle of initializing
pub trait IsInitializing<'a, C, T> {}
impl<'a, C, T> IsInitializing<'a, C, T> for Initializing<'a, C, T> {}

/// A marker trait for JS contexts that have initialized a global
pub trait IsInitialized<'a, C, T> {}
impl<'a, C, T> IsInitialized<'a, C, T> for Initialized<'a, C, T> {}

/// A marker trait for JS contexts that have been entered
pub trait IsEntered<'a, C, T> {}
impl<'a, C, T, S> IsEntered<'a, C, T> for Entered<'a, C, T, S> {}

#[cfg(feature = "smup")]
thread_local!{ static RUNTIME: RefCell<Result<Runtime,()>> = RefCell::new(Runtime::new(true)) }
#[cfg(not(feature = "smup"))]
thread_local!{ static RUNTIME: RefCell<Result<Runtime,()>> = RefCell::new(Runtime::new()) }

impl JSContext<Owned> {
    /// Create a new JSContext.
    pub fn new() -> Result<JSContext<Owned>,()> {
        let runtime = RUNTIME.with(|runtime| runtime.replace(Err(())))?;
        let jsapi_context = runtime.cx();
        #[cfg(feature = "smup")]
        unsafe { JS_AddExtraGCRootsTracer(jsapi_context, Some(trace_thread_local_roots), ptr::null_mut()); }
        #[cfg(not(feature = "smup"))]
        unsafe { JS_AddExtraGCRootsTracer(runtime.rt(), Some(trace_thread_local_roots), ptr::null_mut()); }
        Ok(JSContext {
            jsapi_context: jsapi_context,
            global_js_object: ptr::null_mut(),
            global_raw: ptr::null_mut(),
            auto_compartment: None,
            runtime: Some(runtime),
            marker: PhantomData,
        })
    }
}

impl<S> JSContext<S> {
    /// Get a snapshot of the JS state.
    /// The snapshot only allows access to the methods that are guaranteed not to call GC,
    /// so we don't need to root JS-managed pointers during the lifetime of a snapshot.
    pub fn snapshot<'a>(&'a mut self) -> JSContext<Snapshotted<'a, S>> {
        debug!("Creating snapshot.");
        JSContext {
            jsapi_context: self.jsapi_context,
            global_js_object: self.global_js_object,
            global_raw: self.global_raw,
            auto_compartment: None,
            runtime: None,
            marker: PhantomData,
        }
    }

    /// Enter a known compartment.
    pub fn enter_known_compartment<'a, 'b, C, T>(&'a mut self, managed: JSManaged<'b, C, T>) -> JSContext<Entered<'a, C, T::Aged, S>> where
        T: JSLifetime<'a>,
        C: Compartment,
    {
        debug!("Entering compartment.");
        #[allow(unused_unsafe)]
        let ac = unsafe { JSAutoCompartment::new(self.jsapi_context, managed.to_jsobject()) };
        JSContext {
            jsapi_context: self.jsapi_context,
            global_js_object: managed.to_heap_object(),
            global_raw: managed.to_raw_native() as *mut (),
            auto_compartment: Some(ac),
            runtime: None,
            marker: PhantomData,
        }
    }

    /// Enter a compartment.
    pub fn enter_unknown_compartment<'a, 'b, C, T>(&'a mut self, managed: JSManaged<'b, C, T>) -> JSContext<Entered<'a, BOUND<'a>, T::Aged, S>> where
        T: JSLifetime<'a>,
    {
        debug!("Entering compartment.");
        #[allow(unused_unsafe)]
        let ac = unsafe { JSAutoCompartment::new(self.jsapi_context, managed.to_jsobject()) };
        JSContext {
            jsapi_context: self.jsapi_context,
            global_js_object: managed.to_heap_object(),
            global_raw: managed.to_raw_native() as *mut (),
            auto_compartment: Some(ac),
            runtime: None,
            marker: PhantomData,
        }
    }

    /// Give ownership of data to JS.
    /// This allocates JS heap, which may trigger GC.
    pub fn manage<'a, C, T>(&'a mut self, value: T) -> JSManaged<'a, C, T::Aged> where
        S: CanAlloc + InCompartment<C>,
        C: Compartment,
        T: JSTraceable + JSInitializable + JSLifetime<'a>,
    {
        JSManaged::new(self, value)
    }

    /// Give ownership of data to JS.
    /// This allocates JS heap, which may trigger GC.
    pub fn snapshot_manage<'a, C, T>(&'a mut self, value: T) -> (JSContext<Snapshotted<'a, S>>, JSManaged<'a, C, T::Aged>) where
        S: CanAlloc + InCompartment<C>,
        C: Compartment,
        T: JSTraceable + JSInitializable + JSLifetime<'a>,
    {
        let jsapi_context = self.jsapi_context;
        let global_js_object = self.global_js_object;
        let global_raw =  self.global_raw;
        let managed = self.manage(value);

        debug!("Creating snapshot while managing.");
        let snapshot = JSContext {
            jsapi_context: jsapi_context,
            global_js_object: global_js_object,
            global_raw: global_raw,
            auto_compartment: None,
            runtime: None,
            marker: PhantomData,
        };
        (snapshot, managed)
    }

    /// Create a compartment
    pub fn create_compartment<'a, T>(&'a mut self) -> JSContext<Initializing<'a, BOUND<'a>, T>> where
        S: CanAlloc + CanAccess,
        T: JSInitializable + JSTraceable,
    {
        debug!("Creating compartment.");
        let value: Option<T> = None;
        let boxed_value: Box<JSManageable> = Box::new(value);
        let fat_value: [*const libc::c_void; 2] = unsafe { mem::transmute(boxed_value) };

        let classp = unsafe { T::Init::global_classp() };
        let principals = unsafe { T::Init::global_principals() };
        let hook_options = unsafe { T::Init::global_hook_option() };
        let options = unsafe { T::Init::global_options() };
        let properties = unsafe { T::Init::properties() };
        let functions = unsafe { T::Init::functions() };

        let boxed_jsobject = Box::new(Heap::default());
        debug!("Boxed global {:p}", boxed_jsobject);
        let unboxed_jsobject = unsafe { JS_NewGlobalObject(self.jsapi_context, classp, principals, hook_options, &options) };
        debug!("Unboxed global {:p}", unboxed_jsobject);
        assert!(!unboxed_jsobject.is_null());
        boxed_jsobject.set(unboxed_jsobject);

        // TODO: can we be sure that this won't trigger GC? Or do we need to root the boxed object?
        debug!("Entering compartment.");
        #[allow(unused_unsafe)]
        let ac = unsafe { JSAutoCompartment::new(self.jsapi_context, boxed_jsobject.get()) };

        // Keep a hash map of all the class prototypes
        let prototypes: Box<HashMap<TypeId, Box<Heap<*mut JSObject>>>> = Box::new(HashMap::new());

        // Save a pointer to the native value in a private slot
        #[cfg(feature = "smup")]
        unsafe {
            JS_SetReservedSlot(boxed_jsobject.get(), 0, &PrivateValue(fat_value[0]));
            JS_SetReservedSlot(boxed_jsobject.get(), 1, &PrivateValue(fat_value[1]));
            // TODO: Fix this space leak!
            JS_SetReservedSlot(boxed_jsobject.get(), 2, &PrivateValue(Box::into_raw(prototypes) as *const _));
        }
        #[cfg(not(feature = "smup"))]
        unsafe {
            JS_SetReservedSlot(boxed_jsobject.get(), 0, PrivateValue(fat_value[0]));
            JS_SetReservedSlot(boxed_jsobject.get(), 1, PrivateValue(fat_value[1]));
            // TODO: Fix this space leak!
            JS_SetReservedSlot(boxed_jsobject.get(), 2, PrivateValue(Box::into_raw(prototypes) as *const _));
        }

        // Define the properties and functions of the global
        if !properties.is_null() { unsafe { JS_DefineProperties(self.jsapi_context, boxed_jsobject.handle(), properties) }; }
        if !functions.is_null() { unsafe { JS_DefineFunctions(self.jsapi_context, boxed_jsobject.handle(), functions) }; }

        // TODO: can we be sure that this won't trigger GC? Or do we need to root the boxed object?
        debug!("Initializing compartment.");
        unsafe { T::Init::js_init_global(self.jsapi_context, boxed_jsobject.handle()) };

        debug!("Created compartment.");
        JSContext {
            jsapi_context: self.jsapi_context,
            global_js_object: Box::into_raw(boxed_jsobject),
            global_raw: fat_value[0] as *mut (),
            auto_compartment: Some(ac),
            runtime: None,
            marker: PhantomData,
        }
    }

    /// Finish initializing a JS Context
    pub fn global_manage<'a, C, T>(mut self, value: T) -> JSContext<Initialized<'a, C, T::Aged>> where
        S: IsInitializing<'a, C, T>,
        T: JSTraceable + JSLifetime<'a> + JSCompartmental<C, C, ChangeCompartment = T>,
    {
        debug!("Managing native global.");
        let raw = self.global_raw as *mut Option<T>;
        let uninitialized = unsafe { mem::replace(&mut *raw, Some(value)) };
        mem::forget(uninitialized);

        debug!("Initialized compartment.");
        JSContext {
            jsapi_context: self.jsapi_context,
            global_js_object: self.global_js_object,
            global_raw: self.global_raw,
            auto_compartment: self.auto_compartment.take(),
            runtime: self.runtime.take(),
            marker: PhantomData,
        }
    }

    /// Get the object we entered the current compartment via
    pub fn entered<'a, C, T>(&self) -> JSManaged<'a, C, T> where
        S: IsEntered<'a, C, T>
    {
        unsafe { JSManaged::from_raw(self.global_js_object, self.global_raw) }
    }

    /// Get the global of an initialized context.
    pub fn global<'a, C, T>(&self) -> JSManaged<'a, C, T> where
        S: IsInitialized<'a, C, T>
    {
        unsafe { JSManaged::from_raw(self.global_js_object, self.global_raw) }
    }

    /// Create a new root.
    pub fn new_root<'a, 'b, T>(&'b mut self) -> JSRoot<'a, T> {
        JSRoot::new(self)
    }

    pub fn cx(&self) -> *mut jsapi::JSContext {
        self.jsapi_context
    }

    pub fn rt(&self) -> *mut jsapi::JSRuntime {
        unsafe { JS_GetRuntime(self.jsapi_context) }
    }

    pub fn gc(&mut self) where
        S: CanAlloc,
    {
        #[cfg(feature = "smup")]
        unsafe { JS_GC(self.cx()); }
        #[cfg(not(feature = "smup"))]
        unsafe { JS_GC(self.rt()); }
    }

    pub fn prototype_of<T>(&mut self) -> Handle<*mut JSObject> where
        T: JSInitializable,
    {
        let global = unsafe { &*self.global_js_object }.handle();
        let prototypes = unsafe { &mut *(JS_GetReservedSlot(global.get(), 2).to_private() as *mut HashMap<TypeId, Box<Heap<*mut JSObject>>>) };
        prototypes.entry(TypeId::of::<T::Init>()).or_insert_with(|| {
            let boxed = Box::new(Heap::default());
            boxed.set(unsafe { T::Init::js_init_class(self.jsapi_context, global) });
            boxed
        }).handle()
    }

    pub fn evaluate<C>(&mut self, code: &str) -> Result<Value, JSEvaluateErr> where
        S: InCompartment<C>,
    {
        let cx = self.jsapi_context;

        let options = unsafe { NewCompileOptions(cx, &0, 0) };

        let code_utf16: Vec<u16> = code.encode_utf16().collect();
        let code_ptr = &code_utf16[0] as *const u16;
        let code_len = code_utf16.len() as usize;

        let mut result = UndefinedValue();
        let result_mut = unsafe { MutableHandle::from_marked_location(&mut result) };

        unsafe { Evaluate2(cx, options, code_ptr, code_len, result_mut) };
        self.take_pending_exception()?;

        Ok(result)
    }

    pub fn take_pending_exception(&mut self) -> Result<(), JSEvaluateErr> {
        let cx = self.jsapi_context;
        if !unsafe { JS_IsExceptionPending(cx) } { return Ok(()); }

        let ref mut exn_ref = UndefinedValue();
        let exn_mut = unsafe { MutableHandle::from_marked_location(exn_ref) };
        let _exn_handle = exn_mut.handle();

        unsafe { JS_GetPendingException(cx, exn_mut) };
        // TODO: include the exception in the error report
        unsafe { JS_ClearPendingException(cx) };

        Err(JSEvaluateErr::JSException)
    }
}

pub unsafe fn jscontext_called_from_js(cx: *mut jsapi::JSContext) -> JSContext<FromJS> {
    JSContext {
        jsapi_context: cx,
        global_js_object: ptr::null_mut(),
        global_raw: ptr::null_mut(),
        auto_compartment: None,
        runtime: None,
        marker: PhantomData,
    }
}