Skip to content

Commit 7726a1d

Browse files
committed
Refactor hierarchy of native wrappers.
1 parent 1dc4ab8 commit 7726a1d

File tree

8 files changed

+38
-50
lines changed

8 files changed

+38
-50
lines changed

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939
package com.oracle.graal.python.builtins.objects.cext;
4040

41-
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.NativeWrapper;
41+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
4242
import com.oracle.truffle.api.interop.ForeignAccess;
4343
import com.oracle.truffle.api.interop.TruffleObject;
4444

@@ -49,29 +49,13 @@
4949
*/
5050
public abstract class CArrayWrappers {
5151

52-
public abstract static class CArrayWrapper extends NativeWrapper {
53-
54-
private Object nativePointer;
55-
56-
public Object getNativePointer() {
57-
return nativePointer;
58-
}
59-
60-
public void setNativePointer(Object nativePointer) {
61-
assert this.nativePointer == null;
62-
this.nativePointer = nativePointer;
63-
}
64-
65-
public boolean isNative() {
66-
return nativePointer != null;
67-
}
68-
69-
public abstract Object getDelegate();
52+
public abstract static class CArrayWrapper extends PythonNativeWrapper {
7053

7154
static boolean isInstance(TruffleObject o) {
7255
return o instanceof CArrayWrapper;
7356
}
7457

58+
@Override
7559
public ForeignAccess getForeignAccess() {
7660
return CArrayWrapperMRForeign.ACCESS;
7761
}

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.AsPythonObjectNodeGen;
4646
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ToJavaNodeGen;
4747
import com.oracle.graal.python.builtins.objects.cext.CExtNodesFactory.ToSulongNodeGen;
48-
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.NativeWrapper;
4948
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonClassNativeWrapper;
5049
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
5150
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
@@ -74,7 +73,15 @@
7473
public abstract class CExtNodes {
7574

7675
@ImportStatic(PGuards.class)
77-
public abstract static class ToSulongNode extends PBaseNode {
76+
abstract static class CExtBaseNode extends PBaseNode {
77+
78+
protected static boolean isNativeWrapper(Object obj) {
79+
return obj instanceof PythonNativeWrapper;
80+
}
81+
82+
}
83+
84+
public abstract static class ToSulongNode extends CExtBaseNode {
7885

7986
public abstract Object execute(Object obj);
8087

@@ -152,10 +159,6 @@ protected static boolean isNativeObject(PythonAbstractObject o) {
152159
return o instanceof PythonNativeObject;
153160
}
154161

155-
protected static boolean isNativeWrapper(Object o) {
156-
return o instanceof NativeWrapper;
157-
}
158-
159162
public static ToSulongNode create() {
160163
return ToSulongNodeGen.create();
161164
}
@@ -165,8 +168,7 @@ public static ToSulongNode create() {
165168
* Unwraps objects contained in {@link PythonObjectNativeWrapper} instances or wraps objects
166169
* allocated in native code for consumption in Java.
167170
*/
168-
@ImportStatic(PGuards.class)
169-
public abstract static class AsPythonObjectNode extends PBaseNode {
171+
public abstract static class AsPythonObjectNode extends CExtBaseNode {
170172
public abstract Object execute(Object value);
171173

172174
@Child GetClassNode getClassNode;
@@ -220,10 +222,6 @@ protected boolean isForeignObject(TruffleObject obj) {
220222
return getClassNode.execute(obj) == getCore().getForeignClass();
221223
}
222224

223-
protected boolean isNativeWrapper(Object obj) {
224-
return obj instanceof NativeWrapper;
225-
}
226-
227225
@TruffleBoundary
228226
public static Object doSlowPath(Object object) {
229227
if (object instanceof PythonNativeWrapper) {
@@ -243,7 +241,7 @@ public static AsPythonObjectNode create() {
243241
* Does the same conversion as the native function {@code to_java}. The node tries to avoid
244242
* calling the native function for resolving native handles.
245243
*/
246-
public abstract static class ToJavaNode extends PBaseNode {
244+
public abstract static class ToJavaNode extends CExtBaseNode {
247245
@Child private PCallNativeNode callNativeNode;
248246
@Child private AsPythonObjectNode toJavaNode = AsPythonObjectNode.create();
249247

@@ -282,7 +280,7 @@ public static ToJavaNode create() {
282280
}
283281
}
284282

285-
public abstract static class AsCharPointer extends PBaseNode {
283+
public abstract static class AsCharPointer extends CExtBaseNode {
286284

287285
@CompilationFinal TruffleObject truffle_string_to_cstr;
288286
@CompilationFinal TruffleObject truffle_byte_array_to_native;
@@ -340,7 +338,7 @@ public static AsCharPointer create() {
340338
}
341339
}
342340

343-
public static class FromCharPointerNode extends PBaseNode {
341+
public static class FromCharPointerNode extends CExtBaseNode {
344342

345343
@CompilationFinal TruffleObject truffle_cstr_to_string;
346344
@Child private Node executeNode;
@@ -374,7 +372,7 @@ public static FromCharPointerNode create() {
374372
}
375373
}
376374

377-
public static class GetNativeClassNode extends PBaseNode {
375+
public static class GetNativeClassNode extends CExtBaseNode {
378376

379377
@Child PCallNativeNode callGetObTypeNode;
380378
@Child ToJavaNode toJavaNode;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939
package com.oracle.graal.python.builtins.objects.cext;
4040

41-
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.NativeWrapper;
41+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
4242
import com.oracle.truffle.api.interop.ForeignAccess;
4343
import com.oracle.truffle.api.interop.TruffleObject;
4444

@@ -47,21 +47,23 @@
4747
*/
4848
public abstract class ManagedMethodWrappers {
4949

50-
public abstract static class MethodWrapper extends NativeWrapper {
50+
public abstract static class MethodWrapper extends PythonNativeWrapper {
5151
private final Object method;
5252

5353
public MethodWrapper(Object method) {
5454
this.method = method;
5555
}
5656

57-
public Object getMethod() {
57+
@Override
58+
public Object getDelegate() {
5859
return method;
5960
}
6061

6162
static boolean isInstance(TruffleObject o) {
6263
return o instanceof MethodWrapper;
6364
}
6465

66+
@Override
6567
public ForeignAccess getForeignAccess() {
6668
return ManagedMethodWrappersMRForeign.ACCESS;
6769
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public Object access(MethKeywords object, Object[] arguments) {
103103
Object[] userArgs = posArgsNode.executeWithArguments(null, posStarargsNode.executeWith(converted[0]));
104104
Object[] pArgs = createArgs.execute(userArgs);
105105
PKeyword[] kwargs = expandKwargsNode.executeWith(converted[1]);
106-
return getToSulongNode().execute(getDispatchNode().executeCall(object.getMethod(), pArgs, kwargs));
106+
return getToSulongNode().execute(getDispatchNode().executeCall(object.getDelegate(), pArgs, kwargs));
107107
}
108108

109109
@ExplodeLoop
@@ -117,7 +117,7 @@ public Object access(MethVarargs object, Object[] arguments) {
117117
}
118118

119119
// convert args
120-
return getToSulongNode().execute(executeNode.execute(object.getMethod(), new Object[]{getToJavaNode().execute(arguments[0])}));
120+
return getToSulongNode().execute(executeNode.execute(object.getDelegate(), new Object[]{getToJavaNode().execute(arguments[0])}));
121121
}
122122

123123
private ToJavaNode getToJavaNode() {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@
4949
import com.oracle.truffle.api.object.Shape;
5050

5151
public abstract class NativeWrappers {
52-
public abstract static class NativeWrapper implements TruffleObject {
53-
}
5452

55-
public abstract static class PythonNativeWrapper extends NativeWrapper {
53+
public abstract static class PythonNativeWrapper implements TruffleObject {
5654

5755
private Object nativePointer;
5856

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,19 @@
3838
*/
3939
package com.oracle.graal.python.builtins.objects.cext;
4040

41-
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.NativeWrapper;
41+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
4242
import com.oracle.truffle.api.interop.ForeignAccess;
4343
import com.oracle.truffle.api.interop.TruffleObject;
4444

45-
public abstract class PyAttributeProcsWrapper extends NativeWrapper {
45+
public abstract class PyAttributeProcsWrapper extends PythonNativeWrapper {
4646

4747
private final Object delegate;
4848

4949
public PyAttributeProcsWrapper(Object delegate) {
5050
this.delegate = delegate;
5151
}
5252

53+
@Override
5354
public Object getDelegate() {
5455
return delegate;
5556
}
@@ -58,6 +59,7 @@ static boolean isInstance(TruffleObject o) {
5859
return o instanceof PyAttributeProcsWrapper;
5960
}
6061

62+
@Override
6163
public ForeignAccess getForeignAccess() {
6264
return PyAttributeProcsWrapperMRForeign.ACCESS;
6365
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,20 @@
3838
*/
3939
package com.oracle.graal.python.builtins.objects.cext;
4040

41-
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.NativeWrapper;
41+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
4242
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4343
import com.oracle.truffle.api.interop.ForeignAccess;
4444
import com.oracle.truffle.api.interop.TruffleObject;
4545

46-
public class PyBufferProcsWrapper extends NativeWrapper {
46+
public class PyBufferProcsWrapper extends PythonNativeWrapper {
4747

4848
private final PythonClass delegate;
4949

5050
public PyBufferProcsWrapper(PythonClass delegate) {
5151
this.delegate = delegate;
5252
}
5353

54+
@Override
5455
public PythonClass getDelegate() {
5556
return delegate;
5657
}
@@ -59,6 +60,7 @@ static boolean isInstance(TruffleObject o) {
5960
return o instanceof PyBufferProcsWrapper;
6061
}
6162

63+
@Override
6264
public ForeignAccess getForeignAccess() {
6365
return PyBufferProcsWrapperMRForeign.ACCESS;
6466
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,22 @@
3838
*/
3939
package com.oracle.graal.python.builtins.objects.cext;
4040

41-
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.NativeWrapper;
41+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
4242
import com.oracle.truffle.api.interop.ForeignAccess;
4343
import com.oracle.truffle.api.interop.TruffleObject;
4444

4545
/**
46-
* Wraps a PythonObject to proviate a native view with a shape like {@code PyNumberMethods}.
46+
* Wraps a PythonObject to provide a native view with a shape like {@code PyNumberMethods}.
4747
*/
48-
public class PyNumberMethodsWrapper extends NativeWrapper {
48+
public class PyNumberMethodsWrapper extends PythonNativeWrapper {
4949

5050
private final Object delegate;
5151

5252
public PyNumberMethodsWrapper(Object delegate) {
5353
this.delegate = delegate;
5454
}
5555

56+
@Override
5657
public Object getDelegate() {
5758
return delegate;
5859
}
@@ -61,6 +62,7 @@ static boolean isInstance(TruffleObject o) {
6162
return o instanceof PyNumberMethodsWrapper;
6263
}
6364

65+
@Override
6466
public ForeignAccess getForeignAccess() {
6567
return PyNumberMethodsWrapperMRForeign.ACCESS;
6668
}

0 commit comments

Comments
 (0)