-
-
Save jemc/e8c97ca581e82c65884a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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