Implement FromJSValConvertible for JS<T>.

This commit is contained in:
Ms2ger 2014-03-31 12:13:54 +02:00
parent 6720b8110a
commit ac46cc6047
3 changed files with 55 additions and 7 deletions

View file

@ -1920,6 +1920,32 @@ class CGWrapMethod(CGAbstractMethod):
raw.mut_reflector().set_jsobject(obj);
return raw;""" % CreateBindingJSObject(self.descriptor)
class CGIDLInterface(CGThing):
"""
Class for codegen of an implementation of the IDLInterface trait.
"""
def __init__(self, descriptor):
CGThing.__init__(self)
self.descriptor = descriptor
def define(self):
replacer = {
'type': self.descriptor.name,
'depth': self.descriptor.interface.inheritanceDepth(),
}
return string.Template("""
impl IDLInterface for ${type} {
fn get_prototype_id(_: Option<${type}>) -> PrototypeList::id::ID {
PrototypeList::id::${type}
}
fn get_prototype_depth(_: Option<${type}>) -> uint {
${depth}
}
}
""").substitute(replacer)
class CGAbstractExternMethod(CGAbstractMethod):
"""
Abstract base class for codegen of implementation-only (no
@ -4080,6 +4106,8 @@ class CGDescriptor(CGThing):
cgThings.append(CGWrapMethod(descriptor))
cgThings.append(CGIDLInterface(descriptor))
cgThings = CGList(cgThings, "\n")
cgThings = CGWrapper(cgThings, pre='\n', post='\n')
#self.cgRoot = CGWrapper(CGNamespace(toBindingNamespace(descriptor.name),
@ -4496,6 +4524,7 @@ class CGBindingRoot(CGThing):
'dom::bindings::callback::{CallSetup,ExceptionHandling}',
'dom::bindings::callback::{WrapCallThisObject}',
'dom::bindings::conversions::{FromJSValConvertible, ToJSValConvertible}',
'dom::bindings::conversions::IDLInterface',
'dom::bindings::conversions::{Default, Empty}',
'dom::bindings::codegen::*',
'dom::bindings::codegen::UnionTypes::*',

View file

@ -5,6 +5,7 @@
use dom::bindings::js::JS;
use dom::bindings::utils::Reflectable;
use dom::bindings::utils::jsstring_to_str;
use dom::bindings::utils::unwrap_jsmanaged;
use servo_util::str::DOMString;
use js::jsapi::{JSBool, JSContext};
@ -20,6 +21,15 @@ use js::glue::RUST_JS_NumberValue;
use std::default::Default;
use std::libc;
use dom::bindings::codegen::PrototypeList;
// FIXME (https://github.com/rust-lang/rfcs/pull/4)
// remove Option<Self> arguments.
pub trait IDLInterface {
fn get_prototype_id(_: Option<Self>) -> PrototypeList::id::ID;
fn get_prototype_depth(_: Option<Self>) -> uint;
}
pub trait ToJSValConvertible {
fn to_jsval(&self, cx: *JSContext) -> JSVal;
}
@ -255,6 +265,17 @@ impl<T: Reflectable> ToJSValConvertible for JS<T> {
}
}
impl<T: Reflectable+IDLInterface> FromJSValConvertible<()> for JS<T> {
fn from_jsval(_cx: *JSContext, value: JSVal, _option: ()) -> Result<JS<T>, ()> {
if !value.is_object() {
return Err(());
}
unwrap_jsmanaged(value.to_object(),
IDLInterface::get_prototype_id(None::<T>),
IDLInterface::get_prototype_depth(None::<T>))
}
}
impl<T: ToJSValConvertible> ToJSValConvertible for Option<T> {
fn to_jsval(&self, cx: *JSContext) -> JSVal {
match self {

View file

@ -4,6 +4,7 @@
use dom::bindings::codegen::PrototypeList;
use dom::bindings::codegen::PrototypeList::MAX_PROTO_CHAIN_LENGTH;
use dom::bindings::conversions::FromJSValConvertible;
use dom::bindings::js::JS;
use dom::window;
use servo_util::str::DOMString;
@ -34,8 +35,8 @@ use js::jsapi::{JSString};
use js::jsapi::{JS_AllowGC, JS_InhibitGC};
use js::jsfriendapi::bindgen::JS_NewObjectWithUniqueType;
use js::jsval::JSVal;
use js::jsval::{PrivateValue, ObjectValue, NullValue, Int32Value};
use js::jsval::{UInt32Value, DoubleValue, BooleanValue, UndefinedValue};
use js::jsval::{PrivateValue, ObjectValue, NullValue, ObjectOrNullValue};
use js::jsval::{Int32Value, UInt32Value, DoubleValue, BooleanValue, UndefinedValue};
use js::{JSPROP_ENUMERATE, JSCLASS_IS_GLOBAL, JSCLASS_IS_DOMJSCLASS};
use js::JSPROP_PERMANENT;
use js::{JSFUN_CONSTRUCTOR, JSPROP_READONLY};
@ -532,11 +533,8 @@ pub fn global_object_for_js_object(obj: *JSObject) -> JS<window::Window> {
let global = GetGlobalForObjectCrossCompartment(obj);
let clasp = JS_GetClass(global);
assert!(((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)) != 0);
// FIXME(jdm): Either don't hardcode or sanity assert prototype stuff.
match unwrap_object(global, PrototypeList::id::Window, 1) {
Ok(win) => JS::from_raw(win),
Err(_) => fail!("found DOM global that doesn't unwrap to Window"),
}
FromJSValConvertible::from_jsval(ptr::null(), ObjectOrNullValue(global), ())
.ok().expect("found DOM global that doesn't unwrap to Window")
}
}