Commit 9454c074
9454c0742da0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #309 from ryanseys/rs-up-post-execution
feat(parse,codegen): support PostExecutionNode (END { ... })
Details
- Commit
- 9454c0742da0
- Parent
- 7fc5e446c39f
- Merge parent
- 8897b70f6018
spinel_codegen.rbmodifiedView file
@@ -228,6 +228,12 @@
228228
# the top of main() during emit_main.229229
@pre_execution_blocks = []230230
231+
# `END { ... }` bodies, in source-encounter order. Each emits a232+
# static C function; main() startup registers them via atexit()233+
# which naturally invokes handlers LIFO -- matches CRuby's234+
# reverse-of-source-order END execution.235+
@post_execution_blocks = []236+
231237
# ---- Scope stack for local variables ----232238
@scope_names = "".split(",")233239
@scope_types = "".split(",")@@ -5673,6 +5679,12 @@
56735679
@pre_execution_blocks.push(bid)56745680
end56755681
end5682+
if @nd_type[sid] == "PostExecutionNode"5683+
bid = @nd_body[sid]5684+
if bid >= 05685+
@post_execution_blocks.push(bid)5686+
end5687+
end56765688
}56775689
56785690
# Pass 2.6: hoist `recv.instance_eval do ... end` blocks into@@ -13710,6 +13722,11 @@
1371013722
emit_class_methods1371113723
emit_ieval_funcs1371213724
emit_toplevel_methods13725+
# `END { ... }` -- emit one zero-arg static C function per13726+
# PostExecutionNode body. main() startup will atexit()-register13727+
# them in source order; atexit invokes handlers LIFO, matching13728+
# CRuby's reverse-order END execution.13729+
emit_post_execution_funcs1371313730
# Emit lambda functions before main (they are generated during compilation)1371413731
# We emit them in emit_main after forward declarations1371513732
emit_main@@ -16764,6 +16781,29 @@
1676416781
end1676516782
end1676616783
16784+
# `END { ... }` -- one zero-arg static C function per registered16785+
# PostExecutionNode body. main() registers them via atexit() at16786+
# startup. atexit invokes handlers LIFO, matching CRuby's16787+
# reverse-of-source-order END execution.16788+
def emit_post_execution_funcs16789+
pe = 016790+
while pe < @post_execution_blocks.length16791+
bnid = @post_execution_blocks[pe]16792+
emit_raw("static void sp_end_block_" + pe.to_s + "(void) {")16793+
@indent = 116794+
@in_main = 116795+
push_scope16796+
if bnid >= 016797+
compile_stmt(bnid)16798+
end16799+
pop_scope16800+
@in_main = 016801+
emit_raw("}")16802+
emit_raw("")16803+
pe = pe + 116804+
end16805+
end16806+
1676716807
def emit_toplevel_method(mi)1676816808
mfullname = @meth_names[mi]1676916809
@current_method_name = mfullname@@ -18437,6 +18477,17 @@
1843718477
pi = pi + 11843818478
end1843918479
18480+
# `END { ... }` register-atexit: each PostExecutionNode body18481+
# was emitted as a static `sp_end_block_<n>` function during18482+
# emit_post_execution_funcs. We register them in source order;18483+
# atexit invokes handlers LIFO, matching CRuby's reverse-order18484+
# END semantics.18485+
pe = 018486+
while pe < @post_execution_blocks.length18487+
emit(" atexit(sp_end_block_" + pe.to_s + ");")18488+
pe = pe + 118489+
end18490+
1844018491
# Compile main statements1844118492
stmts.each { |sid|1844218493
if @nd_type[sid] != "DefNode"@@ -26310,6 +26361,13 @@
2631026361
# source location.2631126362
return2631226363
end26364+
if t == "PostExecutionNode"26365+
# `END { ... }`. collect_all pushed the body onto26366+
# @post_execution_blocks; emit_post_execution_funcs emits the26367+
# static sp_end_block_<n> function and emit_main inserts an26368+
# atexit() call at startup.26369+
return26370+
end2631326371
if t == "LocalVariableWriteNode"2631426372
lname = @nd_name[nid]2631526373
# Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/post_execution.rbaddedView file
Loading diff…
test/post_execution.rb.expectedaddedView file
Loading diff…