ffi 0.6.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ffi might be problematic. Click here for more details.

Files changed (92) hide show
  1. data/History.txt +7 -0
  2. data/LICENSE +10 -21
  3. data/README.rdoc +70 -0
  4. data/Rakefile +56 -84
  5. data/ext/ffi_c/AbstractMemory.c +56 -38
  6. data/ext/ffi_c/AbstractMemory.h +15 -22
  7. data/ext/ffi_c/Buffer.c +61 -22
  8. data/ext/ffi_c/Call.c +52 -540
  9. data/ext/ffi_c/Call.h +1 -1
  10. data/ext/ffi_c/DataConverter.c +62 -0
  11. data/ext/ffi_c/DynamicLibrary.c +21 -1
  12. data/ext/ffi_c/Function.c +252 -30
  13. data/ext/ffi_c/MappedType.c +146 -0
  14. data/{libtest/FunctionTest.c → ext/ffi_c/MappedType.h} +32 -25
  15. data/ext/ffi_c/MemoryPointer.c +12 -33
  16. data/ext/ffi_c/Platform.c +2 -0
  17. data/ext/ffi_c/Pointer.c +66 -28
  18. data/ext/ffi_c/Struct.c +19 -306
  19. data/ext/ffi_c/Struct.h +6 -0
  20. data/ext/ffi_c/StructByReference.c +150 -0
  21. data/{libtest/LastErrorTest.c → ext/ffi_c/StructByReference.h} +30 -21
  22. data/ext/ffi_c/StructLayout.c +26 -16
  23. data/ext/ffi_c/Type.c +39 -68
  24. data/ext/ffi_c/Type.h +12 -22
  25. data/ext/ffi_c/Types.c +20 -5
  26. data/ext/ffi_c/Types.h +7 -7
  27. data/ext/ffi_c/Variadic.c +21 -17
  28. data/ext/ffi_c/extconf.rb +4 -0
  29. data/ext/ffi_c/ffi.c +8 -2
  30. data/ext/ffi_c/rbffi.h +1 -0
  31. data/lib/ffi/autopointer.rb +23 -22
  32. data/lib/ffi/enum.rb +36 -21
  33. data/lib/ffi/errno.rb +20 -0
  34. data/lib/ffi/ffi.rb +13 -80
  35. data/lib/ffi/io.rb +12 -20
  36. data/lib/ffi/library.rb +109 -92
  37. data/lib/ffi/managedstruct.rb +1 -1
  38. data/lib/ffi/memorypointer.rb +15 -21
  39. data/lib/ffi/platform.rb +27 -33
  40. data/lib/ffi/pointer.rb +14 -21
  41. data/lib/ffi/struct.rb +98 -49
  42. data/lib/ffi/struct_layout_builder.rb +158 -0
  43. data/lib/ffi/types.rb +99 -128
  44. data/lib/ffi/union.rb +20 -0
  45. data/lib/ffi/variadic.rb +33 -22
  46. data/spec/ffi/async_callback_spec.rb +23 -0
  47. data/spec/ffi/callback_spec.rb +62 -0
  48. data/spec/ffi/custom_param_type.rb +31 -0
  49. data/spec/ffi/custom_type_spec.rb +73 -0
  50. data/spec/ffi/enum_spec.rb +19 -0
  51. data/spec/ffi/ffi_spec.rb +24 -0
  52. data/spec/ffi/pointer_spec.rb +15 -0
  53. data/spec/ffi/rbx/memory_pointer_spec.rb +7 -1
  54. data/spec/ffi/strptr_spec.rb +36 -0
  55. data/spec/ffi/struct_packed_spec.rb +46 -0
  56. data/spec/ffi/struct_spec.rb +19 -5
  57. data/spec/ffi/typedef_spec.rb +14 -0
  58. data/tasks/ann.rake +80 -0
  59. data/tasks/extension.rake +25 -0
  60. data/tasks/gem.rake +200 -0
  61. data/tasks/git.rake +41 -0
  62. data/tasks/notes.rake +27 -0
  63. data/tasks/post_load.rake +34 -0
  64. data/tasks/rdoc.rake +50 -0
  65. data/tasks/rubyforge.rake +55 -0
  66. data/tasks/setup.rb +301 -0
  67. data/tasks/spec.rake +54 -0
  68. data/tasks/svn.rake +47 -0
  69. data/tasks/test.rake +40 -0
  70. metadata +139 -131
  71. data/README.md +0 -109
  72. data/ext/ffi_c/AutoPointer.c +0 -60
  73. data/ext/ffi_c/AutoPointer.h +0 -18
  74. data/ext/ffi_c/Ffi_c.iml +0 -12
  75. data/ffi.gemspec +0 -18
  76. data/gen/log +0 -1
  77. data/lib/Lib.iml +0 -21
  78. data/libtest/Benchmark.c +0 -73
  79. data/libtest/BoolTest.c +0 -52
  80. data/libtest/BufferTest.c +0 -52
  81. data/libtest/ClosureTest.c +0 -173
  82. data/libtest/EnumTest.c +0 -55
  83. data/libtest/GNUmakefile +0 -141
  84. data/libtest/GlobalVariable.c +0 -56
  85. data/libtest/NumberTest.c +0 -145
  86. data/libtest/PointerTest.c +0 -84
  87. data/libtest/ReferenceTest.c +0 -44
  88. data/libtest/StringTest.c +0 -55
  89. data/libtest/StructTest.c +0 -247
  90. data/libtest/UnionTest.c +0 -64
  91. data/libtest/VariadicTest.c +0 -57
  92. data/spec/ffi/Ffi.iml +0 -12
data/ext/ffi_c/Types.h CHANGED
@@ -57,20 +57,20 @@ typedef enum {
57
57
  NATIVE_CHAR_ARRAY,
58
58
  NATIVE_BOOL,
59
59
 
60
- /**
61
- * An immutable string. Nul terminated, but only copies in to the native function
62
- */
60
+ /** An immutable string. Nul terminated, but only copies in to the native function */
63
61
  NATIVE_STRING,
64
- /** A Rubinus :string arg - copies data both ways, and nul terminates */
65
- NATIVE_RBXSTRING,
62
+
66
63
  /** The function takes a variable number of arguments */
67
64
  NATIVE_VARARGS,
68
- /** A typedef-ed enum */
69
- NATIVE_ENUM,
65
+
70
66
  /** Struct-by-value param or result */
71
67
  NATIVE_STRUCT,
68
+
72
69
  /** An array type definition */
73
70
  NATIVE_ARRAY,
71
+
72
+ /** Custom native type */
73
+ NATIVE_MAPPED,
74
74
  } NativeType;
75
75
 
76
76
  #include <ffi.h>
data/ext/ffi_c/Variadic.c CHANGED
@@ -155,7 +155,7 @@ variadic_invoke(VALUE self, VALUE parameterTypes, VALUE parameterValues)
155
155
  void** ffiValues;
156
156
  ffi_type** ffiParamTypes;
157
157
  ffi_type* ffiReturnType;
158
- NativeType* paramTypes;
158
+ Type** paramTypes;
159
159
  VALUE* argv;
160
160
  int paramCount = 0, i;
161
161
  ffi_status ffiStatus;
@@ -165,7 +165,7 @@ variadic_invoke(VALUE self, VALUE parameterTypes, VALUE parameterValues)
165
165
 
166
166
  Data_Get_Struct(self, VariadicInvoker, invoker);
167
167
  paramCount = RARRAY_LEN(parameterTypes);
168
- paramTypes = ALLOCA_N(NativeType, paramCount);
168
+ paramTypes = ALLOCA_N(Type *, paramCount);
169
169
  ffiParamTypes = ALLOCA_N(ffi_type *, paramCount);
170
170
  params = ALLOCA_N(FFIStorage, paramCount);
171
171
  ffiValues = ALLOCA_N(void*, paramCount);
@@ -173,36 +173,40 @@ variadic_invoke(VALUE self, VALUE parameterTypes, VALUE parameterValues)
173
173
  retval = alloca(MAX(invoker->returnType->ffiType->size, FFI_SIZEOF_ARG));
174
174
 
175
175
  for (i = 0; i < paramCount; ++i) {
176
- VALUE entry = rb_ary_entry(parameterTypes, i);
177
- int paramType = rbffi_Type_GetIntValue(entry);
178
- Type* type;
179
- Data_Get_Struct(entry, Type, type);
176
+ VALUE rbType = rb_ary_entry(parameterTypes, i);
177
+
178
+ if (!rb_obj_is_kind_of(rbType, rbffi_TypeClass)) {
179
+ rb_raise(rb_eTypeError, "wrong type. Expected (FFI::Type)");
180
+ }
181
+ Data_Get_Struct(rbType, Type, paramTypes[i]);
180
182
 
181
- switch (paramType) {
183
+ switch (paramTypes[i]->nativeType) {
182
184
  case NATIVE_INT8:
183
185
  case NATIVE_INT16:
184
186
  case NATIVE_INT32:
185
- case NATIVE_ENUM:
186
- paramType = NATIVE_INT32;
187
- ffiParamTypes[i] = &ffi_type_sint;
187
+ rbType = rb_const_get(rbffi_TypeClass, rb_intern("INT32"));
188
+ Data_Get_Struct(rbType, Type, paramTypes[i]);
188
189
  break;
189
190
  case NATIVE_UINT8:
190
191
  case NATIVE_UINT16:
191
192
  case NATIVE_UINT32:
192
- paramType = NATIVE_UINT32;
193
- ffiParamTypes[i] = &ffi_type_uint;
193
+ rbType = rb_const_get(rbffi_TypeClass, rb_intern("UINT32"));
194
+ Data_Get_Struct(rbType, Type, paramTypes[i]);
194
195
  break;
196
+
195
197
  case NATIVE_FLOAT32:
196
- paramType = NATIVE_FLOAT64;
197
- ffiParamTypes[i] = &ffi_type_double;
198
+ rbType = rb_const_get(rbffi_TypeClass, rb_intern("DOUBLE"));
199
+ Data_Get_Struct(rbType, Type, paramTypes[i]);
198
200
  break;
201
+
199
202
  default:
200
- ffiParamTypes[i] = type->ffiType;
201
203
  break;
202
204
  }
203
- paramTypes[i] = paramType;
205
+
206
+
207
+ ffiParamTypes[i] = paramTypes[i]->ffiType;
204
208
  if (ffiParamTypes[i] == NULL) {
205
- rb_raise(rb_eArgError, "Invalid parameter type #%x", paramType);
209
+ rb_raise(rb_eArgError, "Invalid parameter type #%x", paramTypes[i]);
206
210
  }
207
211
  argv[i] = rb_ary_entry(parameterValues, i);
208
212
  }
data/ext/ffi_c/extconf.rb CHANGED
@@ -15,9 +15,13 @@ unless Config::CONFIG['host_os'] =~ /mswin32|mingw32/
15
15
  end
16
16
 
17
17
  have_func('rb_thread_blocking_region')
18
+ have_func('ruby_thread_has_gvl_p')
19
+ have_func('ruby_native_thread_p')
20
+ have_func('rb_thread_call_with_gvl')
18
21
 
19
22
  $defs << "-DHAVE_EXTCONF_H" if $defs.empty? # needed so create_header works
20
23
  $defs << "-DUSE_INTERNAL_LIBFFI" unless libffi_ok
24
+ $defs << "-DRUBY_1_9" if RUBY_VERSION >= "1.9.0"
21
25
 
22
26
  create_header
23
27
 
data/ext/ffi_c/ffi.c CHANGED
@@ -35,9 +35,9 @@
35
35
  #include "AbstractMemory.h"
36
36
  #include "Pointer.h"
37
37
  #include "MemoryPointer.h"
38
- #include "AutoPointer.h"
39
38
  #include "Struct.h"
40
39
  #include "StructByValue.h"
40
+ #include "StructByReference.h"
41
41
  #include "DynamicLibrary.h"
42
42
  #include "Platform.h"
43
43
  #include "Types.h"
@@ -47,6 +47,7 @@
47
47
  #include "MethodHandle.h"
48
48
  #include "Call.h"
49
49
  #include "ArrayType.h"
50
+ #include "MappedType.h"
50
51
 
51
52
  void Init_ffi_c(void);
52
53
 
@@ -59,8 +60,12 @@ Init_ffi_c(void) {
59
60
  rbffi_FFIModule = moduleFFI = rb_define_module("FFI");
60
61
  rb_global_variable(&moduleFFI);
61
62
 
63
+
62
64
  // FFI::Type needs to be initialized before most other classes
63
65
  rbffi_Type_Init(moduleFFI);
66
+
67
+ rbffi_DataConverter_Init(moduleFFI);
68
+
64
69
  rbffi_ArrayType_Init(moduleFFI);
65
70
  rbffi_LastError_Init(moduleFFI);
66
71
  rbffi_Call_Init(moduleFFI);
@@ -69,14 +74,15 @@ Init_ffi_c(void) {
69
74
  rbffi_Platform_Init(moduleFFI);
70
75
  rbffi_AbstractMemory_Init(moduleFFI);
71
76
  rbffi_Pointer_Init(moduleFFI);
72
- rbffi_AutoPointer_Init(moduleFFI);
73
77
  rbffi_Function_Init(moduleFFI);
74
78
  rbffi_MemoryPointer_Init(moduleFFI);
75
79
  rbffi_Buffer_Init(moduleFFI);
76
80
  rbffi_StructByValue_Init(moduleFFI);
81
+ rbffi_StructByReference_Init(moduleFFI);
77
82
  rbffi_Struct_Init(moduleFFI);
78
83
  rbffi_DynamicLibrary_Init(moduleFFI);
79
84
  rbffi_Variadic_Init(moduleFFI);
80
85
  rbffi_Types_Init(moduleFFI);
86
+ rbffi_MappedType_Init(moduleFFI);
81
87
  }
82
88
 
data/ext/ffi_c/rbffi.h CHANGED
@@ -15,6 +15,7 @@ extern void rbffi_Type_Init(VALUE ffiModule);
15
15
  extern void rbffi_Buffer_Init(VALUE ffiModule);
16
16
  extern void rbffi_Invoker_Init(VALUE ffiModule);
17
17
  extern void rbffi_Variadic_Init(VALUE ffiModule);
18
+ extern void rbffi_DataConverter_Init(VALUE ffiModule);
18
19
  extern VALUE rbffi_AbstractMemoryClass, rbffi_InvokerClass;
19
20
  extern int rbffi_type_size(VALUE type);
20
21
 
@@ -1,34 +1,27 @@
1
1
  #
2
- # Copyright (C) 2008, 2009 Wayne Meissner
2
+ # Copyright (C) 2008-2010 Wayne Meissner
3
3
  # Copyright (C) 2008 Mike Dalessio
4
4
  #
5
5
  # All rights reserved.
6
6
  #
7
- # Redistribution and use in source and binary forms, with or without
8
- # modification, are permitted provided that the following conditions are met:
7
+ # This file is part of ruby-ffi.
9
8
  #
10
- # * Redistributions of source code must retain the above copyright notice, this
11
- # list of conditions and the following disclaimer.
12
- # * Redistributions in binary form must reproduce the above copyright notice
13
- # this list of conditions and the following disclaimer in the documentation
14
- # and/or other materials provided with the distribution.
15
- # * Neither the name of the Evan Phoenix nor the names of its contributors
16
- # may be used to endorse or promote products derived from this software
17
- # without specific prior written permission.
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/>.
18
20
  #
19
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
23
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
21
 
30
22
  module FFI
31
23
  class AutoPointer < Pointer
24
+ extend DataConverter
32
25
 
33
26
  # call-seq:
34
27
  # AutoPointer.new(pointer, method) => the passed Method will be invoked at GC time
@@ -57,6 +50,7 @@ module FFI
57
50
  # release(), which by default does nothing.
58
51
  #
59
52
  def initialize(ptr, proc=nil, &block)
53
+ super(ptr)
60
54
  raise TypeError, "Invalid pointer" if ptr.nil? || !ptr.kind_of?(Pointer) \
61
55
  || ptr.kind_of?(MemoryPointer) || ptr.kind_of?(AutoPointer)
62
56
 
@@ -69,7 +63,6 @@ module FFI
69
63
  DefaultReleaser.new(ptr, self.class)
70
64
  end
71
65
 
72
- self.parent = ptr
73
66
  ObjectSpace.define_finalizer(self, @releaser)
74
67
  self
75
68
  end
@@ -115,6 +108,14 @@ module FFI
115
108
  end
116
109
  end
117
110
 
111
+ def self.native_type
112
+ raise RuntimeError.new("no release method defined for #{self.inspect}") unless self.respond_to?(:release)
113
+ Type::POINTER
114
+ end
115
+
116
+ def self.from_native(val, ctx)
117
+ self.new(val)
118
+ end
118
119
  end
119
120
 
120
121
  end
data/lib/ffi/enum.rb CHANGED
@@ -1,30 +1,23 @@
1
1
  #
2
+ # Copyright (C) 2009, 2010 Wayne Meissner
2
3
  # Copyright (C) 2009 Luc Heinrich
3
4
  #
4
5
  # All rights reserved.
5
6
  #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
7
+ # This file is part of ruby-ffi.
8
8
  #
9
- # * Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- # * Redistributions in binary form must reproduce the above copyright notice
12
- # this list of conditions and the following disclaimer in the documentation
13
- # and/or other materials provided with the distribution.
14
- # * Neither the name of the Evan Phoenix nor the names of its contributors
15
- # may be used to endorse or promote products derived from this software
16
- # without specific prior written permission.
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/>.
17
20
  #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
21
 
29
22
  module FFI
30
23
 
@@ -57,6 +50,8 @@ module FFI
57
50
  end
58
51
 
59
52
  class Enum
53
+ include DataConverter
54
+
60
55
  attr_reader :tag
61
56
 
62
57
  def initialize(info, tag=nil)
@@ -99,8 +94,28 @@ module FFI
99
94
  def symbol_map
100
95
  @kv_map
101
96
  end
97
+
102
98
  alias to_h symbol_map
99
+ alias to_hash symbol_map
100
+
101
+ def native_type
102
+ Type::INT
103
+ end
104
+
105
+ def to_native(val, ctx)
106
+ @kv_map[val] || if val.is_a?(Integer)
107
+ val
108
+ elsif val.respond_to?(:to_int)
109
+ val.to_int
110
+ else
111
+ raise ArgumentError, "invalid enum value, #{val.inspect}"
112
+ end
113
+ end
114
+
115
+ def from_native(val, ctx)
116
+ @vk_map[val] || val
117
+ end
103
118
 
104
119
  end
105
120
 
106
- end
121
+ end
data/lib/ffi/errno.rb CHANGED
@@ -1,3 +1,23 @@
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
+
1
21
  module FFI
2
22
  def self.errno
3
23
  FFI::LastError.error
data/lib/ffi/ffi.rb CHANGED
@@ -1,43 +1,21 @@
1
1
  #
2
- # Copyright (C) 2008 JRuby project
3
- # Copyright (c) 2007, 2008 Evan Phoenix
2
+ # Copyright (C) 2008-2010 JRuby project
3
+ #
4
4
  # All rights reserved.
5
5
  #
6
- # Redistribution and use in source and binary forms, with or without
7
- # modification, are permitted provided that the following conditions are met:
6
+ # This file is part of ruby-ffi.
8
7
  #
9
- # * Redistributions of source code must retain the above copyright notice, this
10
- # list of conditions and the following disclaimer.
11
- # * Redistributions in binary form must reproduce the above copyright notice
12
- # this list of conditions and the following disclaimer in the documentation
13
- # and/or other materials provided with the distribution.
14
- # * Neither the name of the Evan Phoenix nor the names of its contributors
15
- # may be used to endorse or promote products derived from this software
16
- # without specific prior written permission.
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.
17
11
  #
18
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
-
29
- module FFI
30
- # Specialised error classes
31
- class NativeError < LoadError; end
32
-
33
- class SignatureError < NativeError; end
34
-
35
- class NotFoundError < NativeError
36
- def initialize(function, *libraries)
37
- super("Function '#{function}' not found in [#{libraries[0].nil? ? 'current process' : libraries.join(", ")}]")
38
- end
39
- end
40
- end
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/>.
41
19
 
42
20
  require 'ffi/platform'
43
21
  require 'ffi/types'
@@ -52,48 +30,3 @@ require 'ffi/io'
52
30
  require 'ffi/autopointer'
53
31
  require 'ffi/variadic'
54
32
  require 'ffi/enum'
55
-
56
- module FFI
57
-
58
- def self.map_library_name(lib)
59
- # Mangle the library name to reflect the native library naming conventions
60
- lib = lib.to_s unless lib.kind_of?(String)
61
- lib = Platform::LIBC if Platform::IS_LINUX && lib == 'c'
62
- if lib && File.basename(lib) == lib
63
- ext = ".#{Platform::LIBSUFFIX}"
64
- lib = Platform::LIBPREFIX + lib unless lib =~ /^#{Platform::LIBPREFIX}/
65
- lib += ext unless lib =~ /#{ext}/
66
- end
67
- lib
68
- end
69
-
70
-
71
- def self.create_invoker(lib, name, args, ret_type, options = { :convention => :default })
72
- # Current artificial limitation based on JRuby::FFI limit
73
- raise SignatureError, 'FFI functions may take max 32 arguments!' if args.size > 32
74
-
75
- # Open the library if needed
76
- library = if lib.kind_of?(DynamicLibrary)
77
- lib
78
- elsif lib.kind_of?(String)
79
- # Allow FFI.create_invoker to be called with a library name
80
- DynamicLibrary.open(FFI.map_library_name(lib), DynamicLibrary::RTLD_LAZY)
81
- elsif lib.nil?
82
- FFI::Library::DEFAULT
83
- else
84
- raise LoadError, "Invalid library '#{lib}'"
85
- end
86
- function = library.find_function(name)
87
- raise NotFoundError.new(name, library.name) unless function
88
-
89
- args = args.map {|e| find_type(e) }
90
- invoker = if args.length > 0 && args[args.length - 1] == FFI::NativeType::VARARGS
91
- FFI::VariadicInvoker.new(function, args, find_type(ret_type), options)
92
- else
93
- FFI::Function.new(find_type(ret_type), args, function, options)
94
- end
95
- raise NotFoundError.new(name, library.name) unless invoker
96
-
97
- return invoker
98
- end
99
- end