Skip to content

Commit 7eba626

Browse files
committed
Translate END node
1 parent a2f9dba commit 7eba626

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

spec/tags/truffle/parsing/parsing_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
fails:Parsing a BEGIN block (BEGIN { ... }) case is parsed correctly
2-
fails:Parsing a END block (END { ... }) case is parsed correctly
32
fails:Parsing a For operator (for ... in ... operator) case is parsed correctly
43

54
# Not supported yet by Prism

spec/truffle/parsing/fixtures/END.yaml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
subject: "END block"
22
description: "END { ... }"
33
notes: >
4-
Is trasnalted into a `Truffle::KernelOperations.at_exit(false) { ... }` call.
4+
Is translated into a `Truffle::KernelOperations.at_exit(false) { ... }` call.
5+
yarp_specific: true # don't optimize reading KernelOperations constant
6+
# with TruffleKernelOperationsModuleLiteralNode
57
focused_on_node: "org.truffleruby.language.control.OnceNode"
68
ruby: |
79
END {
@@ -11,7 +13,7 @@ ruby: |
1113
ast: |
1214
OnceNode
1315
attributes:
14-
flags = 0
16+
flags = 1
1517
holder = org.truffleruby.language.control.OnceNode$Holder@...
1618
children:
1719
child =
@@ -56,7 +58,7 @@ ast: |
5658
callTargets = ProcCallTargets(callTargetForProc = block in <top (required)>, callTargetForLambda = null, altCallTargetCompiler = ...$$Lambda$.../0x...@...)
5759
flags = 0
5860
frameOnStackMarkerSlot = 2
59-
sharedMethodInfo = SharedMethodInfo(staticLexicalScope = :: Object, arity = Arity{preRequired = 0, optional = 0, hasRest = false, postRequired = 0, keywordArguments = [], requiredKeywordArgumentsCount = 0, hasKeywordsRest = false}, originName = block in <top (required)>, blockDepth = 1, parseName = block in <top (required)>, notes = <top (required)>, argumentDescriptors = [])
61+
sharedMethodInfo = SharedMethodInfo(staticLexicalScope = :: Object, arity = Arity{preRequired = 0, optional = 0, hasRest = false, isImplicitRest = false, postRequired = 0, keywordArguments = [], requiredKeywordArgumentsCount = 0, hasKeywordsRest = false}, originName = block in <top (required)>, blockDepth = 1, parseName = block in <top (required)>, notes = <top (required)>, argumentDescriptors = [])
6062
type = PROC
6163
call targets:
6264
RubyProcRootNode
@@ -70,7 +72,7 @@ ast: |
7072
redoProfile = false
7173
retryProfile = false
7274
returnID = org.truffleruby.language.control.ReturnID@...
73-
sharedMethodInfo = SharedMethodInfo(staticLexicalScope = :: Object, arity = Arity{preRequired = 0, optional = 0, hasRest = false, postRequired = 0, keywordArguments = [], requiredKeywordArgumentsCount = 0, hasKeywordsRest = false}, originName = block in <top (required)>, blockDepth = 1, parseName = block in <top (required)>, notes = <top (required)>, argumentDescriptors = [])
75+
sharedMethodInfo = SharedMethodInfo(staticLexicalScope = :: Object, arity = Arity{preRequired = 0, optional = 0, hasRest = false, isImplicitRest = false, postRequired = 0, keywordArguments = [], requiredKeywordArgumentsCount = 0, hasKeywordsRest = false}, originName = block in <top (required)>, blockDepth = 1, parseName = block in <top (required)>, notes = <top (required)>, argumentDescriptors = [])
7476
split = HEURISTIC
7577
children:
7678
body =
@@ -103,6 +105,16 @@ ast: |
103105
value = 2
104106
]
105107
receiver =
106-
TruffleKernelOperationsModuleLiteralNode
108+
ReadConstantWithLexicalScopeNode
107109
attributes:
108-
flags = 0
110+
flags = 0
111+
lexicalScope = :: Object
112+
name = "KernelOperations"
113+
children:
114+
getConstantNode =
115+
GetConstantNodeGen
116+
lookupConstantNode =
117+
LookupConstantWithLexicalScopeNodeGen
118+
attributes:
119+
lexicalScope = :: Object
120+
name = "KernelOperations"

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,17 @@ public RubyNode visitPinnedVariableNode(Nodes.PinnedVariableNode node) {
24252425

24262426
@Override
24272427
public RubyNode visitPostExecutionNode(Nodes.PostExecutionNode node) {
2428-
return defaultVisit(node);
2428+
// END blocks run after any other code - not just code in the same file
2429+
// Turn into a call to Truffle::KernelOperations.at_exit
2430+
2431+
// Create Prism CallNode to avoid duplication block literal related logic
2432+
final var receiver = new Nodes.ConstantReadNode("KernelOperations", 0, 0);
2433+
final var arguments = new Nodes.ArgumentsNode(NO_FLAGS, new Nodes.Node[]{ new Nodes.FalseNode(0, 0) }, 0, 0);
2434+
final var block = new Nodes.BlockNode(StringUtils.EMPTY_STRING_ARRAY, 0, null, node.statements, 0, 0);
2435+
2436+
final var callNode = new Nodes.CallNode(NO_FLAGS, receiver, "at_exit", arguments, block, 0, 0).accept(this);
2437+
final RubyNode rubyNode = new OnceNode(callNode);
2438+
return assignPositionAndFlags(node, rubyNode);
24292439
}
24302440

24312441
@Override

0 commit comments

Comments
 (0)