steadfast 0.0.3 → 0.2.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: c6e3d1e91bd27e7c92b511ef755e05095e691e6489173aded42938aa6ecbdd58
4
- data.tar.gz: 75a299fc9c1b3bfc89e81dbc6e1bfb0085a470b4a239aeffc02b63adefc509ad
3
+ metadata.gz: 13c3012f9ef6e5f2a86d5b5de0b8d05a7156c712ab8cf752ff915abc803cc709
4
+ data.tar.gz: 520fbbb6d3f891cb375018cf11066ebdd79ab475fb3396ea114ec06660f6b228
5
5
  SHA512:
6
- metadata.gz: 070eb4137ab096dcb11625c593c880952ecbd8150057849608eb8972368f047c66ff8837c0b900072620380d026f2d4043ac928aa8654f504803c683ab2a3c4c
7
- data.tar.gz: 314ca97af4862e20ceab634ac1ebe3e6f72a2eac2c67c933a1df27fa1deb84a35a07a09f044311d13c53f0f4cfbde00bf60d09260a7faa72e4402709e6277153
6
+ metadata.gz: 4f62742ad47d98ea556e7feef65441884fc5f887eb8b273b403befc2d00afdaf2e04a62229ec567a8bab3a7cb470b26522408d57f98230e7337cd8fce8f3eb31
7
+ data.tar.gz: 5e5108f0d52422bcdeac4171751881cf8f8009bd8861e4858e08e27d0833e0c0122476e2afd89f5735a3da27065d30bd0096431483beba7ddcadfd2173311f8d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ # 0.2.0 2026-11-24
2
+
3
+ - Use frozen string literals in `eval_ast`
4
+ - Refactor source mapping into separate module
5
+
6
+ # 0.1.0 2026-11-23
7
+
8
+ - Improve source mapping, make it work for derivative synthetic ASTs
9
+ - Add automatic source location info in `Steadfast.eval_ast`
10
+
1
11
  # 0.0.3 2026-11-23
2
12
 
3
13
  - Implement `Steadfast.eval_ast`
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Steadfast
4
+
4
5
  # The Rewriter class is used for rewriting ASTs.
5
6
  class Rewriter
6
7
  # Rewrites the given AST with a either a hash of node replacements, or a
@@ -49,7 +50,7 @@ module Steadfast
49
50
  n.deconstruct_keys(nil).each do |k, v|
50
51
  if v.is_a?(Ruby::Prism::Node)
51
52
  v2 = visit(v)
52
- changes[k] = v2 if (v2 != v)
53
+ changes[k] = v2 if v2 != v
53
54
  elsif v in [Ruby::Prism::Node, *]
54
55
  v2 = v.map { visit(it) }
55
56
  changes[k] = v2 if v2 != v
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Steadfast
4
+
5
+ # SourceMapping manages the mapping of source locations for generated code, in
6
+ # order to allow translating backtrace entries for raised exceptions such that
7
+ # they will point to correct source locations in application code.
8
+ module SourceMapping
9
+ class << self
10
+ # Adds a source mapping entry for the given path/line pairs.
11
+ #
12
+ # @param path1 [String]
13
+ # @param line1 [Integer]
14
+ # @param path2 [String]
15
+ # @param line2 [Integer]
16
+ # @return [void]
17
+ def set(path1, line1, path2, line2)
18
+ (@map ||= {})["#{path1}:#{line1}"] ||= "#{path2}:#{line2}"
19
+ end
20
+
21
+ # Translates entries in the given backtrace according to the global source
22
+ # map.
23
+ #
24
+ # @param bt [Array<String>] backtrace
25
+ # @return [Array<String>] translated source map
26
+ def translate_backtrace(bt)
27
+ return bt if !@map
28
+
29
+ bt.map {
30
+ if (m = it.match(/^(.+)(\:in.+)$/)) && (loc = find_sourcemap_entry(m[1]))
31
+ "#{loc}#{m[2]}"
32
+ else
33
+ it
34
+ end
35
+ }
36
+ end
37
+
38
+ private
39
+
40
+ # Returns a mapped location for the given location entry.
41
+ #
42
+ # @param loc [String] location string of the form file:line
43
+ # @return [String, nil] mapped location
44
+ def find_sourcemap_entry(loc)
45
+ loc = @map[loc]
46
+ return nil if !loc
47
+
48
+ (loc =~ /^\(steadfast-[0-9a-f]+\)\:\d+$/) ? find_sourcemap_entry(loc) : loc
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'steadfast/source_mapping'
4
+
3
5
  module Steadfast
4
6
 
5
7
  # A Sourcifier is used for converting an AST to source code. Sourcifier is
@@ -117,10 +119,11 @@ module Steadfast
117
119
  #
118
120
  # @param start_line []
119
121
  # @return [void]
120
- def initialize(source_location: nil, sourcemap: false)
122
+ def initialize(source_location: nil, sourcemap: false, frozen_string_literal: false)
121
123
  @filename, @line = source_location
122
124
  @line ||= 1
123
125
  @sourcemap = sourcemap
126
+ @frozen_string_literal = frozen_string_literal
124
127
 
125
128
  raise Steadfast::Error, "No filename given, can't generate source map" if @sourcemap && !@filename
126
129
  end
@@ -131,6 +134,10 @@ module Steadfast
131
134
  # @return [String] source code
132
135
  def to_source(ast)
133
136
  @buffer = +''
137
+ if @frozen_string_literal
138
+ @buffer << "#frozen_string_literal: true\n"
139
+ @line += 1
140
+ end
134
141
  visit(ast)
135
142
  @buffer
136
143
  end
@@ -691,9 +698,13 @@ module Steadfast
691
698
 
692
699
  if (filename = source.__filename__) && (filename || last_source.__filename__)
693
700
  emit_newline if different_source && last_source
694
- Steadfast.sourcemap["#{@filename}:#{@line}"] ||= "#{filename}:#{loc.start_line}"
701
+ Steadfast::SourceMapping.set(
702
+ @filename, @line, filename, loc.start_line
703
+ )
695
704
  elsif filename.nil? && @last_loc
696
- Steadfast.sourcemap["#{@filename}:#{@line}"] ||= "#{last_source.__filename__}:#{@last_loc.start_line}"
705
+ Steadfast::SourceMapping.set(
706
+ @filename, @line, last_source.__filename__, @last_loc.start_line
707
+ )
697
708
  end
698
709
  end
699
710
 
@@ -711,7 +722,7 @@ module Steadfast
711
722
  # @return [void]
712
723
  def emit_newline
713
724
  return if @buffer[-1] == "\n"
714
-
725
+
715
726
  @buffer << "\n"
716
727
  @line += 1
717
728
  end
@@ -1,4 +1,4 @@
1
1
  module Steadfast
2
2
  # Steadfast version
3
- VERSION = '0.0.3'
3
+ VERSION = '0.2.0'
4
4
  end
data/lib/steadfast.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'steadfast/source_mapping'
3
4
  require 'steadfast/sourcifier'
4
5
  require 'steadfast/rewriter'
5
6
 
@@ -24,28 +25,6 @@ module Steadfast
24
25
  end
25
26
 
26
27
  class << self
27
- # Returns the global Steadfast source map hash. Currently *not* thread-safe.
28
- #
29
- # @return [Hash] source map
30
- def sourcemap
31
- @sourcemap ||= {}
32
- end
33
-
34
- # Translates entries in the given backtrace according to the global source
35
- # map.
36
- #
37
- # @param bt [Array<String>] backtrace
38
- # @return [Array<String>] translated source map
39
- def translate_backtrace(bt)
40
- bt.map {
41
- if (m = it.match(/^(.+)(\:in.+)$/)) && (loc = sourcemap[m[1]])
42
- "#{sourcemap[m[1]]}#{m[2]}"
43
- else
44
- it
45
- end
46
- }
47
- end
48
-
49
28
  # Returns an AST for the given object.
50
29
  #
51
30
  # @param obj [Proc, Method, UnboundedMethod, Ruby::Prism::Node]
@@ -86,14 +65,31 @@ module Steadfast
86
65
  # @param eval_receiver [any]
87
66
  # @param eval_binding [Binding, nil]
88
67
  # @return [any]
89
- def eval_ast(ast, eval_receiver = nil, eval_binding = nil)
68
+ def eval_ast(ast, eval_receiver = nil, eval_binding = nil, source_location: nil)
90
69
  raise ArgumentError, "Not an AST" if !ast.is_a?(Ruby::Prism::Node)
91
-
92
- src = to_source(ast)
70
+
71
+ case source_location
72
+ when nil
73
+ # Important: ideally we'd set the source location line no to 0 (to
74
+ # compensate for the #frozen_string_literal comment at the top).
75
+ # However, while `eval` respects a starting lineno set to 0, Prism does
76
+ # not, which can lead to off-by-one line nos in exception backtraces for
77
+ # synthetic ASTs.
78
+ #
79
+ # Note that when source_location is not nil, we do decrement the start
80
+ # lineno. Hopefully some day this wart will be removed from Ruby.G
81
+ source_location = ["(steadfast-#{SecureRandom.hex(4)})", 1]
82
+ when :caller
83
+ l = caller_locations[0]
84
+ source_location = [l.path, l.lineno - 1]
85
+ else
86
+ source_location = [source_location[0], source_location[1] - 1]
87
+ end
88
+ src = to_source(ast, source_location:, sourcemap: true, frozen_string_literal: true)
93
89
  if eval_receiver
94
90
  eval_receiver.instance_eval(src)
95
91
  else
96
- eval(src, eval_binding || binding)
92
+ eval(src, eval_binding || binding, source_location[0], source_location[1])
97
93
  end
98
94
  end
99
95
 
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.0.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -83,6 +83,7 @@ files:
83
83
  - README.md
84
84
  - lib/steadfast.rb
85
85
  - lib/steadfast/rewriter.rb
86
+ - lib/steadfast/source_mapping.rb
86
87
  - lib/steadfast/sourcifier.rb
87
88
  - lib/steadfast/version.rb
88
89
  homepage: "http://github.com/digital-fabric/steadfast"