Brian's Waste of Time

Wed, 25 Feb 2004

Stack Frames as First Class Objects

I had a fun idea chatting in #groovy -- make stack frames first class objects and accessible in the runtime context. Allow the twiddling of variables in the stack frames, allow the setting of a continuation in place of the continuation waiting for the return from the stack.

Some pseudo-ruby-java-code for concepts:


def functionOne()
    binding = Thread.current.frame.parent.binding()
    binding["foo"] = "goodbye"
end

foo = "hello"
functionOne()
puts foo

--> "goodbye"

Taking it a step further:



def functionTwo()
    puts "au revoir"
end

def functionThree()
    cont = new Continuation(functionTwo)
    old_next = Thread.current.frame.parent.replaceNext(cont)
    binding["foo"] = "goodbye"
end

foo = "hello"
functionThree()
puts foo

--> "au revoir"

I suspect stackless python does something like this now, where it just keeps a continuation waiting for the function return, not a real stack frame -- or more likely, plugs a call to the continuation in place of the "return" statement, with the returned value as an argument. I have only heard of stackless python though, so no clue if that is what they actually do. If they don't do something like this, it could be fun to play with.

This runtime-hackery is probably far worse than globals or GOTO, but the ideas are still pretty cool.

1 writebacks [/src] permanent link