-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnode_defs.h
156 lines (130 loc) · 6.48 KB
/
node_defs.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2011 Mariano Iglesias <[email protected]>
#ifndef NODE_DEFS_H_
#define NODE_DEFS_H_
#include <node.h>
#define NODE_CONSTANT(constant) v8::Integer::New(constant)
#define NODE_PERSISTENT_SYMBOL(s) v8::Persistent<v8::String>::New(v8::String::NewSymbol(s))
#define NODE_ADD_PROTOTYPE_METHOD(templ, name, callback) \
do { \
v8::Local<v8::Signature> __callback##_SIG = v8::Signature::New(templ); \
v8::Local<v8::FunctionTemplate> __callback##_TEM = \
v8::FunctionTemplate::New(callback, v8::Handle<v8::Value>(), \
__callback##_SIG); \
templ->PrototypeTemplate()->Set(v8::String::NewSymbol(name), \
__callback##_TEM); \
} while (0)
#define NODE_ADD_CONSTANT(target, name, constant) \
(target)->Set(v8::String::NewSymbol(#name), \
v8::Integer::New(constant), \
static_cast<v8::PropertyAttribute>(v8::ReadOnly|v8::DontDelete))
#define THROW_EXCEPTION(message) \
return v8::ThrowException(v8::Exception::Error(v8::String::New(message)));
#define ARG_CHECK_OPTIONAL_STRING(I, VAR) \
if (args.Length() > I && !args[I]->IsString()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid string") \
}
#define ARG_CHECK_STRING(I, VAR) \
if (args.Length() <= I) { \
THROW_EXCEPTION("Argument \"" #VAR "\" is mandatory") \
} else if (!args[I]->IsString()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid string") \
}
#define ARG_CHECK_OPTIONAL_BOOL(I, VAR) \
if (args.Length() > I && !args[I]->IsBoolean()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid boolean") \
}
#define ARG_CHECK_BOOL(I, VAR) \
if (args.Length() <= I) { \
THROW_EXCEPTION("Argument \"" #VAR "\" is mandatory") \
} else if (!args[I]->IsBoolean()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid boolean") \
}
#define ARG_CHECK_OPTIONAL_UINT32(I, VAR) \
if (args.Length() > I && !args[I]->IsUint32()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid UINT32") \
}
#define ARG_CHECK_UINT32(I, VAR) \
if (args.Length() <= I) { \
THROW_EXCEPTION("Argument \"" #VAR "\" is mandatory") \
} else if (!args[I]->IsUint32()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid UINT32") \
}
#define ARG_CHECK_OPTIONAL_OBJECT(I, VAR) \
if (args.Length() > I && (!args[I]->IsObject() || args[I]->IsFunction() || args[I]->IsUndefined())) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid object") \
}
#define ARG_CHECK_OBJECT(I, VAR) \
if (args.Length() <= I) { \
THROW_EXCEPTION("Argument \"" #VAR "\" is mandatory") \
} else if (!args[I]->IsObject() || args[I]->IsFunction() || args[I]->IsArray() || args[I]->IsDate() || args[I]->IsUndefined()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid object") \
}
#define ARG_CHECK_OPTIONAL_FUNCTION(I, VAR) \
if (args.Length() > I && !args[I]->IsFunction()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid function") \
}
#define ARG_CHECK_FUNCTION(I, VAR) \
if (args.Length() <= I) { \
THROW_EXCEPTION("Argument \"" #VAR "\" is mandatory") \
} else if (!args[I]->IsFunction()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid function") \
}
#define ARG_CHECK_OPTIONAL_ARRAY(I, VAR) \
if (args.Length() > I && !args[I]->IsArray()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid array") \
}
#define ARG_CHECK_ARRAY(I, VAR) \
if (args.Length() <= I) { \
THROW_EXCEPTION("Argument \"" #VAR "\" is mandatory") \
} else if (!args[I]->IsArray()) { \
THROW_EXCEPTION("Argument \"" #VAR "\" must be a valid array") \
}
#define ARG_CHECK_OBJECT_ATTR_STRING(VAR, KEY) \
v8::Local<v8::String> KEY##_##key = v8::String::New("" #KEY ""); \
if (!VAR->Has(KEY##_##key)) { \
THROW_EXCEPTION("Option \"" #KEY "\" is mandatory") \
} else if (!VAR->Get(KEY##_##key)->IsString()) { \
THROW_EXCEPTION("Option \"" #KEY "\" must be a valid string") \
}
#define ARG_CHECK_OBJECT_ATTR_OPTIONAL_STRING(VAR, KEY) \
v8::Local<v8::String> KEY##_##key = v8::String::New("" #KEY ""); \
if (VAR->Has(KEY##_##key) && !VAR->Get(KEY##_##key)->IsString()) { \
THROW_EXCEPTION("Option \"" #KEY "\" must be a valid string") \
}
#define ARG_CHECK_OBJECT_ATTR_UINT32(VAR, KEY) \
v8::Local<v8::String> KEY##_##key = v8::String::New("" #KEY ""); \
if (!VAR->Has(KEY##_##key)) { \
THROW_EXCEPTION("Option \"" #KEY "\" is mandatory") \
} else if (!VAR->Get(KEY##_##key)->IsUint32()) { \
THROW_EXCEPTION("Option \"" #KEY "\" must be a valid UINT32") \
}
#define ARG_CHECK_OBJECT_ATTR_OPTIONAL_UINT32(VAR, KEY) \
v8::Local<v8::String> KEY##_##key = v8::String::New("" #KEY ""); \
if (VAR->Has(KEY##_##key) && !VAR->Get(KEY##_##key)->IsUint32()) { \
THROW_EXCEPTION("Option \"" #KEY "\" must be a valid UINT32") \
}
#define ARG_CHECK_OBJECT_ATTR_BOOL(VAR, KEY) \
v8::Local<v8::String> KEY##_##key = v8::String::New("" #KEY ""); \
if (!VAR->Has(KEY##_##key)) { \
THROW_EXCEPTION("Option \"" #KEY "\" is mandatory") \
} else if (!VAR->Get(KEY##_##key)->IsBoolean()) { \
THROW_EXCEPTION("Option \"" #KEY "\" must be a valid boolean") \
}
#define ARG_CHECK_OBJECT_ATTR_OPTIONAL_BOOL(VAR, KEY) \
v8::Local<v8::String> KEY##_##key = v8::String::New("" #KEY ""); \
if (VAR->Has(KEY##_##key) && !VAR->Get(KEY##_##key)->IsBoolean()) { \
THROW_EXCEPTION("Option \"" #KEY "\" must be a valid boolean") \
}
#define ARG_CHECK_OBJECT_ATTR_FUNCTION(VAR, KEY) \
v8::Local<v8::String> KEY##_##key = v8::String::New("" #KEY ""); \
if (!VAR->Has(KEY##_##key)) { \
THROW_EXCEPTION("Option \"" #KEY "\" is mandatory") \
} else if (!VAR->Get(KEY##_##key)->IsFunction()) { \
THROW_EXCEPTION("Option \"" #KEY "\" must be a valid function") \
}
#define ARG_CHECK_OBJECT_ATTR_OPTIONAL_FUNCTION(VAR, KEY) \
v8::Local<v8::String> KEY##_##key = v8::String::New("" #KEY ""); \
if (VAR->Has(KEY##_##key) && !VAR->Get(KEY##_##key)->IsFunction()) { \
THROW_EXCEPTION("Option \"" #KEY "\" must be a valid function") \
}
#endif // NODE_DEFS_H_