diff --git a/ext/ffi_c/AbstractMemory.c b/ext/ffi_c/AbstractMemory.c index 758f2d882..30d96fce4 100644 --- a/ext/ffi_c/AbstractMemory.c +++ b/ext/ffi_c/AbstractMemory.c @@ -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); diff --git a/sig/ffi/abstract_memory.rbs b/sig/ffi/abstract_memory.rbs index f21fe1b49..9678ad01a 100644 --- a/sig/ffi/abstract_memory.rbs +++ b/sig/ffi/abstract_memory.rbs @@ -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 diff --git a/spec/ffi/string_spec.rb b/spec/ffi/string_spec.rb index 5b168593a..657d6ec3f 100644 --- a/spec/ffi/string_spec.rb +++ b/spec/ffi/string_spec.rb @@ -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 @@ -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 @@ -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