auto merge of #1861 : Ms2ger/servo/split-JSValConvertible, r=jdm

Later, we'll want to implement variations of the conversion *from* JS, but
not the conversion *to* JS.
This commit is contained in:
bors-servo 2014-03-10 11:01:52 -04:00
commit 52ab85c45d
2 changed files with 44 additions and 17 deletions

View file

@ -10,8 +10,11 @@ use js::jsval::JSVal;
use js::jsval::{NullValue, BooleanValue, Int32Value, UInt32Value};
use js::glue::RUST_JS_NumberValue;
pub trait JSValConvertible {
pub trait ToJSValConvertible {
fn to_jsval(&self) -> JSVal;
}
pub trait FromJSValConvertible {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<Self, ()>;
}
@ -28,142 +31,166 @@ unsafe fn convert_from_jsval<T: Default>(
}
impl JSValConvertible for bool {
impl ToJSValConvertible for bool {
fn to_jsval(&self) -> JSVal {
BooleanValue(*self)
}
}
impl FromJSValConvertible for bool {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<bool, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToBoolean) };
result.map(|b| b != 0)
}
}
impl JSValConvertible for i8 {
impl ToJSValConvertible for i8 {
fn to_jsval(&self) -> JSVal {
Int32Value(*self as i32)
}
}
impl FromJSValConvertible for i8 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<i8, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) };
result.map(|v| v as i8)
}
}
impl JSValConvertible for u8 {
impl ToJSValConvertible for u8 {
fn to_jsval(&self) -> JSVal {
Int32Value(*self as i32)
}
}
impl FromJSValConvertible for u8 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<u8, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) };
result.map(|v| v as u8)
}
}
impl JSValConvertible for i16 {
impl ToJSValConvertible for i16 {
fn to_jsval(&self) -> JSVal {
Int32Value(*self as i32)
}
}
impl FromJSValConvertible for i16 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<i16, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) };
result.map(|v| v as i16)
}
}
impl JSValConvertible for u16 {
impl ToJSValConvertible for u16 {
fn to_jsval(&self) -> JSVal {
Int32Value(*self as i32)
}
}
impl FromJSValConvertible for u16 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<u16, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToUint16) }
}
}
impl JSValConvertible for i32 {
impl ToJSValConvertible for i32 {
fn to_jsval(&self) -> JSVal {
Int32Value(*self)
}
}
impl FromJSValConvertible for i32 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<i32, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }
}
}
impl JSValConvertible for u32 {
impl ToJSValConvertible for u32 {
fn to_jsval(&self) -> JSVal {
UInt32Value(*self)
}
}
impl FromJSValConvertible for u32 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<u32, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAUint32) }
}
}
impl JSValConvertible for i64 {
impl ToJSValConvertible for i64 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_JS_NumberValue(*self as f64)
}
}
}
impl FromJSValConvertible for i64 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<i64, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToInt64) }
}
}
impl JSValConvertible for u64 {
impl ToJSValConvertible for u64 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_JS_NumberValue(*self as f64)
}
}
}
impl FromJSValConvertible for u64 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<u64, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToUint64) }
}
}
impl JSValConvertible for f32 {
impl ToJSValConvertible for f32 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_JS_NumberValue(*self as f64)
}
}
}
impl FromJSValConvertible for f32 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<f32, ()> {
let result = unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) };
result.map(|f| f as f32)
}
}
impl JSValConvertible for f64 {
impl ToJSValConvertible for f64 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_JS_NumberValue(*self)
}
}
}
impl FromJSValConvertible for f64 {
fn from_jsval(cx: *JSContext, val: JSVal) -> Result<f64, ()> {
unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) }
}
}
impl<T: JSValConvertible> JSValConvertible for Option<T> {
impl<T: ToJSValConvertible> ToJSValConvertible for Option<T> {
fn to_jsval(&self) -> JSVal {
match self {
&Some(ref value) => value.to_jsval(),
&None => NullValue(),
}
}
}
impl<T: FromJSValConvertible> FromJSValConvertible for Option<T> {
fn from_jsval(cx: *JSContext, value: JSVal) -> Result<Option<T>, ()> {
if value.is_null_or_undefined() {
Ok(None)
} else {
let result: Result<T, ()> = JSValConvertible::from_jsval(cx, value);
let result: Result<T, ()> = FromJSValConvertible::from_jsval(cx, value);
result.map(|v| Some(v))
}
}