mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
codegen: Move conversion behaviour to a common function
This commit is contained in:
parent
e977a6e69a
commit
2f1eee599c
1 changed files with 32 additions and 24 deletions
|
@ -775,20 +775,7 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
|
|||
raise TypeError("Can't handle SpiderMonkey interface arguments yet")
|
||||
|
||||
if type.isDOMString():
|
||||
assert not isEnforceRange and not isClamp
|
||||
|
||||
treatAs = {
|
||||
"Default": "StringificationBehavior::Default",
|
||||
"EmptyString": "StringificationBehavior::Empty",
|
||||
}
|
||||
if treatNullAs not in treatAs:
|
||||
raise TypeError("We don't support [TreatNullAs=%s]" % treatNullAs)
|
||||
if type.nullable():
|
||||
# Note: the actual behavior passed here doesn't matter for nullable
|
||||
# strings.
|
||||
nullBehavior = "StringificationBehavior::Default"
|
||||
else:
|
||||
nullBehavior = treatAs[treatNullAs]
|
||||
nullBehavior = getConversionConfigForType(type, isEnforceRange, isClamp, treatNullAs)
|
||||
|
||||
conversionCode = (
|
||||
"match FromJSValConvertible::from_jsval(cx, ${val}, %s) {\n"
|
||||
|
@ -1002,16 +989,7 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
|
|||
if not type.isPrimitive():
|
||||
raise TypeError("Need conversion for argument type '%s'" % str(type))
|
||||
|
||||
if type.isInteger():
|
||||
if isEnforceRange:
|
||||
conversionBehavior = "ConversionBehavior::EnforceRange"
|
||||
elif isClamp:
|
||||
conversionBehavior = "ConversionBehavior::Clamp"
|
||||
else:
|
||||
conversionBehavior = "ConversionBehavior::Default"
|
||||
else:
|
||||
assert not isEnforceRange and not isClamp
|
||||
conversionBehavior = "()"
|
||||
conversionBehavior = getConversionConfigForType(type, isEnforceRange, isClamp, treatNullAs)
|
||||
|
||||
if failureCode is None:
|
||||
failureCode = 'return false'
|
||||
|
@ -1215,6 +1193,36 @@ def typeNeedsCx(type, retVal=False):
|
|||
return type.isAny() or type.isObject()
|
||||
|
||||
|
||||
# Returns a conversion behavior suitable for a type
|
||||
def getConversionConfigForType(type, isEnforceRange, isClamp, treatNullAs):
|
||||
if type.isSequence():
|
||||
return getConversionConfigForType(type.unroll(), isEnforceRange, isClamp, treatNullAs)
|
||||
if type.isDOMString():
|
||||
assert not isEnforceRange and not isClamp
|
||||
|
||||
treatAs = {
|
||||
"Default": "StringificationBehavior::Default",
|
||||
"EmptyString": "StringificationBehavior::Empty",
|
||||
}
|
||||
if treatNullAs not in treatAs:
|
||||
raise TypeError("We don't support [TreatNullAs=%s]" % treatNullAs)
|
||||
if type.nullable():
|
||||
# Note: the actual behavior passed here doesn't matter for nullable
|
||||
# strings.
|
||||
return "StringificationBehavior::Default"
|
||||
else:
|
||||
return treatAs[treatNullAs]
|
||||
if type.isInteger():
|
||||
if isEnforceRange:
|
||||
return "ConversionBehavior::EnforceRange"
|
||||
elif isClamp:
|
||||
return "ConversionBehavior::Clamp"
|
||||
else:
|
||||
return "ConversionBehavior::Default"
|
||||
assert not isEnforceRange and not isClamp
|
||||
return "()"
|
||||
|
||||
|
||||
# Returns a CGThing containing the type of the return value.
|
||||
def getRetvalDeclarationForType(returnType, descriptorProvider):
|
||||
if returnType is None or returnType.isVoid():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue