summaryrefslogtreecommitdiff
path: root/ext/fiddle/lib/fiddle
diff options
context:
space:
mode:
authorsinisterchipmunk <sinisterchipmunk@gmail.com>2020-01-22 20:37:23 -0500
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-05-23 14:29:16 +0900
commit77cc13943cd9ec908d75133d7ac0b89c9ab57deb (patch)
tree43dcfca55a0d6c956eadb378780a987d61a5c849 /ext/fiddle/lib/fiddle
parent4a835621ced099316850a16f2a13534dea62a0b8 (diff)
[ruby/fiddle] Fix assignment to array within struct (#26)
* Allow access to a struct's underlying memory with `struct[offset, length]`. https://github.com/ruby/fiddle/commit/24083690a6
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3068
Diffstat (limited to 'ext/fiddle/lib/fiddle')
-rw-r--r--ext/fiddle/lib/fiddle/struct.rb29
1 files changed, 28 insertions, 1 deletions
diff --git a/ext/fiddle/lib/fiddle/struct.rb b/ext/fiddle/lib/fiddle/struct.rb
index f5385e548c..8a48a1cb8b 100644
--- a/ext/fiddle/lib/fiddle/struct.rb
+++ b/ext/fiddle/lib/fiddle/struct.rb
@@ -20,6 +20,33 @@ module Fiddle
end
end
+ # Wrapper for arrays within a struct
+ class StructArray < Array
+ include ValueUtil
+
+ def initialize(ptr, type, initial_values)
+ @ptr = ptr
+ @type = type
+ @align = PackInfo::ALIGN_MAP[type]
+ @size = Fiddle::PackInfo::SIZE_MAP[type]
+ @pack_format = Fiddle::PackInfo::PACK_MAP[type]
+ super(initial_values.collect { |v| unsigned_value(v, type) })
+ end
+
+ def to_ptr
+ @ptr
+ end
+
+ def []=(index, value)
+ if index < 0 || index >= size
+ raise IndexError, 'index %d outside of array bounds 0...%d' % [index, size]
+ end
+
+ to_ptr[index * @size, @size] = [value].pack(@pack_format)
+ super(index, value)
+ end
+ end
+
# Used to construct C classes (CUnion, CStruct, etc)
#
# Fiddle::Importer#struct and Fiddle::Importer#union wrap this functionality in an
@@ -191,7 +218,7 @@ module Fiddle
if( ty.is_a?(Integer) && (ty < 0) )
return unsigned_value(val, ty)
elsif( ty.is_a?(Array) && (ty[0] < 0) )
- return val.collect{|v| unsigned_value(v,ty[0])}
+ return StructArray.new(self + @offset[idx], ty[0], val)
else
return val
end