Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/mruby/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct RException {
#define MRB_EXC_EXIT 65536
#define MRB_EXC_EXIT_P(e) ((e)->flags & MRB_EXC_EXIT)
/* 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))))
/* exit with SystemExit status */
#define MRB_EXC_CHECK_EXIT(mrb,e) do {if (MRB_EXC_EXIT_P(e)) exit(MRB_EXC_EXIT_STATUS((mrb),(e)));} while (0)

Expand Down
5 changes: 5 additions & 0 deletions mrbgems/mruby-exit/mrblib/mruby-exit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class SystemExit
def status
@status
end
end
Comment on lines +1 to +5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To fully match CRuby's behavior and prevent SystemExit#status from returning nil when SystemExit is instantiated directly (e.g., via SystemExit.new), we should implement a custom initialize method that supports the standard arguments: SystemExit.new(status), SystemExit.new(status, message), and SystemExit.new(message). This ensures @status is always initialized to a valid integer (defaulting to 0), matching CRuby's behavior.

class SystemExit
  def initialize(*args)
    status = 0
    message = nil
    if args.size == 1
      if args[0].is_a?(Integer)
        status = args[0]
      else
        message = args[0]
      end
    elsif args.size >= 2
      status = args[0]
      message = args[1]
    end
    if message
      super(message)
    else
      super()
    end
    @status = status
  end

  def status
    @status
  end
end

2 changes: 1 addition & 1 deletion mrbgems/mruby-exit/src/mruby_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
/* not reached */
return mrb_nil_value();
Expand Down
Loading