4 #include "rubywrapper.h"
9 static_assert(
sizeof(VALUE) ==
sizeof(Ruby::Value),
"Size mismatch for VALUE.");
11 const int Ruby::Nil = Qnil;
12 const int Ruby::False = Qfalse;
13 const int Ruby::True = Qtrue;
15 Ruby::Ruby(
const char *scriptname) {
17 ruby_script(scriptname);
27 rb_eval_string_protect(str, &state);
31 Ruby::defineGlobalConst(
const char *rbname, Value obj) {
32 rb_define_global_const(rbname, obj);
35 Ruby::wrap_obj(Value cl,
void *p,
void (*f)(
void *)) {
36 return Data_Wrap_Struct(cl, 0, f, p);
39 Ruby::unwrap_obj(Value
self) {
41 Data_Get_Struct(
self, wrapped_t, ptr);
46 Ruby::define_method(Value cl,
const char *rbname, Value (*func)(...),
int argnum) {
47 rb_define_method(cl, rbname, func, argnum);
50 Ruby::define_singleton_method(Value obj,
const char *rbname, Value (*func)(...),
int argnum) {
51 rb_define_singleton_method(obj, rbname, func, argnum);
54 Ruby::define_class(
const char *rbname, Value super) {
55 Value c = rb_define_class(rbname, (super != Nil) ? super : rb_cObject);
56 rb_global_variable(&c);
61 rb_raise(rb_eRuntimeError,
"%s", errstr);
65 bool Ruby::isConvertible<const char*>(Value v) {
66 return TYPE(v) == T_STRING;
69 bool Ruby::isConvertible<long>(Value v) {
73 bool Ruby::isConvertible<double>(Value v) {
74 return (TYPE(v) == T_FLOAT) || FIXNUM_P(v) || (TYPE(v) == T_BIGNUM);
77 bool Ruby::isConvertible<bool>(Value v) {
78 return (TYPE(v) == T_TRUE) || (TYPE(v) == T_FALSE);
83 if( !isConvertible<const char*>(v))
84 throw "Type mismatch to STRING.";
85 return RSTRING_PTR(v);
89 if( !isConvertible<long>(v))
90 throw "Type mismatch to LONG.";
95 if( !isConvertible<double>(v))
96 throw "Type mismatch to NUM.";
101 if( !isConvertible<bool>(v))
102 throw "Type mismatch to NUM.";
103 return (TYPE(v) == T_TRUE) ?
true :
false;
107 if(str.empty())
return rb_str_new2(
"");
108 return rb_str_new2(str.c_str());
133 return rb_float_new(v);
138 return v ? Qtrue : Qfalse;
142 Ruby::printErrorInfo() {