From 72e72ada55ef1ddbe19c36393f5449bf6bdd3247 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Wed, 16 May 2012 23:11:23 -0500 Subject: [PATCH] Let enums accept a default value --- ext/v8/object.cc | 15 ++++++--------- ext/v8/rr.h | 10 ++++++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/ext/v8/object.cc b/ext/v8/object.cc index 400df1f..afddff8 100644 --- a/ext/v8/object.cc +++ b/ext/v8/object.cc @@ -82,16 +82,13 @@ VALUE Object::ForceDelete(VALUE self, VALUE key) { VALUE Object::SetAccessor(int argc, VALUE* argv, VALUE self) { VALUE name; VALUE get; VALUE set; VALUE data; VALUE settings; VALUE attribs; rb_scan_args(argc, argv, "24", &name, &get, &set, &data, &settings, &attribs); - AccessControl ac = v8::DEFAULT; - PropertyAttribute attrs = v8::None; - if (RTEST(settings)) { - ac = AccessControl(settings); - } - if (RTEST(attribs)) { - attrs = PropertyAttribute(attribs); - } Accessor::Info accessor(get, set, data); - return Bool(Object(self)->SetAccessor(String(name), accessor.Getter(), accessor.Setter(), accessor, ac, attrs)); + return Bool(Object(self)->SetAccessor(String(name), + accessor.Getter(), + accessor.Setter(), + accessor, + AccessControl(settings), + PropertyAttribute(attribs))); } // diff --git a/ext/v8/rr.h b/ext/v8/rr.h index d690acd..1ba740f 100644 --- a/ext/v8/rr.h +++ b/ext/v8/rr.h @@ -38,14 +38,16 @@ public: template class Enum { public: - Enum(VALUE value) { + Enum(VALUE value, T defaultValue = 0) { this->value = value; + this->defaultValue = defaultValue; } inline operator T() { - return (T)(RTEST(value) ? NUM2INT(value) : 0); + return (T)(RTEST(value) ? NUM2INT(value) : defaultValue); } private: VALUE value; + T defaultValue; }; /** @@ -196,11 +198,11 @@ public: class PropertyAttribute: public Enum { public: - inline PropertyAttribute(VALUE value) : Enum(value) {} + inline PropertyAttribute(VALUE value) : Enum(value, v8::None) {} }; class AccessControl: public Enum { public: - inline AccessControl(VALUE value) : Enum(value) {} + inline AccessControl(VALUE value) : Enum(value, v8::DEFAULT) {} }; class Accessor {