Guest User

Untitled

a guest
Jan 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ruby.h>
  3. #include <stdbool.h>
  4.  
  5. VALUE ruby_protect_eval_string_rescue(VALUE exception, VALUE exception_object)
  6. {
  7. *(bool *)exception = true;
  8. fprintf(stderr, "exception: %s\n",
  9. rb_obj_classname(exception_object));
  10. return Qnil;
  11. }
  12.  
  13. VALUE ruby_protect_eval_string(const char * ruby_expression, bool * exception)
  14. {
  15. *exception = false;
  16. return rb_rescue(rb_eval_string, (VALUE)ruby_expression,
  17. ruby_protect_eval_string_rescue, (VALUE)exception);
  18. }
  19.  
  20. int main(int argc, char **argv)
  21. {
  22. if (argc < 2)
  23. {
  24. fprintf(stderr, "Usage: %s <ruby-expression>\n", argv[0]);
  25. return -1;
  26. }
  27. VALUE v = INT2FIX(0);
  28. bool exception;
  29. {
  30. RUBY_INIT_STACK;
  31. ruby_init();
  32. v = ruby_protect_eval_string(argv[1], &exception);
  33. }
  34. if (exception)
  35. {
  36. fprintf(stderr, "Exception!\n");
  37. }
  38. else
  39. {
  40. if (FIXNUM_P(v))
  41. {
  42. printf("Success! Result is %d\n", FIX2INT(v));
  43. }
  44. else
  45. {
  46. printf("Success! Non-Fixnum result\n");
  47. }
  48. }
  49. return 0;
  50. }
Add Comment
Please, Sign In to add comment