summaryrefslogtreecommitdiff
path: root/lib/ffi/function.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ffi/function.rb')
-rw-r--r--lib/ffi/function.rb17
1 files changed, 17 insertions, 0 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