mirror of
https://github.com/servo/servo.git
synced 2025-08-02 12:10:29 +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)
|
+ "];\n") % (name, specType)
|
||||||
return specsArray + prefArray
|
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
|
# The length of a method is the minimum of the lengths of the
|
||||||
# argument lists of all its overloads.
|
# argument lists of all its overloads.
|
||||||
|
@ -1829,16 +1856,11 @@ class MethodDefiner(PropertyDefiner):
|
||||||
' }')
|
' }')
|
||||||
|
|
||||||
if self.crossorigin:
|
if self.crossorigin:
|
||||||
groups = groupby(array, lambda m: condition(m, self.descriptor))
|
return self.generateUnguardedArray(
|
||||||
assert len(list(groups)) == 1 # can't handle mixed condition
|
array, name,
|
||||||
elems = [specTemplate % specData(m) for m in array]
|
specTemplate, specTerminator,
|
||||||
return dedent(
|
'JSFunctionSpec',
|
||||||
"""
|
condition, specData)
|
||||||
const %s: &[JSFunctionSpec] = &[
|
|
||||||
%s,
|
|
||||||
%s,
|
|
||||||
];
|
|
||||||
""") % (name, ',\n'.join(elems), specTerminator)
|
|
||||||
else:
|
else:
|
||||||
return self.generateGuardedArray(
|
return self.generateGuardedArray(
|
||||||
array, name,
|
array, name,
|
||||||
|
@ -1983,16 +2005,12 @@ class AttrDefiner(PropertyDefiner):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if self.crossorigin:
|
if self.crossorigin:
|
||||||
groups = groupby(array, lambda m: condition(m, self.descriptor))
|
return self.generateUnguardedArray(
|
||||||
assert len(list(groups)) == 1 # can't handle mixed condition
|
array, name,
|
||||||
elems = [template(m) % specData(m) for m in array]
|
template,
|
||||||
return dedent(
|
' JSPropertySpec::ZERO',
|
||||||
"""
|
'JSPropertySpec',
|
||||||
const %s: &[JSPropertySpec] = &[
|
condition, specData)
|
||||||
%s,
|
|
||||||
JSPropertySpec::ZERO,
|
|
||||||
];
|
|
||||||
""") % (name, ',\n'.join(elems))
|
|
||||||
else:
|
else:
|
||||||
return self.generateGuardedArray(
|
return self.generateGuardedArray(
|
||||||
array, name,
|
array, name,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue