Commit 52b22e81

52b22e81cc1eMerged

Yusuke Endoh <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): emit sp_Argv typedef before function bodies

ARGV.length / ARGV[i] may be referenced from any function (e.g. a user-defined def main; ARGV.each {|a| ... }; end called from the C main()), but emit_main previously emitted the typedef ... sp_Argv; and static sp_Argv sp_argv; declarations just before int main(...) — past every other function body in source order — so functions that referenced sp_argv ahead of that point compiled with an undeclared identifier.

Hoist the two declarations into emit_forward_decls so they precede every function body. The runtime initialization (sp_argv.len = argc - 1; ...) stays in emit_main where main() can fill it in from argc / argv.

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

Details

Commit
52b22e81cc1e
Parent
139b5d374568

spinel_codegen.rbmodifiedView file

@@ -17819,6 +17819,14 @@
1781917819
1782017820 # ---- Forward declarations ----
1782117821 def emit_forward_decls
17822+ # ARGV (sp_argv) is referenced from any function that uses
17823+ # `ARGV.length` / `ARGV[i]`, so the declaration has to precede
17824+ # all function bodies — not just main()'s. emit_main keeps the
17825+ # runtime initialization (`sp_argv.len = argc - 1; ...`) where
17826+ # main() can fill it in from `argc` / `argv`.
17827+ emit_raw("typedef struct{const char**data;mrb_int len;}sp_Argv;")
17828+ emit_raw("static sp_Argv sp_argv;")
17829+ emit_raw("")
1782217830 # Emit block helper functions accumulated during collection
1782317831 if @block_funcs != ""
1782417832 emit_raw(@block_funcs)
@@ -20806,9 +20814,6 @@
2080620814
2080720815 def emit_main
2080820816 stmts = get_body_stmts(@root_id)
20809- emit_raw("typedef struct{const char**data;mrb_int len;}sp_Argv;")
20810- emit_raw("static sp_Argv sp_argv;")
20811- emit_raw("")
2081220817 emit_raw("int main(int argc,char**argv){")
2081320818 emit_raw(" sp_argv.len=argc-1;sp_argv.data=(const char**)malloc(sizeof(const char*)*(argc>1?argc-1:1));{int _i;for(_i=0;_i<sp_argv.len;_i++)sp_argv.data[_i]=sp_str_dup_external(argv[_i+1]);}")
2081420819 if @needs_rand == 1