aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMs2ger <ms2ger@gmail.com>2014-03-04 18:55:58 +0100
committerMs2ger <ms2ger@gmail.com>2014-03-04 18:55:58 +0100
commit1608f842e99593242d2ff6aa33dc34ea709e46af (patch)
tree38a99cf469e2554b6780434d69f9bd1c49f4cf8e /src
parentaa9a61a78c240528d5ee7f229e4bac9254407e5e (diff)
downloadservo-1608f842e99593242d2ff6aa33dc34ea709e46af.tar.gz
servo-1608f842e99593242d2ff6aa33dc34ea709e46af.zip
Rewrite the codegen for nullable primitives to use JSValConvertible.
Diffstat (limited to 'src')
-rw-r--r--src/components/script/dom/bindings/codegen/CodegenRust.py35
-rw-r--r--src/components/script/dom/bindings/conversions.rs21
2 files changed, 34 insertions, 22 deletions
diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py
index 20cbe1080ef..2c9b346db96 100644
--- a/src/components/script/dom/bindings/codegen/CodegenRust.py
+++ b/src/components/script/dom/bindings/codegen/CodegenRust.py
@@ -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,14 +1600,6 @@ 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.unwrap()" % result, successCode,
- isCreator, exceptionCode)
- return ("if (%s.is_none()) {\n" % result +
- CGIndenter(CGGeneric(setValue("JSVAL_NULL"))).define() + "\n" +
- "}\n" + recTemplate, recInfal)
-
return (setValue("(%s).to_jsval()" % result), True)
diff --git a/src/components/script/dom/bindings/conversions.rs b/src/components/script/dom/bindings/conversions.rs
index bed52b0338d..2a59b52791b 100644
--- a/src/components/script/dom/bindings/conversions.rs
+++ b/src/components/script/dom/bindings/conversions.rs
@@ -6,8 +6,9 @@ use js::jsapi::{JSVal, JSBool, JSContext};
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::{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;
@@ -165,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))
+ }
+ }
+}