Skip to content

Commit

Permalink
Fix IRB example (close #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Feb 28, 2023
1 parent f5549c3 commit f12978e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -12,7 +12,7 @@ task :recompile => [:clean, :compile]
task :default => [:compile, :test]

task :test do
exec 'ruby test/run.rb --verbose'
exec 'ruby test/run.rb'
end

task :stress_test do
Expand Down
44 changes: 2 additions & 42 deletions lib/polyphony/adapters/irb.rb
Expand Up @@ -2,46 +2,6 @@

require 'polyphony'

if Object.constants.include?(:Reline)
puts "reline"
class Reline::ANSI
def self.select(read_ios = [], write_ios = [], error_ios = [], timeout = nil)
# p [:select, read_ios, timeout]
# puts caller.join("\n")
raise if read_ios.size > 1
raise if write_ios.size > 0
raise if error_ios.size > 0

# p 1
fiber = Fiber.current
timer = spin do
sleep timeout
fiber.cancel
end
# p 2
read_ios.each do |io|
# p wait: io
Polyphony.backend_wait_io(io, false)
# p :done_wait
return [io]
end
# p 3
rescue Polyphony::Cancel
# p :cancel
return nil
ensure
# p :ensure
timer.stop
# p :ensure_done
end
end
else
require_relative './readline'

# RubyLex patches
class ::RubyLex
class TerminateLineInput2 < RuntimeError
end
const_set(:TerminateLineInput, TerminateLineInput2)
end
module Kernel
alias_method :gets, :orig_gets
end
12 changes: 12 additions & 0 deletions lib/polyphony/extensions/io.rb
Expand Up @@ -131,6 +131,16 @@ def getc
nil
end

def ungetc(c)
c = c.chr if c.is_a?(Integer)
if @read_buffer
@read_buffer.prepend(c)
else
@read_buffer = +c
end
end
alias_method :ungetbyte, :ungetc

alias_method :orig_read, :read
def read(len = nil, buf = nil, buf_pos = 0)
if buf
Expand Down Expand Up @@ -252,6 +262,8 @@ def feed_loop(receiver, method = :call, &block)
end

def wait_readable(timeout = nil)
return self if @read_buffer && @read_buffer.size > 0

if timeout
move_on_after(timeout) do
Polyphony.backend_wait_io(self, false)
Expand Down

0 comments on commit f12978e

Please sign in to comment.