From: "Eregon (Benoit Daloze)" Date: 2021-10-04T16:51:18+00:00 Subject: [ruby-core:105542] [Ruby master Feature#18231] `RubyVM.keep_script_lines` Issue #18231 has been updated by Eregon (Benoit Daloze). I clarified a bit with @ko1. First, `script_lines` returns all lines, not the lines of a specific method (I missed that in the description). The debugger use case is to show the code around a breakpoint. Adding something like `caller_locations(0)[0].script_lines` would work too, and it would be portable. @matz Could you consider if `Thread::Backtrace::Location#script_lines` is acceptable? This is easy to implement in other Ruby implementations, while going via ISeq is very hard, and would make the `debug` gem unusable on non-CRuby which would be unfortunate. Also, do we need an explicit `RubyVM.keep_script_lines = true`, or would it be fine to always save the source? FWIW TruffleRuby always keeps the source code (until no code in that loaded file can run anymore). ---------------------------------------- Feature #18231: `RubyVM.keep_script_lines` https://bugs.ruby-lang.org/issues/18231#change-93999 * Author: ko1 (Koichi Sasada) * Status: Open * Priority: Normal * Assignee: ko1 (Koichi Sasada) * Target version: 3.1 ---------------------------------------- This ticket proposes the method `RubyVM.keep_script_lines` to manage the flag to keep the source code. Now, `SCRIPT_LINES__ = {}` constant is defined, the hash will stores the compiled script with a path name (`{path => src}`). However, `eval` source code doesn't stores in it (maybe because it can be bigger and bigger). ## Proposal This proposal to add script lines to the ISeq and AST. ```ruby RubyVM::keep_script_lines = true eval("def foo = nil\ndef bar = nil") pp RubyVM::InstructionSequence.of(method(:foo)).script_lines #=> ["def foo = nil\n", "def bar = nil"] ``` In this case, methods `foo` and `bar` are defined by `eval()` and ISeq of `foo` can returns script lines (all script for `eval`). `ISeq#script_lines` returns compiled script lines. When `ISeq` is GCed, then the source code will be also free'ed. ## Discussion * This feature will be used by debugger (to show the source code) or REPL support (irb and so on) with `error_highlight`. * Of course memory usage will be increased. * We can introduce new status `only_eval` in future which will help REPL systems. * We can implement `ISeq#source` method like `AST#source` method (similar to `toSource` in JavaScript), but this ticket doesn't contain it. ## Implementation https://github.com/ruby/ruby/pull/4913 For the Ractor support script lines object should be immutable (not implemented yet). -- https://bugs.ruby-lang.org/ Unsubscribe: