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
|
/* 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/. */
//! Base classes to work with IDL callbacks.
use dom::bindings::global::global_object_for_js_object;
use dom::bindings::js::JSRef;
use dom::bindings::utils::Reflectable;
use js::jsapi::{JSContext, JSObject, JS_WrapObject, JS_ObjectIsCallable, JS_GetGlobalObject};
use js::jsapi::{JS_GetProperty, JS_IsExceptionPending, JS_ReportPendingException};
use js::jsval::{JSVal, UndefinedValue};
use js::rust::with_compartment;
use std::ffi::CString;
use std::ptr;
/// The exception handling used for a call.
#[derive(Copy, PartialEq)]
pub enum ExceptionHandling {
/// Report any exception and don't throw it to the caller code.
Report,
/// Throw any exception to the caller code.
Rethrow
}
/// A common base class for representing IDL callback function types.
#[derive(Copy, Clone,PartialEq)]
#[jstraceable]
pub struct CallbackFunction {
object: CallbackObject
}
impl CallbackFunction {
/// Create a new `CallbackFunction` for this object.
pub fn new(callback: *mut JSObject) -> CallbackFunction {
CallbackFunction {
object: CallbackObject {
callback: callback
}
}
}
}
/// A common base class for representing IDL callback interface types.
#[derive(Copy, Clone,PartialEq)]
#[jstraceable]
pub struct CallbackInterface {
object: CallbackObject
}
/// A common base class for representing IDL callback function and
/// callback interface types.
#[allow(raw_pointer_derive)]
#[derive(Copy, Clone,PartialEq)]
#[jstraceable]
struct CallbackObject {
/// The underlying `JSObject`.
callback: *mut JSObject,
}
/// A trait to be implemented by concrete IDL callback function and
/// callback interface types.
pub trait CallbackContainer {
/// Create a new CallbackContainer object for the given `JSObject`.
fn new(callback: *mut JSObject) -> Self;
/// Returns the underlying `JSObject`.
fn callback(&self) -> *mut JSObject;
}
impl CallbackInterface {
/// Returns the underlying `JSObject`.
pub fn callback(&self) -> *mut JSObject {
self.object.callback
}
}
impl CallbackFunction {
/// Returns the underlying `JSObject`.
pub fn callback(&self) -> *mut JSObject {
self.object.callback
}
}
impl CallbackInterface {
/// Create a new CallbackInterface object for the given `JSObject`.
pub fn new(callback: *mut JSObject) -> CallbackInterface {
CallbackInterface {
object: CallbackObject {
callback: callback
}
}
}
/// Returns the property with the given `name`, if it is a callable object,
/// or `Err(())` otherwise. If it returns `Err(())`, a JSAPI exception is
/// pending.
pub fn get_callable_property(&self, cx: *mut JSContext, name: &str)
-> Result<JSVal, ()> {
let mut callable = UndefinedValue();
unsafe {
let name = CString::from_slice(name.as_bytes());
if JS_GetProperty(cx, self.callback(), name.as_ptr(), &mut callable) == 0 {
return Err(());
}
if !callable.is_object() ||
JS_ObjectIsCallable(cx, callable.to_object()) == 0 {
// FIXME(#347)
//ThrowErrorMessage(cx, MSG_NOT_CALLABLE, description.get());
return Err(());
}
}
Ok(callable)
}
}
/// Wraps the reflector for `p` into the compartment of `cx`.
pub fn wrap_call_this_object<T: Reflectable>(cx: *mut JSContext,
p: JSRef<T>) -> *mut JSObject {
let mut obj = p.reflector().get_jsobject();
assert!(!obj.is_null());
unsafe {
if JS_WrapObject(cx, &mut obj) == 0 {
return ptr::null_mut();
}
}
return obj;
}
/// A class that performs whatever setup we need to safely make a call while
/// this class is on the stack. After `new` returns, the call is safe to make.
pub struct CallSetup {
/// The `JSContext` used for the call.
cx: *mut JSContext,
/// The exception handling used for the call.
_handling: ExceptionHandling,
}
impl CallSetup {
/// Performs the setup needed to make a call.
#[allow(unrooted_must_root)]
pub fn new<T: CallbackContainer>(callback: T, handling: ExceptionHandling) -> CallSetup {
let global = global_object_for_js_object(callback.callback());
let global = global.root();
let cx = global.r().get_cx();
CallSetup {
cx: cx,
_handling: handling,
}
}
/// Returns the `JSContext` used for the call.
pub fn get_context(&self) -> *mut JSContext {
self.cx
}
}
impl Drop for CallSetup {
fn drop(&mut self) {
let need_to_deal_with_exception = unsafe { JS_IsExceptionPending(self.cx) } != 0;
if need_to_deal_with_exception {
unsafe {
let old_global = JS_GetGlobalObject(self.cx);
with_compartment(self.cx, old_global, || {
JS_ReportPendingException(self.cx)
});
}
}
}
}
|