Daniel Harple
8/8/2006 9:58:00 PM
On Aug 8, 2006, at 5:34 PM, Mauricio Fernandez wrote:
> Of course it won't work that way, being a member function (it is
> given a
> reference to this too...).
>
> You'd have to do something like this (it's been a while without
> writing any
> C++, so take this with a grain of salt):
Thanks, I ended up with something similar.
class Sandbox
{
public:
Sandbox() {
sandbox_ = rb_funcall(rb_const_get(rb_cObject, rb_intern
("Sandbox")),
rb_intern("safe"),
0);
}
virtual ~Sandbox() { }
VALUE eval(char * code) {
int status = 0;
VALUE args[2];
args[0] = reinterpret_cast<VALUE>(this);
args[1] = rb_str_new2(code);
VALUE val = rb_protect(eval_protect,
reinterpret_cast<VALUE>(args),
&status);
if (status) { }
return val;
}
VALUE & sandbox() {
return sandbox_;
}
private:
VALUE sandbox_;
static VALUE eval_protect(VALUE data) {
VALUE *args = reinterpret_cast<VALUE *>(data);
return rb_funcall(reinterpret_cast<Sandbox *>(args[0])-
>sandbox(),
rb_intern("eval"),
1,
args[1]);
}
};
-- Daniel