Extend and update primitive conversions.

This commit is contained in:
Ms2ger 2014-03-04 17:01:30 +01:00
parent 6d3c0bf763
commit 595cd96f24
3 changed files with 65 additions and 10 deletions

View file

@ -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<i8> {
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<u8> {
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<i16> {
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<u64> {
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)
}
}

View file

@ -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 }

View file

@ -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;
};