auto merge of #1814 : Ms2ger/servo/more-primitive-setters, r=jdm

This commit is contained in:
bors-servo 2014-03-05 09:55:35 -05:00
commit 5eb7d1cf30
4 changed files with 250 additions and 98 deletions

View file

@ -1233,23 +1233,24 @@ for (uint32_t i = 0; i < length; ++i) {
failureCode = 'return 0'
if type.nullable():
dataLoc = "${declName}.SetValue()"
nullCondition = "(RUST_JSVAL_IS_NULL(${val}) != 0 || RUST_JSVAL_IS_VOID(${val}) != 0)"
if defaultValue is not None and isinstance(defaultValue, IDLNullValue):
nullCondition = "!(${haveValue}) || " + nullCondition
successVal = "val_"
successVal = "v"
if preSuccess or postSuccess:
successVal = preSuccess + successVal + postSuccess
#XXXjdm support conversionBehavior here
template = (
"if (%s) {\n"
" ${declName} = None;\n"
"} else {\n"
"match JSValConvertible::from_jsval(cx, ${val}) {\n"
" Some(val_) => ${declName} = Some(%s),\n"
" Some(v) => ${declName} = %s,\n"
" None => %s\n"
" }\n"
"}" % (nullCondition, successVal, failureCode))
"}" % (successVal, failureCode))
if defaultValue is not None and isinstance(defaultValue, IDLNullValue):
template = CGWrapper(CGIndenter(CGGeneric(template)),
pre="if ${haveValue} {\n",
post=("\n"
"} else {\n"
" ${declName} = None;\n"
"}")).define()
declType = CGGeneric("Option<" + typeName + ">")
else:
assert(defaultValue is None or
@ -1599,34 +1600,8 @@ if %(resultStr)s.is_null() {
if not type.isPrimitive():
raise TypeError("Need to learn to wrap %s" % type)
if type.nullable():
(recTemplate, recInfal) = getWrapTemplateForType(type.inner, descriptorProvider,
"%s.Value()" % result, successCode,
isCreator, exceptionCode)
return ("if (%s.IsNull()) {\n" % result +
CGIndenter(CGGeneric(setValue("JSVAL_NULL"))).define() + "\n" +
"}\n" + recTemplate, recInfal)
return (setValue("(%s).to_jsval()" % result), True)
tag = type.tag()
if tag in [IDLType.Tags.int8, IDLType.Tags.uint8, IDLType.Tags.int16,
IDLType.Tags.uint16, IDLType.Tags.int32]:
return (setValue("RUST_INT_TO_JSVAL(%s as i32)" % result), True)
elif tag in [IDLType.Tags.int64, IDLType.Tags.uint64, IDLType.Tags.float,
IDLType.Tags.double]:
# XXXbz will cast to double do the "even significand" thing that webidl
# calls for for 64-bit ints? Do we care?
return (setValue("RUST_JS_NumberValue(%s as f64)" % result), True)
elif tag == IDLType.Tags.uint32:
return (setValue("RUST_UINT_TO_JSVAL(%s)" % result), True)
elif tag == IDLType.Tags.bool:
return (setValue("RUST_BOOLEAN_TO_JSVAL(%s as JSBool)" % result), True)
else:
raise TypeError("Need to learn to wrap primitive: %s" % type)
def wrapForType(type, descriptorProvider, templateValues):
"""
@ -1685,7 +1660,7 @@ def getRetvalDeclarationForType(returnType, descriptorProvider):
if returnType.isPrimitive() and returnType.tag() in builtinNames:
result = CGGeneric(builtinNames[returnType.tag()])
if returnType.nullable():
result = CGWrapper(result, pre="Nullable<", post=">")
result = CGWrapper(result, pre="Option<", post=">")
return result, False
if returnType.isString():
result = CGGeneric("DOMString")

View file

@ -3,10 +3,12 @@
* 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::{JSVAL_FALSE, JSVAL_TRUE, JSVAL_NULL};
use js::glue::{RUST_INT_TO_JSVAL, RUST_UINT_TO_JSVAL, RUST_JS_NumberValue};
use js::glue::{RUST_JSVAL_IS_NULL, RUST_JSVAL_IS_VOID};
pub trait JSValConvertible {
fn to_jsval(&self) -> JSVal;
@ -26,54 +28,6 @@ unsafe fn convert_from_jsval<T: Default>(
}
impl JSValConvertible for i64 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_DOUBLE_TO_JSVAL(*self as f64)
}
}
fn from_jsval(cx: *JSContext, val: JSVal) -> Option<i64> {
unsafe { convert_from_jsval(cx, val, JS_ValueToInt64) }
}
}
impl JSValConvertible for u32 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_UINT_TO_JSVAL(*self)
}
}
fn from_jsval(cx: *JSContext, val: JSVal) -> Option<u32> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAUint32) }
}
}
impl JSValConvertible for i32 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_UINT_TO_JSVAL(*self as u32)
}
}
fn from_jsval(cx: *JSContext, val: JSVal) -> Option<i32> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }
}
}
impl JSValConvertible for u16 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_UINT_TO_JSVAL(*self as u32)
}
}
fn from_jsval(cx: *JSContext, val: JSVal) -> Option<u16> {
unsafe { convert_from_jsval(cx, val, JS_ValueToUint16) }
}
}
impl JSValConvertible for bool {
fn to_jsval(&self) -> JSVal {
if *self {
@ -89,10 +43,109 @@ 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 {
RUST_UINT_TO_JSVAL(*self as u32)
}
}
fn from_jsval(cx: *JSContext, val: JSVal) -> Option<u16> {
unsafe { convert_from_jsval(cx, val, JS_ValueToUint16) }
}
}
impl JSValConvertible for i32 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_INT_TO_JSVAL(*self)
}
}
fn from_jsval(cx: *JSContext, val: JSVal) -> Option<i32> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAInt32) }
}
}
impl JSValConvertible for u32 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_UINT_TO_JSVAL(*self)
}
}
fn from_jsval(cx: *JSContext, val: JSVal) -> Option<u32> {
unsafe { convert_from_jsval(cx, val, JS_ValueToECMAUint32) }
}
}
impl JSValConvertible for i64 {
fn to_jsval(&self) -> JSVal {
unsafe {
RUST_JS_NumberValue(*self as f64)
}
}
fn from_jsval(cx: *JSContext, val: JSVal) -> Option<i64> {
unsafe { convert_from_jsval(cx, val, JS_ValueToInt64) }
}
}
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 +158,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)
}
}
@ -113,3 +166,21 @@ impl JSValConvertible for f64 {
unsafe { convert_from_jsval(cx, val, JS_ValueToNumber) }
}
}
impl<T: JSValConvertible> JSValConvertible for Option<T> {
fn to_jsval(&self) -> JSVal {
match self {
&Some(ref value) => value.to_jsval(),
&None => JSVAL_NULL,
}
}
fn from_jsval(cx: *JSContext, value: JSVal) -> Option<Option<T>> {
if unsafe { RUST_JSVAL_IS_NULL(value) != 0 || RUST_JSVAL_IS_VOID(value) != 0 } {
Some(None)
} else {
let result: Option<T> = JSValConvertible::from_jsval(cx, value);
result.map(|v| Some(v))
}
}
}

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 }
@ -29,6 +32,64 @@ impl TestBinding {
pub fn SetFloatAttribute(&self, _: f32) {}
pub fn DoubleAttribute(&self) -> f64 { 0. }
pub fn SetDoubleAttribute(&self, _: f64) {}
pub fn GetBooleanAttributeNullable(&self) -> Option<bool> { Some(false) }
pub fn SetBooleanAttributeNullable(&self, _: Option<bool>) {}
pub fn GetByteAttributeNullable(&self) -> Option<i8> { Some(0) }
pub fn SetByteAttributeNullable(&self, _: Option<i8>) {}
pub fn GetOctetAttributeNullable(&self) -> Option<u8> { Some(0) }
pub fn SetOctetAttributeNullable(&self, _: Option<u8>) {}
pub fn GetShortAttributeNullable(&self) -> Option<i16> { Some(0) }
pub fn SetShortAttributeNullable(&self, _: Option<i16>) {}
pub fn GetUnsignedShortAttributeNullable(&self) -> Option<u16> { Some(0) }
pub fn SetUnsignedShortAttributeNullable(&self, _: Option<u16>) {}
pub fn GetLongAttributeNullable(&self) -> Option<i32> { Some(0) }
pub fn SetLongAttributeNullable(&self, _: Option<i32>) {}
pub fn GetUnsignedLongAttributeNullable(&self) -> Option<u32> { Some(0) }
pub fn SetUnsignedLongAttributeNullable(&self, _: Option<u32>) {}
pub fn GetLongLongAttributeNullable(&self) -> Option<i64> { Some(0) }
pub fn SetLongLongAttributeNullable(&self, _: Option<i64>) {}
pub fn GetUnsignedLongLongAttributeNullable(&self) -> Option<u64> { Some(0) }
pub fn SetUnsignedLongLongAttributeNullable(&self, _: Option<u64>) {}
pub fn GetFloatAttributeNullable(&self) -> Option<f32> { Some(0.) }
pub fn SetFloatAttributeNullable(&self, _: Option<f32>) {}
pub fn GetDoubleAttributeNullable(&self) -> Option<f64> { Some(0.) }
pub fn SetDoubleAttributeNullable(&self, _: Option<f64>) {}
// FIXME (issue #1813) Doesn't currently compile.
// pub fn PassOptionalBoolean(&self, _: Option<bool>) {}
// pub fn PassOptionalByte(&self, _: Option<i8>) {}
// pub fn PassOptionalOctet(&self, _: Option<u8>) {}
// pub fn PassOptionalShort(&self, _: Option<i16>) {}
// pub fn PassOptionalUnsignedShort(&self, _: Option<u16>) {}
// pub fn PassOptionalLong(&self, _: Option<i32>) {}
// pub fn PassOptionalUnsignedLong(&self, _: Option<u32>) {}
// pub fn PassOptionalLongLong(&self, _: Option<i64>) {}
// pub fn PassOptionalUnsignedLongLong(&self, _: Option<u64>) {}
// pub fn PassOptionalFloat(&self, _: Option<f32>) {}
// pub fn PassOptionalDouble(&self, _: Option<f64>) {}
pub fn PassOptionalBooleanWithDefault(&self, _: bool) {}
pub fn PassOptionalByteWithDefault(&self, _: i8) {}
pub fn PassOptionalOctetWithDefault(&self, _: u8) {}
pub fn PassOptionalShortWithDefault(&self, _: i16) {}
pub fn PassOptionalUnsignedShortWithDefault(&self, _: u16) {}
pub fn PassOptionalLongWithDefault(&self, _: i32) {}
pub fn PassOptionalUnsignedLongWithDefault(&self, _: u32) {}
pub fn PassOptionalLongLongWithDefault(&self, _: i64) {}
pub fn PassOptionalUnsignedLongLongWithDefault(&self, _: u64) {}
pub fn PassOptionalNullableBooleanWithDefault(&self, _: Option<bool>) {}
pub fn PassOptionalNullableByteWithDefault(&self, _: Option<i8>) {}
pub fn PassOptionalNullableOctetWithDefault(&self, _: Option<u8>) {}
pub fn PassOptionalNullableShortWithDefault(&self, _: Option<i16>) {}
pub fn PassOptionalNullableUnsignedShortWithDefault(&self, _: Option<u16>) {}
pub fn PassOptionalNullableLongWithDefault(&self, _: Option<i32>) {}
pub fn PassOptionalNullableUnsignedLongWithDefault(&self, _: Option<u32>) {}
pub fn PassOptionalNullableLongLongWithDefault(&self, _: Option<i64>) {}
pub fn PassOptionalNullableUnsignedLongLongWithDefault(&self, _: Option<u64>) {}
pub fn PassOptionalNullableFloatWithDefault(&self, _: Option<f32>) {}
pub fn PassOptionalNullableDoubleWithDefault(&self, _: Option<f64>) {}
}
impl Reflectable for TestBinding {

View file

@ -4,14 +4,59 @@
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;
attribute boolean? booleanAttributeNullable;
attribute byte? byteAttributeNullable;
attribute octet? octetAttributeNullable;
attribute short? shortAttributeNullable;
attribute unsigned short? unsignedShortAttributeNullable;
attribute long? longAttributeNullable;
attribute unsigned long? unsignedLongAttributeNullable;
attribute long long? longLongAttributeNullable;
attribute unsigned long long? unsignedLongLongAttributeNullable;
attribute float? floatAttributeNullable;
attribute double? doubleAttributeNullable;
// FIXME (issue #1813) Doesn't currently compile.
// void passOptionalBoolean(optional boolean arg);
// void passOptionalByte(optional byte arg);
// void passOptionalOctet(optional octet arg);
// void passOptionalShort(optional short arg);
// void passOptionalUnsignedShort(optional unsigned short arg);
// void passOptionalLong(optional long arg);
// void passOptionalUnsignedLong(optional unsigned long arg);
// void passOptionalLongLong(optional long long arg);
// void passOptionalUnsignedLongLong(optional unsigned long long arg);
// void passOptionalFloat(optional float arg);
// void passOptionalDouble(optional double arg);
void passOptionalBooleanWithDefault(optional boolean arg = false);
void passOptionalByteWithDefault(optional byte arg = 0);
void passOptionalOctetWithDefault(optional octet arg = 19);
void passOptionalShortWithDefault(optional short arg = 5);
void passOptionalUnsignedShortWithDefault(optional unsigned short arg = 2);
void passOptionalLongWithDefault(optional long arg = 7);
void passOptionalUnsignedLongWithDefault(optional unsigned long arg = 6);
void passOptionalLongLongWithDefault(optional long long arg = -12);
void passOptionalUnsignedLongLongWithDefault(optional unsigned long long arg = 17);
void passOptionalNullableBooleanWithDefault(optional boolean? arg = null);
void passOptionalNullableByteWithDefault(optional byte? arg = null);
void passOptionalNullableOctetWithDefault(optional octet? arg = null);
void passOptionalNullableShortWithDefault(optional short? arg = null);
void passOptionalNullableUnsignedShortWithDefault(optional unsigned short? arg = null);
void passOptionalNullableLongWithDefault(optional long? arg = null);
void passOptionalNullableUnsignedLongWithDefault(optional unsigned long? arg = null);
void passOptionalNullableLongLongWithDefault(optional long long? arg = null);
void passOptionalNullableUnsignedLongLongWithDefault(optional unsigned long long? arg = null);
};