Remove the argcAndIndex argument from instantiateJSToNativeConversionTemplate.

This commit does not change the generated code.
This commit is contained in:
Ms2ger 2014-05-03 15:46:10 +02:00
parent 5334d8bb25
commit 6ed924f515

View file

@ -275,14 +275,15 @@ class CGMethodCall(CGThing):
# The argument at index distinguishingIndex can't possibly # The argument at index distinguishingIndex can't possibly
# be unset here, because we've already checked that argc is # be unset here, because we've already checked that argc is
# large enough that we can examine this argument. # large enough that we can examine this argument.
template, declType, _, needsRooting = getJSToNativeConversionTemplate(
type, descriptor, failureCode="break;", isDefinitelyObject=True)
testCode = instantiateJSToNativeConversionTemplate( testCode = instantiateJSToNativeConversionTemplate(
getJSToNativeConversionTemplate(type, descriptor, template,
failureCode="break;", {"val": distinguishingArg},
isDefinitelyObject=True), declType,
{ "arg%d" % distinguishingIndex,
"declName" : "arg%d" % distinguishingIndex, needsRooting)
"val" : distinguishingArg
})
# Indent by 4, since we need to indent further than our "do" statement # Indent by 4, since we need to indent further than our "do" statement
caseBody.append(CGIndenter(testCode, 4)); caseBody.append(CGIndenter(testCode, 4));
@ -799,41 +800,24 @@ def getJSToNativeConversionTemplate(type, descriptorProvider, failureCode=None,
return handleOptional(template, declType, isOptional, defaultStr) return handleOptional(template, declType, isOptional, defaultStr)
def instantiateJSToNativeConversionTemplate(templateTuple, replacements, def instantiateJSToNativeConversionTemplate(templateBody, replacements,
argcAndIndex=None): declType, declName, needsRooting):
""" """
Take a tuple as returned by getJSToNativeConversionTemplate and a set of Take the templateBody and declType as returned by
replacements as required by the strings in such a tuple, and generate code getJSToNativeConversionTemplate, a set of replacements as required by the
to convert into stack C++ types. strings in such a templateBody, and a declName, and generate code to
convert into a stack Rust binding with that name.
If argcAndIndex is not None it must be a dict that can be used to
replace ${argc} and ${index}, where ${index} is the index of this
argument (0-based) and ${argc} is the total number of arguments.
""" """
(templateBody, declType, dealWithOptional, needsRooting) = templateTuple
if dealWithOptional and argcAndIndex is None:
raise TypeError("Have to deal with optional things, but don't know how")
if argcAndIndex is not None and declType is None:
raise TypeError("Need to predeclare optional things, so they will be "
"outside the check for big enough arg count!");
result = CGList([], "\n") result = CGList([], "\n")
conversion = CGGeneric( conversion = CGGeneric(
string.Template(templateBody).substitute(replacements) string.Template(templateBody).substitute(replacements)
) )
if argcAndIndex is not None:
condition = string.Template("${index} < ${argc}").substitute(argcAndIndex)
conversion = CGIfElseWrapper(condition,
conversion,
CGGeneric("None"))
if declType is not None: if declType is not None:
newDecl = [ newDecl = [
CGGeneric("let mut "), CGGeneric("let mut "),
CGGeneric(replacements["declName"]), CGGeneric(declName),
CGGeneric(": "), CGGeneric(": "),
declType, declType,
CGGeneric(" = "), CGGeneric(" = "),
@ -849,8 +833,8 @@ def instantiateJSToNativeConversionTemplate(templateTuple, replacements,
result.append(CGGeneric("")) result.append(CGGeneric(""))
if needsRooting: if needsRooting:
rootBody = "let ${declName} = ${declName}.root();" rootBody = "let %s = %s.root();" % (declName, declName)
result.append(CGGeneric(string.Template(rootBody).substitute(replacements))) result.append(CGGeneric(rootBody))
result.append(CGGeneric("")) result.append(CGGeneric(""))
return result; return result;
@ -881,45 +865,45 @@ class CGArgumentConverter(CGThing):
def __init__(self, argument, index, argv, argc, descriptorProvider, def __init__(self, argument, index, argv, argc, descriptorProvider,
invalidEnumValueFatal=True): invalidEnumValueFatal=True):
CGThing.__init__(self) CGThing.__init__(self)
self.argument = argument
if argument.variadic: if argument.variadic:
raise TypeError("We don't support variadic arguments yet " + raise TypeError("We don't support variadic arguments yet " +
str(argument.location)) str(argument.location))
assert(not argument.defaultValue or argument.optional) assert(not argument.defaultValue or argument.optional)
replacer = { replacer = {
"index" : index, "index": index,
"argc" : argc, "argc": argc,
"argv" : argv "argv": argv
} }
self.replacementVariables = { condition = string.Template("${index} < ${argc}").substitute(replacer)
"declName" : "arg%d" % index,
} replacementVariables = {
self.replacementVariables["val"] = string.Template( "val": string.Template("(*${argv}.offset(${index}))").substitute(replacer),
"(*${argv}.offset(${index}))" }
).substitute(replacer)
if argument.defaultValue: if argument.defaultValue:
self.replacementVariables["haveValue"] = string.Template( replacementVariables["haveValue"] = condition
"${index} < ${argc}").substitute(replacer)
self.descriptorProvider = descriptorProvider template, declType, dealWithOptional, needsRooting = getJSToNativeConversionTemplate(
if self.argument.optional and not self.argument.defaultValue: argument.type,
self.argcAndIndex = replacer descriptorProvider,
else: isOptional=argument.optional and not argument.defaultValue,
self.argcAndIndex = None invalidEnumValueFatal=invalidEnumValueFatal,
self.invalidEnumValueFatal = invalidEnumValueFatal defaultValue=argument.defaultValue,
treatNullAs=argument.treatNullAs,
isEnforceRange=argument.enforceRange,
isClamp=argument.clamp)
if dealWithOptional:
template = CGIfElseWrapper(condition,
CGGeneric(template),
CGGeneric("None")).define()
self.converter = instantiateJSToNativeConversionTemplate(
template, replacementVariables, declType, "arg%d" % index,
needsRooting)
def define(self): def define(self):
return instantiateJSToNativeConversionTemplate( return self.converter.define()
getJSToNativeConversionTemplate(self.argument.type,
self.descriptorProvider,
isOptional=(self.argcAndIndex is not None),
invalidEnumValueFatal=self.invalidEnumValueFatal,
defaultValue=self.argument.defaultValue,
treatNullAs=self.argument.treatNullAs,
isEnforceRange=self.argument.enforceRange,
isClamp=self.argument.clamp),
self.replacementVariables,
self.argcAndIndex).define()
def wrapForType(jsvalRef, result='result', successCode='return 1;'): def wrapForType(jsvalRef, result='result', successCode='return 1;'):
@ -2709,7 +2693,7 @@ def getUnionTypeTemplateVars(type, descriptorProvider):
name = type.name name = type.name
typeName = "/*" + type.name + "*/" typeName = "/*" + type.name + "*/"
(template, _, _, _) = getJSToNativeConversionTemplate( template, _, _, _ = getJSToNativeConversionTemplate(
type, descriptorProvider, failureCode="return Ok(None);", type, descriptorProvider, failureCode="return Ok(None);",
exceptionCode='return Err(());', exceptionCode='return Err(());',
isDefinitelyObject=True, isOptional=False) isDefinitelyObject=True, isOptional=False)
@ -3360,13 +3344,14 @@ class CGProxySpecialOperation(CGPerSignatureCall):
if operation.isSetter() or operation.isCreator(): if operation.isSetter() or operation.isCreator():
# arguments[0] is the index or name of the item that we're setting. # arguments[0] is the index or name of the item that we're setting.
argument = arguments[1] argument = arguments[1]
template = getJSToNativeConversionTemplate(argument.type, descriptor, template, declType, _, needsRooting = getJSToNativeConversionTemplate(
treatNullAs=argument.treatNullAs) argument.type, descriptor, treatNullAs=argument.treatNullAs)
templateValues = { templateValues = {
"declName": argument.identifier.name,
"val": "(*desc).value", "val": "(*desc).value",
} }
self.cgRoot.prepend(instantiateJSToNativeConversionTemplate(template, templateValues)) self.cgRoot.prepend(instantiateJSToNativeConversionTemplate(
template, templateValues, declType, argument.identifier.name,
needsRooting))
elif operation.isGetter(): elif operation.isGetter():
self.cgRoot.prepend(CGGeneric("let mut found = false;")) self.cgRoot.prepend(CGGeneric("let mut found = false;"))
@ -4879,21 +4864,23 @@ class CallbackMember(CGNativeMember):
def getResultConversion(self): def getResultConversion(self):
replacements = { replacements = {
"val": "rval", "val": "rval",
"declName" : "rvalDecl", }
}
if isJSImplementedDescriptor(self.descriptorProvider): if isJSImplementedDescriptor(self.descriptorProvider):
isCallbackReturnValue = "JSImpl" isCallbackReturnValue = "JSImpl"
else: else:
isCallbackReturnValue = "Callback" isCallbackReturnValue = "Callback"
template, declType, _, needsRooting = getJSToNativeConversionTemplate(
self.retvalType,
self.descriptorProvider,
exceptionCode=self.exceptionCode,
isCallbackReturnValue=isCallbackReturnValue,
# XXXbz we should try to do better here
sourceDescription="return value")
convertType = instantiateJSToNativeConversionTemplate( convertType = instantiateJSToNativeConversionTemplate(
getJSToNativeConversionTemplate(self.retvalType, template, replacements, declType, "rvalDecl", needsRooting)
self.descriptorProvider,
exceptionCode=self.exceptionCode,
isCallbackReturnValue=isCallbackReturnValue,
# XXXbz we should try to do better here
sourceDescription="return value"),
replacements)
assignRetval = string.Template( assignRetval = string.Template(
self.getRetvalInfo(self.retvalType, self.getRetvalInfo(self.retvalType,
False)[2]).substitute(replacements) False)[2]).substitute(replacements)