Skip to content

Commit 68adeab

Browse files
committed
Changes based on review.
1 parent c3a03ee commit 68adeab

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.truffleruby.cext;
22

3+
import static org.truffleruby.cext.ValueWrapperManager.TAG_MASK;
34
import static org.truffleruby.cext.ValueWrapperManager.LONG_TAG;
45
import static org.truffleruby.cext.ValueWrapperManager.OBJECT_TAG;
6+
import static org.truffleruby.cext.ValueWrapperManager.FALSE_HANDLE;
57

68
import org.truffleruby.Layouts;
79

@@ -51,7 +53,7 @@ public Object unwrapNil(long handle) {
5153

5254
@Specialization(guards = "isTaggedLong(handle)")
5355
public Object unwrapTaggedLong(long handle) {
54-
return handle >> 3;
56+
return handle >> 1;
5557
}
5658

5759
@Specialization(guards = "isTaggedObject(handle)")
@@ -60,11 +62,11 @@ public Object unwrapTaggedObject(long handle) {
6062
}
6163

6264
public boolean isTaggedLong(long handle) {
63-
return (handle & 0x7L) == LONG_TAG;
65+
return (handle & LONG_TAG) == LONG_TAG;
6466
}
6567

6668
public boolean isTaggedObject(long handle) {
67-
return (handle & 0x7L) == OBJECT_TAG;
69+
return handle != FALSE_HANDLE && (handle & TAG_MASK) == OBJECT_TAG;
6870
}
6971

7072
public static UnwrapNativeNode create() {

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,30 @@
1515
import org.truffleruby.RubyContext;
1616
import org.truffleruby.collections.LongHashMap;
1717
import org.truffleruby.extra.ffi.Pointer;
18-
import org.truffleruby.language.NotProvided;
19-
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
2018
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
2119
import com.oracle.truffle.api.object.DynamicObject;
2220

2321
public class ValueWrapperManager {
2422

2523
static final int UNSET_HANDLE = -1;
26-
public static final int FALSE_HANDLE = 0;
27-
public static final int TRUE_HANDLE = 1;
28-
public static final int UNDEF_HANDLE = 2;
29-
public static final int NIL_HANDLE = 3;
3024

31-
public static final int LONG_TAG = 4;
32-
public static final int OBJECT_TAG = 7;
25+
/*
26+
* These constants are taken from ruby.h, and are based on us not tagging doubles.
27+
*/
28+
29+
public static final int FALSE_HANDLE = 0x0;
30+
public static final int TRUE_HANDLE = 0x2;
31+
public static final int NIL_HANDLE = 0x04;
32+
public static final int UNDEF_HANDLE = 0x6;
33+
34+
public static final long LONG_TAG = 1;
35+
public static final long OBJECT_TAG = 0;
36+
37+
public static final long MIN_FIXNUM_VALUE = -(1L << 62);
38+
public static final long MAX_FIXNUM_VALUE = (1L << 62) - 1;
39+
40+
public static final int TAG_BITS = 3;
41+
public static final long TAG_MASK = 0x7;
3342

3443
private final LongHashMap<WeakReference<DynamicObject>> handleMap = new LongHashMap<>(1024);
3544

@@ -79,7 +88,7 @@ public synchronized void removeFromHandleMap(long handle) {
7988
public synchronized long createNativeHandle(DynamicObject wrapper) {
8089
Pointer handlePointer = Pointer.malloc(1);
8190
long handleAddress = handlePointer.getAddress();
82-
if ((handleAddress & 0x7) != 0) {
91+
if ((handleAddress & TAG_MASK) != 0) {
8392
throw new RuntimeException("unaligned malloc for native handle");
8493
}
8594
Layouts.VALUE_WRAPPER.setHandle(wrapper, handleAddress);

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ public DynamicObject wrapInt(int value) {
3131
return wrapLong(value);
3232
}
3333

34-
private final long MIN_VALUE = 0xf000_0000_0000_0000L;
35-
private final long MAX_VALUE = 0x0fff_ffff_ffff_ffffL;
36-
3734
@Specialization
3835
public DynamicObject wrapLong(long value) {
39-
if (value >= MIN_VALUE && value <= MAX_VALUE) {
40-
long val = (value << 3) | LONG_TAG;
36+
if (value >= ValueWrapperManager.MIN_FIXNUM_VALUE && value <= ValueWrapperManager.MAX_FIXNUM_VALUE) {
37+
long val = (value << 1) | LONG_TAG;
4138
return Layouts.VALUE_WRAPPER.createValueWrapper(value, val);
4239
} else {
4340
return getContext().getValueWrapperManager().longWrapper(value);

0 commit comments

Comments
 (0)