51 lines
1.1 KiB
C
51 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <ruby.h>
|
|
#include <stdbool.h>
|
|
|
|
VALUE ruby_protect_eval_string_rescue(VALUE exception, VALUE exception_object)
|
|
{
|
|
*(bool *)exception = true;
|
|
fprintf(stderr, "exception: %s\n",
|
|
rb_obj_classname(exception_object));
|
|
return Qnil;
|
|
}
|
|
|
|
VALUE ruby_protect_eval_string(const char * ruby_expression, bool * exception)
|
|
{
|
|
*exception = false;
|
|
return rb_rescue(rb_eval_string, (VALUE)ruby_expression,
|
|
ruby_protect_eval_string_rescue, (VALUE)exception);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
if (argc < 2)
|
|
{
|
|
fprintf(stderr, "Usage: %s <ruby-expression>\n", argv[0]);
|
|
return -1;
|
|
}
|
|
VALUE v = INT2FIX(0);
|
|
bool exception;
|
|
{
|
|
RUBY_INIT_STACK;
|
|
ruby_init();
|
|
v = ruby_protect_eval_string(argv[1], &exception);
|
|
}
|
|
if (exception)
|
|
{
|
|
fprintf(stderr, "Exception!\n");
|
|
}
|
|
else
|
|
{
|
|
if (FIXNUM_P(v))
|
|
{
|
|
printf("Success! Result is %d\n", FIX2INT(v));
|
|
}
|
|
else
|
|
{
|
|
printf("Success! Non-Fixnum result\n");
|
|
}
|
|
}
|
|
return 0;
|
|
}
|