Skip to content

Commit ebb860c

Browse files
committed
Delegate read/write from/to native member to native object.
1 parent 6b9f783 commit ebb860c

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@
7373
import com.oracle.graal.python.builtins.objects.str.PString;
7474
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
7575
import com.oracle.graal.python.builtins.objects.type.PythonClass;
76-
import com.oracle.graal.python.nodes.BuiltinNames;
7776
import com.oracle.graal.python.nodes.PBaseNode;
7877
import com.oracle.graal.python.nodes.PGuards;
7978
import com.oracle.graal.python.nodes.SpecialAttributeNames;
8079
import com.oracle.graal.python.nodes.SpecialMethodNames;
8180
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
8281
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
82+
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
8383
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
8484
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
8585
import com.oracle.graal.python.nodes.object.GetClassNode;
@@ -104,8 +104,10 @@
104104
import com.oracle.truffle.api.interop.TruffleObject;
105105
import com.oracle.truffle.api.interop.UnknownIdentifierException;
106106
import com.oracle.truffle.api.interop.UnsupportedMessageException;
107+
import com.oracle.truffle.api.interop.UnsupportedTypeException;
107108
import com.oracle.truffle.api.nodes.Node;
108109
import com.oracle.truffle.api.nodes.UnexpectedResultException;
110+
import com.oracle.truffle.api.profiles.ConditionProfile;
109111
import com.oracle.truffle.api.profiles.ValueProfile;
110112

111113
@MessageResolution(receiverType = PythonNativeWrapper.class)
@@ -428,6 +430,22 @@ Object doImFunc(PBuiltinMethod object, @SuppressWarnings("unused") String key) {
428430
return getToSulongNode().execute(object.getFunction());
429431
}
430432

433+
@Specialization
434+
Object doMemoryview(PMemoryView object, String key,
435+
@Cached("create()") ReadAttributeFromObjectNode readAttrNode,
436+
@Cached("createReadNode()") Node readNode,
437+
@Cached("createBinaryProfile()") ConditionProfile isNativeObject) {
438+
Object delegateObj = readAttrNode.execute(object, "__c_memoryview");
439+
if (isNativeObject.profile(delegateObj instanceof PythonNativeObject)) {
440+
try {
441+
return ForeignAccess.sendRead(readNode, (TruffleObject) ((PythonNativeObject) delegateObj).object, key);
442+
} catch (UnsupportedMessageException | UnknownIdentifierException e) {
443+
throw e.raise();
444+
}
445+
}
446+
throw new IllegalStateException("delegate of memoryview object is not native");
447+
}
448+
431449
@Fallback
432450
Object doGeneric(Object object, String key) {
433451
// This is the preliminary generic case: There are native members we know that they
@@ -446,11 +464,6 @@ protected boolean eq(String expected, String actual) {
446464
return expected.equals(actual);
447465
}
448466

449-
protected boolean isMemoryView(Object obj) {
450-
// TODO
451-
return getClass(obj).getName().equals(BuiltinNames.MEMORYVIEW);
452-
}
453-
454467
public static ReadNativeMemberNode create() {
455468
return ReadNativeMemberNodeGen.create();
456469
}
@@ -486,6 +499,10 @@ private PythonClass getClass(Object obj) {
486499
}
487500
return getClassNode.execute(obj);
488501
}
502+
503+
protected Node createReadNode() {
504+
return Message.READ.createNode();
505+
}
489506
}
490507

491508
@Resolve(message = "WRITE")
@@ -554,6 +571,22 @@ Object doMdDef(PythonObject object, @SuppressWarnings("unused") String key, Obje
554571
return value;
555572
}
556573

574+
@Specialization
575+
Object doMemoryview(PMemoryView object, String key, Object value,
576+
@Cached("create()") ReadAttributeFromObjectNode readAttrNode,
577+
@Cached("createWriteNode()") Node writeNode,
578+
@Cached("createBinaryProfile()") ConditionProfile isNativeObject) {
579+
Object delegateObj = readAttrNode.execute(object, "__c_memoryview");
580+
if (isNativeObject.profile(delegateObj instanceof PythonNativeObject)) {
581+
try {
582+
return ForeignAccess.sendWrite(writeNode, (TruffleObject) ((PythonNativeObject) delegateObj).object, key, value);
583+
} catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException e) {
584+
throw e.raise();
585+
}
586+
}
587+
throw new IllegalStateException("delegate of memoryview object is not native");
588+
}
589+
557590
@Fallback
558591
Object doGeneric(Object object, String key, Object value) {
559592
// This is the preliminary generic case: There are native members we know that they
@@ -581,10 +614,13 @@ private HashingStorageNodes.SetItemNode getSetItemNode() {
581614
return setItemNode;
582615
}
583616

617+
protected Node createWriteNode() {
618+
return Message.WRITE.createNode();
619+
}
620+
584621
public static WriteNativeMemberNode create() {
585622
return WriteNativeMemberNodeGen.create();
586623
}
587-
588624
}
589625

590626
@Resolve(message = "EXECUTE")

0 commit comments

Comments
 (0)