Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit de7b0f5

Browse files
committed
Use a constructor for Enum conversion from Ruby values
1 parent ac1d0e0 commit de7b0f5

5 files changed

Lines changed: 41 additions & 31 deletions

File tree

ext/v8/enum.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "rr.h"
2+
3+
#ifndef ENUM_H
4+
#define ENUM_H
5+
6+
namespace rr {
7+
8+
template <class T>
9+
class Enum {
10+
VALUE value;
11+
T defaultValue;
12+
13+
public:
14+
Enum(VALUE value_, T defaultValue_) : value(value_), defaultValue(defaultValue_) { }
15+
16+
operator T() {
17+
return RTEST(value) ? (T)NUM2INT(value) : defaultValue;
18+
}
19+
};
20+
21+
inline void DefineEnums() {
22+
ClassBuilder("AccessControl").
23+
defineConst("DEFAULT", INT2FIX(v8::DEFAULT)).
24+
defineConst("ALL_CAN_READ", INT2FIX(v8::ALL_CAN_READ)).
25+
defineConst("ALL_CAN_WRITE", INT2FIX(v8::ALL_CAN_WRITE)).
26+
defineConst("PROHIBITS_OVERWRITING", INT2FIX(v8::PROHIBITS_OVERWRITING));
27+
28+
ClassBuilder("PropertyAttribute").
29+
defineConst("None", INT2FIX(v8::None)).
30+
defineConst("ReadOnly", INT2FIX(v8::ReadOnly)).
31+
defineConst("DontEnum", INT2FIX(v8::DontEnum)).
32+
defineConst("DontDelete", INT2FIX(v8::DontDelete));
33+
}
34+
35+
};
36+
37+
#endif /* ENUM_H */

ext/v8/enums.h

Lines changed: 0 additions & 27 deletions
This file was deleted.

ext/v8/init.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using namespace rr;
99
extern "C" {
1010
void Init_init() {
1111
V8::Init();
12-
Enums::Init();
12+
DefineEnums();
1313
Isolate::Init();
1414
Handles::Init();
1515
Context::Init();

ext/v8/object.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ namespace rr {
9393
&PropertyCallbackInfo::invokeGetter,
9494
RTEST(setter) ? &PropertyCallbackInfoVoid::invokeSetter : 0,
9595
v8::MaybeLocal<v8::Value>(PropertyCallbackInfo::wrapData(isolate, getter, setter, data)),
96-
RTEST(settings) ? Enums::fromRubyValue<v8::AccessControl>(settings) : v8::DEFAULT,
97-
RTEST(attribute) ? Enums::fromRubyValue<v8::PropertyAttribute>(attribute) : v8::None
96+
Enum<v8::AccessControl>(settings, v8::DEFAULT),
97+
Enum<v8::PropertyAttribute>(attribute, v8::None)
9898
));
9999
}
100100

ext/v8/rr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ inline VALUE not_implemented(const char* message) {
1717
}
1818

1919
#include "class_builder.h"
20-
#include "enums.h"
20+
#include "enum.h"
2121

2222
#include "maybe.h"
2323
#include "equiv.h"

0 commit comments

Comments
 (0)