yakg 0.0.6 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile.lock +1 -1
- data/LICENSE +14 -0
- data/VERSION +1 -1
- data/vendor/gems/ruby/2.0.0/build_info/corefoundation-0.2.0.info +1 -0
- data/vendor/gems/ruby/2.0.0/build_info/ffi-1.8.1.info +1 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/CHANGELOG +12 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/LICENSE +8 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/README.md +40 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/array.rb +123 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/base.rb +197 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/boolean.rb +25 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/data.rb +42 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/date.rb +32 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/dictionary.rb +114 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/extensions.rb +158 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/null.rb +11 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/number.rb +98 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/string.rb +91 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation/version.rb +4 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/lib/corefoundation.rb +13 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/array_spec.rb +92 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/boolean_spec.rb +24 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/data_spec.rb +30 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/date_spec.rb +25 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/dictionary_spec.rb +81 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/extensions_spec.rb +127 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/null_spec.rb +7 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/number_spec.rb +52 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/spec_helper.rb +10 -0
- data/vendor/gems/ruby/2.0.0/gems/corefoundation-0.2.0/spec/string_spec.rb +48 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/COPYING +674 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/COPYING.LESSER +165 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/History.txt +1 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/LICENSE +14 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/README.md +109 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/Rakefile +219 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/ffi.gemspec +22 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/gen/Rakefile +30 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/gen/log +1 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/autopointer.rb +184 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/buffer.rb +4 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/callback.rb +4 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/enum.rb +162 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/errno.rb +33 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/ffi.iml +11 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/ffi.rb +33 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/io.rb +52 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/library.rb +489 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/managedstruct.rb +55 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/memorypointer.rb +1 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/arm-linux/types.conf +104 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/i386-cygwin/types.conf +3 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/i386-darwin/types.conf +100 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/i386-freebsd/types.conf +152 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/i386-linux/types.conf +103 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/i386-netbsd/types.conf +126 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/i386-openbsd/types.conf +126 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/i386-solaris/types.conf +122 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/i386-windows/types.conf +105 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/i486-gnu/types.conf +107 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/ia64-linux/types.conf +104 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/mips-linux/types.conf +102 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/mipsel-linux/types.conf +102 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/powerpc-aix/types.conf +180 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/powerpc-linux/types.conf +100 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/s390-linux/types.conf +102 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/s390x-linux/types.conf +102 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/sparc-linux/types.conf +102 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/sparc-solaris/types.conf +128 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/x86_64-darwin/types.conf +100 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/x86_64-linux/types.conf +102 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/x86_64-netbsd/types.conf +126 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/x86_64-openbsd/types.conf +126 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform/x86_64-windows/types.conf +27 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/platform.rb +139 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/pointer.rb +122 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/struct.rb +356 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/struct_layout_builder.rb +211 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/tools/const_generator.rb +229 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/tools/generator.rb +60 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/tools/generator_task.rb +36 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/tools/struct_generator.rb +194 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/tools/types_generator.rb +135 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/types.rb +177 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/union.rb +32 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/variadic.rb +65 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi/version.rb +4 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi.rb +28 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/lib/ffi_c.bundle +0 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/Benchmark.c +52 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/BoolTest.c +31 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/BufferTest.c +31 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/ClosureTest.c +190 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/EnumTest.c +34 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/FunctionTest.c +58 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/GNUmakefile +149 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/GlobalVariable.c +62 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/LastErrorTest.c +21 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/NumberTest.c +132 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/PointerTest.c +63 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/ReferenceTest.c +23 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/StringTest.c +34 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/StructTest.c +240 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/UnionTest.c +43 -0
- data/vendor/gems/ruby/2.0.0/gems/ffi-1.8.1/libtest/VariadicTest.c +62 -0
- data/vendor/gems/ruby/2.0.0/specifications/corefoundation-0.2.0.gemspec +42 -0
- data/vendor/gems/ruby/2.0.0/specifications/ffi-1.8.1.gemspec +42 -0
- data/yakg.gemspec +3 -2
- metadata +167 -76
@@ -0,0 +1,489 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2008-2010 Wayne Meissner
|
3
|
+
# Copyright (C) 2008 Luc Heinrich <luc@honk-honk.com>
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# This file is part of ruby-ffi.
|
8
|
+
#
|
9
|
+
# This code is free software: you can redistribute it and/or modify it under
|
10
|
+
# the terms of the GNU Lesser General Public License version 3 only, as
|
11
|
+
# published by the Free Software Foundation.
|
12
|
+
#
|
13
|
+
# This code is distributed in the hope that it will be useful, but WITHOUT
|
14
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
16
|
+
# version 3 for more details.
|
17
|
+
#
|
18
|
+
# You should have received a copy of the GNU Lesser General Public License
|
19
|
+
# version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
|
21
|
+
module FFI
|
22
|
+
CURRENT_PROCESS = USE_THIS_PROCESS_AS_LIBRARY = Object.new
|
23
|
+
|
24
|
+
# @param [#to_s] lib library name
|
25
|
+
# @return [String] library name formatted for current platform
|
26
|
+
# Transform a generic library name to a platform library name
|
27
|
+
# @example
|
28
|
+
# # Linux
|
29
|
+
# FFI.map_library_name 'c' # -> "libc.so.6"
|
30
|
+
# FFI.map_library_name 'jpeg' # -> "libjpeg.so"
|
31
|
+
# # Windows
|
32
|
+
# FFI.map_library_name 'c' # -> "msvcrt.dll"
|
33
|
+
# FFI.map_library_name 'jpeg' # -> "jpeg.dll"
|
34
|
+
def self.map_library_name(lib)
|
35
|
+
# Mangle the library name to reflect the native library naming conventions
|
36
|
+
lib = lib.to_s unless lib.kind_of?(String)
|
37
|
+
lib = Library::LIBC if lib == 'c'
|
38
|
+
|
39
|
+
if lib && File.basename(lib) == lib
|
40
|
+
lib = Platform::LIBPREFIX + lib unless lib =~ /^#{Platform::LIBPREFIX}/
|
41
|
+
r = Platform::IS_GNU ? "\\.so($|\\.[1234567890]+)" : "\\.#{Platform::LIBSUFFIX}$"
|
42
|
+
lib += ".#{Platform::LIBSUFFIX}" unless lib =~ /#{r}/
|
43
|
+
end
|
44
|
+
|
45
|
+
lib
|
46
|
+
end
|
47
|
+
|
48
|
+
# Exception raised when a function is not found in libraries
|
49
|
+
class NotFoundError < LoadError
|
50
|
+
def initialize(function, *libraries)
|
51
|
+
super("Function '#{function}' not found in [#{libraries[0].nil? ? 'current process' : libraries.join(", ")}]")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# This module is the base to use native functions.
|
56
|
+
#
|
57
|
+
# A basic usage may be:
|
58
|
+
# require 'ffi'
|
59
|
+
#
|
60
|
+
# module Hello
|
61
|
+
# extend FFI::Library
|
62
|
+
# ffi_lib FFI::Library::LIBC
|
63
|
+
# attach_function 'puts', [ :string ], :int
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# Hello.puts("Hello, World")
|
67
|
+
#
|
68
|
+
#
|
69
|
+
module Library
|
70
|
+
CURRENT_PROCESS = FFI::CURRENT_PROCESS
|
71
|
+
LIBC = FFI::Platform::LIBC
|
72
|
+
|
73
|
+
# @param mod extended object
|
74
|
+
# @return [nil]
|
75
|
+
# @raise {RuntimeError} if +mod+ is not a Module
|
76
|
+
# Test if extended object is a Module. If not, raise RuntimeError.
|
77
|
+
def self.extended(mod)
|
78
|
+
raise RuntimeError.new("must only be extended by module") unless mod.kind_of?(Module)
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
# @param [Array] names names of libraries to load
|
83
|
+
# @return [Array<DynamicLibrary>]
|
84
|
+
# @raise {LoadError} if a library cannot be opened
|
85
|
+
# Load native libraries.
|
86
|
+
def ffi_lib(*names)
|
87
|
+
raise LoadError.new("library names list must not be empty") if names.empty?
|
88
|
+
|
89
|
+
lib_flags = defined?(@ffi_lib_flags) ? @ffi_lib_flags : FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL
|
90
|
+
ffi_libs = names.map do |name|
|
91
|
+
|
92
|
+
if name == FFI::CURRENT_PROCESS
|
93
|
+
FFI::DynamicLibrary.open(nil, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_LOCAL)
|
94
|
+
|
95
|
+
else
|
96
|
+
libnames = (name.is_a?(::Array) ? name : [ name ]).map { |n| [ n, FFI.map_library_name(n) ].uniq }.flatten.compact
|
97
|
+
lib = nil
|
98
|
+
errors = {}
|
99
|
+
|
100
|
+
libnames.each do |libname|
|
101
|
+
begin
|
102
|
+
lib = FFI::DynamicLibrary.open(libname, lib_flags)
|
103
|
+
break if lib
|
104
|
+
|
105
|
+
rescue Exception => ex
|
106
|
+
ldscript = false
|
107
|
+
if ex.message =~ /(([^ \t()])+\.so([^ \t:()])*):([ \t])*invalid ELF header/
|
108
|
+
if File.read($1) =~ /GROUP *\( *([^ \)]+) *\)/
|
109
|
+
libname = $1
|
110
|
+
ldscript = true
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
if ldscript
|
115
|
+
retry
|
116
|
+
else
|
117
|
+
errors[libname] = ex
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
if lib.nil?
|
123
|
+
raise LoadError.new(errors.values.join(".\n"))
|
124
|
+
end
|
125
|
+
|
126
|
+
# return the found lib
|
127
|
+
lib
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
@ffi_libs = ffi_libs
|
132
|
+
end
|
133
|
+
|
134
|
+
# Set the calling convention for {#attach_function} and {#callback}
|
135
|
+
#
|
136
|
+
# @see http://en.wikipedia.org/wiki/Stdcall#stdcall
|
137
|
+
# @note +:stdcall+ is typically used for attaching Windows API functions
|
138
|
+
#
|
139
|
+
# @param [Symbol] convention one of +:default+, +:stdcall+
|
140
|
+
# @return [Symbol] the new calling convention
|
141
|
+
def ffi_convention(convention = nil)
|
142
|
+
@ffi_convention ||= :default
|
143
|
+
@ffi_convention = convention if convention
|
144
|
+
@ffi_convention
|
145
|
+
end
|
146
|
+
|
147
|
+
# @see #ffi_lib
|
148
|
+
# @return [Array<FFI::DynamicLibrary>] array of currently loaded FFI libraries
|
149
|
+
# @raise [LoadError] if no libraries have been loaded (using {#ffi_lib})
|
150
|
+
# Get FFI libraries loaded using {#ffi_lib}.
|
151
|
+
def ffi_libraries
|
152
|
+
raise LoadError.new("no library specified") if !defined?(@ffi_libs) || @ffi_libs.empty?
|
153
|
+
@ffi_libs
|
154
|
+
end
|
155
|
+
|
156
|
+
# Flags used in {#ffi_lib}.
|
157
|
+
#
|
158
|
+
# This map allows you to supply symbols to {#ffi_lib_flags} instead of
|
159
|
+
# the actual constants.
|
160
|
+
FlagsMap = {
|
161
|
+
:global => DynamicLibrary::RTLD_GLOBAL,
|
162
|
+
:local => DynamicLibrary::RTLD_LOCAL,
|
163
|
+
:lazy => DynamicLibrary::RTLD_LAZY,
|
164
|
+
:now => DynamicLibrary::RTLD_NOW
|
165
|
+
}
|
166
|
+
|
167
|
+
# Sets library flags for {#ffi_lib}.
|
168
|
+
#
|
169
|
+
# @example
|
170
|
+
# ffi_lib_flags(:lazy, :local) # => 5
|
171
|
+
#
|
172
|
+
# @param [Symbol, …] flags (see {FlagsMap})
|
173
|
+
# @return [Fixnum] the new value
|
174
|
+
def ffi_lib_flags(*flags)
|
175
|
+
@ffi_lib_flags = flags.inject(0) { |result, f| result | FlagsMap[f] }
|
176
|
+
end
|
177
|
+
|
178
|
+
|
179
|
+
##
|
180
|
+
# @overload attach_function(func, args, returns, options = {})
|
181
|
+
# @example attach function without an explicit name
|
182
|
+
# module Foo
|
183
|
+
# extend FFI::Library
|
184
|
+
# ffi_lib FFI::Library::LIBC
|
185
|
+
# attach_function :malloc, [:size_t], :pointer
|
186
|
+
# end
|
187
|
+
# # now callable via Foo.malloc
|
188
|
+
# @overload attach_function(name, func, args, returns, options = {})
|
189
|
+
# @example attach function with an explicit name
|
190
|
+
# module Bar
|
191
|
+
# extend FFI::Library
|
192
|
+
# ffi_lib FFI::Library::LIBC
|
193
|
+
# attach_function :c_malloc, :malloc, [:size_t], :pointer
|
194
|
+
# end
|
195
|
+
# # now callable via Bar.c_malloc
|
196
|
+
#
|
197
|
+
# Attach C function +func+ to this module.
|
198
|
+
#
|
199
|
+
#
|
200
|
+
# @param [#to_s] name name of ruby method to attach as
|
201
|
+
# @param [#to_s] func name of C function to attach
|
202
|
+
# @param [Array<Symbol>] args an array of types
|
203
|
+
# @param [Symbol] returns type of return value
|
204
|
+
# @option options [Boolean] :blocking (@blocking) set to true if the C function is a blocking call
|
205
|
+
# @option options [Symbol] :convention (:default) calling convention (see {#ffi_convention})
|
206
|
+
# @option options [FFI::Enums] :enums
|
207
|
+
# @option options [Hash] :type_map
|
208
|
+
#
|
209
|
+
# @return [FFI::VariadicInvoker]
|
210
|
+
#
|
211
|
+
# @raise [FFI::NotFoundError] if +func+ cannot be found in the attached libraries (see {#ffi_lib})
|
212
|
+
def attach_function(name, func, args, returns = nil, options = nil)
|
213
|
+
mname, a2, a3, a4, a5 = name, func, args, returns, options
|
214
|
+
cname, arg_types, ret_type, opts = (a4 && (a2.is_a?(String) || a2.is_a?(Symbol))) ? [ a2, a3, a4, a5 ] : [ mname.to_s, a2, a3, a4 ]
|
215
|
+
|
216
|
+
# Convert :foo to the native type
|
217
|
+
arg_types = arg_types.map { |e| find_type(e) }
|
218
|
+
options = {
|
219
|
+
:convention => ffi_convention,
|
220
|
+
:type_map => defined?(@ffi_typedefs) ? @ffi_typedefs : nil,
|
221
|
+
:blocking => defined?(@blocking) && @blocking,
|
222
|
+
:enums => defined?(@ffi_enums) ? @ffi_enums : nil,
|
223
|
+
}
|
224
|
+
|
225
|
+
@blocking = false
|
226
|
+
options.merge!(opts) if opts && opts.is_a?(Hash)
|
227
|
+
|
228
|
+
# Try to locate the function in any of the libraries
|
229
|
+
invokers = []
|
230
|
+
ffi_libraries.each do |lib|
|
231
|
+
if invokers.empty?
|
232
|
+
begin
|
233
|
+
function = nil
|
234
|
+
function_names(cname, arg_types).find do |fname|
|
235
|
+
function = lib.find_function(fname)
|
236
|
+
end
|
237
|
+
raise LoadError unless function
|
238
|
+
|
239
|
+
invokers << if arg_types.length > 0 && arg_types[arg_types.length - 1] == FFI::NativeType::VARARGS
|
240
|
+
VariadicInvoker.new(function, arg_types, find_type(ret_type), options)
|
241
|
+
|
242
|
+
else
|
243
|
+
Function.new(find_type(ret_type), arg_types, function, options)
|
244
|
+
end
|
245
|
+
|
246
|
+
rescue LoadError
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
invoker = invokers.compact.shift
|
251
|
+
raise FFI::NotFoundError.new(cname.to_s, ffi_libraries.map { |lib| lib.name }) unless invoker
|
252
|
+
|
253
|
+
invoker.attach(self, mname.to_s)
|
254
|
+
invoker
|
255
|
+
end
|
256
|
+
|
257
|
+
# @param [#to_s] name function name
|
258
|
+
# @param [Array] arg_types function's argument types
|
259
|
+
# @return [Array<String>]
|
260
|
+
# This function returns a list of possible names to lookup.
|
261
|
+
# @note Function names on windows may be decorated if they are using stdcall. See
|
262
|
+
# * http://en.wikipedia.org/wiki/Name_mangling#C_name_decoration_in_Microsoft_Windows
|
263
|
+
# * http://msdn.microsoft.com/en-us/library/zxk0tw93%28v=VS.100%29.aspx
|
264
|
+
# * http://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions#STDCALL
|
265
|
+
# Note that decorated names can be overridden via def files. Also note that the
|
266
|
+
# windows api, although using, doesn't have decorated names.
|
267
|
+
def function_names(name, arg_types)
|
268
|
+
result = [name.to_s]
|
269
|
+
if ffi_convention == :stdcall
|
270
|
+
# Get the size of each parameter
|
271
|
+
size = arg_types.inject(0) do |mem, arg|
|
272
|
+
mem + arg.size
|
273
|
+
end
|
274
|
+
|
275
|
+
# Next, the size must be a multiple of 4
|
276
|
+
size += (4 - size) % 4
|
277
|
+
|
278
|
+
result << "_#{name.to_s}@#{size}" # win32
|
279
|
+
result << "#{name.to_s}@#{size}" # win64
|
280
|
+
end
|
281
|
+
result
|
282
|
+
end
|
283
|
+
|
284
|
+
# @overload attach_variable(mname, cname, type)
|
285
|
+
# @example
|
286
|
+
# module Bar
|
287
|
+
# extend FFI::Library
|
288
|
+
# ffi_lib 'my_lib'
|
289
|
+
# attach_variable :c_myvar, :myvar, :long
|
290
|
+
# end
|
291
|
+
# # now callable via Bar.c_myvar
|
292
|
+
# @overload attach_variable(cname, type)
|
293
|
+
# @example
|
294
|
+
# module Bar
|
295
|
+
# extend FFI::Library
|
296
|
+
# ffi_lib 'my_lib'
|
297
|
+
# attach_variable :myvar, :long
|
298
|
+
# end
|
299
|
+
# # now callable via Bar.myvar
|
300
|
+
# @param [#to_s] mname name of ruby method to attach as
|
301
|
+
# @param [#to_s] cname name of C variable to attach
|
302
|
+
# @param [DataConverter, Struct, Symbol, Type] type C varaible's type
|
303
|
+
# @return [DynamicLibrary::Symbol]
|
304
|
+
# @raise {FFI::NotFoundError} if +cname+ cannot be found in libraries
|
305
|
+
#
|
306
|
+
# Attach C variable +cname+ to this module.
|
307
|
+
def attach_variable(mname, a1, a2 = nil)
|
308
|
+
cname, type = a2 ? [ a1, a2 ] : [ mname.to_s, a1 ]
|
309
|
+
address = nil
|
310
|
+
ffi_libraries.each do |lib|
|
311
|
+
begin
|
312
|
+
address = lib.find_variable(cname.to_s)
|
313
|
+
break unless address.nil?
|
314
|
+
rescue LoadError
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
raise FFI::NotFoundError.new(cname, ffi_libraries) if address.nil? || address.null?
|
319
|
+
if type.is_a?(Class) && type < FFI::Struct
|
320
|
+
# If it is a global struct, just attach directly to the pointer
|
321
|
+
s = type.new(address)
|
322
|
+
self.module_eval <<-code, __FILE__, __LINE__
|
323
|
+
@@ffi_gvar_#{mname} = s
|
324
|
+
def self.#{mname}
|
325
|
+
@@ffi_gvar_#{mname}
|
326
|
+
end
|
327
|
+
code
|
328
|
+
|
329
|
+
else
|
330
|
+
sc = Class.new(FFI::Struct)
|
331
|
+
sc.layout :gvar, find_type(type)
|
332
|
+
s = sc.new(address)
|
333
|
+
#
|
334
|
+
# Attach to this module as mname/mname=
|
335
|
+
#
|
336
|
+
self.module_eval <<-code, __FILE__, __LINE__
|
337
|
+
@@ffi_gvar_#{mname} = s
|
338
|
+
def self.#{mname}
|
339
|
+
@@ffi_gvar_#{mname}[:gvar]
|
340
|
+
end
|
341
|
+
def self.#{mname}=(value)
|
342
|
+
@@ffi_gvar_#{mname}[:gvar] = value
|
343
|
+
end
|
344
|
+
code
|
345
|
+
|
346
|
+
end
|
347
|
+
|
348
|
+
address
|
349
|
+
end
|
350
|
+
|
351
|
+
|
352
|
+
# @overload callback(name, params, ret)
|
353
|
+
# @overload callback(params, ret)
|
354
|
+
# @param name callback name to add to type map
|
355
|
+
# @param [Array] params array of parameters' types
|
356
|
+
# @param [DataConverter, Struct, Symbol, Type] ret callback return type
|
357
|
+
# @return [FFI::CallbackInfo]
|
358
|
+
def callback(*args)
|
359
|
+
raise ArgumentError, "wrong number of arguments" if args.length < 2 || args.length > 3
|
360
|
+
name, params, ret = if args.length == 3
|
361
|
+
args
|
362
|
+
else
|
363
|
+
[ nil, args[0], args[1] ]
|
364
|
+
end
|
365
|
+
|
366
|
+
native_params = params.map { |e| find_type(e) }
|
367
|
+
raise ArgumentError, "callbacks cannot have variadic parameters" if native_params.include?(FFI::Type::VARARGS)
|
368
|
+
options = Hash.new
|
369
|
+
options[:convention] = ffi_convention
|
370
|
+
options[:enums] = @ffi_enums if defined?(@ffi_enums)
|
371
|
+
cb = FFI::CallbackInfo.new(find_type(ret), native_params, options)
|
372
|
+
|
373
|
+
# Add to the symbol -> type map (unless there was no name)
|
374
|
+
unless name.nil?
|
375
|
+
typedef cb, name
|
376
|
+
end
|
377
|
+
|
378
|
+
cb
|
379
|
+
end
|
380
|
+
|
381
|
+
# @param [DataConverter, Symbol, Type] old
|
382
|
+
# @param add
|
383
|
+
# @param [] info
|
384
|
+
# @return [FFI::Enum, FFI::Type]
|
385
|
+
# Register or get an already registered type definition.
|
386
|
+
#
|
387
|
+
# To register a new type definition, +old+ should be a {FFI::Type}. +add+
|
388
|
+
# is in this case the type definition.
|
389
|
+
#
|
390
|
+
# If +old+ is a {DataConverter}, a {Type::Mapped} is returned.
|
391
|
+
#
|
392
|
+
# If +old+ is +:enum+
|
393
|
+
# * and +add+ is an +Array+, a call to {#enum} is made with +add+ as single parameter;
|
394
|
+
# * in others cases, +info+ is used to create a named enum.
|
395
|
+
#
|
396
|
+
# If +old+ is a key for type map, #typedef get +old+ type definition.
|
397
|
+
def typedef(old, add, info=nil)
|
398
|
+
@ffi_typedefs = Hash.new unless defined?(@ffi_typedefs)
|
399
|
+
|
400
|
+
@ffi_typedefs[add] = if old.kind_of?(FFI::Type)
|
401
|
+
old
|
402
|
+
|
403
|
+
elsif @ffi_typedefs.has_key?(old)
|
404
|
+
@ffi_typedefs[old]
|
405
|
+
|
406
|
+
elsif old.is_a?(DataConverter)
|
407
|
+
FFI::Type::Mapped.new(old)
|
408
|
+
|
409
|
+
elsif old == :enum
|
410
|
+
if add.kind_of?(Array)
|
411
|
+
self.enum(add)
|
412
|
+
else
|
413
|
+
self.enum(info, add)
|
414
|
+
end
|
415
|
+
|
416
|
+
else
|
417
|
+
FFI.find_type(old)
|
418
|
+
end
|
419
|
+
end
|
420
|
+
|
421
|
+
# @overload enum(name, values)
|
422
|
+
# Create a named enum.
|
423
|
+
# @example
|
424
|
+
# enum :foo, [:zero, :one, :two] # named enum
|
425
|
+
# @param [Symbol] name name for new enum
|
426
|
+
# @param [Array] values values for enum
|
427
|
+
# @overload enum(*args)
|
428
|
+
# Create an unnamed enum.
|
429
|
+
# @example
|
430
|
+
# enum :zero, :one, :two # unnamed enum
|
431
|
+
# @param args values for enum
|
432
|
+
# @overload enum(values)
|
433
|
+
# Create an unnamed enum.
|
434
|
+
# @example
|
435
|
+
# enum [:zero, :one, :two] # unnamed enum, equivalent to above example
|
436
|
+
# @param [Array] values values for enum
|
437
|
+
# @return [FFI::Enum]
|
438
|
+
# Create a new {FFI::Enum}.
|
439
|
+
def enum(*args)
|
440
|
+
name, values = if args[0].kind_of?(Symbol) && args[1].kind_of?(Array)
|
441
|
+
[ args[0], args[1] ]
|
442
|
+
elsif args[0].kind_of?(Array)
|
443
|
+
[ nil, args[0] ]
|
444
|
+
else
|
445
|
+
[ nil, args ]
|
446
|
+
end
|
447
|
+
@ffi_enums = FFI::Enums.new unless defined?(@ffi_enums)
|
448
|
+
@ffi_enums << (e = FFI::Enum.new(values, name))
|
449
|
+
|
450
|
+
# If called as enum :foo, [ :zero, :one, :two ], add a typedef alias
|
451
|
+
typedef(e, name) if name
|
452
|
+
e
|
453
|
+
end
|
454
|
+
|
455
|
+
# @param name
|
456
|
+
# @return [FFI::Enum]
|
457
|
+
# Find an enum by name.
|
458
|
+
def enum_type(name)
|
459
|
+
@ffi_enums.find(name) if defined?(@ffi_enums)
|
460
|
+
end
|
461
|
+
|
462
|
+
# @param symbol
|
463
|
+
# @return [FFI::Enum]
|
464
|
+
# Find an enum by a symbol it contains.
|
465
|
+
def enum_value(symbol)
|
466
|
+
@ffi_enums.__map_symbol(symbol)
|
467
|
+
end
|
468
|
+
|
469
|
+
# @param [DataConverter, Type, Struct, Symbol] t type to find
|
470
|
+
# @return [Type]
|
471
|
+
# Find a type definition.
|
472
|
+
def find_type(t)
|
473
|
+
if t.kind_of?(Type)
|
474
|
+
t
|
475
|
+
|
476
|
+
elsif defined?(@ffi_typedefs) && @ffi_typedefs.has_key?(t)
|
477
|
+
@ffi_typedefs[t]
|
478
|
+
|
479
|
+
elsif t.is_a?(Class) && t < Struct
|
480
|
+
Type::POINTER
|
481
|
+
|
482
|
+
elsif t.is_a?(DataConverter)
|
483
|
+
# Add a typedef so next time the converter is used, it hits the cache
|
484
|
+
typedef Type::Mapped.new(t), t
|
485
|
+
|
486
|
+
end || FFI.find_type(t)
|
487
|
+
end
|
488
|
+
end
|
489
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module FFI
|
2
|
+
#
|
3
|
+
# FFI::ManagedStruct allows custom garbage-collection of your FFI::Structs.
|
4
|
+
#
|
5
|
+
# The typical use case would be when interacting with a library
|
6
|
+
# that has a nontrivial memory management design, such as a linked
|
7
|
+
# list or a binary tree.
|
8
|
+
#
|
9
|
+
# When the {Struct} instance is garbage collected, FFI::ManagedStruct will
|
10
|
+
# invoke the class's release() method during object finalization.
|
11
|
+
#
|
12
|
+
# @example Example usage:
|
13
|
+
# module MyLibrary
|
14
|
+
# ffi_lib "libmylibrary"
|
15
|
+
# attach_function :new_dlist, [], :pointer
|
16
|
+
# attach_function :destroy_dlist, [:pointer], :void
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# class DoublyLinkedList < FFI::ManagedStruct
|
20
|
+
# @@@
|
21
|
+
# struct do |s|
|
22
|
+
# s.name 'struct dlist'
|
23
|
+
# s.include 'dlist.h'
|
24
|
+
# s.field :head, :pointer
|
25
|
+
# s.field :tail, :pointer
|
26
|
+
# end
|
27
|
+
# @@@
|
28
|
+
#
|
29
|
+
# def self.release ptr
|
30
|
+
# MyLibrary.destroy_dlist(ptr)
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# begin
|
35
|
+
# ptr = DoublyLinkedList.new(MyLibrary.new_dlist)
|
36
|
+
# # do something with the list
|
37
|
+
# end
|
38
|
+
# # struct is out of scope, and will be GC'd using DoublyLinkedList#release
|
39
|
+
#
|
40
|
+
#
|
41
|
+
class ManagedStruct < FFI::Struct
|
42
|
+
|
43
|
+
# @overload initialize(pointer)
|
44
|
+
# @param [Pointer] pointer
|
45
|
+
# Create a new ManagedStruct which will invoke the class method #release on
|
46
|
+
# @overload initialize
|
47
|
+
# A new instance of FFI::ManagedStruct.
|
48
|
+
def initialize(pointer=nil)
|
49
|
+
raise NoMethodError, "release() not implemented for class #{self}" unless self.class.respond_to? :release
|
50
|
+
raise ArgumentError, "Must supply a pointer to memory for the Struct" unless pointer
|
51
|
+
super AutoPointer.new(pointer, self.class.method(:release))
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# This class is now implemented in C
|
@@ -0,0 +1,104 @@
|
|
1
|
+
rbx.platform.typedef.__u_char = uchar
|
2
|
+
rbx.platform.typedef.__u_short = ushort
|
3
|
+
rbx.platform.typedef.__u_int = uint
|
4
|
+
rbx.platform.typedef.__u_long = ulong
|
5
|
+
rbx.platform.typedef.__int8_t = char
|
6
|
+
rbx.platform.typedef.__uint8_t = uchar
|
7
|
+
rbx.platform.typedef.__int16_t = short
|
8
|
+
rbx.platform.typedef.__uint16_t = ushort
|
9
|
+
rbx.platform.typedef.__int32_t = int
|
10
|
+
rbx.platform.typedef.__uint32_t = uint
|
11
|
+
rbx.platform.typedef.__int64_t = long_long
|
12
|
+
rbx.platform.typedef.__uint64_t = ulong_long
|
13
|
+
rbx.platform.typedef.__quad_t = long_long
|
14
|
+
rbx.platform.typedef.__u_quad_t = ulong_long
|
15
|
+
rbx.platform.typedef.__dev_t = ulong_long
|
16
|
+
rbx.platform.typedef.__uid_t = uint
|
17
|
+
rbx.platform.typedef.__gid_t = uint
|
18
|
+
rbx.platform.typedef.__in_addr_t = uint
|
19
|
+
rbx.platform.typedef.__in_port_t = ushort
|
20
|
+
rbx.platform.typedef.__ino_t = ulong
|
21
|
+
rbx.platform.typedef.__ino64_t = ulong_long
|
22
|
+
rbx.platform.typedef.__mode_t = uint
|
23
|
+
rbx.platform.typedef.__nlink_t = uint
|
24
|
+
rbx.platform.typedef.__off_t = long
|
25
|
+
rbx.platform.typedef.__off64_t = long_long
|
26
|
+
rbx.platform.typedef.__pid_t = int
|
27
|
+
rbx.platform.typedef.__clock_t = long
|
28
|
+
rbx.platform.typedef.__rlim_t = ulong
|
29
|
+
rbx.platform.typedef.__rlim64_t = ulong_long
|
30
|
+
rbx.platform.typedef.__id_t = uint
|
31
|
+
rbx.platform.typedef.__time_t = long
|
32
|
+
rbx.platform.typedef.__useconds_t = uint
|
33
|
+
rbx.platform.typedef.__suseconds_t = long
|
34
|
+
rbx.platform.typedef.__daddr_t = int
|
35
|
+
rbx.platform.typedef.__swblk_t = long
|
36
|
+
rbx.platform.typedef.__key_t = int
|
37
|
+
rbx.platform.typedef.__clockid_t = int
|
38
|
+
rbx.platform.typedef.__timer_t = pointer
|
39
|
+
rbx.platform.typedef.__blksize_t = long
|
40
|
+
rbx.platform.typedef.__blkcnt_t = long
|
41
|
+
rbx.platform.typedef.__blkcnt64_t = long_long
|
42
|
+
rbx.platform.typedef.__fsblkcnt_t = ulong
|
43
|
+
rbx.platform.typedef.__fsblkcnt64_t = ulong_long
|
44
|
+
rbx.platform.typedef.__fsfilcnt_t = ulong
|
45
|
+
rbx.platform.typedef.__fsfilcnt64_t = ulong_long
|
46
|
+
rbx.platform.typedef.__ssize_t = int
|
47
|
+
rbx.platform.typedef.__loff_t = long_long
|
48
|
+
rbx.platform.typedef.*__qaddr_t = long_long
|
49
|
+
rbx.platform.typedef.*__caddr_t = char
|
50
|
+
rbx.platform.typedef.__intptr_t = int
|
51
|
+
rbx.platform.typedef.__socklen_t = uint
|
52
|
+
rbx.platform.typedef.u_char = uchar
|
53
|
+
rbx.platform.typedef.u_short = ushort
|
54
|
+
rbx.platform.typedef.u_int = uint
|
55
|
+
rbx.platform.typedef.u_long = ulong
|
56
|
+
rbx.platform.typedef.quad_t = long_long
|
57
|
+
rbx.platform.typedef.u_quad_t = ulong_long
|
58
|
+
rbx.platform.typedef.loff_t = long_long
|
59
|
+
rbx.platform.typedef.ino_t = ulong_long
|
60
|
+
rbx.platform.typedef.dev_t = ulong_long
|
61
|
+
rbx.platform.typedef.gid_t = uint
|
62
|
+
rbx.platform.typedef.mode_t = uint
|
63
|
+
rbx.platform.typedef.nlink_t = uint
|
64
|
+
rbx.platform.typedef.uid_t = uint
|
65
|
+
rbx.platform.typedef.off_t = long_long
|
66
|
+
rbx.platform.typedef.pid_t = int
|
67
|
+
rbx.platform.typedef.id_t = uint
|
68
|
+
rbx.platform.typedef.ssize_t = int
|
69
|
+
rbx.platform.typedef.daddr_t = int
|
70
|
+
rbx.platform.typedef.key_t = int
|
71
|
+
rbx.platform.typedef.clock_t = long
|
72
|
+
rbx.platform.typedef.time_t = long
|
73
|
+
rbx.platform.typedef.clockid_t = int
|
74
|
+
rbx.platform.typedef.timer_t = pointer
|
75
|
+
rbx.platform.typedef.size_t = uint
|
76
|
+
rbx.platform.typedef.ulong = ulong
|
77
|
+
rbx.platform.typedef.ushort = ushort
|
78
|
+
rbx.platform.typedef.uint = uint
|
79
|
+
rbx.platform.typedef.int8_t = char
|
80
|
+
rbx.platform.typedef.int16_t = short
|
81
|
+
rbx.platform.typedef.int32_t = int
|
82
|
+
rbx.platform.typedef.int64_t = long_long
|
83
|
+
rbx.platform.typedef.u_int8_t = uchar
|
84
|
+
rbx.platform.typedef.u_int16_t = ushort
|
85
|
+
rbx.platform.typedef.u_int32_t = uint
|
86
|
+
rbx.platform.typedef.u_int64_t = ulong_long
|
87
|
+
rbx.platform.typedef.register_t = long
|
88
|
+
rbx.platform.typedef.__sig_atomic_t = int
|
89
|
+
rbx.platform.typedef.suseconds_t = long
|
90
|
+
rbx.platform.typedef.__fd_mask = long
|
91
|
+
rbx.platform.typedef.fd_mask = long
|
92
|
+
rbx.platform.typedef.blksize_t = long
|
93
|
+
rbx.platform.typedef.blkcnt_t = long_long
|
94
|
+
rbx.platform.typedef.fsblkcnt_t = ulong_long
|
95
|
+
rbx.platform.typedef.fsfilcnt_t = ulong_long
|
96
|
+
rbx.platform.typedef.pthread_t = ulong
|
97
|
+
rbx.platform.typedef.pthread_key_t = uint
|
98
|
+
rbx.platform.typedef.pthread_once_t = int
|
99
|
+
rbx.platform.typedef.socklen_t = uint
|
100
|
+
rbx.platform.typedef.sa_family_t = ushort
|
101
|
+
rbx.platform.typedef.rlim_t = ulong_long
|
102
|
+
rbx.platform.typedef.__rlimit_resource_t = int
|
103
|
+
rbx.platform.typedef.__rusage_who_t = int
|
104
|
+
rbx.platform.typedef.__priority_which_t = int
|