Return a JS<T> from *Binding::Wrap rather than a JSObject.

This lets us avoid the sketchy tricks in JS::new and Window::new, where we
kept an unsafe pointer to the native object across the Wrap call that
consumed the owned pointer.
This commit is contained in:
Ms2ger 2014-03-14 13:06:48 +01:00
parent 71f4fd0478
commit 4ad3b6ccd1
6 changed files with 30 additions and 43 deletions

View file

@ -4,7 +4,7 @@
use dom::bindings::utils::{Reflector, Reflectable};
use dom::window::Window;
use js::jsapi::{JSContext, JSObject};
use js::jsapi::JSContext;
use layout_interface::TrustedNodeAddress;
use std::cast;
@ -30,17 +30,10 @@ impl <T> Clone for JS<T> {
}
impl<T: Reflectable> JS<T> {
pub fn new(mut obj: ~T,
pub fn new(obj: ~T,
window: &JS<Window>,
wrap_fn: extern "Rust" fn(*JSContext, &JS<Window>, ~T) -> *JSObject) -> JS<T> {
let cx = window.get().get_cx();
let raw: *mut T = &mut *obj;
if wrap_fn(cx, window, obj).is_null() {
fail!("Could not eagerly wrap object");
}
JS {
ptr: RefCell::new(raw)
}
wrap_fn: extern "Rust" fn(*JSContext, &JS<Window>, ~T) -> JS<T>) -> JS<T> {
wrap_fn(window.get().get_cx(), window, obj)
}
pub unsafe fn from_raw(raw: *mut T) -> JS<T> {