| 1 |
#include <stdlib.h> |
| 2 |
#include <mruby.h> |
| 3 |
#include <mruby/error.h> |
| 4 |
#include <mruby/variable.h> |
| 5 |
#include <mruby/presym.h> |
| 6 |
|
| 7 |
#ifndef EXIT_SUCCESS |
| 8 |
# define EXIT_SUCCESS 0 |
| 9 |
#endif |
| 10 |
|
| 11 |
#ifndef EXIT_FAILURE |
| 12 |
# define EXIT_FAILURE 1 |
| 13 |
#endif |
| 14 |
|
| 15 |
static int |
| 16 |
get_status(mrb_state *mrb) |
| 17 |
{ |
| 18 |
mrb_value status = mrb_true_value(); |
| 19 |
|
| 20 |
mrb_get_args(mrb, "|o", &status); |
| 21 |
if (mrb_true_p(status)) return EXIT_SUCCESS; |
| 22 |
if (mrb_false_p(status)) return EXIT_FAILURE; |
| 23 |
return (int)mrb_as_int(mrb, status); |
| 24 |
} |
| 25 |
|
| 26 |
/* |
| 27 |
* call-seq: |
| 28 |
* exit(status=true) |
| 29 |
* |
| 30 |
* Initiates the termination of the Ruby script by raising the |
| 31 |
* SystemExit exception. This exception may be caught. The |
| 32 |
* optional parameter is used to return a status code to the invoking |
| 33 |
* environment. |
| 34 |
* |
| 35 |
* +true+ and +false+ of _status_ means success and failure |
| 36 |
* respectively. The interpretation of other integer values are |
| 37 |
* system dependent. |
| 38 |
* |
| 39 |
* exit(0) |
| 40 |
*/ |
| 41 |
static mrb_value |
| 42 |
f_exit(mrb_state *mrb, mrb_value self) |
| 43 |
{ |
| 44 |
int status = get_status(mrb); |
| 45 |
mrb_value exc = mrb_obj_new(mrb, mrb_exc_get_id(mrb, MRB_SYM(SystemExit)), 0, NULL); |
| 46 |
struct RException *e = mrb_exc_ptr(exc); |
| 47 |
e->flags |= MRB_EXC_EXIT; |
| 48 |
mrb_iv_set(mrb, exc, MRB_SYM(status), mrb_int_value(mrb, (mrb_int)status)); |
| 49 |
mrb_exc_raise(mrb, exc); |
| 50 |
/* not reached */ |
| 51 |
return mrb_nil_value(); |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
* call-seq: |
| 56 |
* exit!(status=true) |
| 57 |
* |
| 58 |
* Exits the process immediately. No exit handlers are run. |
| 59 |
* <em>status</em> is returned to the underlying system as the |
| 60 |
* exit status. |
| 61 |
* |
| 62 |
* exit!(0) |
| 63 |
*/ |
| 64 |
static mrb_value |
| 65 |
f_exit_bang(mrb_state *mrb, mrb_value self) |
| 66 |
{ |
| 67 |
exit(get_status(mrb)); |
| 68 |
/* not reached */ |
| 69 |
return mrb_nil_value(); |
| 70 |
} |
| 71 |
|
| 72 |
void |
| 73 |
mrb_mruby_exit_gem_init(mrb_state* mrb) |
| 74 |
{ |
| 75 |
mrb_define_class_id(mrb, MRB_SYM(SystemExit), E_EXCEPTION); |
| 76 |
mrb_define_method_id(mrb, mrb->kernel_module, MRB_SYM(exit), f_exit, MRB_ARGS_OPT(1)); |
| 77 |
mrb_define_method_id(mrb, mrb->kernel_module, MRB_SYM_B(exit), f_exit_bang, MRB_ARGS_OPT(1)); |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
mrb_mruby_exit_gem_final(mrb_state* mrb) |
| 82 |
{ |
| 83 |
} |