summaryrefslogtreecommitdiff
path: root/lib/ffi
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ffi')
-rw-r--r--lib/ffi/function.rb17
-rw-r--r--lib/ffi/library.rb1
-rw-r--r--lib/ffi/variadic.rb3
3 files changed, 20 insertions, 1 deletions
diff --git a/lib/ffi/function.rb b/lib/ffi/function.rb
index b4469be..ac4daf0 100644
--- a/lib/ffi/function.rb
+++ b/lib/ffi/function.rb
@@ -50,5 +50,22 @@ module FFI
type.param_types
end
end
+
+ # Stash the Function in a module variable so it can be inspected by attached_functions.
+ # On CRuby it also ensures that it does not get garbage collected.
+ module RegisterAttach
+ def attach(mod, name)
+ funcs = mod.instance_variable_get("@ffi_functions")
+ unless funcs
+ funcs = {}
+ mod.instance_variable_set("@ffi_functions", funcs)
+ end
+ funcs[name.to_sym] = self
+ # Jump to the native attach method of CRuby, JRuby or Tuffleruby
+ super
+ end
+ end
+ private_constant :RegisterAttach
+ prepend RegisterAttach
end
end
diff --git a/lib/ffi/library.rb b/lib/ffi/library.rb
index 9394902..c23112b 100644
--- a/lib/ffi/library.rb
+++ b/lib/ffi/library.rb
@@ -281,6 +281,7 @@ module FFI
# Attach C variable +cname+ to this module.
def attach_variable(mname, a1, a2 = nil)
cname, type = a2 ? [ a1, a2 ] : [ mname.to_s, a1 ]
+ mname = mname.to_sym
address = nil
ffi_libraries.each do |lib|
begin
diff --git a/lib/ffi/variadic.rb b/lib/ffi/variadic.rb
index 389c53c..ee33409 100644
--- a/lib/ffi/variadic.rb
+++ b/lib/ffi/variadic.rb
@@ -54,6 +54,7 @@ module FFI
invoker = self
params = "*args"
call = "call"
+ mname = mname.to_sym
mod.module_eval <<-code, __FILE__, __LINE__
@ffi_functions = {} unless defined?(@ffi_functions)
@ffi_functions[#{mname.inspect}] = invoker
@@ -62,7 +63,7 @@ module FFI
@ffi_functions[#{mname.inspect}].#{call}(#{params})
end
- define_method(#{mname.inspect}, &method(:#{mname}))
+ define_method(#{mname.inspect}, &method(#{mname.inspect}))
code
invoker
end