Skip to content

Instantly share code, notes, and snippets.

@kddnewton
Created November 28, 2022 21:50
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 kddnewton/048f91dae02fa89db4da5c5d87bb2ab3 to your computer and use it in GitHub Desktop.
Save kddnewton/048f91dae02fa89db4da5c5d87bb2ab3 to your computer and use it in GitHub Desktop.
Count the number of YARV instructions in source
def count(iseq)
iseq[12].select { _1 in [:ensure | :rescue, *] }.sum { count(_1[1]) } +
iseq[13].sum do
case _1
in Integer | Symbol
0
in [*, ["YARVInstructionSequence/SimpleDataFormat", *] => child, *]
1 + count(child)
in Array
1
end
end
end
p count(RubyVM::InstructionSequence.compile(ARGF.read).to_a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment