mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
b386fe21ec
* ext/dl/function.c: DL::Function now uses libffi * ext/dl/cfunc.c (rb_dl_set_last_error): set to non static so errors can be exposed. * ext/dl/closure.c: DL::Closure will now be used in place of ext/dl/callback/*. * ext/dl/dl.c: legacy callbacks removed in favor of libffi * ext/dl/dl_converions.(c,h): used for converting ruby types to FFI types. * ext/dl/callback/*: replaced by libffi callbacks. * ext/dl/lib/dl/callback.rb: Converting internal callbacks to use DL::Closure * ext/dl/lib/dl/closure.rb: Ruby parts of the new DL::Closure object * ext/dl/lib/dl/import.rb: More conversion to use DL::Closure object * ext/dl/lib/dl/value.rb (ruby2ffi): adding private method for DL::CPtr to ffi value conversion. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26545 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
130 lines
3.2 KiB
Ruby
130 lines
3.2 KiB
Ruby
require_relative 'test_base'
|
|
require 'dl/func'
|
|
require 'dl/closure'
|
|
|
|
module DL
|
|
class TestClosure < Test::Unit::TestCase
|
|
class Returner < DL::Closure
|
|
attr_accessor :called
|
|
attr_accessor :called_with
|
|
def call *args
|
|
@called = true
|
|
@called_with = args
|
|
a = args.first
|
|
DL::CPtr === a ? a.to_i : a
|
|
end
|
|
end
|
|
|
|
if defined?(TYPE_LONG_LONG)
|
|
def test_long_long
|
|
type = TYPE_LONG_LONG
|
|
addr = Returner.new(type, [type]) do |num|
|
|
called = true
|
|
called_with = num
|
|
end
|
|
func = DL::Function.new(addr, [type])
|
|
assert_equal(9223372036854775807, func.call(9223372036854775807))
|
|
end
|
|
end
|
|
|
|
def test_with_abi
|
|
called = false
|
|
addr = DL::Closure::BlockCaller.new(
|
|
TYPE_INT,
|
|
[TYPE_INT],
|
|
DL::Function::DEFAULT
|
|
) do |num|
|
|
called = true
|
|
num
|
|
end
|
|
func = DL::Function.new(addr, [TYPE_INT])
|
|
func.call(50)
|
|
assert called
|
|
end
|
|
|
|
def test_block_caller
|
|
called = false
|
|
called_with = nil
|
|
addr = DL::Closure::BlockCaller.new(TYPE_INT, [TYPE_INT]) do |num|
|
|
called = true
|
|
called_with = num
|
|
end
|
|
func = DL::Function.new(addr, [TYPE_INT])
|
|
func.call(50)
|
|
assert called, 'function was called'
|
|
assert_equal 50, called_with
|
|
end
|
|
|
|
def test_multival
|
|
adder = Class.new(DL::Closure) {
|
|
def call a, b
|
|
a + b
|
|
end
|
|
}.new(TYPE_INT, [TYPE_INT, TYPE_INT])
|
|
|
|
assert_equal [TYPE_INT, TYPE_INT], adder.args
|
|
func = DL::Function.new(adder, adder.args)
|
|
assert_equal 70, func.call(50, 20)
|
|
end
|
|
|
|
def test_call
|
|
closure = Class.new(DL::Closure) {
|
|
attr_accessor :called_with
|
|
def call num
|
|
@called_with = num
|
|
end
|
|
}.new(TYPE_INT, [TYPE_INT])
|
|
|
|
func = DL::Function.new(closure, [TYPE_INT])
|
|
func.call(50)
|
|
|
|
assert_equal 50, closure.called_with
|
|
end
|
|
|
|
def test_return_value
|
|
closure = Returner.new(TYPE_INT, [TYPE_INT])
|
|
|
|
func = DL::Function.new(closure, [TYPE_INT])
|
|
assert_equal 50, func.call(50)
|
|
end
|
|
|
|
def test_float
|
|
closure = Returner.new(TYPE_FLOAT, [TYPE_FLOAT])
|
|
func = DL::Function.new(closure, [TYPE_FLOAT])
|
|
assert_equal 2.0, func.call(2.0)
|
|
end
|
|
|
|
def test_char
|
|
closure = Returner.new(TYPE_CHAR, [TYPE_CHAR])
|
|
func = DL::Function.new(closure, [TYPE_CHAR])
|
|
assert_equal 60, func.call(60)
|
|
end
|
|
|
|
def test_long
|
|
closure = Returner.new(TYPE_LONG, [TYPE_LONG])
|
|
func = DL::Function.new(closure, [TYPE_LONG])
|
|
assert_equal 60, func.call(60)
|
|
end
|
|
|
|
def test_double
|
|
closure = Returner.new(TYPE_DOUBLE, [TYPE_DOUBLE])
|
|
func = DL::Function.new(closure, [TYPE_DOUBLE])
|
|
assert_equal 60, func.call(60)
|
|
end
|
|
|
|
def test_voidp
|
|
closure = Returner.new(TYPE_VOIDP, [TYPE_VOIDP])
|
|
func = DL::Function.new(closure, [TYPE_VOIDP])
|
|
|
|
voidp = CPtr['foo']
|
|
assert_equal voidp, func.call(voidp)
|
|
end
|
|
|
|
def test_void
|
|
closure = Returner.new(TYPE_VOID, [TYPE_VOID])
|
|
func = DL::Function.new(closure, [TYPE_VOID])
|
|
func.call()
|
|
assert closure.called
|
|
end
|
|
end
|
|
end
|