steadfast 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cb638379609c138ddd9d582bc651c4c89e1ea5109687ff1455fe47a5c22995c
4
- data.tar.gz: 869bf5c02b66b1ecfcc5cfabb2f506cd9d8e3ee1ccb6f2e96304e4d9725cddbb
3
+ metadata.gz: 5fcb668c3dbb5ba803c1a4d0767e565ab17fd7d67aeacc0a9ae7f8ed38db2d2e
4
+ data.tar.gz: 7ef9e7c64f361b92ecc09354fa56c9e1aedae010bb5858bace103bde9ef7f66c
5
5
  SHA512:
6
- metadata.gz: 71bfa853f0cc05c979e2abb8918167c99bde44e0b181d5347a0719ceec7551597393ae02eaed37b73a1fb2c342ac10a9778d35c664c2476c24ab580a7a13ba54
7
- data.tar.gz: 5e5cefe1d89228fa9518456a1df81903157e472522b73d3cebad05d309dfbebccb56304f46c122921d7341bd9f3ac046177a791bf5a65f01a416b7bb213206d8
6
+ metadata.gz: 351611184a6b8d714eb620f1d9c005d9602a39ef4668bee96bfb1b3a2135c07a8301a8a22967fa6dc6c7a4fb9571c445fb4a07d483057bb35960f3733a76a1a0
7
+ data.tar.gz: c50b2765311f0fc7ecb705992b2f92dfa23cd5ac09b1054c2df37571db127a6c94de9653d2441489eb4491ab63d9f08624c3fe08a0b57be066b72258f996733d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 0.7.0 2026-11-28
2
+
3
+ - Add support for automatic backtrace translation in `eval_ast`
4
+ - Streamline `eval_ast` options, add support for class_eval
5
+ - Move core methods to `Steadfast::AST` module
6
+ - Add support for `PreExecutionNode`, `PostExecutionNode`
7
+
1
8
  # 0.6.0 2026-11-27
2
9
 
3
10
  - Add support for emit-nothing with `unquote(:__nop__)`
data/README.md CHANGED
@@ -96,7 +96,7 @@ ast = Steadfast.to_ast(->() { foo() })
96
96
  ast2 = Steadfast.rewrite(ast) { |n|
97
97
  # use next to allow the iteration to continue
98
98
  next n if !(n in Ruby::Prism::CallNode(name: :foo))
99
-
99
+
100
100
  Steadfast.quote { bar() }
101
101
  }
102
102
  ```
@@ -0,0 +1,260 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Steadfast
4
+ module AST
5
+ # Returns an AST for the given object.
6
+ #
7
+ # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
8
+ # @return [Ruby::Prism::Node]
9
+ def to_ast(obj)
10
+ case obj
11
+ when Proc, Method, UnboundMethod
12
+ ast = obj.syntax_tree
13
+ raise Steadfast::Error, "AST not found for given object" if !ast
14
+
15
+ set_source_filename(ast, obj)
16
+ ast
17
+ when Ruby::Prism::Node
18
+ obj
19
+ else
20
+ raise ArgumentError, "Invalid object type"
21
+ end
22
+ end
23
+
24
+ # Returns the source code to the given object by sourcifying its AST.
25
+ #
26
+ # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
27
+ # @return [String]
28
+ def to_source(obj, **opts)
29
+ ast = to_ast(obj)
30
+ Sourcifier.new(**opts).to_source(ast)
31
+ end
32
+
33
+ # Rewrites the given AST using the given hash of node replacements or a
34
+ # rewrite block. Returns either the original AST, or a copy with changes.
35
+ #
36
+ # @param ast [Ruby::Prism::Node]
37
+ # @param replacements [Hash]
38
+ # @return [Ruby::Prism::Node]
39
+ def rewrite(ast, **replacements, &block)
40
+ Rewriter.rewrite(ast, **replacements, &block)
41
+ end
42
+
43
+ # Evaluates the given AST in the context of the given receiver, class or
44
+ # binding. If the receiver: option is given, an instance_eval is performed.
45
+ # If the class: option is given, a class_eval is performed. Otherwise, the
46
+ # given binding: is used for a normal eval. If no binding is given, no
47
+ # binding is passed to eval.
48
+ #
49
+ # @param ast [Ruby::Prism::Node]
50
+ # @param receiver: [any, nil] instance_eval receiver
51
+ # @param class: [Class, nil] class_eval receiver
52
+ # @param binding: [Binding, nil] binding
53
+ # @param translate_backtrace [true, false] auto translate exception backtraces
54
+ # @return [any]
55
+ def eval_ast(ast, source_location: nil, **opts)
56
+ raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
57
+
58
+ source_location = compute_eval_source_location(source_location)
59
+ ast = eval_ast_translate_backtrace(ast) if opts[:translate_backtrace]
60
+ src = to_source(ast, source_location:, sourcemap: true, frozen_string_literal: true)
61
+ if (receiver = opts[:receiver])
62
+ receiver.instance_eval(src, *source_location)
63
+ elsif (receiver = opts[:class])
64
+ receiver.class_eval(src, *source_location)
65
+ else
66
+ eval(src, opts[:binding], *source_location)
67
+ end
68
+ end
69
+
70
+ # Visits the given AST, passing each node in the AST to the given block.
71
+ #
72
+ # @param ast [Ruby::Prism::Node]
73
+ # @return [void]
74
+ def walk(ast, &)
75
+ ast.accept(Visitor.new(&))
76
+ ast
77
+ end
78
+
79
+ # Visits the given AST, passing each node in the AST to the given block.
80
+ # Returns the first node for which the block returns true.
81
+ #
82
+ # @param ast [Ruby::Prism::Node]
83
+ # @return [Ruby::Prism::Node, nil]
84
+ def find(ast, &block)
85
+ ast.accept(Visitor.new { return it if block.(it) })
86
+ nil
87
+ end
88
+
89
+ # Visits the given AST, passing each node in the AST to the given block.
90
+ # Returns all nodes for which the block returns true.
91
+ #
92
+ # @param ast [Ruby::Prism::Node]
93
+ # @return [Array<Ruby::Prism::Node>]
94
+ def find_all(ast, &block)
95
+ nodes = []
96
+ ast.accept(Visitor.new { nodes << it if block.(it) })
97
+ nodes
98
+ end
99
+
100
+ private
101
+
102
+ # Sets the source filename for the given ast to the source location of the
103
+ # given proc or method.
104
+ #
105
+ # @param ast [Ruby::Prism::Node]
106
+ # @param obj [Proc, Method]
107
+ # @return [void]
108
+ def set_source_filename(ast, obj)
109
+ source = ast.send(:source)
110
+ source.__filename__ ||= obj.source_location.first
111
+ end
112
+
113
+ # Computes the source location (filename and line no) for the given
114
+ # source_location spec. If source_location is nil, computes a random
115
+ # filename. If source_location is caller, returns the location of the
116
+ # callsite to eval_ast. Otherwise returns the given source location, with
117
+ # the lineno decremented (in order to compensate for the
118
+ # frozen_string_literal comment emitted before the code.
119
+ #
120
+ # @param source_location [Array, nil, :caller]
121
+ # @return [Array] [filename, lineno]
122
+ def compute_eval_source_location(source_location)
123
+ case source_location
124
+ in nil
125
+ # Important: ideally we'd set the source location line no to 0 (to
126
+ # compensate for the #frozen_string_literal comment at the top).
127
+ # However, while `eval` respects a starting lineno set to 0, Prism does
128
+ # not, which can lead to off-by-one lineno in exception backtraces for
129
+ # synthetic ASTs.
130
+ ["(steadfast-#{SecureRandom.hex(4)})", 1]
131
+ in :caller
132
+ # Wwhen source_location is not nil, we do decrement the start lineno.
133
+ # Hopefully some day this wart will be removed from Ruby.G
134
+ l = caller_locations[1]
135
+ [l.path, l.lineno - 1]
136
+ in [String => filename, Integer => lineno]
137
+ [filename, lineno - 1]
138
+ else
139
+ raise ArgumentError, "Invalid source_location given"
140
+ end
141
+ end
142
+
143
+ # Translates the given AST by inserting a backtrace translation rescue
144
+ # block. All method and lambda definitions will be rewritten to include
145
+ # backtrace translation, as well as the top-level node.
146
+ #
147
+ # @param ast [Ruby::Prism::Node]
148
+ # @return [Ruby::Prism::Node]
149
+ def eval_ast_translate_backtrace(ast)
150
+ ast = Steadfast.rewrite(ast) do |n|
151
+ case n
152
+ when Ruby::Prism::DefNode, Ruby::Prism::LambdaNode
153
+ Steadfast.rewrite(n, n.body => eval_ast_wrapper_ast(n.body))
154
+ else
155
+ n
156
+ end
157
+ end
158
+
159
+ case ast
160
+ when Ruby::Prism::DefNode, Ruby::Prism::LambdaNode
161
+ ast
162
+ else
163
+ eval_ast_wrapper_ast(ast)
164
+ end
165
+ end
166
+
167
+ # Returns an AST wrapping the given statements with a rescue block that
168
+ # translates an unhandled exception's backtrace for source mapping purposes.
169
+ # If the given node is anything other than a StatementsNode, it is wrapped
170
+ # in a StatementsNode.
171
+ #
172
+ # @param statements [Ruby::Prism::Node]
173
+ # @return [Ruby::Prism::Node]
174
+ def eval_ast_wrapper_ast(statements)
175
+ if !statements.is_a?(Ruby::Prism::StatementsNode)
176
+ statements = Steadfast.make_statements([statements])
177
+ end
178
+ Steadfast.quote(binding) {
179
+ begin
180
+ unquote(statements)
181
+ rescue Exception => e
182
+ e.set_backtrace(Steadfast::SourceMapping.translate_backtrace(e.backtrace))
183
+ raise
184
+ end
185
+ }
186
+ end
187
+
188
+ # Finds the AST for the given proc. This method is currently not in use, but
189
+ # we may eventually use this code in order to provide backward compatibility
190
+ # for Ruby < 4.1.
191
+ #
192
+ # @param proc [Proc]
193
+ # @return [Ruby::Prism::Node]
194
+ def proc_ast(proc)
195
+ fn, lineno = proc.source_location
196
+ pr = Prism.parse(get_source(fn), filepath: fn)
197
+ program = pr.value
198
+
199
+ Finder.find(program, proc) do
200
+ on(:lambda) do |node|
201
+ found!(node) if node.location.start_line == lineno
202
+ super(node)
203
+ end
204
+ on(:block) do |node|
205
+ found!(node) if node.location.start_line == lineno
206
+ super(node)
207
+ end
208
+ on(:call) do |node|
209
+ case node.name
210
+ when :proc, :lambda
211
+ found!(node) if node.block && node.block.location.start_line == lineno
212
+ end
213
+ super(node)
214
+ end
215
+ end
216
+ rescue Errno::ENOENT
217
+ raise Sirop::Error, "Could not get source for proc"
218
+ end
219
+
220
+ # Finds the AST for the given method. This method is currently not in use,
221
+ # but we may eventually use this code in order to provide backward
222
+ # compatibility for Ruby < 4.1.
223
+ #
224
+ # @param method [Method, UnboundMethod]
225
+ # @return [Ruby::Prism::Node]
226
+ def method_ast(method)
227
+ fn, lineno = method.source_location
228
+ pr = Prism.parse(get_source(fn), filepath: fn)
229
+ program = pr.value
230
+
231
+ Finder.find(program, method) do
232
+ on(:def) do |node|
233
+ found!(node) if node.name == method.name && node.location.start_line == lineno
234
+ super(node)
235
+ end
236
+ end
237
+ end
238
+
239
+ # Returns the content of the given filename. Returns the IRB line buffer if
240
+ # '(irb)' is given. This method is not currently used.
241
+ #
242
+ # @param fn [String]
243
+ # @return [String] source code
244
+ def get_source(fn)
245
+ return get_irb_source if fn == '(irb)'
246
+
247
+ IO.read(fn)
248
+ end
249
+
250
+ # Returns the IRB line buffer (the source code for the current IRB session.)
251
+ # This method is currently not used.
252
+ #
253
+ # @return [String] IRB line buffer
254
+ def get_irb_source
255
+ io = IRB.CurrentContext.io
256
+ last_line = io.instance_variable_get(:@line_no)
257
+ (1..last_line).map { io.line(it) }.join
258
+ end
259
+ end
260
+ end
@@ -132,7 +132,7 @@ module Steadfast
132
132
 
133
133
  def __unquote_evaluate_target_node__(node, binding, location)
134
134
  node2 = __unquote_rewrite_nested_quote__(node, binding)
135
- v = Steadfast.eval_ast(node2, nil, binding)
135
+ v = Steadfast.eval_ast(node2, binding:)
136
136
  __unquote_convert_value__(v, location)
137
137
  end
138
138
 
@@ -14,7 +14,7 @@ module Steadfast
14
14
  # @return [Ruby::Prism::Node]
15
15
  def self.rewrite(ast, **replacements, &rewrite_block)
16
16
  raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
17
-
17
+
18
18
  rewrite_block ||= ->(n) do
19
19
  if (v = replacements[n]) && v.is_a?(Hash)
20
20
  n.copy(**v)
@@ -110,6 +110,8 @@ module Steadfast
110
110
  optional_keyword_parameter: [:name_loc, :whitespace, :value],
111
111
  or: [:left, :whitespace, :operator_loc, :whitespace, :right],
112
112
  parentheses: [:opening_loc, :body, :closing_loc],
113
+ post_execution: [:keyword_loc, :whitespace, :opening_loc, :newline, :statements, :newline, :closing_loc],
114
+ pre_execution: [:keyword_loc, :whitespace, :opening_loc, :newline, :statements, :newline, :closing_loc],
113
115
  program: [:statements],
114
116
  pinned_expression: [:operator_loc, :lparen_loc, :expression, :rparen_loc],
115
117
  pinned_variable: [:operator_loc, :variable],
@@ -545,7 +547,7 @@ module Steadfast
545
547
  emit("# shareable_constant_value: literal\n")
546
548
  emit_code(node.write)
547
549
  end
548
-
550
+
549
551
  def visit_statements_node(node)
550
552
  first = true
551
553
  node.body&.each do |n|
@@ -1,4 +1,4 @@
1
1
  module Steadfast
2
2
  # Steadfast version
3
- VERSION = '0.6.0'
3
+ VERSION = '0.7.0'
4
4
  end
data/lib/steadfast.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'securerandom'
4
4
 
5
+ require 'steadfast/ast'
5
6
  require 'steadfast/source_mapping'
6
7
  require 'steadfast/sourcifier'
7
8
  require 'steadfast/visitor'
@@ -18,7 +19,6 @@ RubyVM.keep_script_lines = true
18
19
  module Steadfast
19
20
  # Extensions to Ruby::Prism::Source
20
21
  module PrismSourceExtensions
21
-
22
22
  # The __filename__ attribute lets us track the source location for a given
23
23
  # AST.
24
24
  attr_accessor :__filename__
@@ -30,194 +30,8 @@ module Steadfast
30
30
  end
31
31
 
32
32
  class << self
33
+ include Steadfast::AST
33
34
  include Steadfast::Make
34
35
  include Steadfast::Quote
35
-
36
- # Returns an AST for the given object.
37
- #
38
- # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
39
- # @return [Ruby::Prism::Node]
40
- def to_ast(obj)
41
- case obj
42
- when Proc, Method, UnboundMethod
43
- ast = obj.syntax_tree
44
- raise Steadfast::Error, "AST not found for given object" if !ast
45
-
46
- set_source_filename(ast, obj)
47
- ast
48
- when Ruby::Prism::Node
49
- obj
50
- else
51
- raise ArgumentError, "Invalid object type"
52
- end
53
- end
54
-
55
- # Returns the source code to the given object by sourcifying its AST.
56
- #
57
- # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
58
- # @return [String]
59
- def to_source(obj, **opts)
60
- ast = to_ast(obj)
61
- Sourcifier.new(**opts).to_source(ast)
62
- end
63
-
64
- # Rewrites the given AST using the given hash of node replacements or a
65
- # rewrite block. Returns either the original AST, or a copy with changes.
66
- #
67
- # @param ast [Ruby::Prism::Node]
68
- # @param replacements [Hash]
69
- # @return [Ruby::Prism::Node]
70
- def rewrite(ast, **replacements, &block)
71
- Rewriter.rewrite(ast, **replacements, &block)
72
- end
73
-
74
- # Evaluates the given AST in the context of the given receiver or binding.
75
- #
76
- # @param ast [Ruby::Prism::Node]
77
- # @param eval_receiver [any]
78
- # @param eval_binding [Binding, nil]
79
- # @return [any]
80
- def eval_ast(ast, eval_receiver = nil, eval_binding = nil, source_location: nil)
81
- raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
82
-
83
- case source_location
84
- when nil
85
- # Important: ideally we'd set the source location line no to 0 (to
86
- # compensate for the #frozen_string_literal comment at the top).
87
- # However, while `eval` respects a starting lineno set to 0, Prism does
88
- # not, which can lead to off-by-one line nos in exception backtraces for
89
- # synthetic ASTs.
90
- #
91
- # Note that when source_location is not nil, we do decrement the start
92
- # lineno. Hopefully some day this wart will be removed from Ruby.G
93
- source_location = ["(steadfast-#{SecureRandom.hex(4)})", 1]
94
- when :caller
95
- l = caller_locations[0]
96
- source_location = [l.path, l.lineno - 1]
97
- else
98
- source_location = [source_location[0], source_location[1] - 1]
99
- end
100
- src = to_source(ast, source_location:, sourcemap: true, frozen_string_literal: true)
101
- if eval_receiver
102
- eval_receiver.instance_eval(src, source_location[0], source_location[1])
103
- else
104
- eval(src, eval_binding || binding, source_location[0], source_location[1])
105
- end
106
- end
107
-
108
- # Visits the given AST, passing each node in the AST to the given block.
109
- #
110
- # @param ast [Ruby::Prism::Node]
111
- # @return [void]
112
- def walk(ast, &)
113
- ast.accept(Visitor.new(&))
114
- ast
115
- end
116
-
117
- # Visits the given AST, passing each node in the AST to the given block.
118
- # Returns the first node for which the block returns true.
119
- #
120
- # @param ast [Ruby::Prism::Node]
121
- # @return [Ruby::Prism::Node, nil]
122
- def find(ast, &block)
123
- ast.accept(Visitor.new { return it if block.(it) })
124
- nil
125
- end
126
-
127
- # Visits the given AST, passing each node in the AST to the given block.
128
- # Returns all nodes for which the block returns true.
129
- #
130
- # @param ast [Ruby::Prism::Node]
131
- # @return [Array<Ruby::Prism::Node>]
132
- def find_all(ast, &block)
133
- nodes = []
134
- ast.accept(Visitor.new { nodes << it if block.(it) })
135
- nodes
136
- end
137
-
138
- private
139
-
140
- # Sets the source filename for the given ast to the source location of the
141
- # given proc or method.
142
- #
143
- # @param ast [Ruby::Prism::Node]
144
- # @param obj [Proc, Method]
145
- # @return [void]
146
- def set_source_filename(ast, obj)
147
- source = ast.send(:source)
148
- source.__filename__ ||= obj.source_location.first
149
- end
150
-
151
- # Finds the AST for the given proc. This method is currently not in use, but
152
- # we may eventually use this code in order to provide backward compatibility
153
- # for Ruby < 4.1.
154
- #
155
- # @param proc [Proc]
156
- # @return [Ruby::Prism::Node]
157
- def proc_ast(proc)
158
- fn, lineno = proc.source_location
159
- pr = Prism.parse(get_source(fn), filepath: fn)
160
- program = pr.value
161
-
162
- Finder.find(program, proc) do
163
- on(:lambda) do |node|
164
- found!(node) if node.location.start_line == lineno
165
- super(node)
166
- end
167
- on(:block) do |node|
168
- found!(node) if node.location.start_line == lineno
169
- super(node)
170
- end
171
- on(:call) do |node|
172
- case node.name
173
- when :proc, :lambda
174
- found!(node) if node.block && node.block.location.start_line == lineno
175
- end
176
- super(node)
177
- end
178
- end
179
- rescue Errno::ENOENT
180
- raise Sirop::Error, "Could not get source for proc"
181
- end
182
-
183
- # Finds the AST for the given method. This method is currently not in use,
184
- # but we may eventually use this code in order to provide backward
185
- # compatibility for Ruby < 4.1.
186
- #
187
- # @param method [Method, UnboundMethod]
188
- # @return [Ruby::Prism::Node]
189
- def method_ast(method)
190
- fn, lineno = method.source_location
191
- pr = Prism.parse(get_source(fn), filepath: fn)
192
- program = pr.value
193
-
194
- Finder.find(program, method) do
195
- on(:def) do |node|
196
- found!(node) if node.name == method.name && node.location.start_line == lineno
197
- super(node)
198
- end
199
- end
200
- end
201
-
202
- # Returns the content of the given filename. Returns the IRB line buffer if
203
- # '(irb)' is given. This method is not currently used.
204
- #
205
- # @param fn [String]
206
- # @return [String] source code
207
- def get_source(fn)
208
- return get_irb_source if fn == '(irb)'
209
-
210
- IO.read(fn)
211
- end
212
-
213
- # Returns the IRB line buffer (the source code for the current IRB session.)
214
- # This method is currently not used.
215
- #
216
- # @return [String] IRB line buffer
217
- def get_irb_source
218
- io = IRB.CurrentContext.io
219
- last_line = io.instance_variable_get(:@line_no)
220
- (1..last_line).map { io.line(it) }.join
221
- end
222
36
  end
223
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steadfast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -66,6 +66,7 @@ files:
66
66
  - CHANGELOG.md
67
67
  - README.md
68
68
  - lib/steadfast.rb
69
+ - lib/steadfast/ast.rb
69
70
  - lib/steadfast/make.rb
70
71
  - lib/steadfast/quote.rb
71
72
  - lib/steadfast/rewriter.rb