Skip to content

Commit 6698d38

Browse files
committed
Further small changes to getting the wrapper from a native pointer.
1 parent 6468f8f commit 6698d38

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

src/main/java/org/truffleruby/cext/UnwrapNode.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
package org.truffleruby.cext;
1111

1212
import static org.truffleruby.cext.ValueWrapperManager.TAG_MASK;
13+
import static org.truffleruby.cext.ValueWrapperManager.TRUE_HANDLE;
14+
import static org.truffleruby.cext.ValueWrapperManager.UNDEF_HANDLE;
1315
import static org.truffleruby.cext.ValueWrapperManager.LONG_TAG;
16+
import static org.truffleruby.cext.ValueWrapperManager.NIL_HANDLE;
1417
import static org.truffleruby.cext.ValueWrapperManager.OBJECT_TAG;
1518
import static org.truffleruby.cext.ValueWrapperManager.FALSE_HANDLE;
1619

20+
import org.truffleruby.cext.UnwrapNodeGen.NativeToWrapperNodeGen;
1721
import org.truffleruby.cext.UnwrapNodeGen.ToWrapperNodeGen;
1822
import org.truffleruby.cext.UnwrapNodeGen.UnwrapNativeNodeGen;
1923
import org.truffleruby.language.NotProvided;
@@ -82,6 +86,54 @@ public static UnwrapNativeNode create() {
8286
}
8387
}
8488

89+
@ImportStatic(ValueWrapperManager.class)
90+
public static abstract class NativeToWrapperNode extends RubyBaseNode {
91+
92+
public abstract ValueWrapper execute(long handle);
93+
94+
@Specialization(guards = "handle == FALSE_HANDLE")
95+
public ValueWrapper unwrapFalse(long handle) {
96+
return new ValueWrapper(false, FALSE_HANDLE);
97+
}
98+
99+
@Specialization(guards = "handle == TRUE_HANDLE")
100+
public ValueWrapper unwrapTrue(long handle) {
101+
return new ValueWrapper(true, TRUE_HANDLE);
102+
}
103+
104+
@Specialization(guards = "handle == UNDEF_HANDLE")
105+
public ValueWrapper unwrapUndef(long handle) {
106+
return new ValueWrapper(NotProvided.INSTANCE, UNDEF_HANDLE);
107+
}
108+
109+
@Specialization(guards = "handle == NIL_HANDLE")
110+
public ValueWrapper unwrapNil(long handle) {
111+
return new ValueWrapper(nil(), NIL_HANDLE);
112+
}
113+
114+
@Specialization(guards = "isTaggedLong(handle)")
115+
public ValueWrapper unwrapTaggedLong(long handle) {
116+
return new ValueWrapper(handle >> 1, handle);
117+
}
118+
119+
@Specialization(guards = "isTaggedObject(handle)")
120+
public ValueWrapper unwrapTaggedObject(long handle) {
121+
return getContext().getValueWrapperManager().getWrapperFromHandleMap(handle);
122+
}
123+
124+
public boolean isTaggedLong(long handle) {
125+
return (handle & LONG_TAG) == LONG_TAG;
126+
}
127+
128+
public boolean isTaggedObject(long handle) {
129+
return handle != FALSE_HANDLE && (handle & TAG_MASK) == OBJECT_TAG;
130+
}
131+
132+
public static NativeToWrapperNode create() {
133+
return NativeToWrapperNodeGen.create();
134+
}
135+
}
136+
85137
@ImportStatic(Message.class)
86138
public static abstract class ToWrapperNode extends RubyBaseNode {
87139

@@ -96,8 +148,7 @@ public ValueWrapper wrappedValueWrapper(ValueWrapper value) {
96148
public ValueWrapper unwrapTypeCastObject(TruffleObject value,
97149
@Cached("IS_POINTER.createNode()") Node isPointerNode,
98150
@Cached("AS_POINTER.createNode()") Node asPointerNode,
99-
@Cached("create()") UnwrapNativeNode unwrapNativeNode,
100-
@Cached("create()") WrapNode wrapNode,
151+
@Cached("create()") NativeToWrapperNode nativeToWrapperNode,
101152
@Cached("create()") BranchProfile unsupportedProfile,
102153
@Cached("create()") BranchProfile nonPointerProfile) {
103154
if (ForeignAccess.sendIsPointer(isPointerNode, value)) {
@@ -108,12 +159,7 @@ public ValueWrapper unwrapTypeCastObject(TruffleObject value,
108159
unsupportedProfile.enter();
109160
throw new RaiseException(getContext(), coreExceptions().argumentError(e.getMessage(), this, e));
110161
}
111-
Object obj = unwrapNativeNode.execute(handle);
112-
if (obj != null) {
113-
return wrapNode.execute(obj);
114-
} else {
115-
return null;
116-
}
162+
return nativeToWrapperNode.execute(handle);
117163
} else {
118164
nonPointerProfile.enter();
119165
throw new RaiseException(getContext(), coreExceptions().argumentError("Not a handle or a pointer", this));

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ public synchronized Object getFromHandleMap(long handle) {
7676
return wrapper.getObject();
7777
}
7878

79+
@TruffleBoundary
80+
public synchronized ValueWrapper getWrapperFromHandleMap(long handle) {
81+
WeakReference<ValueWrapper> ref = handleMap.get(handle);
82+
ValueWrapper wrapper;
83+
if (ref == null) {
84+
return null;
85+
}
86+
if ((wrapper = ref.get()) == null) {
87+
return null;
88+
}
89+
return wrapper;
90+
}
91+
7992
@TruffleBoundary
8093
public synchronized void removeFromHandleMap(long handle) {
8194
handleMap.remove(handle);

0 commit comments

Comments
 (0)