Skip to content

Commit

Permalink
Add FFI::AbstractMemory#read_array_of_string
Browse files Browse the repository at this point in the history
It was defined but not exposed to Ruby nor tested.

Fixes #1070
  • Loading branch information
larskanis authored and eregon committed Apr 24, 2024
1 parent 769a4bd commit 46c3804
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions ext/ffi_c/AbstractMemory.c
Expand Up @@ -1109,6 +1109,7 @@ rbffi_AbstractMemory_Init(VALUE moduleFFI)
rb_define_method(classMemory, "read_bytes", memory_read_bytes, 1);
rb_define_method(classMemory, "write_bytes", memory_write_bytes, -1);
rb_define_method(classMemory, "get_array_of_string", memory_get_array_of_string, -1);
rb_define_method(classMemory, "read_array_of_string", memory_read_array_of_string, -1);

rb_define_method(classMemory, "get", memory_get, 2);
rb_define_method(classMemory, "put", memory_put, 3);
Expand Down
1 change: 1 addition & 0 deletions sig/ffi/abstract_memory.rbs
Expand Up @@ -144,6 +144,7 @@ module FFI
def read_array_of_float: (Integer length) -> Array[Float]
def read_array_of_double: (Integer length) -> Array[Float]
def read_array_of_pointer: (Integer length) -> Array[Pointer]
def read_array_of_string: (?Integer? count) -> Array[String?]

def write_array_of_int8: (Array[int] ary) -> self
def write_array_of_int16: (Array[int] ary) -> self
Expand Down
3 changes: 3 additions & 0 deletions spec/ffi/string_spec.rb
Expand Up @@ -48,6 +48,7 @@ module StrLibTest
ary.insert(3, nil)
ptrary.write_array_of_pointer(ary)
expect(ptrary.get_array_of_string(0)).to eq(["foo", "bar", "baz"])
expect(ptrary.read_array_of_string).to eq(["foo", "bar", "baz"])
end

it "reads an array of strings of the size specified, substituting nil when a pointer is NULL" do
Expand All @@ -61,6 +62,7 @@ module StrLibTest
ary.insert(2, nil)
ptrary.write_array_of_pointer(ary)
expect(ptrary.get_array_of_string(0, 4)).to eq(["foo", "bar", nil, "baz"])
expect(ptrary.read_array_of_string(4)).to eq(["foo", "bar", nil, "baz"])
end

it "reads an array of strings, taking a memory offset parameter" do
Expand All @@ -85,6 +87,7 @@ module StrLibTest
end
ptrary.write_array_of_pointer(ary)
expect { ptrary.get_array_of_string(0, 6) }.to raise_error(IndexError)
expect { ptrary.read_array_of_string(6) }.to raise_error(IndexError)
end

it "raises an IndexError when trying to read an array of strings using a negative offset" do
Expand Down

0 comments on commit 46c3804

Please sign in to comment.