3838import com .oracle .graal .python .parser .ExecutionCellSlots ;
3939import com .oracle .graal .python .runtime .PythonContext ;
4040import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
41+ import com .oracle .truffle .api .Assumption ;
42+ import com .oracle .truffle .api .CompilerDirectives ;
4143import com .oracle .truffle .api .RootCallTarget ;
44+ import com .oracle .truffle .api .Truffle ;
4245import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
4346import com .oracle .truffle .api .frame .VirtualFrame ;
4447import com .oracle .truffle .api .nodes .ExplodeLoop ;
@@ -57,6 +60,9 @@ public class FunctionDefinitionNode extends ExpressionDefinitionNode {
5760 @ Child private WriteAttributeToDynamicObjectNode writeNameNode = WriteAttributeToDynamicObjectNode .create ();
5861 @ Child private PythonObjectFactory factory = PythonObjectFactory .create ();
5962
63+ private final Assumption sharedCodeStableAssumption = Truffle .getRuntime ().createAssumption ("shared code stable assumption" );
64+ private final Assumption sharedDefaultsStableAssumption = Truffle .getRuntime ().createAssumption ("shared defaults stable assumption" );
65+
6066 public FunctionDefinitionNode (String functionName , String enclosingClassName , ExpressionNode doc , ExpressionNode [] defaults , KwDefaultExpressionNode [] kwDefaults ,
6167 RootCallTarget callTarget ,
6268 DefinitionCellSlots definitionCellSlots , ExecutionCellSlots executionCellSlots ) {
@@ -94,7 +100,26 @@ public Object execute(VirtualFrame frame) {
94100 Object [] defaultValues = computeDefaultValues (frame );
95101 PKeyword [] kwDefaultValues = computeKwDefaultValues (frame );
96102 PCell [] closure = getClosureFromGeneratorOrFunctionLocals (frame );
97- return withDocString (frame , factory ().createFunction (functionName , enclosingClassName , callTarget , PArguments .getGlobals (frame ), defaultValues , kwDefaultValues , closure , writeNameNode ));
103+ Assumption codeStableAssumption ;
104+ Assumption defaultsStableAssumption ;
105+ if (CompilerDirectives .inCompiledCode ()) {
106+ codeStableAssumption = getSharedCodeStableAssumption ();
107+ defaultsStableAssumption = getSharedDefaultsStableAssumption ();
108+ } else {
109+ codeStableAssumption = Truffle .getRuntime ().createAssumption ();
110+ defaultsStableAssumption = Truffle .getRuntime ().createAssumption ();
111+ }
112+ return withDocString (frame , factory ().createFunction (functionName , enclosingClassName , callTarget , PArguments .getGlobals (frame ), defaultValues , kwDefaultValues , closure , writeNameNode ,
113+ codeStableAssumption , defaultsStableAssumption ));
114+
115+ }
116+
117+ private Assumption getSharedDefaultsStableAssumption () {
118+ return sharedDefaultsStableAssumption ;
119+ }
120+
121+ private Assumption getSharedCodeStableAssumption () {
122+ return sharedCodeStableAssumption ;
98123 }
99124
100125 @ ExplodeLoop
0 commit comments