mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
Introduce a friendlier ClassBuilder syntax.
This commit is contained in:
parent
ba7f5dae8a
commit
9884c89074
3 changed files with 38 additions and 5 deletions
21
ext/v8/rr.cc
21
ext/v8/rr.cc
|
@ -14,4 +14,25 @@ namespace rr {
|
|||
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;
|
||||
}
|
||||
}
|
12
ext/v8/rr.h
12
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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
} //namespace rr
|
Loading…
Add table
Reference in a new issue