Skip to content

Commit 625fb0e

Browse files
committed
Add support for polyglot IO.
1 parent 3d785ef commit 625fb0e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/main/ruby/truffleruby/core/posix.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,26 @@ def self.read_to_buffer_native(io, length)
444444
end
445445
end
446446

447-
def self.read_to_buffer_polyglot(io, count)
448-
raise RuntimeError, 'Not supported yet.'
447+
def self.read_to_buffer_polyglot(io, length, &block)
448+
fd = io.fileno
449+
if fd == 0
450+
buffer = Primitive.io_thread_buffer_allocate(length)
451+
begin
452+
read = Primitive.io_read_polyglot length
453+
if read
454+
bytes_read = read.bytesize
455+
buffer.write_string_length(read, bytes_read)
456+
yield buffer, bytes_read
457+
[bytes_read, 0]
458+
else
459+
[0, 0]
460+
end
461+
ensure
462+
Primitive.io_thread_buffer_free(buffer)
463+
end
464+
else
465+
read_to_buffer_native(io, length, &block)
466+
end
449467
end
450468

451469
def self.read_string_polyglot(io, length)

0 commit comments

Comments
 (0)