<p>I've been trying to understand how Ruby blocks work, and to do that I've been trying to implement them in C.</p>

<p>One easy way to implement closures is to pass a <code>void*</code> to the enclosing stack to the closure/function but Ruby blocks also seem to handle returns and break statements from the scope that uses the block.</p>

<pre><code>loop do
  break i if (i &gt;= 4000)
  i *= 2
end
</code></pre>

<p>I think one of the closures proposals for Java works like this also.</p>

<p>So, how would you implement Ruby-blocks/Java-closures in C?</p>

[{'answer_id': 20154, 'author': 'wvdschel', 'author_id': 2018, 'author_profile': 'https://Stackoverflow.com/users/2018', 'pm_score': 4, 'selected': True, 'text': "<p>The concept of closures requires the concept of contexts. C's context is based on the stack and the registers of the CPU, so to create a block/closure, you need to be able to manipulate the stack pointer in a correct (and reentrant) way, and store/restore registers as needed.</p>\n\n<p>The way this is done by interpreters or virtual machines is to have a <code>context</code> structure or something similar, and not use the stack and registers directly. This structure keeps track of a stack and optionally some registers, if you're designing a register based VM. At least, that's the simplest way to do it (though slightly less performant than actually mapping things correctly).</p>\n"}, {'answer_id': 22164, 'author': 'Andrew Harmel-Law', 'author_id': 2455, 'author_profile': 'https://Stackoverflow.com/users/2455', 'pm_score': 2, 'selected': False, 'text': '<p>There\'s a good set of slides on Ruby Blocks as part of the "Rails with Passion" course:</p>\n\n<p><a href="http://www.javapassion.com/rubyonrails/ruby_blocks.pdf" rel="nofollow noreferrer">Ruby_Blocks.pdf</a></p>\n\n<p>This covers representing a block, how they get passed arguments and executed, and even further into things like Proc objects.  It\'s very clearly explained.</p>\n\n<p>It might then be of interest to look at how the JRuby guys handled these in their parsing to Java.  Take a look at the source at <a href="http://svn.codehaus.org/jruby/" rel="nofollow noreferrer">codehaus</a>.</p>\n'}, {'answer_id': 38105, 'author': 'Pramod', 'author_id': 1386292, 'author_profile': 'https://Stackoverflow.com/users/1386292', 'pm_score': 2, 'selected': False, 'text': '<p>I haven\'t actually implemented any of this, so take it with a sack of salt.</p>\n\n<p>There are two parts to a closure: the data environment and the code environment. Like you said, you can probably pass a void* to handle references to data. You could probably use setjmp and longjmp to implement the non-linear control flow jumps that the Ruby break requires. </p>\n\n<p>If you want closures you should probably be programming in a language that actually supports them. :-)</p>\n\n<p>UPDATE: Interesting things are happening in Clang. They\'ve prototyped a closure for C. <a href="http://lists.cs.uiuc.edu/pipermail/cfe-dev/2008-August/002670.html" rel="nofollow noreferrer">http://lists.cs.uiuc.edu/pipermail/cfe-dev/2008-August/002670.html</a> might prove to be interesting reading.</p>\n'}]
['https://Stackoverflow.com/questions/19838', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2148/']