alinta-ffi 1.9.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/COPYING +49 -0
- data/LICENSE +24 -0
- data/README.md +112 -0
- data/Rakefile +243 -0
- data/ext/ffi_c/AbstractMemory.c +1109 -0
- data/ext/ffi_c/AbstractMemory.h +175 -0
- data/ext/ffi_c/ArrayType.c +162 -0
- data/ext/ffi_c/ArrayType.h +59 -0
- data/ext/ffi_c/Buffer.c +365 -0
- data/ext/ffi_c/Call.c +517 -0
- data/ext/ffi_c/Call.h +110 -0
- data/ext/ffi_c/ClosurePool.c +283 -0
- data/ext/ffi_c/ClosurePool.h +57 -0
- data/ext/ffi_c/DataConverter.c +91 -0
- data/ext/ffi_c/DynamicLibrary.c +339 -0
- data/ext/ffi_c/DynamicLibrary.h +98 -0
- data/ext/ffi_c/Function.c +998 -0
- data/ext/ffi_c/Function.h +87 -0
- data/ext/ffi_c/FunctionInfo.c +271 -0
- data/ext/ffi_c/LastError.c +184 -0
- data/ext/ffi_c/LastError.h +47 -0
- data/ext/ffi_c/LongDouble.c +63 -0
- data/ext/ffi_c/LongDouble.h +51 -0
- data/ext/ffi_c/MappedType.c +168 -0
- data/ext/ffi_c/MappedType.h +59 -0
- data/ext/ffi_c/MemoryPointer.c +197 -0
- data/ext/ffi_c/MemoryPointer.h +53 -0
- data/ext/ffi_c/MethodHandle.c +358 -0
- data/ext/ffi_c/MethodHandle.h +55 -0
- data/ext/ffi_c/Platform.c +129 -0
- data/ext/ffi_c/Platform.h +45 -0
- data/ext/ffi_c/Pointer.c +508 -0
- data/ext/ffi_c/Pointer.h +63 -0
- data/ext/ffi_c/Struct.c +829 -0
- data/ext/ffi_c/Struct.h +106 -0
- data/ext/ffi_c/StructByReference.c +190 -0
- data/ext/ffi_c/StructByReference.h +50 -0
- data/ext/ffi_c/StructByValue.c +150 -0
- data/ext/ffi_c/StructByValue.h +55 -0
- data/ext/ffi_c/StructLayout.c +698 -0
- data/ext/ffi_c/Thread.c +352 -0
- data/ext/ffi_c/Thread.h +95 -0
- data/ext/ffi_c/Type.c +397 -0
- data/ext/ffi_c/Type.h +62 -0
- data/ext/ffi_c/Types.c +139 -0
- data/ext/ffi_c/Types.h +89 -0
- data/ext/ffi_c/Variadic.c +304 -0
- data/ext/ffi_c/compat.h +78 -0
- data/ext/ffi_c/extconf.rb +71 -0
- data/ext/ffi_c/ffi.c +98 -0
- data/ext/ffi_c/libffi.bsd.mk +40 -0
- data/ext/ffi_c/libffi.darwin.mk +105 -0
- data/ext/ffi_c/libffi.gnu.mk +32 -0
- data/ext/ffi_c/libffi.mk +18 -0
- data/ext/ffi_c/libffi.vc.mk +26 -0
- data/ext/ffi_c/libffi.vc64.mk +26 -0
- data/ext/ffi_c/rbffi.h +57 -0
- data/ext/ffi_c/rbffi_endian.h +59 -0
- data/ext/ffi_c/win32/stdbool.h +8 -0
- data/ext/ffi_c/win32/stdint.h +201 -0
- data/ffi.gemspec +23 -0
- data/gen/Rakefile +30 -0
- data/lib/ffi.rb +20 -0
- data/lib/ffi/autopointer.rb +203 -0
- data/lib/ffi/buffer.rb +4 -0
- data/lib/ffi/callback.rb +4 -0
- data/lib/ffi/enum.rb +296 -0
- data/lib/ffi/errno.rb +43 -0
- data/lib/ffi/ffi.rb +44 -0
- data/lib/ffi/io.rb +62 -0
- data/lib/ffi/library.rb +590 -0
- data/lib/ffi/managedstruct.rb +84 -0
- data/lib/ffi/memorypointer.rb +1 -0
- data/lib/ffi/platform.rb +164 -0
- data/lib/ffi/platform/aarch64-linux/types.conf +104 -0
- data/lib/ffi/platform/arm-linux/types.conf +104 -0
- data/lib/ffi/platform/i386-cygwin/types.conf +3 -0
- data/lib/ffi/platform/i386-darwin/types.conf +100 -0
- data/lib/ffi/platform/i386-freebsd/types.conf +152 -0
- data/lib/ffi/platform/i386-gnu/types.conf +107 -0
- data/lib/ffi/platform/i386-linux/types.conf +103 -0
- data/lib/ffi/platform/i386-netbsd/types.conf +126 -0
- data/lib/ffi/platform/i386-openbsd/types.conf +128 -0
- data/lib/ffi/platform/i386-solaris/types.conf +122 -0
- data/lib/ffi/platform/i386-windows/types.conf +105 -0
- data/lib/ffi/platform/ia64-linux/types.conf +104 -0
- data/lib/ffi/platform/mips-linux/types.conf +102 -0
- data/lib/ffi/platform/mips64el-linux/types.conf +104 -0
- data/lib/ffi/platform/mipsel-linux/types.conf +102 -0
- data/lib/ffi/platform/powerpc-aix/types.conf +180 -0
- data/lib/ffi/platform/powerpc-darwin/types.conf +100 -0
- data/lib/ffi/platform/powerpc-linux/types.conf +100 -0
- data/lib/ffi/platform/powerpc64-linux/types.conf +104 -0
- data/lib/ffi/platform/s390-linux/types.conf +102 -0
- data/lib/ffi/platform/s390x-linux/types.conf +102 -0
- data/lib/ffi/platform/sparc-linux/types.conf +102 -0
- data/lib/ffi/platform/sparc-solaris/types.conf +128 -0
- data/lib/ffi/platform/sparc64-linux/types.conf +102 -0
- data/lib/ffi/platform/sparcv9-solaris/types.conf +128 -0
- data/lib/ffi/platform/x86_64-cygwin/types.conf +3 -0
- data/lib/ffi/platform/x86_64-darwin/types.conf +126 -0
- data/lib/ffi/platform/x86_64-freebsd/types.conf +128 -0
- data/lib/ffi/platform/x86_64-linux/types.conf +102 -0
- data/lib/ffi/platform/x86_64-netbsd/types.conf +128 -0
- data/lib/ffi/platform/x86_64-openbsd/types.conf +134 -0
- data/lib/ffi/platform/x86_64-solaris/types.conf +122 -0
- data/lib/ffi/platform/x86_64-windows/types.conf +120 -0
- data/lib/ffi/pointer.rb +161 -0
- data/lib/ffi/struct.rb +371 -0
- data/lib/ffi/struct_layout_builder.rb +227 -0
- data/lib/ffi/tools/const_generator.rb +229 -0
- data/lib/ffi/tools/generator.rb +60 -0
- data/lib/ffi/tools/generator_task.rb +36 -0
- data/lib/ffi/tools/struct_generator.rb +194 -0
- data/lib/ffi/tools/types_generator.rb +134 -0
- data/lib/ffi/types.rb +194 -0
- data/lib/ffi/union.rb +43 -0
- data/lib/ffi/variadic.rb +78 -0
- data/lib/ffi/version.rb +4 -0
- data/libtest/Benchmark.c +52 -0
- data/libtest/BoolTest.c +34 -0
- data/libtest/BufferTest.c +31 -0
- data/libtest/ClosureTest.c +205 -0
- data/libtest/EnumTest.c +51 -0
- data/libtest/FunctionTest.c +70 -0
- data/libtest/GNUmakefile +149 -0
- data/libtest/GlobalVariable.c +62 -0
- data/libtest/LastErrorTest.c +21 -0
- data/libtest/NumberTest.c +132 -0
- data/libtest/PointerTest.c +63 -0
- data/libtest/ReferenceTest.c +23 -0
- data/libtest/StringTest.c +34 -0
- data/libtest/StructTest.c +243 -0
- data/libtest/UnionTest.c +43 -0
- data/libtest/VariadicTest.c +99 -0
- data/spec/ffi/LICENSE.SPECS +22 -0
- data/spec/ffi/async_callback_spec.rb +35 -0
- data/spec/ffi/bitmask_spec.rb +575 -0
- data/spec/ffi/bool_spec.rb +32 -0
- data/spec/ffi/buffer_spec.rb +279 -0
- data/spec/ffi/callback_spec.rb +773 -0
- data/spec/ffi/custom_param_type.rb +37 -0
- data/spec/ffi/custom_type_spec.rb +74 -0
- data/spec/ffi/dup_spec.rb +52 -0
- data/spec/ffi/enum_spec.rb +423 -0
- data/spec/ffi/errno_spec.rb +20 -0
- data/spec/ffi/ffi_spec.rb +28 -0
- data/spec/ffi/fixtures/Benchmark.c +52 -0
- data/spec/ffi/fixtures/BitmaskTest.c +51 -0
- data/spec/ffi/fixtures/BoolTest.c +34 -0
- data/spec/ffi/fixtures/BufferTest.c +31 -0
- data/spec/ffi/fixtures/ClosureTest.c +205 -0
- data/spec/ffi/fixtures/EnumTest.c +51 -0
- data/spec/ffi/fixtures/FunctionTest.c +142 -0
- data/spec/ffi/fixtures/GNUmakefile +149 -0
- data/spec/ffi/fixtures/GlobalVariable.c +62 -0
- data/spec/ffi/fixtures/LastErrorTest.c +21 -0
- data/spec/ffi/fixtures/NumberTest.c +132 -0
- data/spec/ffi/fixtures/PipeHelper.h +21 -0
- data/spec/ffi/fixtures/PipeHelperPosix.c +41 -0
- data/spec/ffi/fixtures/PipeHelperWindows.c +72 -0
- data/spec/ffi/fixtures/PointerTest.c +63 -0
- data/spec/ffi/fixtures/ReferenceTest.c +23 -0
- data/spec/ffi/fixtures/StringTest.c +34 -0
- data/spec/ffi/fixtures/StructTest.c +243 -0
- data/spec/ffi/fixtures/UnionTest.c +43 -0
- data/spec/ffi/fixtures/VariadicTest.c +99 -0
- data/spec/ffi/fixtures/classes.rb +438 -0
- data/spec/ffi/function_spec.rb +97 -0
- data/spec/ffi/io_spec.rb +16 -0
- data/spec/ffi/library_spec.rb +286 -0
- data/spec/ffi/long_double.rb +30 -0
- data/spec/ffi/managed_struct_spec.rb +68 -0
- data/spec/ffi/memorypointer_spec.rb +78 -0
- data/spec/ffi/number_spec.rb +247 -0
- data/spec/ffi/platform_spec.rb +114 -0
- data/spec/ffi/pointer_spec.rb +285 -0
- data/spec/ffi/rbx/attach_function_spec.rb +34 -0
- data/spec/ffi/rbx/memory_pointer_spec.rb +198 -0
- data/spec/ffi/rbx/spec_helper.rb +6 -0
- data/spec/ffi/rbx/struct_spec.rb +18 -0
- data/spec/ffi/spec_helper.rb +93 -0
- data/spec/ffi/string_spec.rb +118 -0
- data/spec/ffi/strptr_spec.rb +50 -0
- data/spec/ffi/struct_by_ref_spec.rb +43 -0
- data/spec/ffi/struct_callback_spec.rb +69 -0
- data/spec/ffi/struct_initialize_spec.rb +35 -0
- data/spec/ffi/struct_packed_spec.rb +50 -0
- data/spec/ffi/struct_spec.rb +882 -0
- data/spec/ffi/typedef_spec.rb +91 -0
- data/spec/ffi/union_spec.rb +67 -0
- data/spec/ffi/variadic_spec.rb +132 -0
- data/spec/spec.opts +4 -0
- metadata +309 -0
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module FFI
|
4
|
+
|
5
|
+
# @private
|
6
|
+
class TypesGenerator
|
7
|
+
|
8
|
+
##
|
9
|
+
# Maps different C types to the C type representations we use
|
10
|
+
|
11
|
+
TYPE_MAP = {
|
12
|
+
"char" => :char,
|
13
|
+
"signed char" => :char,
|
14
|
+
"__signed char" => :char,
|
15
|
+
"unsigned char" => :uchar,
|
16
|
+
|
17
|
+
"short" => :short,
|
18
|
+
"signed short" => :short,
|
19
|
+
"signed short int" => :short,
|
20
|
+
"unsigned short" => :ushort,
|
21
|
+
"unsigned short int" => :ushort,
|
22
|
+
|
23
|
+
"int" => :int,
|
24
|
+
"signed int" => :int,
|
25
|
+
"unsigned int" => :uint,
|
26
|
+
|
27
|
+
"long" => :long,
|
28
|
+
"long int" => :long,
|
29
|
+
"signed long" => :long,
|
30
|
+
"signed long int" => :long,
|
31
|
+
"unsigned long" => :ulong,
|
32
|
+
"unsigned long int" => :ulong,
|
33
|
+
"long unsigned int" => :ulong,
|
34
|
+
|
35
|
+
"long long" => :long_long,
|
36
|
+
"long long int" => :long_long,
|
37
|
+
"signed long long" => :long_long,
|
38
|
+
"signed long long int" => :long_long,
|
39
|
+
"unsigned long long" => :ulong_long,
|
40
|
+
"unsigned long long int" => :ulong_long,
|
41
|
+
|
42
|
+
"char *" => :string,
|
43
|
+
"void *" => :pointer,
|
44
|
+
}
|
45
|
+
|
46
|
+
def self.generate(options = {})
|
47
|
+
typedefs = nil
|
48
|
+
Tempfile.open 'ffi_types_generator' do |io|
|
49
|
+
io.puts <<-C
|
50
|
+
#include <sys/types.h>
|
51
|
+
#if !(defined(WIN32))
|
52
|
+
#include <sys/socket.h>
|
53
|
+
#include <sys/resource.h>
|
54
|
+
#endif
|
55
|
+
C
|
56
|
+
|
57
|
+
io.close
|
58
|
+
cc = ENV['CC'] || 'gcc'
|
59
|
+
cmd = "#{cc} -E -x c #{options[:cppflags]} -D_DARWIN_USE_64_BIT_INODE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -c"
|
60
|
+
if options[:input]
|
61
|
+
typedefs = File.read(options[:input])
|
62
|
+
elsif options[:remote]
|
63
|
+
typedefs = `ssh #{options[:remote]} #{cmd} - < #{io.path}`
|
64
|
+
else
|
65
|
+
typedefs = `#{cmd} #{io.path}`
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
code = ""
|
70
|
+
|
71
|
+
typedefs.each_line do |type|
|
72
|
+
# We only care about single line typedef
|
73
|
+
next unless type =~ /typedef/
|
74
|
+
# Ignore unions or structs
|
75
|
+
next if type =~ /union|struct/
|
76
|
+
|
77
|
+
# strip off the starting typedef and ending ;
|
78
|
+
type.gsub!(/^(.*typedef\s*)/, "")
|
79
|
+
type.gsub!(/\s*;\s*$/, "")
|
80
|
+
|
81
|
+
parts = type.split(/\s+/)
|
82
|
+
def_type = parts.join(" ")
|
83
|
+
|
84
|
+
# GCC does mapping with __attribute__ stuf, also see
|
85
|
+
# http://hal.cs.berkeley.edu/cil/cil016.html section 16.2.7. Problem
|
86
|
+
# with this is that the __attribute__ stuff can either occur before or
|
87
|
+
# after the new type that is defined...
|
88
|
+
if type =~ /__attribute__/
|
89
|
+
if parts.last =~ /__QI__|__HI__|__SI__|__DI__|__word__/
|
90
|
+
|
91
|
+
# In this case, the new type is BEFORE __attribute__ we need to
|
92
|
+
# find the final_type as the type before the part that starts with
|
93
|
+
# __attribute__
|
94
|
+
final_type = ""
|
95
|
+
parts.each do |p|
|
96
|
+
break if p =~ /__attribute__/
|
97
|
+
final_type = p
|
98
|
+
end
|
99
|
+
else
|
100
|
+
final_type = parts.pop
|
101
|
+
end
|
102
|
+
|
103
|
+
def_type = case type
|
104
|
+
when /__QI__/ then "char"
|
105
|
+
when /__HI__/ then "short"
|
106
|
+
when /__SI__/ then "int"
|
107
|
+
when /__DI__/ then "long long"
|
108
|
+
when /__word__/ then "long"
|
109
|
+
else "int"
|
110
|
+
end
|
111
|
+
|
112
|
+
def_type = "unsigned #{def_type}" if type =~ /unsigned/
|
113
|
+
else
|
114
|
+
final_type = parts.pop
|
115
|
+
def_type = parts.join(" ")
|
116
|
+
end
|
117
|
+
|
118
|
+
if type = TYPE_MAP[def_type]
|
119
|
+
code << "rbx.platform.typedef.#{final_type} = #{type}\n"
|
120
|
+
TYPE_MAP[final_type] = TYPE_MAP[def_type]
|
121
|
+
else
|
122
|
+
# Fallback to an ordinary pointer if we don't know the type
|
123
|
+
if def_type =~ /\*/
|
124
|
+
code << "rbx.platform.typedef.#{final_type} = pointer\n"
|
125
|
+
TYPE_MAP[final_type] = :pointer
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
code
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
data/lib/ffi/types.rb
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2008-2010 Wayne Meissner
|
3
|
+
# Copyright (c) 2007, 2008 Evan Phoenix
|
4
|
+
#
|
5
|
+
# This file is part of ruby-ffi.
|
6
|
+
#
|
7
|
+
# All rights reserved.
|
8
|
+
#
|
9
|
+
# Redistribution and use in source and binary forms, with or without
|
10
|
+
# modification, are permitted provided that the following conditions are met:
|
11
|
+
#
|
12
|
+
# * Redistributions of source code must retain the above copyright notice, this
|
13
|
+
# list of conditions and the following disclaimer.
|
14
|
+
# * Redistributions in binary form must reproduce the above copyright notice
|
15
|
+
# this list of conditions and the following disclaimer in the documentation
|
16
|
+
# and/or other materials provided with the distribution.
|
17
|
+
# * Neither the name of the Ruby FFI project nor the names of its contributors
|
18
|
+
# may be used to endorse or promote products derived from this software
|
19
|
+
# without specific prior written permission.
|
20
|
+
#
|
21
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
24
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
25
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
26
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
27
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
28
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
29
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
#
|
32
|
+
|
33
|
+
# see {file:README}
|
34
|
+
module FFI
|
35
|
+
|
36
|
+
# @param [Type, DataConverter, Symbol] old type definition used by {FFI.find_type}
|
37
|
+
# @param [Symbol] add new type definition's name to add
|
38
|
+
# @return [Type]
|
39
|
+
# Add a definition type to type definitions.
|
40
|
+
def self.typedef(old, add)
|
41
|
+
TypeDefs[add] = self.find_type(old)
|
42
|
+
end
|
43
|
+
|
44
|
+
# (see FFI.typedef)
|
45
|
+
def self.add_typedef(old, add)
|
46
|
+
typedef old, add
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
# @param [Type, DataConverter, Symbol] name
|
51
|
+
# @param [Hash] type_map if nil, {FFI::TypeDefs} is used
|
52
|
+
# @return [Type]
|
53
|
+
# Find a type in +type_map+ ({FFI::TypeDefs}, by default) from
|
54
|
+
# a type objet, a type name (symbol). If +name+ is a {DataConverter},
|
55
|
+
# a new {Type::Mapped} is created.
|
56
|
+
def self.find_type(name, type_map = nil)
|
57
|
+
if name.is_a?(Type)
|
58
|
+
name
|
59
|
+
|
60
|
+
elsif type_map && type_map.has_key?(name)
|
61
|
+
type_map[name]
|
62
|
+
|
63
|
+
elsif TypeDefs.has_key?(name)
|
64
|
+
TypeDefs[name]
|
65
|
+
|
66
|
+
elsif name.is_a?(DataConverter)
|
67
|
+
(type_map || TypeDefs)[name] = Type::Mapped.new(name)
|
68
|
+
else
|
69
|
+
raise TypeError, "unable to resolve type '#{name}'"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# List of type definitions
|
74
|
+
TypeDefs.merge!({
|
75
|
+
# The C void type; only useful for function return types
|
76
|
+
:void => Type::VOID,
|
77
|
+
|
78
|
+
# C boolean type
|
79
|
+
:bool => Type::BOOL,
|
80
|
+
|
81
|
+
# C nul-terminated string
|
82
|
+
:string => Type::STRING,
|
83
|
+
|
84
|
+
# C signed char
|
85
|
+
:char => Type::CHAR,
|
86
|
+
# C unsigned char
|
87
|
+
:uchar => Type::UCHAR,
|
88
|
+
|
89
|
+
# C signed short
|
90
|
+
:short => Type::SHORT,
|
91
|
+
# C unsigned short
|
92
|
+
:ushort => Type::USHORT,
|
93
|
+
|
94
|
+
# C signed int
|
95
|
+
:int => Type::INT,
|
96
|
+
# C unsigned int
|
97
|
+
:uint => Type::UINT,
|
98
|
+
|
99
|
+
# C signed long
|
100
|
+
:long => Type::LONG,
|
101
|
+
|
102
|
+
# C unsigned long
|
103
|
+
:ulong => Type::ULONG,
|
104
|
+
|
105
|
+
# C signed long long integer
|
106
|
+
:long_long => Type::LONG_LONG,
|
107
|
+
|
108
|
+
# C unsigned long long integer
|
109
|
+
:ulong_long => Type::ULONG_LONG,
|
110
|
+
|
111
|
+
# C single precision float
|
112
|
+
:float => Type::FLOAT,
|
113
|
+
|
114
|
+
# C double precision float
|
115
|
+
:double => Type::DOUBLE,
|
116
|
+
|
117
|
+
# C long double
|
118
|
+
:long_double => Type::LONGDOUBLE,
|
119
|
+
|
120
|
+
# Native memory address
|
121
|
+
:pointer => Type::POINTER,
|
122
|
+
|
123
|
+
# 8 bit signed integer
|
124
|
+
:int8 => Type::INT8,
|
125
|
+
# 8 bit unsigned integer
|
126
|
+
:uint8 => Type::UINT8,
|
127
|
+
|
128
|
+
# 16 bit signed integer
|
129
|
+
:int16 => Type::INT16,
|
130
|
+
# 16 bit unsigned integer
|
131
|
+
:uint16 => Type::UINT16,
|
132
|
+
|
133
|
+
# 32 bit signed integer
|
134
|
+
:int32 => Type::INT32,
|
135
|
+
# 32 bit unsigned integer
|
136
|
+
:uint32 => Type::UINT32,
|
137
|
+
|
138
|
+
# 64 bit signed integer
|
139
|
+
:int64 => Type::INT64,
|
140
|
+
# 64 bit unsigned integer
|
141
|
+
:uint64 => Type::UINT64,
|
142
|
+
|
143
|
+
:buffer_in => Type::BUFFER_IN,
|
144
|
+
:buffer_out => Type::BUFFER_OUT,
|
145
|
+
:buffer_inout => Type::BUFFER_INOUT,
|
146
|
+
|
147
|
+
# Used in function prototypes to indicate the arguments are variadic
|
148
|
+
:varargs => Type::VARARGS,
|
149
|
+
})
|
150
|
+
|
151
|
+
# This will convert a pointer to a Ruby string (just like `:string`), but
|
152
|
+
# also allow to work with the pointer itself. This is useful when you want
|
153
|
+
# a Ruby string already containing a copy of the data, but also the pointer
|
154
|
+
# to the data for you to do something with it, like freeing it, in case the
|
155
|
+
# library handed the memory to off to the caller (Ruby-FFI).
|
156
|
+
#
|
157
|
+
# It's {typedef}'d as +:strptr+.
|
158
|
+
class StrPtrConverter
|
159
|
+
extend DataConverter
|
160
|
+
native_type Type::POINTER
|
161
|
+
|
162
|
+
# @param [Pointer] val
|
163
|
+
# @param ctx not used
|
164
|
+
# @return [Array(String, Pointer)]
|
165
|
+
# Returns a [ String, Pointer ] tuple so the C memory for the string can be freed
|
166
|
+
def self.from_native(val, ctx)
|
167
|
+
[ val.null? ? nil : val.get_string(0), val ]
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
typedef(StrPtrConverter, :strptr)
|
172
|
+
|
173
|
+
# @param type +type+ is an instance of class accepted by {FFI.find_type}
|
174
|
+
# @return [Numeric]
|
175
|
+
# Get +type+ size, in bytes.
|
176
|
+
def self.type_size(type)
|
177
|
+
find_type(type).size
|
178
|
+
end
|
179
|
+
|
180
|
+
# Load all the platform dependent types
|
181
|
+
begin
|
182
|
+
File.open(File.join(Platform::CONF_DIR, 'types.conf'), "r") do |f|
|
183
|
+
prefix = "rbx.platform.typedef."
|
184
|
+
f.each_line { |line|
|
185
|
+
if line.index(prefix) == 0
|
186
|
+
new_type, orig_type = line.chomp.slice(prefix.length..-1).split(/\s*=\s*/)
|
187
|
+
typedef(orig_type.to_sym, new_type.to_sym)
|
188
|
+
end
|
189
|
+
}
|
190
|
+
end
|
191
|
+
typedef :pointer, :caddr_t
|
192
|
+
rescue Errno::ENOENT
|
193
|
+
end
|
194
|
+
end
|
data/lib/ffi/union.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2009 Andrea Fazzi <andrea.fazzi@alcacoop.it>
|
3
|
+
#
|
4
|
+
# This file is part of ruby-ffi.
|
5
|
+
#
|
6
|
+
# All rights reserved.
|
7
|
+
#
|
8
|
+
# Redistribution and use in source and binary forms, with or without
|
9
|
+
# modification, are permitted provided that the following conditions are met:
|
10
|
+
#
|
11
|
+
# * Redistributions of source code must retain the above copyright notice, this
|
12
|
+
# list of conditions and the following disclaimer.
|
13
|
+
# * Redistributions in binary form must reproduce the above copyright notice
|
14
|
+
# this list of conditions and the following disclaimer in the documentation
|
15
|
+
# and/or other materials provided with the distribution.
|
16
|
+
# * Neither the name of the Ruby FFI project nor the names of its contributors
|
17
|
+
# may be used to endorse or promote products derived from this software
|
18
|
+
# without specific prior written permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
24
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
#
|
31
|
+
|
32
|
+
require 'ffi/struct'
|
33
|
+
|
34
|
+
module FFI
|
35
|
+
|
36
|
+
class Union < FFI::Struct
|
37
|
+
def self.builder
|
38
|
+
b = StructLayoutBuilder.new
|
39
|
+
b.union = true
|
40
|
+
b
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/ffi/variadic.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (C) 2008, 2009 Wayne Meissner
|
3
|
+
# Copyright (C) 2009 Luc Heinrich
|
4
|
+
#
|
5
|
+
# This file is part of ruby-ffi.
|
6
|
+
#
|
7
|
+
# All rights reserved.
|
8
|
+
#
|
9
|
+
# Redistribution and use in source and binary forms, with or without
|
10
|
+
# modification, are permitted provided that the following conditions are met:
|
11
|
+
#
|
12
|
+
# * Redistributions of source code must retain the above copyright notice, this
|
13
|
+
# list of conditions and the following disclaimer.
|
14
|
+
# * Redistributions in binary form must reproduce the above copyright notice
|
15
|
+
# this list of conditions and the following disclaimer in the documentation
|
16
|
+
# and/or other materials provided with the distribution.
|
17
|
+
# * Neither the name of the Ruby FFI project nor the names of its contributors
|
18
|
+
# may be used to endorse or promote products derived from this software
|
19
|
+
# without specific prior written permission.
|
20
|
+
#
|
21
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
24
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
25
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
26
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
27
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
28
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
29
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
#
|
32
|
+
|
33
|
+
module FFI
|
34
|
+
class VariadicInvoker
|
35
|
+
def init(arg_types, type_map)
|
36
|
+
@fixed = Array.new
|
37
|
+
@type_map = type_map
|
38
|
+
arg_types.each_with_index do |type, i|
|
39
|
+
@fixed << type unless type == Type::VARARGS
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def call(*args, &block)
|
45
|
+
param_types = Array.new(@fixed)
|
46
|
+
param_values = Array.new
|
47
|
+
@fixed.each_with_index do |t, i|
|
48
|
+
param_values << args[i]
|
49
|
+
end
|
50
|
+
i = @fixed.length
|
51
|
+
while i < args.length
|
52
|
+
param_types << FFI.find_type(args[i], @type_map)
|
53
|
+
param_values << args[i + 1]
|
54
|
+
i += 2
|
55
|
+
end
|
56
|
+
invoke(param_types, param_values, &block)
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# Attach the invoker to module +mod+ as +mname+
|
61
|
+
#
|
62
|
+
def attach(mod, mname)
|
63
|
+
invoker = self
|
64
|
+
params = "*args"
|
65
|
+
call = "call"
|
66
|
+
mod.module_eval <<-code
|
67
|
+
@@#{mname} = invoker
|
68
|
+
def self.#{mname}(#{params})
|
69
|
+
@@#{mname}.#{call}(#{params})
|
70
|
+
end
|
71
|
+
def #{mname}(#{params})
|
72
|
+
@@#{mname}.#{call}(#{params})
|
73
|
+
end
|
74
|
+
code
|
75
|
+
invoker
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|