Skip to content

Instantly share code, notes, and snippets.

@jemc
Created December 9, 2014 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jemc/e8c97ca581e82c65884a to your computer and use it in GitHub Desktop.
Save jemc/e8c97ca581e82c65884a to your computer and use it in GitHub Desktop.
module BytecodeHelpers
def g
self
end
def puts_string string
g.push_rubinius
g.push_literal(string)
g.send(:puts, 1, true)
g.pop
end
def inspect_top
g.dup_top
g.push_rubinius
g.swap
g.send(:p, 1, true)
g.pop
end
end
class Demo
dynamic_method(:jit_debauchery_check, "(snippet)") { |g|
g.extend BytecodeHelpers
done_label = g.new_label
g.push_literal("a string")
g.push_int("a string".size)
g.send(:chr_at, 1) # This should ALWAYS be nil
g.dup_top
g.goto_if_nil(done_label) # This should ALWAYS goto
# This code should NEVER be reached
# If reached, it should raise PrimitiveFailure because the first arg is nil
# instead of the expected string
g.push_literal("abc")
g.swap # The stack top is now our nil from before
g.meta_push_0
g.send(:find_string, 2)
done_label.set!
g.ret
g.close
}
end
demo = Demo.new
puts "Let's try it one time..."
demo.jit_debauchery_check
puts 'it "worked" (skipped the find_string call)!'
puts
puts "Let's try it a second time..."
demo.jit_debauchery_check
puts 'it "worked" (skipped the find_string call)!'
puts
puts "Let's try it many times until PrimitiveFailure is raised..."
while true
demo.jit_debauchery_check
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment