202
202
import com .oracle .truffle .api .frame .VirtualFrame ;
203
203
import com .oracle .truffle .api .interop .UnsupportedMessageException ;
204
204
import com .oracle .truffle .api .library .CachedLibrary ;
205
+ import com .oracle .truffle .api .nodes .ExplodeLoop ;
205
206
import com .oracle .truffle .api .nodes .Node ;
206
207
import com .oracle .truffle .api .nodes .UnexpectedResultException ;
207
208
import com .oracle .truffle .api .object .HiddenKey ;
@@ -2177,10 +2178,16 @@ public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String
2177
2178
return factory ().createFunction (name , getTypeName (cls ), code , globals , null );
2178
2179
}
2179
2180
2181
+ @ Specialization
2182
+ public PFunction function (LazyPythonClass cls , PCode code , PDict globals , @ SuppressWarnings ("unused" ) PNone name , @ SuppressWarnings ("unused" ) PNone defaultArgs , PTuple closure ,
2183
+ @ Shared ("getObjectArrayNode" ) @ Cached GetObjectArrayNode getObjectArrayNode ) {
2184
+ return factory ().createFunction ("anonymous" , getTypeName (cls ), code , globals , getClosure (getObjectArrayNode .execute (closure )));
2185
+ }
2186
+
2180
2187
@ Specialization
2181
2188
public PFunction function (LazyPythonClass cls , PCode code , PDict globals , String name , @ SuppressWarnings ("unused" ) PNone defaultArgs , PTuple closure ,
2182
2189
@ Shared ("getObjectArrayNode" ) @ Cached GetObjectArrayNode getObjectArrayNode ) {
2183
- return factory ().createFunction (name , getTypeName (cls ), code , globals , ( PCell []) getObjectArrayNode .execute (closure ));
2190
+ return factory ().createFunction (name , getTypeName (cls ), code , globals , getClosure ( getObjectArrayNode .execute (closure ) ));
2184
2191
}
2185
2192
2186
2193
@ Specialization
@@ -2194,7 +2201,18 @@ public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String
2194
2201
public PFunction function (LazyPythonClass cls , PCode code , PDict globals , String name , PTuple defaultArgs , PTuple closure ,
2195
2202
@ Shared ("getObjectArrayNode" ) @ Cached GetObjectArrayNode getObjectArrayNode ) {
2196
2203
// TODO split defaults of positional args from kwDefaults
2197
- return factory ().createFunction (name , getTypeName (cls ), code , globals , getObjectArrayNode .execute (defaultArgs ), null , (PCell []) getObjectArrayNode .execute (closure ));
2204
+ return factory ().createFunction (name , getTypeName (cls ), code , globals , getObjectArrayNode .execute (defaultArgs ), null , getClosure (getObjectArrayNode .execute (closure )));
2205
+ }
2206
+
2207
+ @ ExplodeLoop
2208
+ private PCell [] getClosure (Object [] closure ) {
2209
+ assert closure != null ;
2210
+ PCell [] cells = new PCell [closure .length ];
2211
+ for (int i = 0 ; i < closure .length ; i ++) {
2212
+ assert closure [i ] instanceof PCell ;
2213
+ cells [i ] = (PCell ) closure [i ];
2214
+ }
2215
+ return cells ;
2198
2216
}
2199
2217
2200
2218
@ Fallback
@@ -2986,6 +3004,35 @@ Object call(VirtualFrame frame, LazyPythonClass cls, int argcount, int kwonlyarg
2986
3004
lnotabBytes );
2987
3005
}
2988
3006
3007
+ @ Specialization (guards = {"codestringBufferLib.isBuffer(codestring)" , "lnotabBufferLib.isBuffer(lnotab)" }, limit = "2" , rewriteOn = UnsupportedMessageException .class )
3008
+ Object call (VirtualFrame frame , LazyPythonClass cls , Object argcount , Object kwonlyargcount ,
3009
+ Object nlocals , Object stacksize , Object flags ,
3010
+ Object codestring , PTuple constants , PTuple names ,
3011
+ PTuple varnames , Object filename , Object name ,
3012
+ Object firstlineno , Object lnotab ,
3013
+ PTuple freevars , PTuple cellvars ,
3014
+ @ CachedLibrary ("codestring" ) PythonObjectLibrary codestringBufferLib ,
3015
+ @ CachedLibrary ("lnotab" ) PythonObjectLibrary lnotabBufferLib ,
3016
+ @ CachedLibrary (limit = "2" ) PythonObjectLibrary objectLibrary ,
3017
+ @ Cached CodeNodes .CreateCodeNode createCodeNode ,
3018
+ @ Cached GetObjectArrayNode getObjectArrayNode ) throws UnsupportedMessageException {
3019
+ byte [] codeBytes = codestringBufferLib .getBufferBytes (codestring );
3020
+ byte [] lnotabBytes = lnotabBufferLib .getBufferBytes (lnotab );
3021
+
3022
+ Object [] constantsArr = getObjectArrayNode .execute (constants );
3023
+ Object [] namesArr = getObjectArrayNode .execute (names );
3024
+ Object [] varnamesArr = getObjectArrayNode .execute (varnames );
3025
+ Object [] freevarsArr = getObjectArrayNode .execute (freevars );
3026
+ Object [] cellcarsArr = getObjectArrayNode .execute (cellvars );
3027
+
3028
+ return createCodeNode .execute (frame , cls , objectLibrary .asSize (argcount ), objectLibrary .asSize (kwonlyargcount ),
3029
+ objectLibrary .asSize (nlocals ), objectLibrary .asSize (stacksize ), objectLibrary .asSize (flags ),
3030
+ codeBytes , constantsArr , namesArr ,
3031
+ varnamesArr , freevarsArr , cellcarsArr ,
3032
+ getStringArg (filename ), getStringArg (name ), objectLibrary .asSize (firstlineno ),
3033
+ lnotabBytes );
3034
+ }
3035
+
2989
3036
@ Fallback
2990
3037
@ SuppressWarnings ("unused" )
2991
3038
Object call (Object cls , Object argcount , Object kwonlyargcount ,
0 commit comments