Add way to get c_void ptr or c_char from caller

This commit is contained in:
mckaymatt 2017-04-02 18:37:27 -04:00
parent 41e712786e
commit 63f9ad022f
5 changed files with 32 additions and 10 deletions

View file

@ -8,6 +8,7 @@ use dom::bindings::error::{Error, Fallible, report_pending_exception};
use dom::bindings::js::{JS, Root};
use dom::bindings::reflector::DomObject;
use dom::bindings::settings_stack::{AutoEntryScript, AutoIncumbentScript};
use dom::bindings::utils::AsCCharPtrPtr;
use dom::globalscope::GlobalScope;
use js::jsapi::{Heap, MutableHandleObject};
use js::jsapi::{IsCallable, JSContext, JSObject, JS_WrapObject, AddRawValueRoot};
@ -81,7 +82,7 @@ impl CallbackObject {
self.callback.set(callback);
self.permanent_js_root.set(ObjectValue(callback));
assert!(AddRawValueRoot(cx, self.permanent_js_root.get_unsafe(),
b"CallbackObject::root\n" as *const _ as *const _));
b"CallbackObject::root\n".as_c_char_ptr()));
}
}

View file

@ -3086,7 +3086,7 @@ let traps = ProxyTraps {
isConstructor: None,
};
CreateProxyHandler(&traps, &Class as *const _ as *const _)\
CreateProxyHandler(&traps, Class.as_void_ptr())\
""" % args)
@ -5565,6 +5565,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'dom::bindings::namespace::create_namespace_object',
'dom::bindings::reflector::MutDomObject',
'dom::bindings::reflector::DomObject',
'dom::bindings::utils::AsVoidPtr',
'dom::bindings::utils::DOMClass',
'dom::bindings::utils::DOMJSClass',
'dom::bindings::utils::DOM_PROTO_UNFORGEABLE_HOLDER_SLOT',

View file

@ -33,7 +33,7 @@ use js::jsval::{JSVal, UndefinedValue};
use js::rust::{GCMethods, ToString, get_object_class, is_dom_class};
use libc;
use std::ffi::CString;
use std::os::raw::c_void;
use std::os::raw::{c_char, c_void};
use std::ptr;
use std::slice;
@ -513,3 +513,24 @@ unsafe extern "C" fn instance_class_has_proto_at_depth(clasp: *const js::jsapi::
pub const DOM_CALLBACKS: DOMCallbacks = DOMCallbacks {
instanceClassMatchesProto: Some(instance_class_has_proto_at_depth),
};
// Generic method for returning libc::c_void from caller
pub trait AsVoidPtr {
fn as_void_ptr(&self) -> *const libc::c_void;
}
impl<T> AsVoidPtr for T {
fn as_void_ptr(&self) -> *const libc::c_void {
self as *const T as *const libc::c_void
}
}
// Generic method for returning c_char from caller
pub trait AsCCharPtrPtr {
fn as_c_char_ptr(&self) -> *const c_char;
}
impl AsCCharPtrPtr for [u8] {
fn as_c_char_ptr(&self) -> *const c_char {
self as *const [u8] as *const c_char
}
}