File: embed-test.rb

package info (click to toggle)
jruby 9.3.9.0%2Bds-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,856 kB
  • sloc: ruby: 517,823; java: 260,094; xml: 31,930; ansic: 5,777; yacc: 4,973; sh: 1,163; makefile: 105; jsp: 48; tcl: 40; exp: 11
file content (51 lines) | stat: -rwxr-xr-x 1,267 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
#
# This file is part of ruby-ffi.
# For licensing, see LICENSE.SPECS
#

# This test specifically avoids calling native code through FFI.
# Instead, the stock extension mechanism is used. The reason is
# that the C extension initializes FFI and then calls a callback
# which deadlocked in earlier FFI versions, see
# https://github.com/ffi/ffi/issues/527

require 'rbconfig'
require 'ffi'

EXT = File.expand_path("ext/embed_test.#{RbConfig::CONFIG['DLEXT']}", File.dirname(__FILE__))
old = Dir.pwd
Dir.chdir(File.dirname(EXT))

nul = File.open(File::NULL)
make = system('type gmake', { :out => nul, :err => nul }) && 'gmake' || 'make'

# create Makefile
system(RbConfig.ruby, "extconf.rb")

# compile extension
unless system(make)
  raise "Unable to compile \"#{EXT}\""
end

Dir.chdir(old)

puts "load #{EXT}"
require EXT

module LibWrap
  extend FFI::Library
  ffi_lib EXT
  callback :completion_function, [:string, :long, :uint8], :void
  attach_function :do_work, [:string, :completion_function], :int
  Callback = Proc.new do |buf_ptr, count, code|
    puts "callback called with #{[buf_ptr, count, code].inspect}"
    nil
  end
end

puts "call do_work()"
LibWrap.do_work("test", LibWrap::Callback)

puts "call testfunc()"
EmbedTest::testfunc