2012-05-01 14:53:01 -04:00
|
|
|
#include "rr.h"
|
|
|
|
|
|
|
|
namespace rr {
|
2012-05-04 01:04:01 -04:00
|
|
|
VALUE defineClass(const char *name, VALUE superclass = rb_cObject) {
|
2012-05-01 14:53:01 -04:00
|
|
|
VALUE V8 = rb_define_module("V8");
|
|
|
|
VALUE V8_C = rb_define_module_under(V8, "C");
|
|
|
|
VALUE klass = rb_define_class_under(V8_C, name, superclass);
|
|
|
|
rb_funcall(klass, rb_intern("private_class_method"), 1, rb_str_new2("new"));
|
|
|
|
return klass;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE defineModule(const char *name) {
|
|
|
|
VALUE V8 = rb_define_module("V8");
|
|
|
|
VALUE V8_C = rb_define_module_under(V8, "C");
|
|
|
|
return rb_define_module_under(V8_C, name);
|
2012-05-03 17:34:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ClassBuilder::ClassBuilder(const char* name, VALUE superclass) {
|
|
|
|
this->value = defineClass(name, superclass);
|
|
|
|
}
|
|
|
|
|
2012-05-04 01:04:01 -04:00
|
|
|
ClassBuilder::ClassBuilder(const char* name, const char* supername) {
|
|
|
|
VALUE superclass = defineClass(supername);
|
|
|
|
this->value = defineClass(name, superclass);
|
|
|
|
}
|
2012-05-08 13:08:58 -04:00
|
|
|
ClassBuilder& ClassBuilder::defineMethod(const char* name, VALUE (*impl)(int, VALUE*, VALUE)) {
|
|
|
|
rb_define_method(this->value, name, (VALUE (*)(...))impl, -1);
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-03 17:34:51 -04:00
|
|
|
ClassBuilder& ClassBuilder::defineMethod(const char* name, VALUE (*impl)(VALUE)) {
|
|
|
|
rb_define_method(this->value, name, (VALUE (*)(...))impl, 0);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ClassBuilder& ClassBuilder::defineMethod(const char* name, VALUE (*impl)(VALUE, VALUE)) {
|
|
|
|
rb_define_method(this->value, name, (VALUE (*)(...))impl, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-03 17:47:23 -04:00
|
|
|
ClassBuilder& ClassBuilder::defineMethod(const char* name, VALUE (*impl)(VALUE, VALUE, VALUE)) {
|
|
|
|
rb_define_method(this->value, name, (VALUE (*)(...))impl, 2);
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-08 13:08:58 -04:00
|
|
|
ClassBuilder& ClassBuilder::defineSingletonMethod(const char* name, VALUE (*impl)(int, VALUE*, VALUE)) {
|
|
|
|
rb_define_singleton_method(this->value, name, (VALUE (*)(...))impl, -1);
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-03 17:34:51 -04:00
|
|
|
ClassBuilder& ClassBuilder::defineSingletonMethod(const char* name, VALUE (*impl)(VALUE)) {
|
|
|
|
rb_define_singleton_method(this->value, name, (VALUE (*)(...))impl, 0);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ClassBuilder& ClassBuilder::defineSingletonMethod(const char* name, VALUE (*impl)(VALUE, VALUE)) {
|
|
|
|
rb_define_singleton_method(this->value, name, (VALUE (*)(...))impl, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-03 17:47:23 -04:00
|
|
|
ClassBuilder& ClassBuilder::defineSingletonMethod(const char* name, VALUE (*impl)(VALUE, VALUE, VALUE)) {
|
|
|
|
rb_define_singleton_method(this->value, name, (VALUE (*)(...))impl, 2);
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-08 12:45:06 -04:00
|
|
|
ClassBuilder& ClassBuilder::defineEnumConst(const char* name, int value) {
|
|
|
|
rb_define_const(this->value, name, INT2FIX(value));
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-08 17:04:47 -04:00
|
|
|
ClassBuilder& ClassBuilder::store(VALUE* storage) {
|
|
|
|
rb_gc_register_address(storage);
|
|
|
|
*storage = this->value;
|
|
|
|
return *this;
|
|
|
|
}
|
2012-05-01 14:53:01 -04:00
|
|
|
}
|