Using FFI or Fiddle on an Embedded Ruby


#1

I would like to be able to pass Ruby varargs as an argument to a C
function created with rb_define_method. The typical prototype for a C
function with argc = -1 is VALUE func(int argc, VALUE *argv, VALUE obj).

The following is a code sample that would be representative of the
situation but yet would work with a standard Ruby.

########
require(“fiddle”)
require(“fiddle/import”)

module EmbeddedRuby
extend Fiddle::Importer
dlload(“msvcrt-ruby220.dll”)

typealias “VALUE”, “unsigned long”

extern “VALUE rb_ary_cat(VALUE, const VALUE *, long)”
end

a = [ “a”, “b”, “c” ]
b = [“d”, “e”, “f”]

puts a.dup.push(b).inspect
puts a.dup.push(*b).inspect

puts Fiddle::Pointer.new(
EmbeddedRuby.rb_ary_cat(
Fiddle::Pointer.new(a.id << 1),
Fiddle::Pointer.new(b.id << 1).ref,
b.size)).to_value.inspect
########

The Ruby script outputs the following on Ruby 2.2.6:

[“a”, “b”, “c”, [“d”, “e”, “f”]]
[“a”, “b”, “c”, “d”, “e”, “f”]
[“a”, “b”, “c”, [“d”, “e”, “f”], false, false]

The expected results would be [“a”, “b”, “c”, “d”, “e”, “f”]. Anyone
knows how to do this with either Fiddle or FFI (or both)?