|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +unless defined?(::TruffleRuby) |
| 4 | + require 'objspace.so' |
| 5 | +end |
| 6 | + |
| 7 | +module ObjectSpace |
| 8 | + class << self |
| 9 | + private :_dump |
| 10 | + private :_dump_all |
| 11 | + end |
| 12 | + |
| 13 | + module_function |
| 14 | + |
| 15 | + # call-seq: |
| 16 | + # ObjectSpace.dump(obj[, output: :string]) # => "{ ... }" |
| 17 | + # ObjectSpace.dump(obj, output: :file) # => #<File:/tmp/rubyobj20131125-88733-1xkfmpv.json> |
| 18 | + # ObjectSpace.dump(obj, output: :stdout) # => nil |
| 19 | + # |
| 20 | + # Dump the contents of a ruby object as JSON. |
| 21 | + # |
| 22 | + # This method is only expected to work with C Ruby. |
| 23 | + # This is an experimental method and is subject to change. |
| 24 | + # In particular, the function signature and output format are |
| 25 | + # not guaranteed to be compatible in future versions of ruby. |
| 26 | + def dump(obj, output: :string) |
| 27 | + out = case output |
| 28 | + when :file, nil |
| 29 | + require 'tempfile' |
| 30 | + Tempfile.create(%w(rubyobj .json)) |
| 31 | + when :stdout |
| 32 | + STDOUT |
| 33 | + when :string |
| 34 | + +'' |
| 35 | + when IO |
| 36 | + output |
| 37 | + else |
| 38 | + raise ArgumentError, "wrong output option: #{output.inspect}" |
| 39 | + end |
| 40 | + |
| 41 | + ret = _dump(obj, out) |
| 42 | + return nil if output == :stdout |
| 43 | + ret |
| 44 | + end |
| 45 | + |
| 46 | + |
| 47 | + # call-seq: |
| 48 | + # ObjectSpace.dump_all([output: :file]) # => #<File:/tmp/rubyheap20131125-88469-laoj3v.json> |
| 49 | + # ObjectSpace.dump_all(output: :stdout) # => nil |
| 50 | + # ObjectSpace.dump_all(output: :string) # => "{...}\n{...}\n..." |
| 51 | + # ObjectSpace.dump_all(output: |
| 52 | + # File.open('heap.json','w')) # => #<File:heap.json> |
| 53 | + # ObjectSpace.dump_all(output: :string, |
| 54 | + # since: 42) # => "{...}\n{...}\n..." |
| 55 | + # |
| 56 | + # Dump the contents of the ruby heap as JSON. |
| 57 | + # |
| 58 | + # _since_ must be a non-negative integer or +nil+. |
| 59 | + # |
| 60 | + # If _since_ is a positive integer, only objects of that generation and |
| 61 | + # newer generations are dumped. The current generation can be accessed using |
| 62 | + # GC::count. |
| 63 | + # |
| 64 | + # Objects that were allocated without object allocation tracing enabled |
| 65 | + # are ignored. See ::trace_object_allocations for more information and |
| 66 | + # examples. |
| 67 | + # |
| 68 | + # If _since_ is omitted or is +nil+, all objects are dumped. |
| 69 | + # |
| 70 | + # This method is only expected to work with C Ruby. |
| 71 | + # This is an experimental method and is subject to change. |
| 72 | + # In particular, the function signature and output format are |
| 73 | + # not guaranteed to be compatible in future versions of ruby. |
| 74 | + def dump_all(output: :file, full: false, since: nil) |
| 75 | + out = case output |
| 76 | + when :file, nil |
| 77 | + require 'tempfile' |
| 78 | + Tempfile.create(%w(rubyheap .json)) |
| 79 | + when :stdout |
| 80 | + STDOUT |
| 81 | + when :string |
| 82 | + +'' |
| 83 | + when IO |
| 84 | + output |
| 85 | + else |
| 86 | + raise ArgumentError, "wrong output option: #{output.inspect}" |
| 87 | + end |
| 88 | + |
| 89 | + ret = _dump_all(out, full, since) |
| 90 | + return nil if output == :stdout |
| 91 | + ret |
| 92 | + end |
| 93 | +end |
0 commit comments