Permalink
Browse files

Implement a MakeFunction3 to handle setting annotations for both comp…

…iled and interpreted cases.
  • Loading branch information...
1 parent b424a13 commit f2080052745a378dfedb0a6d09a21f0125739ec6 @jdhardy committed Mar 25, 2013
View
1 Languages/IronPython/IronPython/Compiler/Ast/AstMethods.cs
@@ -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);
View
25 Languages/IronPython/IronPython/Compiler/Ast/FunctionDefinition.cs
@@ -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());
@@ -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
);
}
@@ -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;
@@ -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) {
@@ -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) {
@@ -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;
View
7 Languages/IronPython/IronPython/Runtime/Operations/PythonOps.cs
@@ -3139,6 +3139,13 @@ public static partial class PythonOps {
}
[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);

0 comments on commit f208005

Please sign in to comment.