mirror of
https://github.com/servo/servo.git
synced 2025-07-25 08:10:21 +01:00
refactor(script): refactor common code into PropertyDefiner.generateUnguardedArray
There are code fragments in `(Method|Attr)Definer.generateArray` that are much alike. This commit refactors them into a new method of `PropertyDefiner` named `generateUnguardedArray` (in contrast to the existing method `generateGuardedArray`).
This commit is contained in:
parent
c28e98ec40
commit
2e0dd0816f
1 changed files with 38 additions and 20 deletions
|
@ -1642,6 +1642,33 @@ class PropertyDefiner:
|
|||
+ "];\n") % (name, specType)
|
||||
return specsArray + prefArray
|
||||
|
||||
def generateUnguardedArray(self, array, name, specTemplate, specTerminator,
|
||||
specType, getCondition, getDataTuple):
|
||||
"""
|
||||
Takes the same set of parameters as generateGuardedArray but instead
|
||||
generates a single, flat array of type `&[specType]` that contains all
|
||||
provided members. The provided members' conditions shall be homogeneous,
|
||||
or else this method will fail.
|
||||
"""
|
||||
|
||||
# this method can't handle heterogeneous condition
|
||||
groups = groupby(array, lambda m: getCondition(m, self.descriptor))
|
||||
assert len(list(groups)) == 1
|
||||
|
||||
origTemplate = specTemplate
|
||||
if isinstance(specTemplate, str):
|
||||
specTemplate = lambda _: origTemplate # noqa
|
||||
|
||||
specsArray = [specTemplate(m) % getDataTuple(m) for m in array]
|
||||
specsArray.append(specTerminator)
|
||||
|
||||
return dedent(
|
||||
"""
|
||||
const %s: &[%s] = &[
|
||||
%s
|
||||
];
|
||||
""") % (name, specType, ',\n'.join(specsArray))
|
||||
|
||||
|
||||
# The length of a method is the minimum of the lengths of the
|
||||
# argument lists of all its overloads.
|
||||
|
@ -1829,16 +1856,11 @@ class MethodDefiner(PropertyDefiner):
|
|||
' }')
|
||||
|
||||
if self.crossorigin:
|
||||
groups = groupby(array, lambda m: condition(m, self.descriptor))
|
||||
assert len(list(groups)) == 1 # can't handle mixed condition
|
||||
elems = [specTemplate % specData(m) for m in array]
|
||||
return dedent(
|
||||
"""
|
||||
const %s: &[JSFunctionSpec] = &[
|
||||
%s,
|
||||
%s,
|
||||
];
|
||||
""") % (name, ',\n'.join(elems), specTerminator)
|
||||
return self.generateUnguardedArray(
|
||||
array, name,
|
||||
specTemplate, specTerminator,
|
||||
'JSFunctionSpec',
|
||||
condition, specData)
|
||||
else:
|
||||
return self.generateGuardedArray(
|
||||
array, name,
|
||||
|
@ -1983,16 +2005,12 @@ class AttrDefiner(PropertyDefiner):
|
|||
"""
|
||||
|
||||
if self.crossorigin:
|
||||
groups = groupby(array, lambda m: condition(m, self.descriptor))
|
||||
assert len(list(groups)) == 1 # can't handle mixed condition
|
||||
elems = [template(m) % specData(m) for m in array]
|
||||
return dedent(
|
||||
"""
|
||||
const %s: &[JSPropertySpec] = &[
|
||||
%s,
|
||||
JSPropertySpec::ZERO,
|
||||
];
|
||||
""") % (name, ',\n'.join(elems))
|
||||
return self.generateUnguardedArray(
|
||||
array, name,
|
||||
template,
|
||||
' JSPropertySpec::ZERO',
|
||||
'JSPropertySpec',
|
||||
condition, specData)
|
||||
else:
|
||||
return self.generateGuardedArray(
|
||||
array, name,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue