/* 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 dom::bindings::utils::{Reflector, Reflectable}; use dom::window::Window; use js::jsapi::JSContext; use layout_interface::TrustedNodeAddress; use std::cast; use std::cell::RefCell; pub struct JS { priv ptr: RefCell<*mut T> } impl Eq for JS { fn eq(&self, other: &JS) -> bool { self.ptr == other.ptr } } impl Clone for JS { fn clone(&self) -> JS { JS { ptr: self.ptr.clone() } } } impl JS { pub fn new(obj: ~T, window: &JS, wrap_fn: extern "Rust" fn(*JSContext, &JS, ~T) -> JS) -> JS { wrap_fn(window.get().get_cx(), window, obj) } pub unsafe fn from_raw(raw: *mut T) -> JS { JS { ptr: RefCell::new(raw) } } pub unsafe fn from_trusted_node_address(inner: TrustedNodeAddress) -> JS { JS { ptr: RefCell::new(inner as *mut T) } } } impl Reflectable for JS { fn reflector<'a>(&'a self) -> &'a Reflector { self.get().reflector() } fn mut_reflector<'a>(&'a mut self) -> &'a mut Reflector { self.get_mut().mut_reflector() } } impl JS { pub fn get<'a>(&'a self) -> &'a T { let borrowed = self.ptr.borrow(); unsafe { &(**borrowed.get()) } } pub fn get_mut<'a>(&'a mut self) -> &'a mut T { let mut borrowed = self.ptr.borrow_mut(); unsafe { &mut (**borrowed.get()) } } /// Returns an unsafe pointer to the interior of this JS object without touching the borrow /// flags. This is the only method that be safely accessed from layout. (The fact that this /// is unsafe is what necessitates the layout wrappers.) pub unsafe fn unsafe_get(&self) -> *mut T { cast::transmute_copy(&self.ptr) } } impl JS { //XXXjdm It would be lovely if this could be private. pub unsafe fn transmute(self) -> JS { cast::transmute(self) } pub unsafe fn transmute_copy(&self) -> JS { cast::transmute_copy(self) } }