Sha256: 5daff2aadfd2ce6290f7a5534d377b22c88353963f0d0b26a833dcdb77aab57c

Contents?: true

Size: 808 Bytes

Versions: 3

Compression:

Stored size: 808 Bytes

Contents

module Rufus::Lua
  # Auto-unref Lua ref when it goes out of scope in Ruby.
  class Ref
    def initialize_with_finalizer(*args)
      initialize_without_finalizer(*args)

      ObjectSpace.define_finalizer self, self.class.finalize(@pointer, @ref)
    end
    alias_method :initialize_without_finalizer, :initialize
    alias_method :initialize, :initialize_with_finalizer

    def self.finalize(pointer, ref)
      # Hack to test if the Lua state was closed and de-refed.
      original_pointer_address = pointer.read_pointer
      proc do
        Lib.luaL_unref(pointer, LUA_REGISTRYINDEX, ref) if original_pointer_address == pointer.read_pointer
      end
    end

    undef :free
    def free
      # Noop. Taken care of by finalizer. Should not conflict.
    end

    public :load_onto_stack
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
immunio-0.15.4 lib/immunio/rufus_lua_ext/ref.rb
immunio-0.15.3 lib/immunio/rufus_lua_ext/ref.rb
immunio-0.15.2 lib/immunio/rufus_lua_ext/ref.rb