Class: FFIDB::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/ffidb/exporter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream = $stdout, **kwargs) ⇒ Exporter



30
31
32
33
# File 'lib/ffidb/exporter.rb', line 30

def initialize(stream = $stdout, **kwargs)
  @stream = stream
  @options = kwargs.transform_keys(&:to_sym).freeze
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/ffidb/exporter.rb', line 28

def options
  @options
end

Class Method Details

.for(format) ⇒ Object

TODO



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ffidb/exporter.rb', line 9

def self.for(format) # TODO
  require_relative 'exporters'
  case format&.to_sym
    when :c, :c99, :c11, :c18 then Exporters::C
    when :'c++', :'c++11', :'c++14', :'c++17', :'c++20', :cpp, :cxx then Exporters::Cpp
    when :csv then Exporters::CSV
    when :dart, :flutter then Exporters::Dart
    when :go, :cgo then Exporters::Go
    when :java, :jna then Exporters::Java
    when :json then Exporters::JSON
    when :lisp, :'common-lisp' then Exporters::Lisp
    when :python, :py then Exporters::Python
    when :ruby, :rb then Exporters::Ruby
    # TODO: csharp, haskell, julia, luajit, nim, nodejs, ocaml, php, racket, rust, zig
    when :yaml then Exporters::YAML
    else raise "unknown output format: #{format}"
  end
end

Instance Method Details

#beginObject



65
# File 'lib/ffidb/exporter.rb', line 65

def begin() end

#begin_library(library) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/ffidb/exporter.rb', line 67

def begin_library(library)
  @library = library
  @libraries ||= []
  @libraries << library
  @typedefs ||= {}
  @enums ||= {}
  @structs ||= {}
  @unions ||= {}
  @functions ||= {}
end

#closeObject



117
# File 'lib/ffidb/exporter.rb', line 117

def close() end

#debug?Boolean



35
36
37
# File 'lib/ffidb/exporter.rb', line 35

def debug?
  self.options[:debug]
end

#dlopen_paths_for(library) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/ffidb/exporter.rb', line 57

def dlopen_paths_for(library)
  if library_path = self.options[:library_path]
    library.objects.map { |lib| library_path.delete_suffix('/') << "/" << lib }
  else
    library.objects + library.dlopen
  end
end

#emit(&block) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/ffidb/exporter.rb', line 47

def emit(&block)
  begin
    self.begin
    yield self
    self.finish
  ensure
    self.close
  end
end

#export_enum(enum, disabled: nil) ⇒ Object



95
96
97
# File 'lib/ffidb/exporter.rb', line 95

def export_enum(enum, disabled: nil)
  (@enums[@library] ||= []) << enum
end

#export_function(function, disabled: nil) ⇒ Object



107
108
109
# File 'lib/ffidb/exporter.rb', line 107

def export_function(function, disabled: nil)
  (@functions[@library] ||= []) << self.resolve_function(function)
end

#export_header(header) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/ffidb/exporter.rb', line 78

def export_header(header)
  header.typedefs.sort.each { |typedef| self.export_typedef(typedef) }
  header.enums.sort.each { |enum| self.export_enum(enum) }
  header.structs.sort.each { |struct| self.export_struct(struct) }
  header.unions.sort.each { |union| self.export_union(union) }
  header.functions.sort.each { |function| self.export_function(function) }
end

#export_struct(struct, disabled: nil) ⇒ Object



99
100
101
# File 'lib/ffidb/exporter.rb', line 99

def export_struct(struct, disabled: nil)
  (@structs[@library] ||= []) << self.resolve_struct(struct)
end

#export_symbol(symbol, disabled: nil) ⇒ Object



86
87
88
# File 'lib/ffidb/exporter.rb', line 86

def export_symbol(symbol, disabled: nil)
  self.__send__("export_#{symbol.kind}", symbol, disabled: disabled)
end

#export_typedef(typedef, disabled: nil) ⇒ Object



90
91
92
93
# File 'lib/ffidb/exporter.rb', line 90

def export_typedef(typedef, disabled: nil)
  @typedefs[@library] ||= {}
  @typedefs[@library][typedef.name] = typedef
end

#export_union(union, disabled: nil) ⇒ Object



103
104
105
# File 'lib/ffidb/exporter.rb', line 103

def export_union(union, disabled: nil)
  (@unions[@library] ||= []) << self.resolve_union(union)
end

#finishObject



115
# File 'lib/ffidb/exporter.rb', line 115

def finish() end

#finish_libraryObject



111
112
113
# File 'lib/ffidb/exporter.rb', line 111

def finish_library
  @library = nil
end

#header?Boolean



43
44
45
# File 'lib/ffidb/exporter.rb', line 43

def header?
  self.options[:header]
end

#verbose?Boolean



39
40
41
# File 'lib/ffidb/exporter.rb', line 39

def verbose?
  self.options[:verbose] || self.debug?
end