File tree Expand file tree Collapse file tree 2 files changed +20
-4
lines changed
lib/truffle/truffle/ffi_backend
src/main/java/org/truffleruby/cext Expand file tree Collapse file tree 2 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -178,15 +178,16 @@ def free
178
178
end
179
179
end
180
180
181
+ # Returns a Truffle::FFI::Pointer, to correctly keep the argument alive during the native call
181
182
private def get_pointer_value ( value )
182
183
if Truffle ::FFI ::Pointer === value
183
- value . address
184
+ value
184
185
elsif nil . equal? ( value )
185
- 0
186
+ Truffle :: FFI :: Pointer :: NULL
186
187
elsif String === value
187
- Truffle ::CExt . string_pointer_to_native ( value )
188
+ Truffle ::CExt . string_to_pointer ( value )
188
189
elsif value . respond_to? ( :to_ptr )
189
- value . to_ptr . address
190
+ Truffle :: Type . coerce_to value , Truffle :: FFI :: Pointer , : to_ptr
190
191
else
191
192
raise ArgumentError , "#{ value . inspect } (#{ value . class } ) is not a pointer"
192
193
end
Original file line number Diff line number Diff line change 90
90
import org .truffleruby .language .dispatch .DoesRespondDispatchHeadNode ;
91
91
import org .truffleruby .language .methods .DeclarationContext ;
92
92
import org .truffleruby .language .methods .InternalMethod ;
93
+ import org .truffleruby .language .objects .AllocateObjectNode ;
93
94
import org .truffleruby .language .objects .InitializeClassNode ;
94
95
import org .truffleruby .language .objects .InitializeClassNodeGen ;
95
96
import org .truffleruby .language .objects .IsFrozenNode ;
@@ -1002,6 +1003,20 @@ public long toNative(DynamicObject string,
1002
1003
1003
1004
}
1004
1005
1006
+ @ CoreMethod (names = "string_to_pointer" , onSingleton = true , required = 1 )
1007
+ public abstract static class StringToPointerNode extends CoreMethodArrayArgumentsNode {
1008
+
1009
+ @ Specialization (guards = "isRubyString(string)" )
1010
+ public DynamicObject toNative (DynamicObject string ,
1011
+ @ Cached ("create()" ) StringToNativeNode stringToNativeNode ,
1012
+ @ Cached ("create()" ) AllocateObjectNode allocateObjectNode ) {
1013
+ final NativeRope nativeRope = stringToNativeNode .executeToNative (string );
1014
+
1015
+ return allocateObjectNode .allocate (coreLibrary ().getTruffleFFIPointerClass (), nativeRope .getNativePointer ());
1016
+ }
1017
+
1018
+ }
1019
+
1005
1020
@ CoreMethod (names = "string_pointer_is_native?" , onSingleton = true , required = 1 )
1006
1021
public abstract static class StringPointerIsNativeNode extends CoreMethodArrayArgumentsNode {
1007
1022
You can’t perform that action at this time.
0 commit comments