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:105530] [Ruby master Feature#18231] `RubyVM.keep_script_lines`
Date: Sun, 03 Oct 2021 09:59:13 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-93983.20211003095912.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).


duerst (Martin Dürst) wrote in #note-8:
> Can you say exactly what the problem is? Is it that the file may get changed?

Yes, it might get changed, which might then cause confusing errors when parsing it again.
The file could also have been removed in between.
Of course that approach cannot work for `eval` (there is no file).
And finally it might also break when .rb files are packaged (e.g., ruby-packer, in some archive, or some virtual filesystem) since then there is also no real file.

`eval` is the most visible issue, because the value of `__FILE__` in eval is not reffering to a real file, or if it is that file typically it has different contents (the whole file and not just the String eval'd).

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

* 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-03  9:59 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 ` [ruby-core:105522] " Eregon (Benoit Daloze)
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 ` Eregon (Benoit Daloze) [this message]
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-93983.20211003095912.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).