From 595cd96f24359f5f143b4fe95f5f7c5bff426615 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 4 Mar 2014 17:01:30 +0100 Subject: [PATCH] Extend and update primitive conversions. --- .../script/dom/bindings/conversions.rs | 64 +++++++++++++++++-- src/components/script/dom/testbinding.rs | 3 + .../script/dom/webidls/TestBinding.webidl | 8 +-- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/components/script/dom/bindings/conversions.rs b/src/components/script/dom/bindings/conversions.rs index c6908ac40f6..bed52b0338d 100644 --- a/src/components/script/dom/bindings/conversions.rs +++ b/src/components/script/dom/bindings/conversions.rs @@ -3,10 +3,11 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use js::jsapi::{JSVal, JSBool, JSContext}; -use js::jsapi::{JS_ValueToInt64, JS_ValueToECMAInt32, JS_ValueToECMAUint32}; +use js::jsapi::{JS_ValueToUint64, JS_ValueToInt64}; +use js::jsapi::{JS_ValueToECMAUint32, JS_ValueToECMAInt32}; use js::jsapi::{JS_ValueToUint16, JS_ValueToNumber, JS_ValueToBoolean}; use js::{JSVAL_FALSE, JSVAL_TRUE}; -use js::glue::{RUST_UINT_TO_JSVAL, RUST_DOUBLE_TO_JSVAL}; +use js::glue::{RUST_INT_TO_JSVAL, RUST_UINT_TO_JSVAL, RUST_JS_NumberValue}; pub trait JSValConvertible { fn to_jsval(&self) -> JSVal; @@ -41,6 +42,45 @@ impl JSValConvertible for bool { } } +impl JSValConvertible for i8 { + fn to_jsval(&self) -> JSVal { + unsafe { + RUST_INT_TO_JSVAL(*self as i32) + } + } + + fn from_jsval(cx: *JSContext, val: JSVal) -> Option { + let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }; + result.map(|v| v as i8) + } +} + +impl JSValConvertible for u8 { + fn to_jsval(&self) -> JSVal { + unsafe { + RUST_INT_TO_JSVAL(*self as i32) + } + } + + fn from_jsval(cx: *JSContext, val: JSVal) -> Option { + let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }; + result.map(|v| v as u8) + } +} + +impl JSValConvertible for i16 { + fn to_jsval(&self) -> JSVal { + unsafe { + RUST_INT_TO_JSVAL(*self as i32) + } + } + + fn from_jsval(cx: *JSContext, val: JSVal) -> Option { + let result = unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }; + result.map(|v| v as i16) + } +} + impl JSValConvertible for u16 { fn to_jsval(&self) -> JSVal { unsafe { @@ -56,7 +96,7 @@ impl JSValConvertible for u16 { impl JSValConvertible for i32 { fn to_jsval(&self) -> JSVal { unsafe { - RUST_UINT_TO_JSVAL(*self as u32) + RUST_INT_TO_JSVAL(*self) } } @@ -80,7 +120,7 @@ impl JSValConvertible for u32 { impl JSValConvertible for i64 { fn to_jsval(&self) -> JSVal { unsafe { - RUST_DOUBLE_TO_JSVAL(*self as f64) + RUST_JS_NumberValue(*self as f64) } } @@ -89,10 +129,22 @@ impl JSValConvertible for i64 { } } +impl JSValConvertible for u64 { + fn to_jsval(&self) -> JSVal { + unsafe { + RUST_JS_NumberValue(*self as f64) + } + } + + fn from_jsval(cx: *JSContext, val: JSVal) -> Option { + unsafe { convert_from_jsval(cx, val, JS_ValueToUint64) } + } +} + impl JSValConvertible for f32 { fn to_jsval(&self) -> JSVal { unsafe { - RUST_DOUBLE_TO_JSVAL(*self as f64) + RUST_JS_NumberValue(*self as f64) } } @@ -105,7 +157,7 @@ impl JSValConvertible for f32 { impl JSValConvertible for f64 { fn to_jsval(&self) -> JSVal { unsafe { - RUST_DOUBLE_TO_JSVAL(*self as f64) + RUST_JS_NumberValue(*self) } } diff --git a/src/components/script/dom/testbinding.rs b/src/components/script/dom/testbinding.rs index c19b41fa303..361710ad17f 100644 --- a/src/components/script/dom/testbinding.rs +++ b/src/components/script/dom/testbinding.rs @@ -13,8 +13,11 @@ impl TestBinding { pub fn BooleanAttribute(&self) -> bool { false } pub fn SetBooleanAttribute(&self, _: bool) {} pub fn ByteAttribute(&self) -> i8 { 0 } + pub fn SetByteAttribute(&self, _: i8) {} pub fn OctetAttribute(&self) -> u8 { 0 } + pub fn SetOctetAttribute(&self, _: u8) {} pub fn ShortAttribute(&self) -> i16 { 0 } + pub fn SetShortAttribute(&self, _: i16) {} pub fn UnsignedShortAttribute(&self) -> u16 { 0 } pub fn SetUnsignedShortAttribute(&self, _: u16) {} pub fn LongAttribute(&self) -> i32 { 0 } diff --git a/src/components/script/dom/webidls/TestBinding.webidl b/src/components/script/dom/webidls/TestBinding.webidl index b43b455cef0..34dd569e8ee 100644 --- a/src/components/script/dom/webidls/TestBinding.webidl +++ b/src/components/script/dom/webidls/TestBinding.webidl @@ -4,14 +4,14 @@ interface TestBinding { attribute boolean booleanAttribute; - readonly attribute byte byteAttribute; - readonly attribute octet octetAttribute; - readonly attribute short shortAttribute; + attribute byte byteAttribute; + attribute octet octetAttribute; + attribute short shortAttribute; attribute unsigned short unsignedShortAttribute; attribute long longAttribute; attribute unsigned long unsignedLongAttribute; attribute long long longLongAttribute; - readonly attribute unsigned long long unsignedLongLongAttribute; + attribute unsigned long long unsignedLongLongAttribute; attribute float floatAttribute; attribute double doubleAttribute; };