mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Auto merge of #5564 - Ms2ger:FromJSValConvertible, r=saneyuki
This commit is contained in:
commit
71e07013de
3 changed files with 23 additions and 60 deletions
|
@ -897,23 +897,12 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
|
|||
if type.nullable():
|
||||
declType = CGWrapper(declType, pre="Option<", post=">")
|
||||
|
||||
template = ""
|
||||
if type.isFloat() and not type.isUnrestricted():
|
||||
template = (
|
||||
"match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n"
|
||||
" Ok(v) => v,\n"
|
||||
" Err(_) => {\n"
|
||||
" throw_type_error(cx, \"this argument is not a finite floating-point value\");\n"
|
||||
" %s\n"
|
||||
" }\n"
|
||||
"}" % exceptionCode)
|
||||
else:
|
||||
#XXXjdm support conversionBehavior here
|
||||
template = (
|
||||
"match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n"
|
||||
" Ok(v) => v,\n"
|
||||
" Err(_) => { %s }\n"
|
||||
"}" % exceptionCode)
|
||||
#XXXjdm support conversionBehavior here
|
||||
template = (
|
||||
"match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n"
|
||||
" Ok(v) => v,\n"
|
||||
" Err(_) => { %s }\n"
|
||||
"}" % exceptionCode)
|
||||
|
||||
if defaultValue is not None:
|
||||
if isinstance(defaultValue, IDLNullValue):
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
//! | union types | `T` |
|
||||
|
||||
use dom::bindings::codegen::PrototypeList;
|
||||
use dom::bindings::error::throw_type_error;
|
||||
use dom::bindings::js::{JSRef, Root, Unrooted};
|
||||
use dom::bindings::num::Finite;
|
||||
use dom::bindings::str::{ByteString, USVString};
|
||||
|
@ -58,6 +59,7 @@ use libc;
|
|||
use std::borrow::ToOwned;
|
||||
use std::default;
|
||||
use std::marker::MarkerTrait;
|
||||
use std::num::Float;
|
||||
use std::slice;
|
||||
|
||||
/// A trait to retrieve the constants necessary to check if a `JSObject`
|
||||
|
@ -257,24 +259,6 @@ impl FromJSValConvertible for f32 {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToJSValConvertible for Finite<f32> {
|
||||
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
|
||||
let value = **self;
|
||||
value.to_jsval(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromJSValConvertible for Finite<f32> {
|
||||
type Config = ();
|
||||
fn from_jsval(cx: *mut JSContext, val: JSVal, option: ()) -> Result<Finite<f32>, ()> {
|
||||
let result = FromJSValConvertible::from_jsval(cx, val, option);
|
||||
let result = result.and_then(|v| {
|
||||
Finite::<f32>::new(v).ok_or(())
|
||||
});
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl ToJSValConvertible for f64 {
|
||||
fn to_jsval(&self, _cx: *mut JSContext) -> JSVal {
|
||||
unsafe {
|
||||
|
@ -290,7 +274,7 @@ impl FromJSValConvertible for f64 {
|
|||
}
|
||||
}
|
||||
|
||||
impl ToJSValConvertible for Finite<f64> {
|
||||
impl<T: Float + ToJSValConvertible> ToJSValConvertible for Finite<T> {
|
||||
#[inline]
|
||||
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
|
||||
let value = **self;
|
||||
|
@ -298,14 +282,18 @@ impl ToJSValConvertible for Finite<f64> {
|
|||
}
|
||||
}
|
||||
|
||||
impl FromJSValConvertible for Finite<f64> {
|
||||
impl<T: Float + FromJSValConvertible<Config=()>> FromJSValConvertible for Finite<T> {
|
||||
type Config = ();
|
||||
fn from_jsval(cx: *mut JSContext, val: JSVal, option: ()) -> Result<Finite<f64>, ()> {
|
||||
let result = FromJSValConvertible::from_jsval(cx, val, option);
|
||||
let result = result.and_then(|v| {
|
||||
Finite::<f64>::new(v).ok_or(())
|
||||
});
|
||||
result
|
||||
|
||||
fn from_jsval(cx: *mut JSContext, value: JSVal, option: ()) -> Result<Finite<T>, ()> {
|
||||
let result = try!(FromJSValConvertible::from_jsval(cx, value, option));
|
||||
match Finite::new(result) {
|
||||
Some(v) => Ok(v),
|
||||
None => {
|
||||
throw_type_error(cx, "this argument is not a finite floating-point value");
|
||||
Err(())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -569,17 +557,6 @@ pub fn native_from_reflector_jsmanaged<T>(mut obj: *mut JSObject) -> Result<Unro
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Reflectable+IDLInterface> FromJSValConvertible for Unrooted<T> {
|
||||
type Config = ();
|
||||
fn from_jsval(_cx: *mut JSContext, value: JSVal, _option: ())
|
||||
-> Result<Unrooted<T>, ()> {
|
||||
if !value.is_object() {
|
||||
return Err(());
|
||||
}
|
||||
native_from_reflector_jsmanaged(value.to_object())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Reflectable> ToJSValConvertible for Root<T> {
|
||||
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
|
||||
self.r().reflector().to_jsval(cx)
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
//! This module contains smart pointers to global scopes, to simplify writing
|
||||
//! code that works in workers as well as window scopes.
|
||||
|
||||
use dom::bindings::conversions::FromJSValConvertible;
|
||||
use dom::bindings::conversions::native_from_reflector_jsmanaged;
|
||||
use dom::bindings::js::{JS, JSRef, Root, Unrooted};
|
||||
use dom::bindings::utils::{Reflectable, Reflector};
|
||||
use dom::workerglobalscope::{WorkerGlobalScope, WorkerGlobalScopeHelpers};
|
||||
|
@ -22,11 +22,8 @@ use js::{JSCLASS_IS_GLOBAL, JSCLASS_IS_DOMJSCLASS};
|
|||
use js::glue::{GetGlobalForObjectCrossCompartment};
|
||||
use js::jsapi::{JSContext, JSObject};
|
||||
use js::jsapi::{JS_GetClass};
|
||||
use js::jsval::ObjectOrNullValue;
|
||||
use url::Url;
|
||||
|
||||
use std::ptr;
|
||||
|
||||
/// A freely-copyable reference to a rooted global object.
|
||||
#[derive(Copy)]
|
||||
pub enum GlobalRef<'a> {
|
||||
|
@ -189,12 +186,12 @@ pub fn global_object_for_js_object(obj: *mut JSObject) -> GlobalUnrooted {
|
|||
let global = GetGlobalForObjectCrossCompartment(obj);
|
||||
let clasp = JS_GetClass(global);
|
||||
assert!(((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)) != 0);
|
||||
match FromJSValConvertible::from_jsval(ptr::null_mut(), ObjectOrNullValue(global), ()) {
|
||||
match native_from_reflector_jsmanaged(global) {
|
||||
Ok(window) => return GlobalUnrooted::Window(window),
|
||||
Err(_) => (),
|
||||
}
|
||||
|
||||
match FromJSValConvertible::from_jsval(ptr::null_mut(), ObjectOrNullValue(global), ()) {
|
||||
match native_from_reflector_jsmanaged(global) {
|
||||
Ok(worker) => return GlobalUnrooted::Worker(worker),
|
||||
Err(_) => (),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue