aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/script/dom/bindings/callback.rs
blob: 1633ebb241149a1aa532107a768a0db18bf4c1d2 (plain) (blame)
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
/* 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::Reflectable;
use js::jsapi::{JSContext, JSObject, JS_WrapObject, JS_ObjectIsCallable};
use js::jsapi::{JS_GetProperty, JSTracer, JS_CallTracer};
use js::jsval::{JSVal, UndefinedValue};
use js::JSTRACE_OBJECT;

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

use serialize::{Encodable, Encoder};

pub enum ExceptionHandling {
    // Report any exception and don't throw it to the caller code.
    ReportExceptions,
    // Throw an exception to the caller code if the thrown exception is a
    // binding object for a DOMError from the caller's scope, otherwise report
    // it.
    RethrowContentExceptions,
    // Throw any exception to the caller code.
    RethrowExceptions
}

#[deriving(Clone,Eq)]
pub struct CallbackInterface {
    pub callback: *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);
            });
        };
        Ok(())
    }
}

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

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

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

    pub fn GetCallableProperty(&self, cx: *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 {
                return Err(());
            }

            if !callable.is_object() ||
               JS_ObjectIsCallable(cx, callable.to_object()) == 0 {
                //ThrowErrorMessage(cx, MSG_NOT_CALLABLE, description.get());
                return Err(());
            }
        }
        Ok(callable)
    }
}

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

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

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

    return obj;
}

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

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

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