MacRuby/MacRuby — pull request #91 by macrotis (2012-05-25 23:55:46+00:00)
Title: Use blocks for attribute accessors in Struct to facilitate proper aliasing behavior

Consider the following code:

```ruby
class Large < Struct.new(:one, :two, :three, :four, :five, :six, :seven, :eight, :nine, :ten, :eleven)
alias :o :one
alias :o= :one=
alias :l :eleven
alias :l= :eleven=
end

l = Large.new
['o', 'l'].each do |s|
['', '='].each do |t|
begin
m = s + t
if t != '=' then
l.send(m.to_sym)
else
l.send(m.to_sym, 4)
end
puts "SUCCESS: alias #{m} calls the attribute accessor"
rescue
puts "FAILURE: alias #{m} does not call the attribute accessor"
end
end
end
```

The expected output:
```
SUCCESS: alias o calls the attribute accessor
SUCCESS: alias o= calls the attribute accessor
SUCCESS: alias l calls the attribute accessor
SUCCESS: alias l= calls the attribute accessor
```

The output as of 69434c940c75c66020d68da1cea93ce9ceb3a672 and release 0.11:
```
SUCCESS: alias o calls the attribute accessor
FAILURE: alias o= does not call the attribute accessor
FAILURE: alias l does not call the attribute accessor
FAILURE: alias l= does not call the attribute accessor
```

That is, no alias to an attribute writer of a class derived from Struct will function (attempting to send it a message yields a `NameError`), and only aliases to attribute readers of the first ten members work properly (similarly, any other attempt to access a member through an alias yields a `NameError`).

These changes re-implement the Struct attribute accessor methods as blocks in `struct.c`. Each block is generated in the scope of the for-loop which adds accessor methods to the Objective-C runtime, saving the offset of the member in the block's own scope. The generation of compatible Objective-C method implementations from blocks was officially introduced to OS X in Lion, but by using landonf/plblockimp@372da0b6bbf20ebcd996bc626cc3711bbe335ec7 it should work on Snow Leopard as well.

I've only been able to test this on Lion, but it does pass all the same tests as 69434c940c75c66020d68da1cea93ce9ceb3a672 while fixing the issue.