Skip to content

Commit

Permalink
auto merge of #2487 : Ms2ger/servo/sm-mut, r=jdm
Browse files Browse the repository at this point in the history
  • Loading branch information
bors-servo committed May 26, 2014
2 parents ea00ee8 + 51d2891 commit f057719
Show file tree
Hide file tree
Showing 18 changed files with 293 additions and 307 deletions.
46 changes: 20 additions & 26 deletions src/components/script/dom/bindings/callback.rs
Expand Up @@ -2,13 +2,12 @@
* 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::trace::trace_object;
use dom::bindings::utils::Reflectable;
use js::jsapi::{JSContext, JSObject, JS_WrapObject, JS_ObjectIsCallable};
use js::jsapi::{JS_GetProperty, JSTracer, JS_CallTracer};
use js::jsapi::{JS_GetProperty, JSTracer};
use js::jsval::{JSVal, UndefinedValue};
use js::JSTRACE_OBJECT;

use libc;
use std::cast;
use std::ptr;

Expand All @@ -27,45 +26,40 @@ pub enum ExceptionHandling {

#[deriving(Clone,Eq)]
pub struct CallbackInterface {
pub callback: *JSObject
pub callback: *mut JSObject
}

impl<S: Encoder<E>, E> Encodable<S, E> for CallbackInterface {
fn encode(&self, s: &mut S) -> Result<(), E> {
unsafe {
let tracer: *mut JSTracer = cast::transmute(s);
"callback".to_c_str().with_ref(|name| {
(*tracer).debugPrinter = ptr::null();
(*tracer).debugPrintIndex = -1;
(*tracer).debugPrintArg = name as *libc::c_void;
JS_CallTracer(tracer as *JSTracer, self.callback, JSTRACE_OBJECT as u32);
});
};
trace_object(tracer, "callback", self.callback);
}
Ok(())
}
}

pub trait CallbackContainer {
fn callback(&self) -> *JSObject;
fn callback(&self) -> *mut JSObject;
}

impl CallbackContainer for CallbackInterface {
fn callback(&self) -> *JSObject {
fn callback(&self) -> *mut JSObject {
self.callback
}
}

impl CallbackInterface {
pub fn new(callback: *JSObject) -> CallbackInterface {
pub fn new(callback: *mut JSObject) -> CallbackInterface {
CallbackInterface {
callback: callback
}
}

pub fn GetCallableProperty(&self, cx: *JSContext, name: &str) -> Result<JSVal, ()> {
pub fn GetCallableProperty(&self, cx: *mut JSContext, name: &str) -> Result<JSVal, ()> {
let mut callable = UndefinedValue();
unsafe {
if name.to_c_str().with_ref(|name| JS_GetProperty(cx, self.callback, name, &mut callable as *mut JSVal as *JSVal)) == 0 {
if name.to_c_str().with_ref(|name| JS_GetProperty(cx, self.callback, name, &mut callable)) == 0 {
return Err(());
}

Expand All @@ -79,39 +73,39 @@ impl CallbackInterface {
}
}

pub fn GetJSObjectFromCallback<T: CallbackContainer>(callback: &T) -> *JSObject {
pub fn GetJSObjectFromCallback<T: CallbackContainer>(callback: &T) -> *mut JSObject {
callback.callback()
}

pub fn WrapCallThisObject<T: 'static + CallbackContainer + Reflectable>(cx: *JSContext,
_scope: *JSObject,
p: Box<T>) -> *JSObject {
let obj = GetJSObjectFromCallback(p);
pub fn WrapCallThisObject<T: 'static + CallbackContainer + Reflectable>(cx: *mut JSContext,
_scope: *mut JSObject,
p: Box<T>) -> *mut JSObject {
let mut obj = GetJSObjectFromCallback(p);
assert!(obj.is_not_null());

unsafe {
if JS_WrapObject(cx, &obj) == 0 {
return ptr::null();
if JS_WrapObject(cx, &mut obj) == 0 {
return ptr::mut_null();
}
}

return obj;
}

pub struct CallSetup {
pub cx: *JSContext,
pub cx: *mut JSContext,
pub handling: ExceptionHandling
}

impl CallSetup {
pub fn new(cx: *JSContext, handling: ExceptionHandling) -> CallSetup {
pub fn new(cx: *mut JSContext, handling: ExceptionHandling) -> CallSetup {
CallSetup {
cx: cx,
handling: handling
}
}

pub fn GetContext(&self) -> *JSContext {
pub fn GetContext(&self) -> *mut JSContext {
self.cx
}
}

0 comments on commit f057719

Please sign in to comment.