-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathv8_flags.cc
137 lines (112 loc) · 3.98 KB
/
v8_flags.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <nan.h>
#include "v8.h"
#include "atomicops.h"
#include "flags.h"
#include <node.h>
namespace i = v8::internal;
void ensureBoolean(v8::Local<v8::Value> arg, const char* error) {
if (arg->IsBoolean()) return;
NanThrowError(error, -1);
/*NanThrowError("Need to supply argument of type "
" when setting '" "' flag!", -1);*/
}
void ensureInt32(v8::Local<v8::Value> arg, const char* error) {
if (arg->IsInt32()) return;
NanThrowError(error, -1);
}
void ensureNumber(v8::Local<v8::Value> arg, const char* error) {
if (arg->IsNumber()) return;
NanThrowError(error, -1);
}
void ensureString(v8::Local<v8::Value> arg, const char* error) {
if (arg->IsString()) return;
NanThrowError(error, -1);
}
bool convert(v8::Handle<v8::Boolean> arg) {
return arg->BooleanValue();
}
int32_t convert(v8::Handle<v8::Integer> arg) {
return arg->Int32Value();
}
double convert(v8::Handle<v8::Number> arg) {
return arg->NumberValue();
}
char* convert(v8::Handle<v8::String> arg) {
size_t len;
return NanCString(arg, &len);
}
// The below are just to avoid warning when checking for null for primitive types
// via the Get_##nam macros. Overloaded functions are a quick and dirty way to run differnent code per type.
bool is_null(bool val) {
return false;
}
bool is_null(int val) {
return false;
}
bool is_null(float val) {
return false;
}
bool is_null(const char* val) {
return val == NULL;
}
/* v8 flag-definitions.h not properly cleaning this one up */
#undef DEFINE_float
#undef DEFINE_args
#undef FLAG
#define S(x) #x
#define DEFINE_bool(nam, def, cmt) FLAG(Boolean, nam, Boolean)
#define DEFINE_int(nam, def, cmt) FLAG(Integer, nam, Int32)
#define DEFINE_float(nam, def, cmt) FLAG(Number, nam, Number)
#define DEFINE_string(nam, def, cmt) FLAG(String, nam, String)
#define DEFINE_implication(x, y)
#define DEFINE_neg_implication(x, y)
#define DEFINE_maybe_bool(name, def)
#define FLAG_FULL(type, nam, istype) \
NAN_METHOD(Access_##nam) { \
NanScope(); \
if (args.Length()) { \
ensure##istype(args[0], \
"Need to supply argument of type " S(type) \
" when setting '" S(nam) "' flag!"); \
\
v8::Handle<v8::type> flag = args[0]->To##type(); \
i::FLAG_##nam = convert(flag); \
NanReturnUndefined(); \
} else { \
if (is_null(i::FLAG_##nam)) { \
NanReturnUndefined(); \
} else { \
NanReturnValue(NanNew<v8::type>(i::FLAG_##nam)); \
} \
} \
}
#define FLAG_READONLY(type, nam, istype) \
NAN_METHOD(Access_##nam) { \
NanScope(); \
if (is_null(i::FLAG_##nam)) { \
NanReturnUndefined(); \
} else { \
NanReturnValue(NanNew<v8::type>(i::FLAG_##nam)); \
} \
}
#include "v8_flag_definitions.h"
#undef FLAG_FULL
#undef FLAG_READONLY
#undef FLAG
#define FLAG_FULL(type, nam, istype) \
exports->Set(NanNew<v8::String>(S(nam)), NanNew<v8::FunctionTemplate>(Access_##nam)->GetFunction());
#define FLAG_READONLY(type, nam, istype) FLAG_FULL(type, nam, istype)
void init(v8::Handle<v8::Object> exports) {
#include "v8_flag_definitions.h"
}
#undef DEFINE_bool
#undef DEFINE_int
#undef DEFINE_float
#undef DEFINE_string
#undef FLAG_FULL
#undef FLAG_READONLY
#undef S
#undef DEFINE_implication
#undef DEFINE_neg_implication
#undef DEFINE_maybe_bool
NODE_MODULE(v8_flags, init)