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"
" None => %s\n"
" }\n"
"}" % (nullCondition, successVal, failureCode))
"match JSValConvertible::from_jsval(cx, ${val}) {\n"
" Some(v) => ${declName} = %s,\n"
" None => %s\n"
"}" % (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)
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)
return (setValue("(%s).to_jsval()" % 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))
}
}
}