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,177 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2008-2010 Wayne Meissner
|
3
|
+
# All rights reserved.
|
4
|
+
#
|
5
|
+
# This file is part of ruby-ffi.
|
6
|
+
#
|
7
|
+
# This code is free software: you can redistribute it and/or modify it under
|
8
|
+
# the terms of the GNU Lesser General Public License version 3 only, as
|
9
|
+
# published by the Free Software Foundation.
|
10
|
+
#
|
11
|
+
# This code is distributed in the hope that it will be useful, but WITHOUT
|
12
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
13
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
14
|
+
# version 3 for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public License
|
17
|
+
# version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
#
|
19
|
+
|
20
|
+
# see {file:README}
|
21
|
+
module FFI
|
22
|
+
|
23
|
+
# @param [Type, DataConverter, Symbol] old type definition used by {FFI.find_type}
|
24
|
+
# @param [Symbol] add new type definition's name to add
|
25
|
+
# @return [Type]
|
26
|
+
# Add a definition type to type definitions.
|
27
|
+
def self.typedef(old, add)
|
28
|
+
TypeDefs[add] = self.find_type(old)
|
29
|
+
end
|
30
|
+
|
31
|
+
# (see FFI.typedef)
|
32
|
+
def self.add_typedef(old, add)
|
33
|
+
typedef old, add
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
# @param [Type, DataConverter, Symbol] name
|
38
|
+
# @param [Hash] type_map if nil, {FFI::TypeDefs} is used
|
39
|
+
# @return [Type]
|
40
|
+
# Find a type in +type_map+ ({FFI::TypeDefs}, by default) from
|
41
|
+
# a type objet, a type name (symbol). If +name+ is a {DataConverter},
|
42
|
+
# a new {Type::Mapped} is created.
|
43
|
+
def self.find_type(name, type_map = nil)
|
44
|
+
if name.is_a?(Type)
|
45
|
+
name
|
46
|
+
|
47
|
+
elsif type_map && type_map.has_key?(name)
|
48
|
+
type_map[name]
|
49
|
+
|
50
|
+
elsif TypeDefs.has_key?(name)
|
51
|
+
TypeDefs[name]
|
52
|
+
|
53
|
+
elsif name.is_a?(DataConverter)
|
54
|
+
(type_map || TypeDefs)[name] = Type::Mapped.new(name)
|
55
|
+
|
56
|
+
else
|
57
|
+
raise TypeError, "unable to resolve type '#{name}'"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# List of type definitions
|
62
|
+
TypeDefs.merge!({
|
63
|
+
# The C void type; only useful for function return types
|
64
|
+
:void => Type::VOID,
|
65
|
+
|
66
|
+
# C boolean type
|
67
|
+
:bool => Type::BOOL,
|
68
|
+
|
69
|
+
# C nul-terminated string
|
70
|
+
:string => Type::STRING,
|
71
|
+
|
72
|
+
# C signed char
|
73
|
+
:char => Type::CHAR,
|
74
|
+
# C unsigned char
|
75
|
+
:uchar => Type::UCHAR,
|
76
|
+
|
77
|
+
# C signed short
|
78
|
+
:short => Type::SHORT,
|
79
|
+
# C unsigned short
|
80
|
+
:ushort => Type::USHORT,
|
81
|
+
|
82
|
+
# C signed int
|
83
|
+
:int => Type::INT,
|
84
|
+
# C unsigned int
|
85
|
+
:uint => Type::UINT,
|
86
|
+
|
87
|
+
# C signed long
|
88
|
+
:long => Type::LONG,
|
89
|
+
|
90
|
+
# C unsigned long
|
91
|
+
:ulong => Type::ULONG,
|
92
|
+
|
93
|
+
# C signed long long integer
|
94
|
+
:long_long => Type::LONG_LONG,
|
95
|
+
|
96
|
+
# C unsigned long long integer
|
97
|
+
:ulong_long => Type::ULONG_LONG,
|
98
|
+
|
99
|
+
# C single precision float
|
100
|
+
:float => Type::FLOAT,
|
101
|
+
|
102
|
+
# C double precision float
|
103
|
+
:double => Type::DOUBLE,
|
104
|
+
|
105
|
+
# C long double
|
106
|
+
:long_double => Type::LONGDOUBLE,
|
107
|
+
|
108
|
+
# Native memory address
|
109
|
+
:pointer => Type::POINTER,
|
110
|
+
|
111
|
+
# 8 bit signed integer
|
112
|
+
:int8 => Type::INT8,
|
113
|
+
# 8 bit unsigned integer
|
114
|
+
:uint8 => Type::UINT8,
|
115
|
+
|
116
|
+
# 16 bit signed integer
|
117
|
+
:int16 => Type::INT16,
|
118
|
+
# 16 bit unsigned integer
|
119
|
+
:uint16 => Type::UINT16,
|
120
|
+
|
121
|
+
# 32 bit signed integer
|
122
|
+
:int32 => Type::INT32,
|
123
|
+
# 32 bit unsigned integer
|
124
|
+
:uint32 => Type::UINT32,
|
125
|
+
|
126
|
+
# 64 bit signed integer
|
127
|
+
:int64 => Type::INT64,
|
128
|
+
# 64 bit unsigned integer
|
129
|
+
:uint64 => Type::UINT64,
|
130
|
+
|
131
|
+
:buffer_in => Type::BUFFER_IN,
|
132
|
+
:buffer_out => Type::BUFFER_OUT,
|
133
|
+
:buffer_inout => Type::BUFFER_INOUT,
|
134
|
+
|
135
|
+
# Used in function prototypes to indicate the arguments are variadic
|
136
|
+
:varargs => Type::VARARGS,
|
137
|
+
})
|
138
|
+
|
139
|
+
|
140
|
+
class StrPtrConverter
|
141
|
+
extend DataConverter
|
142
|
+
native_type Type::POINTER
|
143
|
+
|
144
|
+
# @param [Pointer] val
|
145
|
+
# @param [] ctx
|
146
|
+
# @return [Array(String, Pointer)]
|
147
|
+
# Returns a [ String, Pointer ] tuple so the C memory for the string can be freed
|
148
|
+
def self.from_native(val, ctx)
|
149
|
+
[ val.null? ? nil : val.get_string(0), val ]
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
typedef(StrPtrConverter, :strptr)
|
155
|
+
|
156
|
+
# @param type +type+ is an instance of class accepted by {FFI.find_type}
|
157
|
+
# @return [Numeric]
|
158
|
+
# Get +type+ size, in bytes.
|
159
|
+
def self.type_size(type)
|
160
|
+
find_type(type).size
|
161
|
+
end
|
162
|
+
|
163
|
+
# Load all the platform dependent types
|
164
|
+
begin
|
165
|
+
File.open(File.join(Platform::CONF_DIR, 'types.conf'), "r") do |f|
|
166
|
+
prefix = "rbx.platform.typedef."
|
167
|
+
f.each_line { |line|
|
168
|
+
if line.index(prefix) == 0
|
169
|
+
new_type, orig_type = line.chomp.slice(prefix.length..-1).split(/\s*=\s*/)
|
170
|
+
typedef(orig_type.to_sym, new_type.to_sym)
|
171
|
+
end
|
172
|
+
}
|
173
|
+
end
|
174
|
+
typedef :pointer, :caddr_t
|
175
|
+
rescue Errno::ENOENT
|
176
|
+
end
|
177
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2008-2010 Wayne Meissner
|
3
|
+
#
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# This file is part of ruby-ffi.
|
7
|
+
#
|
8
|
+
# This code is free software: you can redistribute it and/or modify it under
|
9
|
+
# the terms of the GNU Lesser General Public License version 3 only, as
|
10
|
+
# published by the Free Software Foundation.
|
11
|
+
#
|
12
|
+
# This code is distributed in the hope that it will be useful, but WITHOUT
|
13
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
14
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
15
|
+
# version 3 for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU Lesser General Public License
|
18
|
+
# version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
#
|
20
|
+
|
21
|
+
require 'ffi/struct'
|
22
|
+
|
23
|
+
module FFI
|
24
|
+
|
25
|
+
class Union < FFI::Struct
|
26
|
+
def self.builder
|
27
|
+
b = StructLayoutBuilder.new
|
28
|
+
b.union = true
|
29
|
+
b
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2008, 2009 Wayne Meissner
|
3
|
+
# Copyright (C) 2009 Luc Heinrich
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# This file is part of ruby-ffi.
|
7
|
+
#
|
8
|
+
# This code is free software: you can redistribute it and/or modify it under
|
9
|
+
# the terms of the GNU Lesser General Public License version 3 only, as
|
10
|
+
# published by the Free Software Foundation.
|
11
|
+
#
|
12
|
+
# This code is distributed in the hope that it will be useful, but WITHOUT
|
13
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
14
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
15
|
+
# version 3 for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU Lesser General Public License
|
18
|
+
# version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
|
19
|
+
|
20
|
+
module FFI
|
21
|
+
class VariadicInvoker
|
22
|
+
def init(arg_types, type_map)
|
23
|
+
@fixed = Array.new
|
24
|
+
@type_map = type_map
|
25
|
+
arg_types.each_with_index do |type, i|
|
26
|
+
@fixed << type unless type == Type::VARARGS
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def call(*args, &block)
|
32
|
+
param_types = Array.new(@fixed)
|
33
|
+
param_values = Array.new
|
34
|
+
@fixed.each_with_index do |t, i|
|
35
|
+
param_values << args[i]
|
36
|
+
end
|
37
|
+
i = @fixed.length
|
38
|
+
while i < args.length
|
39
|
+
param_types << FFI.find_type(args[i], @type_map)
|
40
|
+
param_values << args[i + 1]
|
41
|
+
i += 2
|
42
|
+
end
|
43
|
+
invoke(param_types, param_values, &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# Attach the invoker to module +mod+ as +mname+
|
48
|
+
#
|
49
|
+
def attach(mod, mname)
|
50
|
+
invoker = self
|
51
|
+
params = "*args"
|
52
|
+
call = "call"
|
53
|
+
mod.module_eval <<-code
|
54
|
+
@@#{mname} = invoker
|
55
|
+
def self.#{mname}(#{params})
|
56
|
+
@@#{mname}.#{call}(#{params})
|
57
|
+
end
|
58
|
+
def #{mname}(#{params})
|
59
|
+
@@#{mname}.#{call}(#{params})
|
60
|
+
end
|
61
|
+
code
|
62
|
+
invoker
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
if !defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby' || RUBY_ENGINE == 'rbx'
|
2
|
+
Object.send(:remove_const, :FFI) if defined?(::FFI)
|
3
|
+
begin
|
4
|
+
if RUBY_VERSION =~ /1.8/
|
5
|
+
require '1.8/ffi_c'
|
6
|
+
elsif RUBY_VERSION =~ /1.9/
|
7
|
+
require '1.9/ffi_c'
|
8
|
+
elsif RUBY_VERSION =~ /2.0/
|
9
|
+
require '2.0/ffi_c'
|
10
|
+
else
|
11
|
+
require 'ffi_c'
|
12
|
+
end
|
13
|
+
rescue Exception
|
14
|
+
require 'ffi_c'
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'ffi/ffi'
|
18
|
+
|
19
|
+
elsif defined?(RUBY_ENGINE)
|
20
|
+
# Remove the ffi gem dir from the load path, then reload the internal ffi implementation
|
21
|
+
$LOAD_PATH.delete(File.dirname(__FILE__))
|
22
|
+
$LOAD_PATH.delete(File.join(File.dirname(__FILE__), 'ffi'))
|
23
|
+
unless $LOADED_FEATURES.nil?
|
24
|
+
$LOADED_FEATURES.delete(__FILE__)
|
25
|
+
$LOADED_FEATURES.delete('ffi.rb')
|
26
|
+
end
|
27
|
+
require 'ffi.rb'
|
28
|
+
end
|
Binary file
|
@@ -0,0 +1,52 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007 Wayne Meissner. All rights reserved.
|
3
|
+
*
|
4
|
+
* For licensing, see LICENSE.SPECS
|
5
|
+
*/
|
6
|
+
#include <sys/types.h>
|
7
|
+
#include <stdint.h>
|
8
|
+
|
9
|
+
void returnVoid() {
|
10
|
+
|
11
|
+
}
|
12
|
+
|
13
|
+
void returnVoidI(int arg) {
|
14
|
+
|
15
|
+
}
|
16
|
+
int returnInt() {
|
17
|
+
return 0;
|
18
|
+
}
|
19
|
+
|
20
|
+
int returnIntI(int arg) {
|
21
|
+
return arg;
|
22
|
+
}
|
23
|
+
|
24
|
+
typedef int8_t s8;
|
25
|
+
typedef uint8_t u8;
|
26
|
+
typedef int16_t s16;
|
27
|
+
typedef uint16_t u16;
|
28
|
+
typedef int32_t s32;
|
29
|
+
typedef uint32_t u32;
|
30
|
+
typedef int64_t s64;
|
31
|
+
typedef uint64_t u64;
|
32
|
+
typedef float f32;
|
33
|
+
typedef double f64;
|
34
|
+
typedef void v;
|
35
|
+
typedef char* S;
|
36
|
+
typedef void* P;
|
37
|
+
|
38
|
+
#define B6(R, T1, T2, T3, T4, T5, T6) R bench_##T1##T2##T3##T4##T5##T6##_##R(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) {}
|
39
|
+
#define B5(R, T1, T2, T3, T4, T5) R bench_##T1##T2##T3##T4##T5##_##R(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) {}
|
40
|
+
#define B4(R, T1, T2, T3, T4) R bench_##T1##T2##T3##T4##_##R(T1 a1, T2 a2, T3 a3, T4 a4) {}
|
41
|
+
#define B3(R, T1, T2, T3) R bench_##T1##T2##T3##_##R(T1 a1, T2 a2, T3 a3) {}
|
42
|
+
#define B2(R, T1, T2) R bench_##T1##T2##_##R(T1 a1, T2 a2) {}
|
43
|
+
#define B1(R, T1) R bench_##T1##_##R(T1 a1) {}
|
44
|
+
#define BrV(T) B1(v, T); B2(v, T, T); B3(v, T, T, T); B4(v, T, T, T, T); B5(v, T, T, T, T, T); B6(v, T, T, T, T, T, T);
|
45
|
+
BrV(u32);
|
46
|
+
BrV(s32);
|
47
|
+
BrV(s64);
|
48
|
+
BrV(u64);
|
49
|
+
BrV(f32);
|
50
|
+
BrV(f64);
|
51
|
+
BrV(S);
|
52
|
+
BrV(P);
|
@@ -0,0 +1,31 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007 Wayne Meissner. All rights reserved.
|
3
|
+
*
|
4
|
+
* For licensing, see LICENSE.SPECS
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <stdbool.h>
|
8
|
+
|
9
|
+
bool
|
10
|
+
bool_return_true()
|
11
|
+
{
|
12
|
+
return true;
|
13
|
+
}
|
14
|
+
|
15
|
+
bool
|
16
|
+
bool_return_false()
|
17
|
+
{
|
18
|
+
return false;
|
19
|
+
}
|
20
|
+
|
21
|
+
bool
|
22
|
+
bool_return_val(bool value)
|
23
|
+
{
|
24
|
+
return value;
|
25
|
+
}
|
26
|
+
|
27
|
+
bool
|
28
|
+
bool_reverse_val(bool value)
|
29
|
+
{
|
30
|
+
return value ? false : true;
|
31
|
+
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007 Wayne Meissner. All rights reserved.
|
3
|
+
*
|
4
|
+
* For licensing, see LICENSE.SPECS
|
5
|
+
*/
|
6
|
+
|
7
|
+
|
8
|
+
#define MEMSET(buf, value, size) do { \
|
9
|
+
int i; for (i = 0; i < size; ++i) buf[i] = value; \
|
10
|
+
} while(0)
|
11
|
+
#define MEMCPY(dst, src, size) do { \
|
12
|
+
int i; for (i = 0; i < size; ++i) dst[i] = src[i]; \
|
13
|
+
} while(0)
|
14
|
+
|
15
|
+
#define FILL(JTYPE, CTYPE) \
|
16
|
+
void fill##JTYPE##Buffer(CTYPE* buf, CTYPE value, int size) { MEMSET(buf, value, size); }
|
17
|
+
|
18
|
+
#define COPY(JTYPE, CTYPE) \
|
19
|
+
void copy##JTYPE##Buffer(CTYPE* dst, CTYPE* src, int size) { MEMCPY(dst, src, size); }
|
20
|
+
|
21
|
+
#define FUNC(JTYPE, CTYPE) \
|
22
|
+
FILL(JTYPE, CTYPE); \
|
23
|
+
COPY(JTYPE, CTYPE)
|
24
|
+
|
25
|
+
FUNC(Byte, char);
|
26
|
+
FUNC(Short, short);
|
27
|
+
FUNC(Int, int);
|
28
|
+
FUNC(Long, long long);
|
29
|
+
FUNC(Float, float);
|
30
|
+
FUNC(Double, double);
|
31
|
+
|
@@ -0,0 +1,190 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007 Wayne Meissner. All rights reserved.
|
3
|
+
*
|
4
|
+
* For licensing, see LICENSE.SPECS
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <stdlib.h>
|
8
|
+
#include <stdbool.h>
|
9
|
+
#ifndef _WIN32
|
10
|
+
# include <pthread.h>
|
11
|
+
#else
|
12
|
+
# include <windows.h>
|
13
|
+
# include <process.h>
|
14
|
+
#endif
|
15
|
+
|
16
|
+
#define R(T, rtype) rtype testClosureVr##T(rtype (*closure)(void)) { \
|
17
|
+
return closure != NULL ? (*closure)() : (rtype) 0; \
|
18
|
+
}
|
19
|
+
|
20
|
+
#define P(T, ptype) void testClosure##T##rV(void (*closure)(ptype), ptype a1) { \
|
21
|
+
if (closure != NULL) (*closure)(a1); \
|
22
|
+
}
|
23
|
+
|
24
|
+
void testClosureVrV(void (*closure)(void))
|
25
|
+
{
|
26
|
+
(*closure)();
|
27
|
+
}
|
28
|
+
|
29
|
+
R(Z, bool);
|
30
|
+
R(B, char);
|
31
|
+
R(S, short);
|
32
|
+
R(I, int);
|
33
|
+
R(L, long);
|
34
|
+
R(J, long long);
|
35
|
+
R(LL, long long);
|
36
|
+
R(F, float);
|
37
|
+
R(D, double);
|
38
|
+
R(P, const void*);
|
39
|
+
|
40
|
+
|
41
|
+
P(Z, bool);
|
42
|
+
P(B, char);
|
43
|
+
P(S, short);
|
44
|
+
P(I, int);
|
45
|
+
P(L, long);
|
46
|
+
P(J, long long);
|
47
|
+
P(LL, long long);
|
48
|
+
P(F, float);
|
49
|
+
P(D, double);
|
50
|
+
P(P, const void*);
|
51
|
+
P(UL, unsigned long);
|
52
|
+
|
53
|
+
void testOptionalClosureBrV(void (*closure)(char), char a1)
|
54
|
+
{
|
55
|
+
if (closure) {
|
56
|
+
(*closure)(a1);
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
|
61
|
+
struct ThreadVrV {
|
62
|
+
void (*closure)(void);
|
63
|
+
int count;
|
64
|
+
};
|
65
|
+
|
66
|
+
static void *
|
67
|
+
threadVrV(void *arg)
|
68
|
+
{
|
69
|
+
struct ThreadVrV* t = (struct ThreadVrV *) arg;
|
70
|
+
|
71
|
+
int i;
|
72
|
+
for (i = 0; i < t->count; i++) {
|
73
|
+
(*t->closure)();
|
74
|
+
}
|
75
|
+
|
76
|
+
return NULL;
|
77
|
+
}
|
78
|
+
|
79
|
+
void testThreadedClosureVrV(void (*closure)(void), int n)
|
80
|
+
{
|
81
|
+
struct ThreadVrV arg = {closure, n};
|
82
|
+
#ifndef _WIN32
|
83
|
+
pthread_t t;
|
84
|
+
pthread_create(&t, NULL, threadVrV, &arg);
|
85
|
+
pthread_join(t, NULL);
|
86
|
+
#else
|
87
|
+
HANDLE hThread = (HANDLE) _beginthread((void (*)(void *))threadVrV, 0, &arg);
|
88
|
+
WaitForSingleObject(hThread, INFINITE);
|
89
|
+
#endif
|
90
|
+
}
|
91
|
+
|
92
|
+
struct s8f32s32 {
|
93
|
+
char s8;
|
94
|
+
float f32;
|
95
|
+
int s32;
|
96
|
+
};
|
97
|
+
|
98
|
+
// Takes a struct argument
|
99
|
+
void testClosureTrV(void (*closure)(struct s8f32s32 s), struct s8f32s32* s)
|
100
|
+
{
|
101
|
+
(*closure)(*s);
|
102
|
+
}
|
103
|
+
|
104
|
+
// Returns a struct value
|
105
|
+
struct s8f32s32 testClosureVrT(struct s8f32s32 (*closure)())
|
106
|
+
{
|
107
|
+
return (*closure)();
|
108
|
+
}
|
109
|
+
|
110
|
+
typedef int (*returnTypeClosure_t)(int) ;
|
111
|
+
typedef returnTypeClosure_t (*lookupClosure_t)();
|
112
|
+
|
113
|
+
int testReturnsClosure(lookupClosure_t lookup, int val)
|
114
|
+
{
|
115
|
+
returnTypeClosure_t func = lookup ? (*lookup)() : NULL;
|
116
|
+
return func ? (*func)(val) : 0;
|
117
|
+
}
|
118
|
+
|
119
|
+
static int multiplyByTwo(int value)
|
120
|
+
{
|
121
|
+
return value * 2;
|
122
|
+
}
|
123
|
+
|
124
|
+
returnTypeClosure_t testReturnsFunctionPointer()
|
125
|
+
{
|
126
|
+
return multiplyByTwo;
|
127
|
+
}
|
128
|
+
|
129
|
+
typedef int (*argumentClosure_t)(int);
|
130
|
+
typedef int (*withArgumentClosure_t)(argumentClosure_t, int);
|
131
|
+
|
132
|
+
int testArgumentClosure(withArgumentClosure_t closure_with, argumentClosure_t closure_arg, int val)
|
133
|
+
{
|
134
|
+
return (*closure_with)(closure_arg, val);
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
//
|
139
|
+
// These macros produce functions of the form:
|
140
|
+
// testClosureBIrV(void (*closure)(char, int), char a1, int a2) {}
|
141
|
+
//
|
142
|
+
#define C2_(J1, J2, N1, N2) \
|
143
|
+
void testClosure##J1##J2##rV(void (*closure)(N1, N2), N1 a1, N2 a2) \
|
144
|
+
{ \
|
145
|
+
if (closure != NULL) (*closure)(a1, a2); \
|
146
|
+
}
|
147
|
+
|
148
|
+
#define C2(J, N) \
|
149
|
+
C2_(B, J, char, N) \
|
150
|
+
C2_(S, J, short, N) \
|
151
|
+
C2_(I, J, int, N) \
|
152
|
+
C2_(LL, J, long long, N) \
|
153
|
+
C2_(F, J, float, N) \
|
154
|
+
C2_(D, J, double, N) \
|
155
|
+
|
156
|
+
|
157
|
+
C2(B, char);
|
158
|
+
C2(S, short);
|
159
|
+
C2(I, int);
|
160
|
+
C2(LL, long long);
|
161
|
+
C2(F, float);
|
162
|
+
C2(D, double);
|
163
|
+
|
164
|
+
#define C3_(J1, J2, J3, N1, N2, N3) \
|
165
|
+
void testClosure##J1##J2##J3##rV(void (*closure)(N1, N2, N3), N1 a1, N2 a2, N3 a3) \
|
166
|
+
{ \
|
167
|
+
(*closure)(a1, a2, a3); \
|
168
|
+
}
|
169
|
+
|
170
|
+
|
171
|
+
#define C3(J, N) \
|
172
|
+
C3_(B, J, B, char, N, char) \
|
173
|
+
C3_(S, J, S, short, N, short) \
|
174
|
+
C3_(I, J, I, int, N, int) \
|
175
|
+
C3_(LL, J, LL, long long, N, long long) \
|
176
|
+
C3_(F, J, F, float, N, float) \
|
177
|
+
C3_(D, J, D, double, N, double) \
|
178
|
+
|
179
|
+
C3(B, char);
|
180
|
+
C3(S, short);
|
181
|
+
C3(I, int);
|
182
|
+
C3(LL, long long);
|
183
|
+
C3(F, float);
|
184
|
+
C3(D, double);
|
185
|
+
C3_(B, S, I, char, short, int);
|
186
|
+
C3_(B, S, LL, char, short, long long);
|
187
|
+
C3_(LL, S, B, long long, short, char);
|
188
|
+
C3_(LL, B, S, long long, char, short);
|
189
|
+
|
190
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007 Wayne Meissner. All rights reserved.
|
3
|
+
*
|
4
|
+
* For licensing, see LICENSE.SPECS
|
5
|
+
*/
|
6
|
+
|
7
|
+
int test_untagged_enum(int val) {
|
8
|
+
return val;
|
9
|
+
}
|
10
|
+
|
11
|
+
int test_untagged_typedef_enum(int val) {
|
12
|
+
return val;
|
13
|
+
}
|
14
|
+
|
15
|
+
typedef enum {c1, c2, c3, c4} enum_type1;
|
16
|
+
enum_type1 test_tagged_typedef_enum1(enum_type1 val) {
|
17
|
+
return val;
|
18
|
+
}
|
19
|
+
|
20
|
+
typedef enum {c5 = 42, c6, c7, c8} enum_type2;
|
21
|
+
enum_type2 test_tagged_typedef_enum2(enum_type2 val) {
|
22
|
+
return val;
|
23
|
+
}
|
24
|
+
|
25
|
+
typedef enum {c9 = 42, c10, c11 = 4242, c12} enum_type3;
|
26
|
+
enum_type3 test_tagged_typedef_enum3(enum_type3 val) {
|
27
|
+
return val;
|
28
|
+
}
|
29
|
+
|
30
|
+
typedef enum {c13 = 42, c14 = 4242, c15 = 424242, c16 = 42424242} enum_type4;
|
31
|
+
enum_type4 test_tagged_typedef_enum4(enum_type4 val) {
|
32
|
+
return val;
|
33
|
+
}
|
34
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2007 Wayne Meissner. All rights reserved.
|
3
|
+
*
|
4
|
+
* For licensing, see LICENSE.SPECS
|
5
|
+
*/
|
6
|
+
|
7
|
+
#ifdef _WIN32
|
8
|
+
#include <windows.h>
|
9
|
+
#define sleep(x) Sleep(x)
|
10
|
+
#endif
|
11
|
+
|
12
|
+
#ifndef _WIN32
|
13
|
+
#include <unistd.h>
|
14
|
+
#include <pthread.h>
|
15
|
+
#endif
|
16
|
+
|
17
|
+
int testAdd(int a, int b)
|
18
|
+
{
|
19
|
+
return a + b;
|
20
|
+
};
|
21
|
+
|
22
|
+
int testFunctionAdd(int a, int b, int (*f)(int, int))
|
23
|
+
{
|
24
|
+
return f(a, b);
|
25
|
+
};
|
26
|
+
|
27
|
+
void testBlocking(int seconds) {
|
28
|
+
sleep(seconds);
|
29
|
+
};
|
30
|
+
|
31
|
+
struct async_data {
|
32
|
+
void (*fn)(int);
|
33
|
+
int value;
|
34
|
+
};
|
35
|
+
|
36
|
+
static void* asyncThreadCall(void *data)
|
37
|
+
{
|
38
|
+
struct async_data* d = (struct async_data *) data;
|
39
|
+
if (d != NULL && d->fn != NULL) {
|
40
|
+
(*d->fn)(d->value);
|
41
|
+
}
|
42
|
+
|
43
|
+
return NULL;
|
44
|
+
}
|
45
|
+
|
46
|
+
void testAsyncCallback(void (*fn)(int), int value)
|
47
|
+
{
|
48
|
+
#ifndef _WIN32
|
49
|
+
pthread_t t;
|
50
|
+
struct async_data d;
|
51
|
+
d.fn = fn;
|
52
|
+
d.value = value;
|
53
|
+
pthread_create(&t, NULL, asyncThreadCall, &d);
|
54
|
+
pthread_join(t, NULL);
|
55
|
+
#else
|
56
|
+
(*fn)(value);
|
57
|
+
#endif
|
58
|
+
}
|