summaryrefslogtreecommitdiff
path: root/lib/ffi/function.rb
diff options
context:
space:
mode:
authorLars Kanis <lars@greiz-reinsdorf.de>2023-05-08 08:57:10 +0200
committerLars Kanis <lars@greiz-reinsdorf.de>2023-05-08 08:57:10 +0200
commitb448759f390bb50eceb9b48a307efeba68c3581c (patch)
tree1e8b91533f99a3934a8eb83567d3a545df6a39ec /lib/ffi/function.rb
parentb0ed4717ea08c61d8a1d0c9205cedff3e33805e3 (diff)
downloadffi-b448759f390bb50eceb9b48a307efeba68c3581c.tar.gz
Register functions in Ruby code
For consistency and to ease proting to JRuby and Truffleruby.
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