diff --git a/ext/v8/rr.cc b/ext/v8/rr.cc index 5339b27..2e2a943 100644 --- a/ext/v8/rr.cc +++ b/ext/v8/rr.cc @@ -13,5 +13,26 @@ namespace rr { VALUE V8 = rb_define_module("V8"); VALUE V8_C = rb_define_module_under(V8, "C"); return rb_define_module_under(V8_C, name); - } + } + + ClassBuilder::ClassBuilder(const char* name, VALUE superclass) { + this->value = defineClass(name, superclass); + } + + 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; + } + 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; + } } \ No newline at end of file diff --git a/ext/v8/rr.h b/ext/v8/rr.h index 5594d09..24e902b 100644 --- a/ext/v8/rr.h +++ b/ext/v8/rr.h @@ -124,6 +124,18 @@ public: static void Init(); }; +class ClassBuilder { +public: + ClassBuilder(const char* name, VALUE superclass = rb_cObject); + ClassBuilder& defineMethod(const char* name, VALUE (*impl)(VALUE)); + ClassBuilder& defineMethod(const char* name, VALUE (*impl)(VALUE, VALUE)); + ClassBuilder& defineSingletonMethod(const char* name, VALUE (*impl)(VALUE)); + ClassBuilder& defineSingletonMethod(const char* name, VALUE (*impl)(VALUE, VALUE)); + inline operator VALUE() {return this->value;} +private: + VALUE value; +}; + VALUE defineClass(const char *name, VALUE superclass = rb_cObject); VALUE defineModule(const char *name); } diff --git a/ext/v8/string.cc b/ext/v8/string.cc index bc82f58..abb60bb 100644 --- a/ext/v8/string.cc +++ b/ext/v8/string.cc @@ -25,8 +25,8 @@ VALUE String::Class; void String::Init() { rb_gc_register_address(&Class); - Class = defineClass("String"); - RR_DEFINE_SINGLETON_METHOD(Class, "New", &New, 1); - RR_DEFINE_METHOD(Class, "Utf8Value", &Utf8Value, 0); + Class = ClassBuilder("String"). + defineSingletonMethod("New", &New). + defineMethod("Utf8Value", &Utf8Value); } -} \ No newline at end of file +} //namespace rr \ No newline at end of file