Introduce Finite<T: Float> for restricted values defined in WebIDL.

This commit is contained in:
Tetsuharu OHZEKI 2015-03-04 20:28:57 +09:00
parent 2bf2c0020b
commit f7fd34c0aa
5 changed files with 114 additions and 30 deletions

View file

@ -81,9 +81,9 @@ builtinNames = {
IDLType.Tags.uint32: 'u32',
IDLType.Tags.uint64: 'u64',
IDLType.Tags.unrestricted_float: 'f32',
IDLType.Tags.float: 'f32',
IDLType.Tags.float: 'Finite<f32>',
IDLType.Tags.unrestricted_double: 'f64',
IDLType.Tags.double: 'f64'
IDLType.Tags.double: 'Finite<f64>'
}
numericTags = [
@ -4688,6 +4688,7 @@ class CGBindingRoot(CGThing):
'dom::bindings::proxyhandler',
'dom::bindings::proxyhandler::{fill_property_descriptor, get_expando_object}',
'dom::bindings::proxyhandler::{get_property_descriptor}',
'dom::bindings::num::Finite',
'dom::bindings::str::ByteString',
'dom::bindings::str::USVString',
'libc',

View file

@ -17,9 +17,9 @@
//! | long long | `i64` |
//! | unsigned long long | `u64` |
//! | unrestricted float | `f32` |
//! | float | `f32` |
//! | float | `Finite<f32>` |
//! | unrestricted double | `f64` |
//! | double | `f64` |
//! | double | `Finite<f64>` |
//! | DOMString | `DOMString` |
//! | USVString | `USVString` |
//! | ByteString | `ByteString` |
@ -34,6 +34,7 @@
use dom::bindings::codegen::PrototypeList;
use dom::bindings::js::{JSRef, Root, Unrooted};
use dom::bindings::num::Finite;
use dom::bindings::str::{ByteString, USVString};
use dom::bindings::utils::{Reflectable, Reflector, DOMClass};
use util::str::DOMString;
@ -256,6 +257,24 @@ impl FromJSValConvertible for f32 {
}
}
impl ToJSValConvertible for Finite<f32> {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
let value = self.clone().unwrap();
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 {
@ -271,6 +290,25 @@ impl FromJSValConvertible for f64 {
}
}
impl ToJSValConvertible for Finite<f64> {
#[inline]
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
let value = self.clone().unwrap();
value.to_jsval(cx)
}
}
impl FromJSValConvertible for Finite<f64> {
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
}
}
impl ToJSValConvertible for str {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
unsafe {

View file

@ -37,6 +37,7 @@ pub mod callback;
pub mod error;
pub mod conversions;
pub mod proxyhandler;
pub mod num;
pub mod str;
pub mod structuredclone;
pub mod trace;

View file

@ -0,0 +1,43 @@
/* 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/. */
//! The `Finite<T>` struct.
use core::nonzero::Zeroable;
use std::num::Float;
use std::ops::Deref;
/// Encapsulates the IDL restricted float type.
#[derive(Clone,Eq,PartialEq)]
#[jstraceable]
pub struct Finite<T: Float>(T);
unsafe impl<T: Float> Zeroable for Finite<T> {}
impl<T: Float> Finite<T> {
/// Create a new `Finite<T: Float>` safely.
pub fn new(value: T) -> Option<Finite<T>> {
if value.is_finite() {
Some(Finite(value))
} else {
None
}
}
/// Create a new `Finite<T: Float>`.
#[inline]
pub fn wrap(value: T) -> Finite<T> {
assert!(value.is_finite(), "Finite<T> doesn't encapsulate unrestricted value.");
Finite(value)
}
}
impl<T: Float> Deref for Finite<T> {
type Target = T;
fn deref(&self) -> &T {
let &Finite(ref value) = self;
value
}
}