Replace NonZero<*mut JSObject> with a wrapper to enable local trait impls.

This commit is contained in:
Simon Sapin 2017-10-14 12:54:57 +02:00
parent 115d859551
commit e2fafd2dfc
22 changed files with 108 additions and 69 deletions

View file

@ -1407,7 +1407,7 @@ def getRetvalDeclarationForType(returnType, descriptorProvider):
if returnType.isAny():
return CGGeneric("JSVal")
if returnType.isObject() or returnType.isSpiderMonkeyInterface():
result = CGGeneric("NonZero<*mut JSObject>")
result = CGGeneric("NonNullJSObjectPtr")
if returnType.nullable():
result = CGWrapper(result, pre="Option<", post=">")
return result
@ -2253,6 +2253,7 @@ def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config):
'dom::bindings::conversions::StringificationBehavior',
'dom::bindings::conversions::root_from_handlevalue',
'dom::bindings::error::throw_not_in_union',
'dom::bindings::nonnull::NonNullJSObjectPtr',
'dom::bindings::mozmap::MozMap',
'dom::bindings::root::DomRoot',
'dom::bindings::str::ByteString',
@ -5785,6 +5786,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'dom::bindings::proxyhandler::get_expando_object',
'dom::bindings::proxyhandler::get_property_descriptor',
'dom::bindings::mozmap::MozMap',
'dom::bindings::nonnull::NonNullJSObjectPtr',
'dom::bindings::num::Finite',
'dom::bindings::str::ByteString',
'dom::bindings::str::DOMString',

View file

@ -34,6 +34,7 @@
use dom::bindings::error::{Error, Fallible};
use dom::bindings::inheritance::Castable;
use dom::bindings::nonnull::NonNullJSObjectPtr;
use dom::bindings::num::Finite;
use dom::bindings::reflector::{DomObject, Reflector};
use dom::bindings::root::DomRoot;
@ -53,7 +54,7 @@ use js::jsapi::{JS_GetLatin1StringCharsAndLength, JS_GetProperty, JS_GetReserved
use js::jsapi::{JS_GetTwoByteStringCharsAndLength, JS_IsArrayObject, JS_IsExceptionPending};
use js::jsapi::{JS_NewStringCopyN, JS_StringHasLatin1Chars, MutableHandleValue};
use js::jsval::{ObjectValue, StringValue, UndefinedValue};
use js::rust::{ToString, get_object_class, is_dom_class, is_dom_object, maybe_wrap_value};
use js::rust::{ToString, get_object_class, is_dom_class, is_dom_object, maybe_wrap_value, maybe_wrap_object_value};
use libc;
use num_traits::Float;
use servo_config::opts;
@ -69,6 +70,15 @@ pub trait IDLInterface {
#[rustc_on_unimplemented = "The IDL interface `{Self}` is not derived from `{T}`."]
pub trait DerivedFrom<T: Castable>: Castable {}
// https://heycam.github.io/webidl/#es-object
impl ToJSValConvertible for NonNullJSObjectPtr {
#[inline]
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
rval.set(ObjectValue(self.get()));
maybe_wrap_object_value(cx, rval);
}
}
impl<T: Float + ToJSValConvertible> ToJSValConvertible for Finite<T> {
#[inline]
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {

View file

@ -6,17 +6,17 @@
//! Implementation of `iterable<...>` and `iterable<..., ...>` WebIDL declarations.
use core::nonzero::NonZero;
use dom::bindings::codegen::Bindings::IterableIteratorBinding::IterableKeyAndValueResult;
use dom::bindings::codegen::Bindings::IterableIteratorBinding::IterableKeyOrValueResult;
use dom::bindings::error::Fallible;
use dom::bindings::nonnull::NonNullJSObjectPtr;
use dom::bindings::reflector::{DomObject, Reflector, reflect_dom_object};
use dom::bindings::root::{Dom, DomRoot};
use dom::bindings::trace::JSTraceable;
use dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
use js::conversions::ToJSValConvertible;
use js::jsapi::{HandleValue, Heap, JSContext, JSObject, MutableHandleObject};
use js::jsapi::{HandleValue, Heap, JSContext, MutableHandleObject};
use js::jsval::UndefinedValue;
use std::cell::Cell;
use std::ptr;
@ -73,7 +73,7 @@ impl<T: DomObject + JSTraceable + Iterable> IterableIterator<T> {
/// Return the next value from the iterable object.
#[allow(non_snake_case)]
pub fn Next(&self, cx: *mut JSContext) -> Fallible<NonZero<*mut JSObject>> {
pub fn Next(&self, cx: *mut JSContext) -> Fallible<NonNullJSObjectPtr> {
let index = self.index.get();
rooted!(in(cx) let mut value = UndefinedValue());
rooted!(in(cx) let mut rval = ptr::null_mut());
@ -106,7 +106,7 @@ impl<T: DomObject + JSTraceable + Iterable> IterableIterator<T> {
self.index.set(index + 1);
result.map(|_| {
assert!(!rval.is_null());
unsafe { NonZero::new_unchecked(rval.get()) }
unsafe { NonNullJSObjectPtr::new_unchecked(rval.get()) }
})
}
}

View file

@ -144,6 +144,7 @@ pub mod interface;
pub mod iterable;
pub mod mozmap;
pub mod namespace;
pub mod nonnull;
pub mod num;
pub mod proxyhandler;
pub mod refcounted;

View file

@ -0,0 +1,24 @@
/* 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/. */
//! A wrapper type for `NonZero<*mut JSObject>`, to enable local trait impls
use js::jsapi::JSObject;
use nonzero::NonZero;
/// A wrapper type for `NonZero<*mut JSObject>`, to enable local trait impls
#[derive(Clone, Copy)]
pub struct NonNullJSObjectPtr(NonZero<*mut JSObject>);
impl NonNullJSObjectPtr {
#[inline]
pub unsafe fn new_unchecked(ptr: *mut JSObject) -> Self {
NonNullJSObjectPtr(NonZero::new_unchecked(ptr))
}
#[inline]
pub fn get(self) -> *mut JSObject {
self.0.get()
}
}