Skip to content

Commit 67d73a1

Browse files
committed
truffle library migration: TruffleObjectNativeWrapper
1 parent 4cbb5c6 commit 67d73a1

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
@ExportLibrary(InteropLibrary.class)
159159
@ExportLibrary(NativeTypeLibrary.class)
160160
public abstract class DynamicObjectNativeWrapper extends PythonNativeWrapper {
161-
private static final String GP_OBJECT = "gp_object";
161+
static final String GP_OBJECT = "gp_object";
162162
private static final Layout OBJECT_LAYOUT = Layout.newLayout().build();
163163
private static final Shape SHAPE = OBJECT_LAYOUT.createShape(new ObjectType());
164164

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

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,22 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext;
4242

43+
import com.oracle.graal.python.builtins.objects.cext.DynamicObjectNativeWrapper.PAsPointerNode;
44+
import com.oracle.graal.python.builtins.objects.cext.DynamicObjectNativeWrapper.ReadNativeMemberDispatchNode;
45+
import com.oracle.graal.python.builtins.objects.cext.DynamicObjectNativeWrapper.ToNativeNode;
46+
import com.oracle.graal.python.runtime.interop.InteropArray;
47+
import com.oracle.truffle.api.dsl.Cached;
48+
import com.oracle.truffle.api.dsl.Specialization;
49+
import com.oracle.truffle.api.interop.InteropLibrary;
4350
import com.oracle.truffle.api.interop.TruffleObject;
51+
import com.oracle.truffle.api.interop.UnknownIdentifierException;
52+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
53+
import com.oracle.truffle.api.library.ExportLibrary;
54+
import com.oracle.truffle.api.library.ExportMessage;
55+
import com.oracle.truffle.llvm.spi.NativeTypeLibrary;
4456

57+
@ExportLibrary(InteropLibrary.class)
58+
@ExportLibrary(NativeTypeLibrary.class)
4559
public class TruffleObjectNativeWrapper extends PythonNativeWrapper {
4660

4761
public TruffleObjectNativeWrapper(TruffleObject foreignObject) {
@@ -52,4 +66,76 @@ public static TruffleObjectNativeWrapper wrap(TruffleObject foreignObject) {
5266
assert !(foreignObject instanceof PythonNativeWrapper) : "attempting to wrap a native wrapper";
5367
return new TruffleObjectNativeWrapper(foreignObject);
5468
}
55-
}
69+
70+
@ExportMessage
71+
boolean hasMembers() {
72+
return true;
73+
}
74+
75+
@ExportMessage
76+
protected boolean isMemberReadable(@SuppressWarnings("unused") String member) {
77+
// TODO(fa) should that be refined?
78+
return true;
79+
}
80+
81+
@ExportMessage
82+
protected Object getMembers(@SuppressWarnings("unused") boolean includeInternal) {
83+
return new InteropArray(new String[]{DynamicObjectNativeWrapper.GP_OBJECT});
84+
}
85+
86+
@ExportMessage(name = "readMember")
87+
abstract static class ReadNode {
88+
89+
@Specialization(guards = {"key == cachedObBase", "isObBase(cachedObBase)"}, limit = "1")
90+
static Object doObBaseCached(TruffleObjectNativeWrapper object, @SuppressWarnings("unused") String key,
91+
@Cached("key") @SuppressWarnings("unused") String cachedObBase) {
92+
return object;
93+
}
94+
95+
@Specialization
96+
static Object execute(TruffleObjectNativeWrapper object, String key,
97+
@Cached ReadNativeMemberDispatchNode readNativeMemberNode,
98+
@Cached CExtNodes.AsPythonObjectNode getDelegate) throws UnsupportedMessageException, UnknownIdentifierException {
99+
Object delegate = getDelegate.execute(object);
100+
101+
// special key for the debugger
102+
if (key.equals(DynamicObjectNativeWrapper.GP_OBJECT)) {
103+
return delegate;
104+
}
105+
return readNativeMemberNode.execute(delegate, key);
106+
}
107+
108+
protected static boolean isObBase(String key) {
109+
return NativeMemberNames.OB_BASE.equals(key);
110+
}
111+
}
112+
113+
@ExportMessage
114+
protected boolean isPointer(
115+
@Cached CExtNodes.IsPointerNode pIsPointerNode) {
116+
return pIsPointerNode.execute(this);
117+
}
118+
119+
@ExportMessage
120+
protected long asPointer(
121+
@Cached PAsPointerNode pAsPointerNode) {
122+
return pAsPointerNode.execute(this);
123+
}
124+
125+
@ExportMessage
126+
protected void toNative(
127+
@Cached.Exclusive @Cached ToNativeNode toNativeNode) {
128+
toNativeNode.execute(this);
129+
}
130+
131+
@ExportMessage
132+
protected boolean hasNativeType() {
133+
return true;
134+
}
135+
136+
@ExportMessage
137+
protected Object getNativeType(
138+
@Cached PGetDynamicTypeNode getDynamicTypeNode) {
139+
return getDynamicTypeNode.execute(this);
140+
}
141+
}

0 commit comments

Comments
 (0)