mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Rewrite the codegen for nullable primitives to use JSValConvertible.
This commit is contained in:
parent
aa9a61a78c
commit
1608f842e9
2 changed files with 34 additions and 22 deletions
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue