From 922d191948702509eb026edf8e86f853f9d004c2 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 6 Mar 2014 22:52:57 +0100 Subject: [PATCH 1/3] Common up some code in the conversion to nullable and non-nullable primitive types. --- .../dom/bindings/codegen/CodegenRust.py | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index bbdd6b9420e..b08a209a879 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -1238,17 +1238,17 @@ for (uint32_t i = 0; i < length; ++i) { if failureCode is None: failureCode = 'return 0' - if type.nullable(): - successVal = "v" - if preSuccess or postSuccess: - successVal = preSuccess + successVal + postSuccess - #XXXjdm support conversionBehavior here - template = ( - "match JSValConvertible::from_jsval(cx, ${val}) {\n" - " Ok(v) => ${declName} = %s,\n" - " Err(_) => %s\n" - "}" % (successVal, failureCode)) + successVal = "v" + if preSuccess or postSuccess: + successVal = preSuccess + successVal + postSuccess + #XXXjdm support conversionBehavior here + template = ( + "match JSValConvertible::from_jsval(cx, ${val}) {\n" + " Ok(v) => ${declName} = %s,\n" + " Err(_) => %s\n" + "}" % (successVal, failureCode)) + if type.nullable(): if defaultValue is not None and isinstance(defaultValue, IDLNullValue): template = CGWrapper(CGIndenter(CGGeneric(template)), pre="if ${haveValue} {\n", @@ -1261,16 +1261,8 @@ for (uint32_t i = 0; i < length; ++i) { else: assert(defaultValue is None or not isinstance(defaultValue, IDLNullValue)) - #XXXjdm conversionBehavior should be used - successVal = "v" - if preSuccess or postSuccess: - successVal = preSuccess + successVal + postSuccess - template = ( - "match JSValConvertible::from_jsval(cx, ${val}) {\n" - " Err(_) => %s,\n" - " Ok(v) => ${declName} = %s\n" - "}" % (failureCode, successVal)) declType = CGGeneric(typeName) + if (defaultValue is not None and # We already handled IDLNullValue, so just deal with the other ones not isinstance(defaultValue, IDLNullValue)): From 9fba4bcfbaa966b360159eb4d897276074caffcb Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 6 Mar 2014 22:56:10 +0100 Subject: [PATCH 2/3] Simplify the code flow in the conversion to primitive types so that default values are handled together. --- .../dom/bindings/codegen/CodegenRust.py | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index b08a209a879..25b92afcecc 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -1249,39 +1249,36 @@ for (uint32_t i = 0; i < length; ++i) { "}" % (successVal, failureCode)) if type.nullable(): - if defaultValue is not None and isinstance(defaultValue, IDLNullValue): + declType = CGGeneric("Option<" + typeName + ">") + else: + declType = CGGeneric(typeName) + + if defaultValue is not None: + if isinstance(defaultValue, IDLNullValue): + assert type.nullable() 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 - not isinstance(defaultValue, IDLNullValue)) - declType = CGGeneric(typeName) - - if (defaultValue is not None and - # We already handled IDLNullValue, so just deal with the other ones - not isinstance(defaultValue, IDLNullValue)): - tag = defaultValue.type.tag() - if tag in numericTags: - defaultStr = defaultValue.value else: - assert(tag == IDLType.Tags.bool) - defaultStr = toStringBool(defaultValue.value) + tag = defaultValue.type.tag() + if tag in numericTags: + defaultStr = defaultValue.value + else: + assert(tag == IDLType.Tags.bool) + defaultStr = toStringBool(defaultValue.value) - if type.nullable(): - defaultStr = "Some(%s)" % defaultStr + if type.nullable(): + defaultStr = "Some(%s)" % defaultStr - template = CGWrapper(CGIndenter(CGGeneric(template)), - pre="if ${haveValue} {\n", - post=("\n" - "} else {\n" - " ${declName} = %s;\n" - "}" % defaultStr)).define() + template = CGWrapper(CGIndenter(CGGeneric(template)), + pre="if ${haveValue} {\n", + post=("\n" + "} else {\n" + " ${declName} = %s;\n" + "}" % defaultStr)).define() initialVal = "false" if typeName == "bool" else ("0 as %s" % typeName) if type.nullable(): From 3a0b7fe62144b119a5d98fbb2da27bab4541ba3a Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 6 Mar 2014 23:07:42 +0100 Subject: [PATCH 3/3] Common up some more code in the handling of default values for primitive types. --- .../dom/bindings/codegen/CodegenRust.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/CodegenRust.py b/src/components/script/dom/bindings/codegen/CodegenRust.py index 25b92afcecc..daff75a16ac 100644 --- a/src/components/script/dom/bindings/codegen/CodegenRust.py +++ b/src/components/script/dom/bindings/codegen/CodegenRust.py @@ -1256,12 +1256,7 @@ for (uint32_t i = 0; i < length; ++i) { if defaultValue is not None: if isinstance(defaultValue, IDLNullValue): assert type.nullable() - template = CGWrapper(CGIndenter(CGGeneric(template)), - pre="if ${haveValue} {\n", - post=("\n" - "} else {\n" - " ${declName} = None;\n" - "}")).define() + defaultStr = "None" else: tag = defaultValue.type.tag() if tag in numericTags: @@ -1273,12 +1268,12 @@ for (uint32_t i = 0; i < length; ++i) { if type.nullable(): defaultStr = "Some(%s)" % defaultStr - template = CGWrapper(CGIndenter(CGGeneric(template)), - pre="if ${haveValue} {\n", - post=("\n" - "} else {\n" - " ${declName} = %s;\n" - "}" % defaultStr)).define() + template = CGWrapper(CGIndenter(CGGeneric(template)), + pre="if ${haveValue} {\n", + post=("\n" + "} else {\n" + " ${declName} = %s;\n" + "}" % defaultStr)).define() initialVal = "false" if typeName == "bool" else ("0 as %s" % typeName) if type.nullable():