Skip to content

Commit f0fff4d

Browse files
committed
Move delegate field to base class.
1 parent 13f0a55 commit f0fff4d

File tree

2 files changed

+35
-50
lines changed

2 files changed

+35
-50
lines changed

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

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
4444
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CStringWrapper;
4545
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage.PythonObjectDictStorage;
46-
import com.oracle.graal.python.builtins.objects.object.PythonObject;
4746
import com.oracle.graal.python.builtins.objects.str.PString;
4847
import com.oracle.graal.python.builtins.objects.type.PythonClass;
4948
import com.oracle.truffle.api.CompilerAsserts;
@@ -58,9 +57,23 @@ public abstract class NativeWrappers {
5857

5958
public abstract static class PythonNativeWrapper implements TruffleObject {
6059

60+
private Object delegate;
6161
private Object nativePointer;
6262

63-
public abstract Object getDelegate();
63+
public PythonNativeWrapper() {
64+
}
65+
66+
public PythonNativeWrapper(Object delegate) {
67+
this.delegate = delegate;
68+
}
69+
70+
public Object getDelegate() {
71+
return delegate;
72+
}
73+
74+
protected void setDelegate(Object delegate) {
75+
this.delegate = delegate;
76+
}
6477

6578
public Object getNativePointer() {
6679
return nativePointer;
@@ -92,6 +105,13 @@ public abstract static class DynamicObjectNativeWrapper extends PythonNativeWrap
92105

93106
private PythonObjectDictStorage nativeMemberStore;
94107

108+
public DynamicObjectNativeWrapper() {
109+
}
110+
111+
public DynamicObjectNativeWrapper(Object delegate) {
112+
super(delegate);
113+
}
114+
95115
public PythonObjectDictStorage createNativeMemberStore() {
96116
if (nativeMemberStore == null) {
97117
nativeMemberStore = new PythonObjectDictStorage(SHAPE.newInstance());
@@ -110,14 +130,12 @@ public PythonObjectDictStorage getNativeMemberStore() {
110130
*/
111131
public static class PythonObjectNativeWrapper extends DynamicObjectNativeWrapper {
112132

113-
private final PythonAbstractObject pythonObject;
114-
115133
public PythonObjectNativeWrapper(PythonAbstractObject object) {
116-
this.pythonObject = object;
134+
super(object);
117135
}
118136

119137
public PythonAbstractObject getPythonObject() {
120-
return pythonObject;
138+
return (PythonAbstractObject) getDelegate();
121139
}
122140

123141
public static DynamicObjectNativeWrapper wrap(PythonAbstractObject obj, ConditionProfile noWrapperProfile) {
@@ -130,38 +148,25 @@ public static DynamicObjectNativeWrapper wrap(PythonAbstractObject obj, Conditio
130148
return nativeWrapper;
131149
}
132150

133-
@Override
134-
public Object getDelegate() {
135-
return pythonObject;
136-
}
137-
138151
@Override
139152
public String toString() {
140153
CompilerAsserts.neverPartOfCompilation();
141-
return String.format("PythonObjectNativeWrapper(%s, isNative=%s)", pythonObject, isNative());
154+
return String.format("PythonObjectNativeWrapper(%s, isNative=%s)", getDelegate(), isNative());
142155
}
143156
}
144157

145158
public abstract static class PrimitiveNativeWrapper extends DynamicObjectNativeWrapper {
146159

147-
private PythonObject materializedObject;
148-
149160
protected abstract Object getBoxedValue();
150161

151-
@Override
152-
public Object getDelegate() {
153-
if (materializedObject != null) {
154-
return materializedObject;
155-
}
156-
return getBoxedValue();
162+
// this method exists just for readability
163+
public Object getMaterializedObject() {
164+
return getDelegate();
157165
}
158166

159-
void setMaterializedObject(PythonObject materializedObject) {
160-
this.materializedObject = materializedObject;
161-
}
162-
163-
PythonObject getMaterializedObject() {
164-
return materializedObject;
167+
// this method exists just for readability
168+
public void setMaterializedObject(Object materializedPrimitive) {
169+
setDelegate(materializedPrimitive);
165170
}
166171
}
167172

@@ -372,21 +377,14 @@ public String toString() {
372377
*/
373378
public static class PySequenceArrayWrapper extends PythonNativeWrapper {
374379

375-
private final Object delegate;
376-
377380
/** Number of bytes that constitute a single element. */
378381
private final int elementAccessSize;
379382

380383
public PySequenceArrayWrapper(Object delegate, int elementAccessSize) {
381-
this.delegate = delegate;
384+
super(delegate);
382385
this.elementAccessSize = elementAccessSize;
383386
}
384387

385-
@Override
386-
public Object getDelegate() {
387-
return delegate;
388-
}
389-
390388
public int getElementAccessSize() {
391389
return elementAccessSize;
392390
}
@@ -402,37 +400,26 @@ public ForeignAccess getForeignAccess() {
402400
}
403401

404402
public static class TruffleObjectNativeWrapper extends PythonNativeWrapper {
405-
private final TruffleObject foreignObject;
406403

407404
public TruffleObjectNativeWrapper(TruffleObject foreignObject) {
408-
this.foreignObject = foreignObject;
409-
}
410-
411-
public TruffleObject getForeignObject() {
412-
return foreignObject;
405+
super(foreignObject);
413406
}
414407

415408
public static TruffleObjectNativeWrapper wrap(TruffleObject foreignObject) {
416409
assert !(foreignObject instanceof PythonNativeWrapper) : "attempting to wrap a native wrapper";
417410
return new TruffleObjectNativeWrapper(foreignObject);
418411
}
419-
420-
@Override
421-
public Object getDelegate() {
422-
return foreignObject;
423-
}
424412
}
425413

426414
abstract static class PyUnicodeWrapper extends PythonNativeWrapper {
427-
private final PString delegate;
428415

429416
public PyUnicodeWrapper(PString delegate) {
430-
this.delegate = delegate;
417+
super(delegate);
431418
}
432419

433420
@Override
434421
public PString getDelegate() {
435-
return delegate;
422+
return (PString) super.getDelegate();
436423
}
437424

438425
@Override
@@ -452,7 +439,6 @@ public static class PyUnicodeData extends PyUnicodeWrapper {
452439
public PyUnicodeData(PString delegate) {
453440
super(delegate);
454441
}
455-
456442
}
457443

458444
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
import com.oracle.truffle.api.interop.TruffleObject;
8989
import com.oracle.truffle.api.interop.UnsupportedMessageException;
9090
import com.oracle.truffle.api.nodes.Node;
91-
import com.oracle.truffle.api.nodes.UnexpectedResultException;
9291
import com.oracle.truffle.api.profiles.ConditionProfile;
9392
import com.oracle.truffle.api.profiles.ValueProfile;
9493

0 commit comments

Comments
 (0)