Repository navigation
Description
The mruby-exit gem stores the exit status as an instance variable using MRB_SYM(status) instead of MRB_IVSYM(status), resulting in the value being stored under the symbol status rather than @status. mruby's Ruby-level instance variable APIs only
enumerate/access @-prefixed names, so the status value is effectively unreachable from Ruby code.
This also diverges from CRuby's SystemExit#status accessor that Ruby callers expect to use.
Reproduction
$ git clone https://github.com/mruby/mruby && cd mruby && rake
$ ./bin/mruby -e '
begin
exit 42
rescue SystemExit => e
puts "instance_variables: #{e.instance_variables.inspect}"
puts "@status: #{e.instance_variable_get(:@status).inspect}"
end'
instance_variables: []
@status: nilExpected: instance_variables: [:@status] and @status: 42 (matching CRuby behavior and standard mruby ivar conventions).
Root cause
In mrbgems/mruby-exit/src/mruby-exit.c:
mrb_iv_set(mrb, exc, MRB_SYM(status), mrb_int_value(mrb, (mrb_int)status));
MRB_SYM(name) expands to the plain symbol name. MRB_IVSYM(name) expands to the @name symbol that Ruby ivar machinery expects (see include/mruby/presym.h). Other places in the mruby codebase consistently use MRB_IVSYM for ivar set/get (e.g. src/error.c:492,636,637).
The internal MRB_EXC_EXIT_STATUS macro in include/mruby/error.h:31 uses the same wrong symbol, so CLI runners' MRB_EXC_CHECK_EXIT macro reads the correct value — but Ruby code cannot.
Fix
Change both sites consistently:
diff --git a/mrbgems/mruby-exit/src/mruby-exit.c b/mrbgems/mruby-exit/src/mruby-exit.c
@@ -44,7 +44,7 @@ f_exit(mrb_state *mrb, mrb_value self)
mrb_value exc = mrb_obj_new(mrb, mrb_exc_get_id(mrb, MRB_SYM(SystemExit)), 0, NULL);
struct RException *e = mrb_exc_ptr(exc);
e->flags |= MRB_EXC_EXIT;
- mrb_iv_set(mrb, exc, MRB_SYM(status), mrb_int_value(mrb, (mrb_int)status));
+ mrb_iv_set(mrb, exc, MRB_IVSYM(status), mrb_int_value(mrb, (mrb_int)status));
mrb_exc_raise(mrb, exc);
diff --git a/include/mruby/error.h b/include/mruby/error.h
@@ -28,7 +28,7 @@ struct RException {
/* retrieve status value from exc; need <mruby/variable.h> and <mruby/presym.h> */
-#define MRB_EXC_EXIT_STATUS(mrb,e) ((int)mrb_as_int((mrb),mrb_obj_iv_get((mrb),(e),MRB_SYM(status))))
+#define MRB_EXC_EXIT_STATUS(mrb,e) ((int)mrb_as_int((mrb),mrb_obj_iv_get((mrb),(e),MRB_IVSYM(status))))Optionally add a SystemExit#status accessor method (matching CRuby) — def status; @status; end in an mrblib/mruby-exit.rb, or define it as a C method in mrb_mruby_exit_gem_init.
Affected versions
Introduced in commit 07470eb ("mruby-exit.c: make exit() to raise SystemExit exception", merged for v3.2.0+). Reproducible on current HEAD.
Activity