/// <summary>
/// Creates the LambdaExpression which implements the body of the function.
///
/// The functions signature is either "object Function(PythonFunction, ...)"
/// where there is one object parameter for each user defined parameter or
/// object Function(PythonFunction, object[]) for functions which take more
/// than PythonCallTargets.MaxArgs arguments.
/// </summary>
private LightLambdaExpression CreateFunctionLambda()
{
bool needsWrapperMethod = _parameters.Length > PythonCallTargets.MaxArgs;
Type delegateType = GetDelegateType(_parameters, needsWrapperMethod, out _);
MSAst.ParameterExpression localContext = null;
ReadOnlyCollectionBuilder <MSAst.ParameterExpression> locals = new ReadOnlyCollectionBuilder <MSAst.ParameterExpression>();
if (NeedsLocalContext)
{
localContext = LocalCodeContextVariable;
locals.Add(localContext);
}
MSAst.ParameterExpression[] parameters = CreateParameters(needsWrapperMethod, locals);
List <MSAst.Expression> init = new List <MSAst.Expression>();
foreach (var param in _parameters)
{
if (GetVariableExpression(param.PythonVariable) is IPythonVariableExpression pyVar)
{
var varInit = pyVar.Create();
if (varInit != null)
{
init.Add(varInit);
}
}
}
// Transform the parameters.
init.Add(Ast.ClearDebugInfo(GlobalParent.Document));
locals.Add(PythonAst._globalContext);
init.Add(Ast.Assign(PythonAst._globalContext, new GetGlobalContextExpression(_parentContext)));
GlobalParent.PrepareScope(locals, init);
// Create variables and references. Since references refer to
// parameters, do this after parameters have been created.
CreateFunctionVariables(locals, init);
// Initialize parameters - unpack tuples.
// Since tuples unpack into locals, this must be done after locals have been created.
InitializeParameters(init, needsWrapperMethod, parameters);
List <MSAst.Expression> statements = new List <MSAst.Expression>();
// add beginning sequence point
var start = GlobalParent.IndexToLocation(StartIndex);
statements.Add(GlobalParent.AddDebugInfo(
AstUtils.Empty(),
new SourceSpan(new SourceLocation(0, start.Line, start.Column), new SourceLocation(0, start.Line, int.MaxValue))));
// For generators, we need to do a check before the first statement for Generator.Throw() / Generator.Close().
// The exception traceback needs to come from the generator's method body, and so we must do the check and throw
// from inside the generator.
if (IsGenerator)
{
MSAst.Expression s1 = YieldExpression.CreateCheckThrowExpression(SourceSpan.None);
statements.Add(s1);
}
if (Body.CanThrow && !(Body is SuiteStatement) && Body.StartIndex != -1)
{
statements.Add(UpdateLineNumber(GlobalParent.IndexToLocation(Body.StartIndex).Line));
}
statements.Add(Body);
MSAst.Expression body = Ast.Block(statements);
if (Body.CanThrow && GlobalParent.PyContext.PythonOptions.Frames)
{
body = AddFrame(LocalContext, Ast.Property(_functionParam, typeof(PythonFunction).GetProperty(nameof(PythonFunction.__code__))), body);
locals.Add(FunctionStackVariable);
}
body = AddProfiling(body);
body = WrapScopeStatements(body, Body.CanThrow);
body = Ast.Block(body, AstUtils.Empty());
body = AddReturnTarget(body);
MSAst.Expression bodyStmt = body;
if (localContext != null)
{
var createLocal = CreateLocalContext(_parentContext);
init.Add(
Ast.Assign(
localContext,
createLocal
)
);
}
init.Add(bodyStmt);
bodyStmt = Ast.Block(init);
// wrap a scope if needed
bodyStmt = Ast.Block(locals.ToReadOnlyCollection(), bodyStmt);
return(AstUtils.LightLambda(
typeof(object),
delegateType,
AddDefaultReturn(bodyStmt, typeof(object)),
Name + "$" + Interlocked.Increment(ref _lambdaId),
parameters
));
}