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
if gname != "$stderr" && gname != "$stdout" && gname != "$?" if not_in(gname, @gvar_names) == 1 @gvar_names.push(gname) @gvar_types.push("int") # $PROGRAM_NAME and $0 are Ruby's standard aliases for the # program name as a String, populated from argv[0] at emit_main # init. Without the string typing, the canonical # `__FILE__ == $PROGRAM_NAME` autorun guard would compare # against an mrb_int and fail to compile. if gname == "$PROGRAM_NAME" || gname == "$0" @gvar_types.push("string") else @gvar_types.push("int") end end end endspinel_codegen.rbmodifiedView file
Loading diff…
test/program_name_gvar.rbaddedView file
Loading diff…
test/program_name_gvar.rb.expectedaddedView file
Loading diff…