ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "bkuhlmann (Brooke Kuhlmann) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "bkuhlmann (Brooke Kuhlmann)" <noreply@ruby-lang.org>
Subject: [ruby-core:120490] [Ruby master Feature#20999] Add RubyVM object source support
Date: Sun, 05 Jan 2025 21:07:33 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-111273.20250105210733.40939@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-20999.20250103170317.40939@ruby-lang.org>

Issue #20999 has been updated by bkuhlmann (Brooke Kuhlmann).


**Kevin**: Thanks and thanks for reminding of `RubyVM::InstructionSequence.of().script_lines`. I'm using that as a fallback when the absolute path of the instruction sequence can't be found.

**Benoit**: Thanks. I've also updated [Feature 6012](https://bugs.ruby-lang.org/issues/6012) as well.

**Both**: Thanks for all of the feedback. I've logged a new feature request here: [21005](https://bugs.ruby-lang.org/issues/21005). *Feel free to close this issue.*

----------------------------------------
Feature #20999: Add RubyVM object source support
https://bugs.ruby-lang.org/issues/20999#change-111273

* Author: bkuhlmann (Brooke Kuhlmann)
* Status: Open
----------------------------------------
Hello. 👋

I'd like to propose adding the ability to acquire the source of any object within memory via the RubyVM. A couple use cases come to mind:

- This would simplify the [Method Source](https://github.com/banister/method_source) gem implementation and possibly eliminate the need for the gem.
- Another use case is this allows DSLs, like [Initable](https://alchemists.io/projects/initable), to elegantly acquire the source code of objects and/or functions (in my case, I'm most interested in the lazy evaluation of function bodies).

 ⚠️ I'm also aware that the [RubyVM](https://docs.ruby-lang.org/en/master/RubyVM.html) documentation clearly stats this isn't meant for production use:

> This module is for very limited purposes, such as debugging, prototyping, and research. Normal users must not use it. This module is not portable between Ruby implementations.

...but I'd like to entertain this proposed feature request, regardless. Here's an example, using the aforementioned [Initable](https://alchemists.io/projects/initable) gem, where I use the RubyVM to obtain the source of a `Proc`:


``` ruby
class Demo
  include Initable[%i[req name], [:key, :default, proc { Object.new }]]
end

puts Demo.new("demo").inspect
#<Demo:0x000000014349a400 @name="demo", @default=#<Object:0x000000014349a360>>
```

With the above, I'm lazily obtaining the source code of the `Proc` in order to dynamically define the `#initialize` method (essentially a `module_eval` on `Demo`, simply speaking) using a nested array as specified by [Method#parameters](https://docs.ruby-lang.org/en/master/Method.html#method-i-parameters) because I don't want an instance of `Object` until initialization is necessary.


## Context

Prior to the release of Ruby 3.4.0, you could do this:

``` ruby
function = proc { Object.new }
ast = RubyVM::AbstractSyntaxTree.of function
ast.children.last.source

# "Object.new"
```

Unfortunately, with the release of Ruby 3.4.0 -- which defaults to the [Prism](https://ruby.github.io/prism/rb/index.html) parser -- the ability to acquire source code is a bit more complicated. For example, to achieve what is shown above, you have to do this:

``` ruby
function = proc { Object.new }
RubyVM::InstructionSequence.of(function).script_lines

# [
#   "function = proc { Object.new }\n",
#   "RubyVM::InstructionSequence.of(function).script_lines\n",
#   "\n",
#   ""
# ]
```

Definitely doable, but now requires more work to pluck `"Object.new"` from the body of the `Proc`. One solution is to use a regular expression to find and extract the first line of the result. Example:

``` ruby
/
  proc          # Proc statement.
  \s*           # Optional space.
  \{            # Block open.
  (?<body>.*?)  # Source code body.
  \}            # Block close.
/x
```

Definitely doesn't account for all use cases (like when a `Proc` spans multiple lines or uses `do...end` syntax) but will get you close.

## How

I think there are a couple of paths that might be nice to support this use case. 

### Option A

Teach `RubyVM::InstructionSequence` to respond to `#source` which would be similar to what was possible prior to Ruby 3.4.0. Example:

``` ruby
function = proc { Object.new }
RubyVM::InstructionSequence.of(function).source

# "Object.new"
```

### Option B

This is something that Samuel Williams [mentioned](https://bugs.ruby-lang.org/issues/6012#note-13) in [Feature 6012](https://bugs.ruby-lang.org/issues/6012) which would be to provide a `Source` object as answered by `Method#source` and `Proc#source`. Example (using a `Proc`):

``` ruby
# Implementation
# Method#source (i.e. Source.new path, line_number, line_count, body)

# Usage:

function = proc { Object.new }

method.source.code      # "Object.new"
method.source.path      # "$HOME/demo.rb"
method.source.location  # [2, 0, 3, 3]
```

### Option C

It could be nice to support both Option A and B.




-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

      parent reply	other threads:[~2025-01-05 21:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-03 17:03 [ruby-core:120467] " bkuhlmann (Brooke Kuhlmann) via ruby-core
2025-01-03 17:37 ` [ruby-core:120468] " kddnewton (Kevin Newton) via ruby-core
2025-01-03 23:10 ` [ruby-core:120475] " bkuhlmann (Brooke Kuhlmann) via ruby-core
2025-01-04 20:14 ` [ruby-core:120477] " kddnewton (Kevin Newton) via ruby-core
2025-01-05 14:06 ` [ruby-core:120486] " Eregon (Benoit Daloze) via ruby-core
2025-01-05 21:07 ` bkuhlmann (Brooke Kuhlmann) via ruby-core [this message]

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-all from there: mbox

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

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

  git send-email \
    --in-reply-to=redmine.journal-111273.20250105210733.40939@ruby-lang.org \
    --to=ruby-core@ml.ruby-lang.org \
    --cc=noreply@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 NNTP newsgroup(s).