Guest User

Untitled

a guest
Jun 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. require "fiddle/import"
  2. class RubyVM::InstructionSequence
  3. module InternalFunction
  4. extend Fiddle::Importer
  5. dlload Fiddle::Handle::DEFAULT
  6.  
  7. if Fiddle::SIZEOF_LONG == Fiddle::SIZEOF_VOIDP
  8. typealias "VALUE", "unsigned long"
  9. elsif Fiddle::SIZEOF_LONG_LONG == Fiddle::SIZEOF_VOIDP
  10. typealias "VALUE", "unsigned long long"
  11. end
  12. extern "const rb_iseq_t *rb_iseqw_to_iseq(VALUE iseqw)"
  13. begin
  14. extern "void rb_iseq_code_location(const rb_iseq_t *iseq, int *first_lineno, int *first_column, int *last_lineno, int *last_column)"
  15. rescue Fiddle::DLError
  16. extern "void rb_iseq_code_range(const rb_iseq_t *iseq, int *first_lineno, int *first_column, int *last_lineno, int *last_column)"
  17. singleton_class.alias_method :rb_iseq_code_location, :rb_iseq_code_range
  18. end
  19. end
  20.  
  21. def first_column
  22. code_locations[1]
  23. end
  24.  
  25. def last_lineno
  26. code_locations[2]
  27. end
  28.  
  29. def last_column
  30. code_locations[3]
  31. end
  32.  
  33. def code_locations
  34. size = Fiddle::SIZEOF_INT
  35. buf = "\0" * (size * 4)
  36. ptr = Fiddle::Pointer[buf]
  37.  
  38. iseq = InternalFunction.rb_iseqw_to_iseq(Fiddle.dlwrap(self))
  39. InternalFunction.rb_iseq_code_location(iseq, ptr, ptr + size, ptr + size * 2, ptr + size * 3)
  40. buf.unpack("i4")
  41. end
  42. end
  43.  
  44. if $0 == __FILE__
  45. b1 = ->(){ foo() }; a = 1; b2 = ->(){ bar() }
  46. p RubyVM::InstructionSequence.of(b1).code_locations
  47. p RubyVM::InstructionSequence.of(b2).yield_self {|iseq| [iseq.first_lineno, iseq.first_column, iseq.last_lineno, iseq.last_column] }
  48. end
Add Comment
Please, Sign In to add comment