ruby_functor.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Pingus - A free Lemmings clone
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <iostream>
  17. #include "flexlay_wrap.hpp"
  18. #include "ruby_functor.hpp"
  19. void
  20. RubyFunctor::print_error()
  21. {
  22. // FIXME: Potential memory leak
  23. std::cout << "######################################################" << std::endl;
  24. std::cout << "RubyException: "
  25. << RSTRING(rb_inspect(rb_errinfo()))->as.heap.ptr
  26. << std::endl;
  27. VALUE trace = rb_funcall(rb_errinfo(), rb_intern("backtrace"), 0);
  28. for (int i = 0; i < RARRAY(trace)->as.heap.len; ++i)
  29. std::cout << RSTRING(rb_ary_entry(trace, i))->as.heap.ptr << std::endl;
  30. std::cout << "######################################################" << std::endl;
  31. }
  32. VALUE
  33. RubyFunctor::funcall_protect(VALUE self)
  34. {
  35. return rb_funcall(reinterpret_cast<RubyFunctor*>(self)->val.ptr(), rb_intern("call"), 0);
  36. }
  37. VALUE
  38. RubyFunctor::funcall_protect1(VALUE self)
  39. {
  40. VALUE* args = reinterpret_cast<VALUE*>(self);
  41. return rb_funcall(reinterpret_cast<RubyFunctor*>(args[0])->val.ptr(), rb_intern("call"), 1,
  42. args[1]);
  43. }
  44. VALUE
  45. RubyFunctor::funcall_protect2(VALUE self)
  46. {
  47. VALUE* args = reinterpret_cast<VALUE*>(self);
  48. return rb_funcall(reinterpret_cast<RubyFunctor*>(args[0])->val.ptr(), rb_intern("call"), 2,
  49. args[1], args[2]);
  50. }
  51. RubyFunctor::RubyFunctor(const RubyObject& val_)
  52. : val(val_)
  53. {
  54. }
  55. RubyFunctor::~RubyFunctor()
  56. {
  57. }
  58. void
  59. RubyFunctor::operator()()
  60. {
  61. int state = 0;
  62. rb_protect(&RubyFunctor::funcall_protect, reinterpret_cast<VALUE>(this), &state);
  63. if (state)
  64. print_error();
  65. }
  66. // FIXME: Protect the function calls with arguments too
  67. void
  68. RubyFunctor::operator()(int i)
  69. {
  70. //rb_funcall(val.ptr(), rb_intern("call"), 1, INT2FIX(i));
  71. int state = 0;
  72. VALUE args[2];
  73. args[0] = reinterpret_cast<VALUE>(this);
  74. args[1] = INT2FIX(i);
  75. rb_protect(&RubyFunctor::funcall_protect1, reinterpret_cast<VALUE>(args), &state);
  76. if (state)
  77. print_error();
  78. }
  79. void
  80. RubyFunctor::operator()(int x, int y)
  81. {
  82. // rb_funcall(val.ptr(), rb_intern("call"), 2, INT2FIX(x), INT2FIX(y));
  83. int state = 0;
  84. VALUE args[3];
  85. args[0] = reinterpret_cast<VALUE>(this);
  86. args[1] = INT2FIX(x);
  87. args[2] = INT2FIX(y);
  88. rb_protect(&RubyFunctor::funcall_protect2, reinterpret_cast<VALUE>(args), &state);
  89. if (state)
  90. print_error();
  91. }
  92. /* EOF */