ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "Eregon (Benoit Daloze)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:105522] [Ruby master Feature#18231] `RubyVM.keep_script_lines`
Date: Sat, 02 Oct 2021 10:36:58 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-93973.20211002103657.17@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18231.20210930081612.17@ruby-lang.org

Issue #18231 has been updated by Eregon (Benoit Daloze).


I am strongly against this proposal as it currently stands.

This is yet another hack added under RubyVM.
I think adding new APIs under RubyVM is careless and lazy, because:
* It hurts the portability of the Ruby language, this code won't work on other Ruby implementations
* Because it's under RubyVM CRuby devs feel they can add anything because it's a "special module where things are experimental", even if the API is hacky and unreliable
* Such API will typically be used by gems at some points(e.g. because no other proper public API provides the functionality), but that's bad because it's typically hacky and unreliable, i.e., it's likely to break the usages whenever it evolves (e.g., RubyVM exposing the internal order or AST children which obviously will evolve when the syntax evolves)

I think every time someone thinks to add a new API under RubyVM they should instead think what a proper API would look like, and propose that instead.
Take the time to design a new API, don't rush a hack under RubyVM.

So, for our concrete use cases here:

For `error_highlight` I already mentioned from the start it was a bad idea to read code from disk and re-parse. And now it's clear this also doesn't work for eval'd code.
What we should do is design a proper API.
What does error_highlight need? Is it just the code location (i.e. col+line+length info) of the whole call and of the receiver? Maybe we can add such info in Exception#backtrace_locations and as NameError#receiver_code_location then.
Is it more complicated than that and it needs more info from the AST? How about preserving the needed metadata when parsing (so as some bytecode metadata for CRuby), and then expose it cleanly like NameError#highlight_code_location.
Since `error_highlight` needs to show the code, we also need a way to access the code for a loaded source. Those `code_location` could be objects and have that functionality (e.g., via to_s or some other method).
It's not hard, but both of these designs are much more reliable than the current `error_highlight`, they are portable and not hacky.

For the debugger/debug gem, could you describe what is needed?

`RubyVM::keep_script_lines = true` feels hacky.
Either CRuby is OK to preserve the source for every source loaded (until the whole file/source is GC'd), or it's not.
Also returning a String of the code location's code seems better than already splitting in lines for `script_lines`.
I would think having `code_location` on exceptions and methods is also enough for the debugger.

----------------------------------------
Feature #18231: `RubyVM.keep_script_lines`
https://bugs.ruby-lang.org/issues/18231#change-93973

* 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/

  parent reply	other threads:[~2021-10-02 10:37 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-30  8:16 [ruby-core:105500] [Ruby master Feature#18231] `RubyVM.keep_script_lines` ko1 (Koichi Sasada)
2021-09-30  9:13 ` [ruby-core:105501] " Eregon (Benoit Daloze)
2021-09-30 12:00 ` [ruby-core:105505] " ko1 (Koichi Sasada)
2021-09-30 20:03 ` [ruby-core:105513] " Eregon (Benoit Daloze)
2021-10-01  1:07 ` [ruby-core:105516] " ko1 (Koichi Sasada)
2021-10-02 10:36 ` Eregon (Benoit Daloze) [this message]
2021-10-02 14:46 ` [ruby-core:105524] " ko1 (Koichi Sasada)
2021-10-02 14:51 ` [ruby-core:105525] " ko1 (Koichi Sasada)
2021-10-03  8:31 ` [ruby-core:105528] " duerst
2021-10-03  9:53 ` [ruby-core:105529] " Eregon (Benoit Daloze)
2021-10-03  9:59 ` [ruby-core:105530] " Eregon (Benoit Daloze)
2021-10-04  3:05 ` [ruby-core:105535] " matz (Yukihiro Matsumoto)
2021-10-04  9:38 ` [ruby-core:105538] " Eregon (Benoit Daloze)
2021-10-04 12:29 ` [ruby-core:105539] " ko1 (Koichi Sasada)
2021-10-04 12:38 ` [ruby-core:105540] " Eregon (Benoit Daloze)
2021-10-04 15:02 ` [ruby-core:105541] " ko1 (Koichi Sasada)
2021-10-04 16:51 ` [ruby-core:105542] " Eregon (Benoit Daloze)
2021-10-04 17:20 ` [ruby-core:105543] " ko1 (Koichi Sasada)
2021-12-14 16:58 ` [ruby-core:106672] " ko1 (Koichi Sasada)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-93973.20211002103657.17@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).