Permalink
Browse files

Add FFI::Union

  • Loading branch information...
1 parent 49fd6db commit 51b52ed4e46d7578fd04b90854345fcde41907e1 @dbussink dbussink committed Aug 6, 2012
View
@@ -50,6 +50,8 @@ def type_size(type)
return type_size(find_type(type))
when Rubinius::NativeFunction
return type_size(TYPE_PTR)
+ when FFI::Enum
+ return type_size(TYPE_ENUM)
end
raise PrimitiveFailure, "FFI.type_size primitive failed: #{type}"
@@ -198,6 +200,28 @@ def initialize(struct)
end
Struct = StructByValue
+
+ OBJECT = TYPE_OBJECT
+ CHAR = TYPE_CHAR
+ UCHAR = TYPE_UCHAR
+ BOOL = TYPE_BOOL
+ SHORT = TYPE_SHORT
+ USHORT = TYPE_USHORT
+ INT = TYPE_INT
+ UINT = TYPE_UINT
+ LONG = TYPE_LONG
+ ULONG = TYPE_ULONG
+ LL = TYPE_LL
+ ULL = TYPE_ULL
+ FLOAT = TYPE_FLOAT
+ DOUBLE = TYPE_DOUBLE
+ PTR = TYPE_PTR
+ VOID = TYPE_VOID
+ STRING = TYPE_STRING
+ STATE = TYPE_STATE
+ STRPTR = TYPE_STRPTR
+ CHARARR = TYPE_CHARARR
+ ENUM = TYPE_ENUM
end
end
@@ -261,7 +261,7 @@ def find_type(name)
@typedefs ||= Rubinius::LookupTable.new
if name.kind_of? Rubinius::NativeFunction or name.kind_of? FFI::Enum
- return name
+ return name
end
if type = @typedefs[name]
@@ -8,3 +8,4 @@ file.rbc
math.rbc
posix.rbc
struct.rbc
+union.rbc
@@ -9,3 +9,4 @@ math.rbc
math19.rbc
posix.rbc
struct.rbc
+union.rbc
@@ -9,3 +9,4 @@ math.rbc
math19.rbc
posix.rbc
struct.rbc
+union.rbc
View
@@ -6,6 +6,8 @@ module FFI
class Struct
+ @offset_zero = false
+
class InlineArray
def initialize(type, ptr)
@pointer = ptr
@@ -124,7 +126,7 @@ def self.layout(*spec)
type = FFI::Type::Array.new(type_code, ary_size, klass)
element_size = type_size * ary_size
- elsif f.kind_of?(Class) and f < FFI::Struct
+ elsif f.kind_of?(Class) and (f < FFI::Struct || f < FFI::Union)
type = FFI::Type::StructByValue.new(f)
element_size = type_size = f.size
else
@@ -143,12 +145,16 @@ def self.layout(*spec)
if offset.kind_of?(Fixnum)
i += 3
else
- offset = @size
+ if self < FFI::Union
+ offset = 0
+ else
+ offset = @size
- mod = offset % type_size
- unless mod == 0
- # we need to align it.
- offset += (type_size - mod)
+ mod = offset % type_size
+ unless mod == 0
+ # we need to align it.
+ offset += (type_size - mod)
+ end
end
i += 2
@@ -194,13 +200,23 @@ def size
end
def self.offset_of(name)
- offset, type = @cspec[name]
- return offset
+ @layout[name].first
end
def offset_of(name)
- offset, type = @cspec[name]
- return offset
+ @cspec[name].first
+ end
+
+ def self.offsets
+ members.map do |member|
+ [member, @layout[member].first]
+ end
+ end
+
+ def offsets
+ members.map do |member|
+ [member, @cspec[member].first]
+ end
end
def self.members
View
@@ -0,0 +1,10 @@
+# -*- encoding: us-ascii -*-
+
+module FFI
+ ##
+ # Represents a C union as ruby class.
+
+ class Union < Struct
+ end
+end
+
@@ -123,6 +123,7 @@ namespace rubinius {
case RBX_FFI_TYPE_INT:
case RBX_FFI_TYPE_UINT:
+ case RBX_FFI_TYPE_ENUM:
return sizeof(int);
case RBX_FFI_TYPE_LONG:
View
@@ -53,5 +53,6 @@ namespace rubinius {
mod->set_const(state, "TYPE_STATE", Fixnum::from(RBX_FFI_TYPE_STATE));
mod->set_const(state, "TYPE_STRPTR", Fixnum::from(RBX_FFI_TYPE_STRPTR));
mod->set_const(state, "TYPE_CHARARR", Fixnum::from(RBX_FFI_TYPE_CHARARR));
+ mod->set_const(state, "TYPE_ENUM", Fixnum::from(RBX_FFI_TYPE_ENUM));
}
};

0 comments on commit 51b52ed

Please sign in to comment.