10
10
package org .truffleruby .cext ;
11
11
12
12
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 ;
13
15
import static org .truffleruby .cext .ValueWrapperManager .LONG_TAG ;
16
+ import static org .truffleruby .cext .ValueWrapperManager .NIL_HANDLE ;
14
17
import static org .truffleruby .cext .ValueWrapperManager .OBJECT_TAG ;
15
18
import static org .truffleruby .cext .ValueWrapperManager .FALSE_HANDLE ;
16
19
20
+ import org .truffleruby .cext .UnwrapNodeGen .NativeToWrapperNodeGen ;
17
21
import org .truffleruby .cext .UnwrapNodeGen .ToWrapperNodeGen ;
18
22
import org .truffleruby .cext .UnwrapNodeGen .UnwrapNativeNodeGen ;
19
23
import org .truffleruby .language .NotProvided ;
@@ -82,6 +86,54 @@ public static UnwrapNativeNode create() {
82
86
}
83
87
}
84
88
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
+
85
137
@ ImportStatic (Message .class )
86
138
public static abstract class ToWrapperNode extends RubyBaseNode {
87
139
@@ -96,8 +148,7 @@ public ValueWrapper wrappedValueWrapper(ValueWrapper value) {
96
148
public ValueWrapper unwrapTypeCastObject (TruffleObject value ,
97
149
@ Cached ("IS_POINTER.createNode()" ) Node isPointerNode ,
98
150
@ Cached ("AS_POINTER.createNode()" ) Node asPointerNode ,
99
- @ Cached ("create()" ) UnwrapNativeNode unwrapNativeNode ,
100
- @ Cached ("create()" ) WrapNode wrapNode ,
151
+ @ Cached ("create()" ) NativeToWrapperNode nativeToWrapperNode ,
101
152
@ Cached ("create()" ) BranchProfile unsupportedProfile ,
102
153
@ Cached ("create()" ) BranchProfile nonPointerProfile ) {
103
154
if (ForeignAccess .sendIsPointer (isPointerNode , value )) {
@@ -108,12 +159,7 @@ public ValueWrapper unwrapTypeCastObject(TruffleObject value,
108
159
unsupportedProfile .enter ();
109
160
throw new RaiseException (getContext (), coreExceptions ().argumentError (e .getMessage (), this , e ));
110
161
}
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 );
117
163
} else {
118
164
nonPointerProfile .enter ();
119
165
throw new RaiseException (getContext (), coreExceptions ().argumentError ("Not a handle or a pointer" , this ));
0 commit comments