Skip to content

Commit eafb2c1

Browse files
committed
- missing (more) general specialization for code creation
- fix function creation when closure is specified
1 parent 62095a2 commit eafb2c1

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
import com.oracle.truffle.api.frame.VirtualFrame;
203203
import com.oracle.truffle.api.interop.UnsupportedMessageException;
204204
import com.oracle.truffle.api.library.CachedLibrary;
205+
import com.oracle.truffle.api.nodes.ExplodeLoop;
205206
import com.oracle.truffle.api.nodes.Node;
206207
import com.oracle.truffle.api.nodes.UnexpectedResultException;
207208
import com.oracle.truffle.api.object.HiddenKey;
@@ -2177,10 +2178,16 @@ public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String
21772178
return factory().createFunction(name, getTypeName(cls), code, globals, null);
21782179
}
21792180

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+
21802187
@Specialization
21812188
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, PTuple closure,
21822189
@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)));
21842191
}
21852192

21862193
@Specialization
@@ -2194,7 +2201,18 @@ public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String
21942201
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, PTuple defaultArgs, PTuple closure,
21952202
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
21962203
// 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;
21982216
}
21992217

22002218
@Fallback
@@ -2986,6 +3004,35 @@ Object call(VirtualFrame frame, LazyPythonClass cls, int argcount, int kwonlyarg
29863004
lnotabBytes);
29873005
}
29883006

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+
29893036
@Fallback
29903037
@SuppressWarnings("unused")
29913038
Object call(Object cls, Object argcount, Object kwonlyargcount,

0 commit comments

Comments
 (0)