00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "TRuby.h"
00011
00012 #include "TROOT.h"
00013 #include "TSystem.h"
00014
00015 #include "ruby.h"
00016
00017 ClassImp(TRuby)
00018
00019 extern VALUE cTObject;
00020
00021 bool TRuby::Initialize()
00022 {
00023 static int IsInitialized = 0;
00024
00025 if (!IsInitialized)
00026 {
00027 ruby_init();
00028 IsInitialized = 1;
00029 }
00030
00031 return true;
00032 }
00033
00034 void TRuby::Exec(const char *cmd)
00035 {
00036 int state = 0;
00037
00038 TRuby::Initialize();
00039 rb_eval_string_protect(cmd, &state);
00040
00041
00042 if (state) rb_eval_string("puts $!");
00043 }
00044
00045 TObject *TRuby::Eval(const char* expr)
00046 {
00047 TObject *res;
00048 int state = 0;
00049
00050 TRuby::Initialize();
00051 VALUE ret = rb_eval_string_protect(expr, &state);
00052
00053
00054 if (state)
00055 {
00056 rb_eval_string("puts $!");
00057 return (TObject*)(0);
00058 }
00059
00060 if (NIL_P(ret)) return (TObject*)0;
00061
00062
00063
00064
00065 VALUE ptr = rb_iv_get(ret, "__rr__");
00066 if (!NIL_P(ptr))
00067 {
00068 Data_Get_Struct(rb_iv_get(ret, "__rr__"), TObject, res);
00069 return res;
00070 }
00071
00072 return (TObject*)0;
00073 }
00074
00075 bool TRuby::Bind(TObject *obj, const char *label)
00076 {
00077 VALUE *v = ALLOC(VALUE);
00078
00079 *v = rb_class_new_instance (0, 0, cTObject);
00080
00081 rb_iv_set(*v, "__rr__", Data_Wrap_Struct (cTObject, 0, 0, obj));
00082 rb_define_variable(label, v);
00083
00084 return true;
00085 }
00086
00087 void TRuby::Prompt()
00088 {
00089 gSystem->Exec("irb");
00090 }