Skip to content

Commit ce333f2

Browse files
committed
Use special wrapper for initializing a native Python class.
1 parent cbf2aac commit ce333f2

File tree

8 files changed

+50
-113
lines changed

8 files changed

+50
-113
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ inline void* handle_exception(void* val) {
156156
return val == ERROR_MARKER ? NULL : val;
157157
}
158158

159+
// Heuristic to test if some value is a pointer object
160+
// TODO we need a reliable solution for that
161+
#define IS_POINTER(__val__) (polyglot_is_value(__val__) && !polyglot_fits_in_i64(__val__))
162+
159163
void* native_to_java(PyObject* obj) {
160164
if (obj == Py_None) {
161165
return Py_None;
@@ -167,6 +171,8 @@ void* native_to_java(PyObject* obj) {
167171
return truffle_managed_from_handle(obj);
168172
} else if (truffle_is_handle_to_managed(obj->ob_refcnt)) {
169173
return truffle_managed_from_handle(obj->ob_refcnt);
174+
} else if (IS_POINTER(obj->ob_refcnt)) {
175+
return obj->ob_refcnt;
170176
}
171177
return obj;
172178
}

graalpython/com.oracle.graal.python.cext/src/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ int PyType_Ready(PyTypeObject* cls) {
245245
native_to_java(native_members));
246246

247247
// remember the managed wrapper
248-
((PyObject*)cls)->ob_refcnt = truffle_handle_for_managed(javacls);
248+
((PyObject*)cls)->ob_refcnt = javacls;
249249
if (cls->tp_dict != NULL) {
250250
javacls->tp_dict = native_to_java(cls->tp_dict);
251251
} else {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CStringWrapper;
7171
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
7272
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PySequenceArrayWrapper;
73+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassInitNativeWrapper;
7374
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassNativeWrapper;
7475
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
7576
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
@@ -574,7 +575,7 @@ Object run(Object typestruct, PythonClass metaClass, PTuple baseClasses, PDict n
574575
if (module != null) {
575576
writeNode.execute(cclass, SpecialAttributeNames.__MODULE__, module);
576577
}
577-
return PythonClassNativeWrapper.wrap(cclass);
578+
return new PythonClassInitNativeWrapper(cclass);
578579
}
579580

580581
private String getStringItem(PDict nativeMembers, String key) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/NativeWrappers.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ public String toString() {
188188
}
189189
}
190190

191+
/**
192+
* Used to wrap {@link PythonClass} just for the time when a natively defined type is processed
193+
* in {@code PyType_Ready} and we need to pass the mirroring managed class to native to marry
194+
* these two objects.
195+
*/
196+
public static class PythonClassInitNativeWrapper extends PythonObjectNativeWrapper {
197+
198+
public PythonClassInitNativeWrapper(PythonClass object) {
199+
super(object);
200+
}
201+
202+
@Override
203+
public String toString() {
204+
return String.format("PythonClassNativeInitWrapper(%s, isNative=%s)", getPythonObject(), isNative());
205+
}
206+
}
207+
191208
/**
192209
* Wraps a sequence object (like a list) such that it behaves like a bare C array.
193210
*/

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PySequenceArrayWrapperMR.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Object access(PySequenceArrayWrapper obj) {
284284
}
285285
}
286286

287-
abstract static class ToNativeArrayNode extends TransformToNativeNode {
287+
abstract static class ToNativeArrayNode extends PBaseNode {
288288
@CompilationFinal private TruffleObject PyObjectHandle_FromJavaObject;
289289
@Child private PCallNativeNode callNativeBinary;
290290
@Child private ToNativeStorageNode toNativeStorageNode;
@@ -306,7 +306,7 @@ Object doPSequence(PySequenceArrayWrapper object) {
306306
@Fallback
307307
Object doGeneric(PySequenceArrayWrapper object) {
308308
// TODO correct element size
309-
return ensureIsPointer(callBinaryIntoCapi(getNativeHandleForArray(), object, 8L));
309+
return callBinaryIntoCapi(getNativeHandleForArray(), object, 8L);
310310
}
311311

312312
private TruffleObject getNativeHandleForArray() {
@@ -449,7 +449,7 @@ public static GetTypeIDNode create() {
449449
}
450450
}
451451

452-
static abstract class ToNativeStorageNode extends TransformToNativeNode {
452+
static abstract class ToNativeStorageNode extends PBaseNode {
453453
@Child private StorageToNativeNode storageToNativeNode;
454454

455455
public abstract NativeSequenceStorage execute(SequenceStorage object);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PyUnicodeWrapperMR.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMR.ToPyObjectNode;
5353
import com.oracle.graal.python.builtins.objects.cext.UnicodeObjectNodes.UnicodeAsWideCharNode;
5454
import com.oracle.graal.python.builtins.objects.str.PString;
55+
import com.oracle.graal.python.nodes.PBaseNode;
5556
import com.oracle.truffle.api.CompilerDirectives;
5657
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
5758
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -179,7 +180,7 @@ long access(PyUnicodeWrapper obj) {
179180
}
180181
}
181182

182-
abstract static class PyUnicodeToNativeNode extends TransformToNativeNode {
183+
abstract static class PyUnicodeToNativeNode extends PBaseNode {
183184
@CompilationFinal private TruffleObject derefHandleIntrinsic;
184185
@Child private PCallNativeNode callNativeUnary;
185186
@Child private PCallNativeNode callNativeBinary;
@@ -189,7 +190,7 @@ abstract static class PyUnicodeToNativeNode extends TransformToNativeNode {
189190

190191
@Specialization
191192
Object doUnicodeWrapper(PyUnicodeWrapper object) {
192-
return ensureIsPointer(callUnaryIntoCapi(getPyObjectHandle_ForJavaType(), object));
193+
return callUnaryIntoCapi(getPyObjectHandle_ForJavaType(), object);
193194
}
194195

195196
private TruffleObject getPyObjectHandle_ForJavaType() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PythonObjectNativeWrapperMR.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PySequenceArrayWrapper;
5454
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PyUnicodeData;
5555
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PyUnicodeState;
56+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassInitNativeWrapper;
5657
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
5758
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
5859
import com.oracle.graal.python.builtins.objects.cext.PythonObjectNativeWrapperMRFactory.PAsPointerNodeGen;
@@ -790,7 +791,15 @@ public Object access(Object object) {
790791
abstract static class ToNativeNode extends Node {
791792
@Child private ToPyObjectNode toPyObjectNode = ToPyObjectNode.create();
792793

794+
Object access(PythonClassInitNativeWrapper obj) {
795+
if (!obj.isNative()) {
796+
obj.setNativePointer(toPyObjectNode.getHandleForObject(obj.getDelegate(), 0));
797+
}
798+
return obj;
799+
}
800+
793801
Object access(PythonNativeWrapper obj) {
802+
assert !(obj instanceof PythonClassInitNativeWrapper);
794803
if (!obj.isNative()) {
795804
obj.setNativePointer(toPyObjectNode.execute(obj.getDelegate()));
796805
}
@@ -857,7 +866,7 @@ public static PAsPointerNode create() {
857866

858867
}
859868

860-
abstract static class ToPyObjectNode extends TransformToNativeNode {
869+
abstract static class ToPyObjectNode extends PBaseNode {
861870
@CompilationFinal private TruffleObject PyObjectHandle_FromJavaObject;
862871
@CompilationFinal private TruffleObject PyObjectHandle_FromJavaType;
863872
@CompilationFinal private TruffleObject PyNoneHandle;
@@ -870,23 +879,27 @@ abstract static class ToPyObjectNode extends TransformToNativeNode {
870879

871880
@Specialization
872881
Object runNativeClass(PythonNativeClass object) {
873-
return ensureIsPointer(object.object);
882+
return object.object;
874883
}
875884

876885
@Specialization
877886
Object runNativeObject(PythonNativeObject object) {
878-
return ensureIsPointer(object.object);
887+
return object.object;
879888
}
880889

881890
@Specialization(guards = "isManagedPythonClass(object)")
882891
Object runClass(PythonClass object) {
883-
return ensureIsPointer(callUnaryIntoCapi(getPyObjectHandle_ForJavaType(), getToSulongNode().execute(object)));
892+
return callUnaryIntoCapi(getPyObjectHandle_ForJavaType(), getToSulongNode().execute(object));
884893
}
885894

886895
@Fallback
887896
Object runObject(Object object) {
888897
PythonClass clazz = getClassNode().execute(object);
889-
return ensureIsPointer(callBinaryIntoCapi(getPyObjectHandle_ForJavaObject(), getToSulongNode().execute(object), clazz.getFlags()));
898+
return callBinaryIntoCapi(getPyObjectHandle_ForJavaObject(), getToSulongNode().execute(object), clazz.getFlags());
899+
}
900+
901+
Object getHandleForObject(Object object, long flags) {
902+
return callBinaryIntoCapi(getPyObjectHandle_ForJavaObject(), object, flags);
890903
}
891904

892905
private TruffleObject getPyObjectHandle_ForJavaType() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/TransformToNativeNode.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)