mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
dccd66f9ef
The Difference Since Ruby1.9, the keyword arguments were emulated by Ruby using the hash object at the bottom of the arguments. But we have gradually moved toward keyword arguments separated from normal (positinal) arguments. At the same time, we value compatibility, so that Ruby3.0 keyword arguments are somewhat compromise. Basically, keyword arguments are separated from positional arguments, except when the method does not take any formal keyword arguments, given keyword arguments (packed in the hash object) are considered as the last argument. And we also allow non symbol keys in the keyword arguments. In that case, those keys are just passed in the `**` hash (or raise `ArgumentError` for unknown keys). The Instruction Changes We have changed `OP_SEND` instruction. `OP_SEND` instruction used to take 3 operands, the register, the symbol, the number of (positional) arguments. The meaning of the third operand has been changed. It is now considered as `n|(nk<<4)`, where `n` is the number of positional arguments, and `nk` is the number of keyword arguments, both occupies 4 bits in the operand. The number `15` in both `n` and `nk` means variable sized arguments are packed in the object. Positional arguments will be packed in the array, and keyword arguments will be packed in the hash object. That means arguments more than 14 values are always packed in the object. Arguments information for other instructions (`OP_SENDB` and `OP_SUPER`) are also changed. It works as the third operand of `OP_SEND`. the difference between `OP_SEND` and `OP_SENDB` is just trivial. It assigns `nil` to the block hidden arguments (right after arguments). The instruction `OP_SENDV` and `OP_SENDVB` are removed. Those instructions are replaced by `OP_SEND` and `OP_SENDB` respectively with the `15` (variable sized) argument information. Calling Convention When calling a method, the stack elements shall be in the order of the receiver of the method, positional arguments, keyword arguments and the block argument. If the number of positional or keyword arugument (`n` or `nk`) is zero, corresponding arguments will be empty. So when `n=0` and `nk=0` the stack layout (from bottom to top) will be: +-----------------------+ | recv | block (or nil) | +-----------------------+ The last elements `block` should be explicitly filled before `OP_SEND` or assigned to `nil` by `OP_SENDB` internally. In other words, the following have exactly same behavior: OP_SENDB clears `block` implicitly: ``` OP_SENDB reg sym 0 ``` OP_SEND clears `block` implicitly: ``` OP_LOADNIL R2 OP_SEND R2 sym 0 ``` When calling a method with only positional arguments (n=0..14) without keyword arguments, the stack layout will be like following: +--------------------------------------------+ | recv | arg1 | ... | arg_n | block (or nil) | +--------------------------------------------+ When calling a method with arguments packed in the array (n=15) which means argument splat (*) is used in the actual arguments, or more than 14 arguments are passed the stack layout will be like following: +-------------------------------+ | recv | array | block (or nil) | +-------------------------------+ The number of the actual arguments is determined by the length of the argument array. When keyword arguments are given (nk>0), keyword arguments are passed between positional arguments and the block argument. For example, when we pass one positional argument `1` and one keyword argument `a: 2`, the stack layout will be like: +------------------------------------+ | recv | 1 | :a | 2 | block (or nil) | +------------------------------------+ Note that keyword arguments consume `2*nk` elements in the stack when `nk=0..14` (unpacked). When calling a method with keyword arguments packed in the hash object (nk=15) which means keyword argument splat (**) is used or more than 14 keyword arguments in the actual arguments, the stack layout will be like: +------------------------------+ | recv | hash | block (or nil) | +------------------------------+ Note for mruby/c When mruby/c authors try to support new keyword arguments, they need to handle the new meaning of the argument information operand. If they choose not to support keyword arguments in mruby/c, it just raise error when `nk` (taken by `(c>>4)&0xf`) is not zero. And combine `OP_SENDV` behavior with `OP_SEND` when `n` is `15`. If they want to support keyword arguments seriously, contact me at <matz@ruby.or.jp> or `@yukihiro_matz`. I can help you.
377 lines
6.2 KiB
Ruby
377 lines
6.2 KiB
Ruby
##
|
|
# IO
|
|
|
|
class IOError < StandardError; end
|
|
class EOFError < IOError; end
|
|
|
|
class IO
|
|
SEEK_SET = 0
|
|
SEEK_CUR = 1
|
|
SEEK_END = 2
|
|
|
|
BUF_SIZE = 4096
|
|
|
|
def self.open(*args, &block)
|
|
io = self.new(*args)
|
|
|
|
return io unless block
|
|
|
|
begin
|
|
yield io
|
|
ensure
|
|
begin
|
|
io.close unless io.closed?
|
|
rescue StandardError
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.popen(command, mode = 'r', **opts, &block)
|
|
if !self.respond_to?(:_popen)
|
|
raise NotImplementedError, "popen is not supported on this platform"
|
|
end
|
|
io = self._popen(command, mode, **opts)
|
|
return io unless block
|
|
|
|
begin
|
|
yield io
|
|
ensure
|
|
begin
|
|
io.close unless io.closed?
|
|
rescue IOError
|
|
# nothing
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.pipe(&block)
|
|
if !self.respond_to?(:_pipe)
|
|
raise NotImplementedError, "pipe is not supported on this platform"
|
|
end
|
|
if block
|
|
begin
|
|
r, w = IO._pipe
|
|
yield r, w
|
|
ensure
|
|
r.close unless r.closed?
|
|
w.close unless w.closed?
|
|
end
|
|
else
|
|
IO._pipe
|
|
end
|
|
end
|
|
|
|
def self.read(path, length=nil, offset=0, mode: "r")
|
|
str = ""
|
|
fd = -1
|
|
io = nil
|
|
begin
|
|
if path[0] == "|"
|
|
io = IO.popen(path[1..-1], mode)
|
|
else
|
|
fd = IO.sysopen(path, mode)
|
|
io = IO.open(fd, mode)
|
|
end
|
|
io.seek(offset) if offset > 0
|
|
str = io.read(length)
|
|
ensure
|
|
if io
|
|
io.close
|
|
elsif fd != -1
|
|
IO._sysclose(fd)
|
|
end
|
|
end
|
|
str
|
|
end
|
|
|
|
def flush
|
|
# mruby-io always writes immediately (no output buffer).
|
|
raise IOError, "closed stream" if self.closed?
|
|
self
|
|
end
|
|
|
|
def hash
|
|
# We must define IO#hash here because IO includes Enumerable and
|
|
# Enumerable#hash will call IO#read...
|
|
self.__id__
|
|
end
|
|
|
|
def write(string)
|
|
str = string.is_a?(String) ? string : string.to_s
|
|
return 0 if str.empty?
|
|
unless @buf.empty?
|
|
# reset real pos ignore buf
|
|
seek(pos, SEEK_SET)
|
|
end
|
|
len = syswrite(str)
|
|
len
|
|
end
|
|
|
|
def <<(str)
|
|
write(str)
|
|
self
|
|
end
|
|
|
|
def eof?
|
|
_check_readable
|
|
begin
|
|
_read_buf
|
|
return @buf.empty?
|
|
rescue EOFError
|
|
return true
|
|
end
|
|
end
|
|
alias_method :eof, :eof?
|
|
|
|
def pos
|
|
raise IOError if closed?
|
|
sysseek(0, SEEK_CUR) - @buf.bytesize
|
|
end
|
|
alias_method :tell, :pos
|
|
|
|
def pos=(i)
|
|
seek(i, SEEK_SET)
|
|
end
|
|
|
|
def rewind
|
|
seek(0, SEEK_SET)
|
|
end
|
|
|
|
def seek(i, whence = SEEK_SET)
|
|
raise IOError if closed?
|
|
sysseek(i, whence)
|
|
@buf = ''
|
|
0
|
|
end
|
|
|
|
def _read_buf
|
|
return @buf if @buf && @buf.bytesize > 0
|
|
sysread(BUF_SIZE, @buf)
|
|
end
|
|
|
|
def ungetc(substr)
|
|
raise TypeError.new "expect String, got #{substr.class}" unless substr.is_a?(String)
|
|
if @buf.empty?
|
|
@buf.replace(substr)
|
|
else
|
|
@buf[0,0] = substr
|
|
end
|
|
nil
|
|
end
|
|
|
|
def ungetbyte(c)
|
|
if c.is_a? String
|
|
c = c.getbyte(0)
|
|
else
|
|
c &= 0xff
|
|
end
|
|
s = " "
|
|
s.setbyte(0,c)
|
|
ungetc s
|
|
end
|
|
|
|
def read(length = nil, outbuf = "")
|
|
unless length.nil?
|
|
unless length.is_a? Integer
|
|
raise TypeError.new "can't convert #{length.class} into Integer"
|
|
end
|
|
if length < 0
|
|
raise ArgumentError.new "negative length: #{length} given"
|
|
end
|
|
if length == 0
|
|
return "" # easy case
|
|
end
|
|
end
|
|
|
|
array = []
|
|
while true
|
|
begin
|
|
_read_buf
|
|
rescue EOFError
|
|
array = nil if array.empty? and (not length.nil?) and length != 0
|
|
break
|
|
end
|
|
|
|
if length
|
|
consume = (length <= @buf.bytesize) ? length : @buf.bytesize
|
|
array.push IO._bufread(@buf, consume)
|
|
length -= consume
|
|
break if length == 0
|
|
else
|
|
array.push @buf
|
|
@buf = ''
|
|
end
|
|
end
|
|
|
|
if array.nil?
|
|
outbuf.replace("")
|
|
nil
|
|
else
|
|
outbuf.replace(array.join)
|
|
end
|
|
end
|
|
|
|
def readline(arg = "\n", limit = nil)
|
|
case arg
|
|
when String
|
|
rs = arg
|
|
when Integer
|
|
rs = "\n"
|
|
limit = arg
|
|
else
|
|
raise ArgumentError
|
|
end
|
|
|
|
if rs.nil?
|
|
return read
|
|
end
|
|
|
|
if rs == ""
|
|
rs = "\n\n"
|
|
end
|
|
|
|
array = []
|
|
while true
|
|
begin
|
|
_read_buf
|
|
rescue EOFError
|
|
array = nil if array.empty?
|
|
break
|
|
end
|
|
|
|
if limit && limit <= @buf.size
|
|
array.push @buf[0, limit]
|
|
@buf[0, limit] = ""
|
|
break
|
|
elsif idx = @buf.index(rs)
|
|
len = idx + rs.size
|
|
array.push @buf[0, len]
|
|
@buf[0, len] = ""
|
|
break
|
|
else
|
|
array.push @buf
|
|
@buf = ''
|
|
end
|
|
end
|
|
|
|
raise EOFError.new "end of file reached" if array.nil?
|
|
|
|
array.join
|
|
end
|
|
|
|
def gets(*args)
|
|
begin
|
|
readline(*args)
|
|
rescue EOFError
|
|
nil
|
|
end
|
|
end
|
|
|
|
def readchar
|
|
_read_buf
|
|
_readchar(@buf)
|
|
end
|
|
|
|
def getc
|
|
begin
|
|
readchar
|
|
rescue EOFError
|
|
nil
|
|
end
|
|
end
|
|
|
|
def readbyte
|
|
_read_buf
|
|
IO._bufread(@buf, 1).getbyte(0)
|
|
end
|
|
|
|
def getbyte
|
|
readbyte
|
|
rescue EOFError
|
|
nil
|
|
end
|
|
|
|
# 15.2.20.5.3
|
|
def each(&block)
|
|
return to_enum unless block
|
|
|
|
while line = self.gets
|
|
block.call(line)
|
|
end
|
|
self
|
|
end
|
|
|
|
# 15.2.20.5.4
|
|
def each_byte(&block)
|
|
return to_enum(:each_byte) unless block
|
|
|
|
while byte = self.getbyte
|
|
block.call(byte)
|
|
end
|
|
self
|
|
end
|
|
|
|
# 15.2.20.5.5
|
|
alias each_line each
|
|
|
|
def each_char(&block)
|
|
return to_enum(:each_char) unless block
|
|
|
|
while char = self.getc
|
|
block.call(char)
|
|
end
|
|
self
|
|
end
|
|
|
|
def readlines
|
|
ary = []
|
|
while (line = gets)
|
|
ary << line
|
|
end
|
|
ary
|
|
end
|
|
|
|
def puts(*args)
|
|
i = 0
|
|
len = args.size
|
|
while i < len
|
|
s = args[i]
|
|
if s.kind_of?(Array)
|
|
puts(*s)
|
|
else
|
|
s = s.to_s
|
|
write s
|
|
write "\n" if (s[-1] != "\n")
|
|
end
|
|
i += 1
|
|
end
|
|
write "\n" if len == 0
|
|
nil
|
|
end
|
|
|
|
def print(*args)
|
|
i = 0
|
|
len = args.size
|
|
while i < len
|
|
write args[i].to_s
|
|
i += 1
|
|
end
|
|
end
|
|
|
|
def printf(*args)
|
|
write sprintf(*args)
|
|
nil
|
|
end
|
|
|
|
alias_method :to_i, :fileno
|
|
alias_method :tty?, :isatty
|
|
end
|
|
|
|
STDIN = IO.open(0, "r")
|
|
STDOUT = IO.open(1, "w")
|
|
STDERR = IO.open(2, "w")
|
|
|
|
$stdin = STDIN
|
|
$stdout = STDOUT
|
|
$stderr = STDERR
|