<p>I'm designing a language. First, I want to decide what code to generate. The language will have lexical closures and prototype based inheritance similar to javascript. But I'm not a fan of gc and try to avoid as much as possible. So the question: Is there an elegant way to implement closures without resorting to allocate the stack frame on the heap and leave it to garbage collector?</p>

<p>My first thoughts:</p>

<ol>
<li>Use reference counting and garbage collect the cycles (I don't really like this)</li>
<li>Use spaghetti stack (looks very inefficient)</li>
<li>Limit forming of closures to some contexts such a way that, I can get away with a return address stack and a locals' stack.</li>
</ol>

<p>I won't use a high level language or follow any call conventions, so I can smash the stack as much as I like.</p>

<p>(Edit: I know reference counting is a form of garbage collection but I am using gc in its more common meaning)</p>

This would be a better question if you can explain what you're trying to avoid by not using GC. As I'm sure you're aware, most languages that provide lexical closures allocate them on the heap and allow them to retain references to variable bindings in the activation record that created them.
The only alternative to that approach that I'm aware of is what
```
gcc
```
uses for nested functions: create a trampoline for the function and allocate it on the stack. But as the gcc manual says:
If you try to call the nested function through its address after the containing function has exited, all hell will break loose. If you try to call it after a containing scope level has exited, and if it refers to some of the variables that are no longer in scope, you may be lucky, but it's not wise to take the risk. If, however, the nested function does not refer to anything that has gone out of scope, you should be safe.
Short version is, you have three main choices:
allocate closures on the stack, and don't allow their use after their containing function exits.
allocate closures on the heap, and use garbage collection of some kind.
do original research, maybe starting from the region stuff that ML, Cyclone, etc. have.