Commit 1f18075f

1f18075f73b3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

feat(codegen): $stdout.flush / STDOUT.flush (and stderr variants)

Compiles $stdout.flush, $stderr.flush, STDOUT.flush, STDERR.flush to the corresponding fflush() call instead of warning "cannot resolve call to 'flush' on int (emitting 0)".

Motivation: on Windows / MSYS2 mingw64 ruby, stdio is fully buffered when stdout is a pipe and ruby doesn't auto-flush before system() the way Linux does. make test runs each .rb under command substitution, so the system-test repro printed hello_from_system before the surrounding putses. Letting test code request an explicit flush fixes the order on the reference side; the spinel binary already emits fflush(stdout) before the system() call.

Also adds the explicit flush to test/system.rb.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
1f18075f73b3
Parent
d920667f4cc7

spinel_codegen.rbmodifiedView file

@@ -21959,6 +21959,33 @@
2195921959 end
2196021960 end
2196121961 end
21962+ # $stdout.flush / $stderr.flush / STDOUT.flush / STDERR.flush.
21963+ # Important on Windows where stdio is fully-buffered when stdout
21964+ # is a pipe — without an explicit flush, output written via puts
21965+ # before a `system(...)` call appears after the child process's
21966+ # output. The expression-context system path already emits
21967+ # fflush(stdout) implicitly; this lets user code do the same
21968+ # explicitly.
21969+ if mname == "flush" && recv >= 0
21970+ stream = ""
21971+ if @nd_type[recv] == "GlobalVariableReadNode"
21972+ if @nd_name[recv] == "$stdout"
21973+ stream = "stdout"
21974+ elsif @nd_name[recv] == "$stderr"
21975+ stream = "stderr"
21976+ end
21977+ elsif @nd_type[recv] == "ConstantReadNode"
21978+ if @nd_name[recv] == "STDOUT"
21979+ stream = "stdout"
21980+ elsif @nd_name[recv] == "STDERR"
21981+ stream = "stderr"
21982+ end
21983+ end
21984+ if stream != ""
21985+ emit(" fflush(" + stream + ");")
21986+ return 1
21987+ end
21988+ end
2196221989 if mname == "print"
2196321990 if recv < 0
2196421991 compile_print(nid)