Skip to content

Instantly share code, notes, and snippets.

@txus
Created June 13, 2011 14:47
Show Gist options
  • Save txus/1022892 to your computer and use it in GitHub Desktop.
Save txus/1022892 to your computer and use it in GitHub Desktop.
Rbx bytecode helpers - draft
class Rubinius::Generator
# Shows the topmost element on the stack and the stack's current size.
#
# Calling #show_stack neither consumes nor produces stack, it's just used
# for debugging the current state.
#
# @param [String] name an optional label to print out, to make debugging
# easier.
#
def show_stack(name = nil)
size = @current_block.instance_eval { @stack }
dup
push :self
swap_stack
send :inspect, 0, true
push_literal "[#{name || 'DEBUG'}] [STACK SIZE: #{size}]: "
swap_stack
push_literal "\n"
send :print, 3, true
pop
end
end
class Foo
dynamic_method :bar do |g|
g.push :self
g.push_literal "hello"
g.show_stack 'just after pushed literal hello'
g.send :puts, 1, true
g.show_stack 'this should be nil and the stack size should be 1'
g.ret
end
end
Foo.new.bar
# prints:
#
# [just after pushed literal hello] [STACK SIZE: 2]: "hello"
# hello
# [this should be nil and the stack size should be 1] [STACK SIZE: 1]: nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment