summaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorwmeissner <devnull@localhost>2008-09-12 19:14:30 +1000
committerwmeissner <devnull@localhost>2008-09-12 19:14:30 +1000
commitfc8fac9d844fd8139f1cb61b8d07a9b56d608692 (patch)
treed8e779cdbe2b244a98a5d8fb9c00001ad61fd371 /samples
parent0bfdb2d5283da73a20c8136d2008c26bf71138d7 (diff)
downloadffi-fc8fac9d844fd8139f1cb61b8d07a9b56d608692.tar.gz
Callbacks are working sufficiently for qsort.rb to work
Diffstat (limited to 'samples')
-rw-r--r--samples/getpid.rb.orig6
-rw-r--r--samples/qsort.rb17
2 files changed, 23 insertions, 0 deletions
diff --git a/samples/getpid.rb.orig b/samples/getpid.rb.orig
new file mode 100644
index 0000000..a4d194b
--- /dev/null
+++ b/samples/getpid.rb.orig
@@ -0,0 +1,6 @@
+require 'ffi'
+module Foo
+ extend FFI::Library
+ attach_function :getpid, [ ], :int
+end
+puts "My pid=#{Foo.getpid do; end}"
diff --git a/samples/qsort.rb b/samples/qsort.rb
new file mode 100644
index 0000000..167a014
--- /dev/null
+++ b/samples/qsort.rb
@@ -0,0 +1,17 @@
+require 'ffi'
+
+module LibC
+ extend FFI::Library
+ callback :qsort_cmp, [ :pointer, :pointer ], :int
+ attach_function :qsort, [ :pointer, :int, :int, :qsort_cmp ], :int
+end
+
+p = MemoryPointer.new(:int, 2)
+p.put_array_of_int32(0, [ 2, 1 ])
+puts "Before qsort #{p.get_array_of_int32(0, 2).join(', ')}"
+LibC.qsort(p, 2, 4) do |p1, p2|
+ i1 = p1.get_int32(0)
+ i2 = p2.get_int32(0)
+ i1 < i2 ? -1 : i1 > i2 ? 1 : 0
+end
+puts "After qsort #{p.get_array_of_int32(0, 2).join(', ')}"