Commit d53dd027

d53dd0271e05Merged

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

Description

fix: $PROGRAM_NAME / $0 typed as String, populated from argv[0] (#442)

$PROGRAM_NAME (Ruby's standard alias for $0) was emitted as `static mrb_int gv_PROGRAM_NAME = 0` because scan_features' GlobalVariableReadNode arm registered every unwritten gvar with the default int type. Any `FILE == $PROGRAM_NAME` autorun guard then failed C compile passing mrb_int to sp_str_eq's const char * parameter.

analyze: scan_features now special-cases $PROGRAM_NAME / $0 to register them as type "string" rather than the int default.

codegen: emit_main appends `gv_PROGRAM_NAME = (argc > 0) ? argv[0] : "";` after the sp_argv ingest (gated on the global appearing in @gvar_names with type string, so programs that don't reference it pay nothing).

Note: the runtime value is the binary's argv[0], not the original Ruby source path that `FILE` carries. The canonical `FILE == $PROGRAM_NAME` autorun guard typically won't match on a compiled-binary invocation. This fix is about compile-correctness; the semantic gap between compile-time-baked FILE and runtime argv[0] is a known limitation.

Closes #442

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

Details

Commit
d53dd0271e05
Parent
a8e1b7641e91

spinel_analyze.rbmodifiedView file

@@ -13931,7 +13931,16 @@
1393113931 if gname != "$stderr" && gname != "$stdout" && gname != "$?"
1393213932 if not_in(gname, @gvar_names) == 1
1393313933 @gvar_names.push(gname)
13934- @gvar_types.push("int")
13934+ # $PROGRAM_NAME and $0 are Ruby's standard aliases for the
13935+ # program name as a String, populated from argv[0] at emit_main
13936+ # init. Without the string typing, the canonical
13937+ # `__FILE__ == $PROGRAM_NAME` autorun guard would compare
13938+ # against an mrb_int and fail to compile.
13939+ if gname == "$PROGRAM_NAME" || gname == "$0"
13940+ @gvar_types.push("string")
13941+ else
13942+ @gvar_types.push("int")
13943+ end
1393513944 end
1393613945 end
1393713946 end