Class: Libcall::Caller

Inherits:
Object
  • Object
show all
Defined in:
lib/libcall/caller.rb

Overview

Execute C function calls via Fiddle FFI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lib_path, func_name, arg_pairs: [], return_type: :void) ⇒ Caller

Returns a new instance of Caller.



10
11
12
13
14
15
# File 'lib/libcall/caller.rb', line 10

def initialize(lib_path, func_name, arg_pairs: [], return_type: :void)
  @lib_path = lib_path
  @func_name = func_name
  @return_type = return_type
  @arg_pairs = arg_pairs
end

Instance Attribute Details

#arg_pairsObject (readonly)

Returns the value of attribute arg_pairs.



8
9
10
# File 'lib/libcall/caller.rb', line 8

def arg_pairs
  @arg_pairs
end

#func_nameObject (readonly)

Returns the value of attribute func_name.



8
9
10
# File 'lib/libcall/caller.rb', line 8

def func_name
  @func_name
end

#lib_pathObject (readonly)

Returns the value of attribute lib_path.



8
9
10
# File 'lib/libcall/caller.rb', line 8

def lib_path
  @lib_path
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



8
9
10
# File 'lib/libcall/caller.rb', line 8

def return_type
  @return_type
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/libcall/caller.rb', line 17

def call
  processor = ArgumentProcessor.new(arg_pairs)
  processed = processor.process

  raw_result = execute_function(processed)
  formatted_result = format_result(raw_result, return_type)

  output_reader = OutputReader.new(processed.out_refs)
  if output_reader.empty?
    formatted_result
  else
    { result: formatted_result, outputs: output_reader.read }
  end
rescue Fiddle::DLError => e
  raise Error, "Failed to load library or function: #{e.message}"
end