Files
mruby-mruby/mrbgems/mruby-struct/test/struct.rb
T
Yukihiro "Matz" Matsumoto dccd66f9ef Support Ruby3.0 keyword arguments.
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.
2021-10-12 20:16:36 +09:00

211 lines
4.8 KiB
Ruby

##
# Struct ISO Test
assert('Struct', '15.2.18') do
assert_equal Class, Struct.class
end
assert('Struct.new', '15.2.18.3.1') do
c = Struct.new(:m1, :m2)
assert_equal Struct, c.superclass
assert_equal [:m1, :m2], c.members
end
assert('Struct#==', '15.2.18.4.1') do
c = Struct.new(:m1, :m2)
cc1 = c.new(1,2)
cc2 = c.new(1,2)
assert_true cc1 == cc2
Struct.new(:m1, :m2) { def foo; end }
assert_raise(NoMethodError) { Struct.new(:m1).new.foo }
end
assert('Struct#[]', '15.2.18.4.2') do
c = Struct.new(:m1, :m2)
cc = c.new(1,2)
assert_equal 1, cc[:m1]
assert_equal 2, cc["m2"]
assert_equal 1, cc[0]
assert_equal 2, cc[-1]
assert_raise(TypeError) { cc[[]] }
assert_raise(IndexError) { cc[2] }
assert_raise(NameError) { cc['tama'] }
end
assert('Struct#[]=', '15.2.18.4.3') do
c = Struct.new(:m1, :m2)
cc = c.new(1,2)
cc[:m1] = 3
assert_equal 3, cc[:m1]
cc["m2"] = 3
assert_equal 3, cc["m2"]
cc[0] = 4
assert_equal 4, cc[0]
cc[-1] = 5
assert_equal 5, cc[-1]
assert_raise(TypeError) { cc[[]] = 3 }
assert_raise(IndexError) { cc[2] = 7 }
assert_raise(NameError) { cc['pochi'] = 8 }
end
assert('Struct#each', '15.2.18.4.4') do
c = Struct.new(:m1, :m2)
cc = c.new(1,2)
a = []
cc.each{|x|
a << x
}
assert_equal [1, 2], a
end
assert('Struct#each_pair', '15.2.18.4.5') do
c = Struct.new(:m1, :m2)
cc = c.new(1,2)
a = []
cc.each_pair{|k,v|
a << [k,v]
}
assert_equal [[:m1, 1], [:m2, 2]], a
end
assert('Struct#members', '15.2.18.4.6') do
c = Struct.new(:m1, :m2)
assert_equal [:m1, :m2], c.new(1,2).members
end
assert('Struct#select', '15.2.18.4.7') do
c = Struct.new(:m1, :m2)
assert_equal([2]) { c.new(1,2).select{|v| v % 2 == 0} }
end
assert('large struct') do
c = Struct.new(:m1, :m2, :m3, :m4, :m5, :m6, :m7, :m8, :m9, :m10, :m11, :m12, :m13)
cc = c.new(1,2,3,4,5,6,7,8,9,10,11,12,13)
assert_equal 1, cc.m1
assert_equal 2, cc.m2
assert_equal 3, cc.m3
assert_equal 4, cc.m4
assert_equal 5, cc.m5
assert_equal 6, cc.m6
assert_equal 7, cc.m7
assert_equal 8, cc.m8
assert_equal 9, cc.m9
assert_equal 10, cc.m10
assert_equal 13, cc.m13
cc.m13 = 'test'
assert_equal 'test', cc.m13
assert_raise(NoMethodError) { cc.m14 }
end
assert('wrong struct arg count') do
c = Struct.new(:m1)
assert_raise ArgumentError do
cc = c.new(1,2,3)
end
end
assert('struct dup') do
c = Struct.new(:m1, :m2, :m3, :m4, :m5)
cc = c.new(1,2,3,4,5)
assert_nothing_raised {
assert_equal(cc, cc.dup)
}
end
assert('struct inspect') do
c = Struct.new(:m1, :m2, :m3, :m4, :m5, :recur)
cc = c.new(1,2,3,4,5,nil)
cc.recur = cc
assert_equal "#<struct m1=1, m2=2, m3=3, m4=4, m5=5, recur=#<struct #{cc.class}:...>>", cc.inspect
end
assert('Struct#length, Struct#size') do
s = Struct.new(:f1, :f2).new(0, 1)
assert_equal 2, s.size
assert_equal 2, s.length
end
assert('Struct#to_a, Struct#values') do
s = Struct.new(:mem1, :mem2).new('a', 'b')
assert_equal ['a', 'b'], s.to_a
assert_equal ['a', 'b'], s.values
end
assert('Struct#to_h') do
s = Struct.new(:white, :red, :green).new('ruuko', 'yuzuki', 'hitoe')
assert_equal({:white => 'ruuko', :red => 'yuzuki', :green => 'hitoe'}) { s.to_h }
end
assert('Struct#values_at') do
a = Struct.new(:blue, :purple).new('aki', 'io')
assert_equal ['aki'], a.values_at(0)
assert_equal ['io', 'aki'], a.values_at(1, 0)
assert_raise(IndexError) { a.values_at 2 }
end
assert("Struct#dig") do
a = Struct.new(:blue, :purple).new('aki', Struct.new(:red).new(1))
assert_equal 'aki', a.dig(:blue)
assert_equal 1, a.dig(:purple, :red)
assert_equal 1, a.dig(1, 0)
end
# TODO: suppress redefining Struct warning during test
# assert("Struct.new removes existing constant") do
# begin
# assert_not_equal Struct.new("Test", :a), Struct.new("Test", :a, :b)
# ensure
# Struct.remove_const :Test
# end
# end
assert("Struct#initialize_copy requires struct to be the same type") do
begin
Struct.new("Test", :a)
a = Struct::Test.new("a")
Struct.remove_const :Test
Struct.new("Test", :a, :b)
assert_raise(TypeError) do
a.initialize_copy(Struct::Test.new("a", "b"))
end
ensure
Struct.remove_const :Test
end
end
assert("Struct.new does not allow array") do
assert_raise(TypeError) do
Struct.new("Test", [:a])
end
end
assert("Struct.new does not allow invalid class name") do
assert_raise(NameError) { Struct.new("Test-", :a) }
end
assert("Struct.new generates subclass of Struct") do
begin
original_struct = Struct
Struct = String
assert_equal original_struct, original_struct.new(:foo).superclass
ensure
Struct = original_struct
end
end
assert 'Struct#freeze' do
c = Struct.new :m
o = c.new
o.m = :test
assert_equal :test, o.m
o.freeze
assert_raise(FrozenError) { o.m = :modify }
assert_raise(FrozenError) { o[:m] = :modify }
assert_equal :test, o.m
end