Skip to content

Commit 2e58dec

Browse files
committed
Run END keyword block only once at exit
1 parent f556eef commit 2e58dec

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Bug fixes:
4545
* Make `Enumerable#chunk` work without a block (#1518).
4646
* Fixed issue with `SystemCallError.new` setting a backtrace.
4747
* Fixed `BigDecimal#to_s` formatting issue (#1711).
48+
* Run `END` keyword block only once at exit.
4849

4950
Compatibility:
5051

spec/ruby/language/END_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require_relative '../spec_helper'
2+
3+
describe "The END keyword" do
4+
it "runs only once for multiple calls" do
5+
ruby_exe("10.times { END { puts 'foo' }; } ").should == "foo\n"
6+
end
7+
8+
it "runs last in a given code unit" do
9+
ruby_exe("END { puts 'bar' }; puts'foo'; ").should == "foo\nbar\n"
10+
end
11+
12+
it "runs multiple ends in LIFO order" do
13+
ruby_exe("END { puts 'foo' }; END { puts 'bar' }").should == "bar\nfoo\n"
14+
end
15+
end

spec/tags/language/END_tags.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
slow:The END keyword runs only once for multiple calls
2+
slow:The END keyword runs last in a given code unit
3+
slow:The END keyword runs multiple ends in LIFO order

src/main/java/org/truffleruby/parser/BodyTranslator.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,18 +2694,20 @@ public RubyNode visitPostExeNode(PostExeParseNode node) {
26942694

26952695
final SourceIndexLength position = node.getPosition();
26962696

2697-
return translateCallNode(
2698-
new CallParseNode(
2699-
position,
2700-
new TruffleFragmentParseNode(
2697+
return new OnceNode(
2698+
translateCallNode(
2699+
new CallParseNode(
27012700
position,
2702-
new ObjectLiteralNode(context.getCoreLibrary().getTruffleKernelOperationsModule())),
2703-
"at_exit",
2704-
new ArrayParseNode(position, new TrueParseNode(position)),
2705-
new IterParseNode(position, node.getArgsNode(), scope, node.getBodyNode())),
2706-
false,
2707-
false,
2708-
false);
2701+
new TruffleFragmentParseNode(
2702+
position,
2703+
new ObjectLiteralNode(
2704+
context.getCoreLibrary().getTruffleKernelOperationsModule())),
2705+
"at_exit",
2706+
new ArrayParseNode(position, new TrueParseNode(position)),
2707+
new IterParseNode(position, node.getArgsNode(), scope, node.getBodyNode())),
2708+
false,
2709+
false,
2710+
false));
27092711
}
27102712

27112713
@Override

0 commit comments

Comments
 (0)