Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement callback returning type :string #770

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions ext/ffi_c/Function.c
Expand Up @@ -793,6 +793,15 @@ invoke_callback(VALUE data)
case NATIVE_FLOAT64:
*((double *) retval) = NUM2DBL(rbReturnValue);
break;

case NATIVE_STRING:
if (NIL_P(rbReturnValue)) {
*((void **) retval) = NULL;
} else {
*((void **) retval) = StringValueCStr(rbReturnValue);
}
break;

case NATIVE_POINTER:
if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_PointerClass)) {
*((void **) retval) = ((AbstractMemory *) DATA_PTR(rbReturnValue))->address;
Expand Down
11 changes: 11 additions & 0 deletions spec/ffi/callback_spec.rb
Expand Up @@ -51,6 +51,7 @@ class S8F32S32 < FFI::Struct
callback :cbVrUL, [ ], :ulong
callback :cbVrS64, [ ], :long_long
callback :cbVrU64, [ ], :ulong_long
callback :cbVrA, [], :string
callback :cbVrP, [], :pointer
callback :cbVrZ, [], :bool
callback :cbCrV, [ :char ], :void
Expand All @@ -75,6 +76,7 @@ class S8F32S32 < FFI::Struct
attach_function :testCallbackVrUL, :testClosureVrL, [ :cbVrUL ], :ulong
attach_function :testCallbackVrS64, :testClosureVrLL, [ :cbVrS64 ], :long_long
attach_function :testCallbackVrU64, :testClosureVrLL, [ :cbVrU64 ], :ulong_long
attach_function :testCallbackVrA, :testClosureVrP, [ :cbVrA ], :string
attach_function :testCallbackVrP, :testClosureVrP, [ :cbVrP ], :pointer
attach_function :testCallbackReturningFunction, :testClosureVrP, [ :cbVrP ], :cbVrP
attach_function :testCallbackVrY, :testClosureVrP, [ :cbVrY ], S8F32S32.ptr
Expand Down Expand Up @@ -261,6 +263,15 @@ class S8F32S32 < FFI::Struct
expect(LibTest.testCallbackVrZ { true }).to be true
end

it "returning :string (nil)" do
expect(LibTest.testCallbackVrA { nil }).to be_nil
end

it "returning :string" do
str = "String ä"
expect(LibTest.testCallbackVrA { str }).to eq(str.b)
end

it "returning :pointer (nil)" do
expect(LibTest.testCallbackVrP { nil }).to be_null
end
Expand Down