Merge the 'new' and 'Init' functions for dictionaries.

This moves the code in callers more in line with conversions for other types
and ensures the default values given to the dictionary fields (as defined by
the defaultValue function in CGDictionary.impl) do not escape the 'new'
method. The new code is also more in line with the code used by the
FromJSValConvertible trait.

This is the first step of my planned rewrite of the dictionary initialization
that will remove the default values entirely and reduce the code
duplication in the 'Init' (now 'new') function.
This commit is contained in:
Ms2ger 2014-04-18 14:46:01 +02:00
parent 8e596d142e
commit 88c5e733b5

View file

@ -794,7 +794,6 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
assert not isOptional assert not isOptional
typeName = CGDictionary.makeDictionaryName(type.inner) typeName = CGDictionary.makeDictionaryName(type.inner)
selfRef = "${declName}"
declType = CGGeneric(typeName) declType = CGGeneric(typeName)
@ -806,10 +805,10 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
else: else:
val = "${val}" val = "${val}"
template = ("%s = %s::new();\n" template = ("${declName} = match %s::new(cx, %s) {\n"
"if %s.Init(cx, %s) == 0 {\n" " Ok(dictionary) => dictionary,\n"
" return 0;\n" " Err(_) => return 0,\n"
"}" % (selfRef, typeName, selfRef, val)) "};" % (typeName, val))
return (template, declType, None, False, None) return (template, declType, None, False, None)
@ -4143,7 +4142,9 @@ class CGDictionary(CGThing):
descriptorProvider, descriptorProvider,
isMember=True, isMember=True,
isOptional=(not member.defaultValue), isOptional=(not member.defaultValue),
defaultValue=member.defaultValue)) defaultValue=member.defaultValue,
failureCode="return Err(());",
exceptionCode="return Err(());"))
for member in dictionary.members ] for member in dictionary.members ]
def define(self): def define(self):
@ -4172,10 +4173,11 @@ class CGDictionary(CGThing):
def impl(self): def impl(self):
d = self.dictionary d = self.dictionary
if d.parent: if d.parent:
initParent = ("// Per spec, we init the parent's members first\n" initParent = ("parent: match %s::%s::new(cx, val) {\n"
"if self.parent.Init(cx, val) == 0 {\n" " Ok(parent) => parent,\n"
" return 0;\n" " Err(_) => return Err(()),\n"
"}\n") "},\n") % (self.makeModuleName(d.parent),
self.makeClassName(d.parent))
else: else:
initParent = "" initParent = ""
@ -4196,27 +4198,23 @@ class CGDictionary(CGThing):
return string.Template( return string.Template(
"impl ${selfName} {\n" "impl ${selfName} {\n"
" pub fn new() -> ${selfName} {\n" " pub fn new(cx: *JSContext, val: JSVal) -> Result<${selfName}, ()> {\n"
" ${selfName} {\n" + " let mut result = ${selfName} {\n"
((" parent: %s::%s::new(),\n" % (self.makeModuleName(d.parent), "${initParent}" +
self.makeClassName(d.parent))) if d.parent else "") +
"\n".join(" %s: %s," % (self.makeMemberName(m[0].identifier.name), defaultValue(self.getMemberType(m))) for m in self.memberInfo) + "\n" "\n".join(" %s: %s," % (self.makeMemberName(m[0].identifier.name), defaultValue(self.getMemberType(m))) for m in self.memberInfo) + "\n"
" }\n" " };\n"
" }\n"
"\n" "\n"
" pub fn Init(&mut self, cx: *JSContext, val: JSVal) -> JSBool {\n"
" unsafe {\n" " unsafe {\n"
"${initParent}"
" let mut found: JSBool = 0;\n" " let mut found: JSBool = 0;\n"
" let temp: JSVal = NullValue();\n" " let temp: JSVal = NullValue();\n"
" let isNull = val.is_null_or_undefined();\n" " let isNull = val.is_null_or_undefined();\n"
" if !isNull && val.is_primitive() {\n" " if !isNull && val.is_primitive() {\n"
" return 0; //XXXjdm throw properly here\n" " return Err(()); //XXXjdm throw properly here\n"
" //return Throw(cx, NS_ERROR_XPC_BAD_CONVERT_JS);\n" " //return Throw(cx, NS_ERROR_XPC_BAD_CONVERT_JS);\n"
" }\n" " }\n"
"\n" "\n"
"${initMembers}\n" "${initMembers}\n"
" return 1;\n" " Ok(result)\n"
" }\n" " }\n"
" }\n" " }\n"
"}").substitute({ "}").substitute({
@ -4255,7 +4253,7 @@ class CGDictionary(CGThing):
holderType, dealWithOptional, initialValue)) = memberInfo holderType, dealWithOptional, initialValue)) = memberInfo
replacements = { "val": "temp", replacements = { "val": "temp",
"valPtr": "&temp", "valPtr": "&temp",
"declName": ("self.%s" % self.makeMemberName(member.identifier.name)), "declName": ("result.%s" % self.makeMemberName(member.identifier.name)),
# We need a holder name for external interfaces, but # We need a holder name for external interfaces, but
# it's scoped down to the conversion so we can just use # it's scoped down to the conversion so we can just use
# anything we want. # anything we want.
@ -4282,13 +4280,13 @@ class CGDictionary(CGThing):
conversion = ("if isNull {\n" conversion = ("if isNull {\n"
" found = 0;\n" " found = 0;\n"
"} else if ${propCheck} == 0 {\n" "} else if ${propCheck} == 0 {\n"
" return 0;\n" " return Err(());\n"
"}\n") "}\n")
if member.defaultValue: if member.defaultValue:
conversion += ( conversion += (
"if found != 0 {\n" "if found != 0 {\n"
" if ${propGet} == 0 {\n" " if ${propGet} == 0 {\n"
" return 0;\n" " return Err(());\n"
" }\n" " }\n"
"}\n" "}\n"
"${convert}") "${convert}")
@ -4297,7 +4295,7 @@ class CGDictionary(CGThing):
"if found != 0 {\n" "if found != 0 {\n"
" ${prop}.Construct();\n" " ${prop}.Construct();\n"
" if ${propGet} == 0 {\n" " if ${propGet} == 0 {\n"
" return 0;\n" " return Err(());\n"
" }\n" " }\n"
"${convert}\n" "${convert}\n"
"}") "}")