Skip to content

Commit

Permalink
Implement a MakeFunction3 to handle setting annotations for both comp…
Browse files Browse the repository at this point in the history
…iled and interpreted cases.
  • Loading branch information
jdhardy committed Mar 25, 2013
1 parent b424a13 commit f208005
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions Languages/IronPython/IronPython/Compiler/Ast/AstMethods.cs
Expand Up @@ -77,6 +77,7 @@ static class AstMethods {
public static readonly MethodInfo GetGlobalContext = GetMethod((Func<CodeContext, CodeContext>)PythonOps.GetGlobalContext);
public static readonly MethodInfo GetParentContextFromFunction = GetMethod((Func<PythonFunction, CodeContext>)PythonOps.GetParentContextFromFunction);
public static readonly MethodInfo MakeFunction = GetMethod((Func<CodeContext, FunctionCode, object, object[], object>)PythonOps.MakeFunction);
public static readonly MethodInfo MakeFunction3 = GetMethod((Func<CodeContext, FunctionCode, object, object[], object[], object>)PythonOps.MakeFunction3);
public static readonly MethodInfo MakeFunctionDebug = GetMethod((Func<CodeContext/*!*/, FunctionCode, object, object[], Delegate, object>)PythonOps.MakeFunctionDebug);
public static readonly MethodInfo MakeClosureCell = GetMethod((Func<ClosureCell>)PythonOps.MakeClosureCell);
public static readonly MethodInfo MakeClosureCellWithValue = GetMethod((Func<object, ClosureCell>)PythonOps.MakeClosureCellWithValue);
Expand Down
25 changes: 17 additions & 8 deletions Languages/IronPython/IronPython/Compiler/Ast/FunctionDefinition.cs
Expand Up @@ -350,10 +350,21 @@ public FunctionDefinition(string name, Parameter[] parameters, Statement body, S
/// </summary>
internal MSAst.Expression MakeFunctionExpression() {
List<MSAst.Expression> defaults = new List<MSAst.Expression>(0);
var annotations = new List<MSAst.Expression>();
foreach (var param in _parameters) {
if (param.DefaultValue != null) {
defaults.Add(AstUtils.Convert(param.DefaultValue, typeof(object)));
}

if (param.Annotation != null) {
annotations.Add(AstUtils.Convert(param.Annotation, typeof(object)));
annotations.Add(AstUtils.Constant(param.Name, typeof(object)));
}
}

if (ReturnAnnotation != null) {
annotations.Add(AstUtils.Convert(ReturnAnnotation, typeof(object)));
annotations.Add(AstUtils.Constant("return", typeof(object)));
}

MSAst.Expression funcCode = GlobalParent.Constant(GetOrMakeFunctionCode());
Expand All @@ -380,13 +391,14 @@ public FunctionDefinition(string name, Parameter[] parameters, Statement body, S
);
} else {
ret = Ast.Call(
AstMethods.MakeFunction, // method
AstMethods.MakeFunction3, // method
Parent.LocalContext, // 1. Emit CodeContext
FuncCodeExpr, // 2. FunctionCode
((IPythonGlobalExpression)GetVariableExpression(_nameVariable)).RawValue(), // 3. module name
defaults.Count == 0 ? // 4. default values
AstUtils.Constant(null, typeof(object[])) :
(MSAst.Expression)Ast.NewArrayInit(typeof(object), defaults)
(MSAst.Expression)Ast.NewArrayInit(typeof(object), defaults),
(MSAst.Expression)Ast.NewArrayInit(typeof(object), annotations) // 5. Annotations
);
}

Expand All @@ -396,7 +408,7 @@ public FunctionDefinition(string name, Parameter[] parameters, Statement body, S
#region IInstructionProvider Members

void IInstructionProvider.AddInstructions(LightCompiler compiler) {
if (_decorators != null) {
if ((_decorators != null) || true) {
// decorators aren't supported, skip using the optimized instruction.
compiler.Compile(Reduce());
return;
Expand Down Expand Up @@ -459,8 +471,6 @@ public FunctionDefinition(string name, Parameter[] parameters, Statement body, S
}
// make the array
compiler.Instructions.EmitNewArrayInit(typeof(object), annotations.Count * 2);
// create a PythonDictionary from the array
compiler.EmitCall(AstMethods.MakeDictFromItems);
}

private static object[] FlattenForCommonDictionaryStorage<TKey, TValue>(IDictionary<TKey, TValue> dict) {
Expand Down Expand Up @@ -531,7 +541,7 @@ class FunctionDefinitionInstruction : Instruction {
}

public override int Run(InterpretedFrame frame) {
PythonDictionary annotations = (PythonDictionary)frame.Pop();
object[] annotationsData = (object[])frame.Pop();

object[] defaults;
if (_defaultCount > 0) {
Expand All @@ -552,8 +562,7 @@ class FunctionDefinitionInstruction : Instruction {

CodeContext context = (CodeContext)frame.Pop();

PythonFunction func = (PythonFunction)PythonOps.MakeFunction(context, _def.FunctionCode, modName, defaults);
func.__annotations__ = annotations;
PythonFunction func = (PythonFunction)PythonOps.MakeFunction3(context, _def.FunctionCode, modName, defaults, annotationsData);
frame.Push(func);

return +1;
Expand Down
Expand Up @@ -3138,6 +3138,13 @@ public static partial class PythonOps {
return new PythonFunction(context, funcInfo, modName, defaults, null);
}

[NoSideEffects]
public static object MakeFunction3(CodeContext/*!*/ context, FunctionCode funcInfo, object modName, object[] defaults, object[] annotations) {
var func = new PythonFunction(context, funcInfo, modName, defaults, null);
func.__annotations__ = MakeDictFromItems(annotations);
return func;
}

[NoSideEffects]
public static object MakeFunctionDebug(CodeContext/*!*/ context, FunctionCode funcInfo, object modName, object[] defaults, Delegate target) {
funcInfo.SetDebugTarget(PythonContext.GetContext(context), target);
Expand Down

0 comments on commit f208005

Please sign in to comment.