Skip to content

Commit

Permalink
Fix embed-test on Windows
Browse files Browse the repository at this point in the history
Windows doesn't resolve symbols of external DLLs, if called with
CURRENT_PROCESS. Instead the DLL has to be named explicit.

Add an expectation to the spec while being over it.
  • Loading branch information
larskanis committed Jan 25, 2019
1 parent 544b721 commit a98dbb9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 36 deletions.
2 changes: 2 additions & 0 deletions spec/ffi/callback_spec.rb
Expand Up @@ -862,6 +862,8 @@ def assert_callback_in_same_thread_called_once
raise "external process failed:\n#{ File.read("embed-test.log") }"
end
end

expect(File.read("embed-test.log")).to match(/callback called with \["hello", 5, 0\]/)
end
end
end
18 changes: 18 additions & 0 deletions spec/ffi/embed-test/embed-test.rb
Expand Up @@ -28,5 +28,23 @@

Dir.chdir(old)

puts "load #{EXT}"
require EXT

require 'ffi'
module LibWrap
extend FFI::Library
ffi_lib EXT
callback :completion_function, [:string, :long, :uint8], :void
attach_function :do_work, [:pointer, :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
38 changes: 2 additions & 36 deletions spec/ffi/embed-test/ext/embed.c
Expand Up @@ -29,21 +29,6 @@
#include <stdio.h>
#include <ruby.h>

const char script[] =
"require 'rubygems'\n"
"require 'ffi'\n"
"module LibWrap\n"
" extend FFI::Library\n"
" ffi_lib FFI::CURRENT_PROCESS\n"
" callback :completion_function, [:string, :long, :uint8], :void\n"
" attach_function :do_work, [:pointer, :completion_function], :int\n"
" Callback = Proc.new do |buf_ptr, count, code|\n"
" nil\n"
" end\n"
"end\n"
"\n"
"LibWrap.do_work(\"test\", LibWrap::Callback)\n";

typedef void completion_function(const char *buffer, long count, unsigned char code);

static completion_function *ruby_func;
Expand All @@ -53,7 +38,6 @@ static completion_function *ruby_func;
#endif
int do_work(const char *buffer, completion_function *fn)
{
/* Calling fn directly here works */
ruby_func = fn;
return 0;
}
Expand All @@ -68,25 +52,7 @@ void Init_embed_test(void)

static VALUE testfunc(VALUE self)
{
int state = 0;
VALUE ret;

rb_eval_string_protect(script, &state);

if (state)
{
VALUE e = rb_errinfo();
ret = rb_funcall(e, rb_intern("message"), 0);
fprintf(stderr, "exc %s\n", StringValueCStr(ret));
rb_set_errinfo(Qnil);
exit(1);
}
else
{
/* Calling fn here hangs, because ffi expects an initial ruby stack
* frame. Spawn a thread to kill the process, otherwise the deadlock
* would prevent completing the test. */
ruby_func("hello", 5, 0);
}
printf("testfunc() called\n");
ruby_func("hello", 5, 0);
return Qnil;
}

0 comments on commit a98dbb9

Please sign in to comment.