Skip to content

Commit

Permalink
Removed monkey patch for Fiddle
Browse files Browse the repository at this point in the history
Now loading LibUI will not change the Fiddle
  • Loading branch information
kojix2 committed Sep 26, 2021
1 parent 7864c3b commit ed620e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/libui/ffi.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

require 'fiddle/import'
require_relative 'fiddle_ext'
require_relative 'fiddle_patch'

module LibUI
module FFI
extend Fiddle::Importer
extend FiddlePatch

begin
dlload LibUI.ffi_lib
Expand Down
38 changes: 24 additions & 14 deletions lib/libui/fiddle_ext.rb → lib/libui/fiddle_patch.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
require 'fiddle/import'

module Fiddle
# Change the Function to hold a little more information.
class Function
# Note:
# Ruby 2.7 Fiddle::Function dose not have @argument_types
# Ruby 3.0 Fiddle::Function has @argument_types
attr_accessor :callback_argument_types, :argument_types
end

module Importer
module LibUI
module FiddlePatch
def parse_signature(signature, tymap = nil)
tymap ||= {}
ctype, func, args = case compact(signature)
Expand Down Expand Up @@ -42,10 +32,29 @@ def extern(signature, *opts)
opt = parse_bind_options(opts)
func = import_function(symname, ctype, argtype, opt[:call_type])

func.callback_argument_types = callback_argument_types # Added
# callback_argument_types
func.instance_variable_set(:@callback_argument_types,
callback_argument_types) # Added
# attr_reader
def func.callback_argument_types
@callback_argument_types
end

# argument_types
# Ruby 2.7 Fiddle::Function dose not have @argument_types
# Ruby 3.0 Fiddle::Function has @argument_types
func.argument_types = argtype
if func.instance_variable_defined?(:@argument_types)
# check if @argument_types are the same
if func.instance_variable_get(:@argument_types) != argtype
warn "#{symname} func.argument_types:#{func.argument_types} != argtype #{argtype}"
end
else
func.instance_variable_set(:@argument_types, argtype)
end
# attr_reader
def func.argument_types
@argument_types
end

name = symname.gsub(/@.+/, '')
@func_map[name] = func
Expand All @@ -66,4 +75,5 @@ def #{name}(*args, &block)
func
end
end
private_constant :FiddlePatch
end

0 comments on commit ed620e9

Please sign in to comment.