-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmacros.h
178 lines (151 loc) · 6.42 KB
/
macros.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#define SN_THROWS(condition, message) \
if ((condition)) { \
napi_throw_error(env, NULL, message); \
return NULL; \
}
#define SN_STATUS_THROWS(call, message) \
if ((call) != napi_ok) { \
napi_throw_error(env, NULL, message); \
return NULL; \
}
#define SN_ARGV(n, method_name) \
napi_value argv[n]; \
size_t argc = n; \
SN_STATUS_THROWS(napi_get_cb_info(env, info, &argc, argv, NULL, NULL), "") \
if (argc != n) { \
napi_throw_type_error(env, NULL, #method_name " requires " #n " argument(s)"); \
return NULL; \
}
#define SN_TYPE_ASSERT(name, var, type, message) \
napi_valuetype name##_valuetype; \
SN_STATUS_THROWS(napi_typeof(env, var, &name##_valuetype), "") \
if (name##_valuetype != type) { \
napi_throw_type_error(env, NULL, message); \
return NULL; \
}
#define SN_TYPEDARRAY_ASSERT(name, var, message) \
bool name##_is_typedarray; \
SN_STATUS_THROWS(napi_is_typedarray(env, var, &name##_is_typedarray), "") \
if (name##_is_typedarray != true) { \
napi_throw_type_error(env, NULL, message); \
return NULL; \
}
#define SN_ASSERT_LENGTH(length, constant, name) \
SN_THROWS(length != constant, #name " must be " #constant " bytes long")
#define SN_ASSERT_MIN_LENGTH(length, constant, name) \
SN_THROWS(length < constant, #name " must be at least " #constant " bytes long")
#define SN_ASSERT_MAX_LENGTH(length, constant, name) \
SN_THROWS(length > constant, #name " must be at most " #constant " bytes long")
#define SN_UINT8(name, val) \
uint32_t name##_int32; \
if (napi_get_value_uint32(env, val, &name##_int32) != napi_ok) { \
napi_throw_error(env, "EINVAL", "Expected number"); \
return NULL; \
} \
SN_THROWS(name##_int32 > 255, "expect uint8") \
unsigned char name = 0xff & name##_int32;
#define SN_UINT32(name, var) \
uint32_t name; \
if (napi_get_value_uint32(env, var, &name) != napi_ok) { \
napi_throw_error(env, "EINVAL", "Expected number"); \
return NULL; \
}
#define SN_TYPEDARRAY(name, var) \
napi_typedarray_type name##_type; \
void *name##_data; \
size_t name##_length; \
assert(napi_get_typedarray_info(env, (var), &name##_type, &name##_length, &name##_data, NULL, NULL) == napi_ok); \
uint8_t name##_width = typedarray_width(name##_type); \
SN_THROWS(name##_width == 0, "Unexpected TypedArray type.") \
size_t name##_size = name##_length * name##_width;
#define SN_TYPEDARRAY_PTR(name, var) \
napi_typedarray_type name##_type; \
void *name##_data; \
size_t name##_length; \
assert(napi_get_typedarray_info(env, (var), &name##_type, &name##_length, &name##_data, NULL, NULL) == napi_ok); \
uint8_t name##_width = typedarray_width(name##_type); \
SN_THROWS(name##_width == 0, "Unexpected TypedArray type.")
#define SN_OPT_TYPEDARRAY(name, var) \
napi_typedarray_type name##_type; \
size_t name##_length; \
assert(napi_get_typedarray_info(env, (var), &name##_type, &name##_length, &name##_data, NULL, NULL) == napi_ok); \
uint8_t name##_width = typedarray_width(name##_type); \
SN_THROWS(name##_width == 0, "Unexpected TypedArray type") \
name##_size = name##_length * name##_width;
#define SN_OPT_TYPEDARRAY_PTR(name, var) \
napi_typedarray_type name##_type; \
size_t name##_length; \
assert(napi_get_typedarray_info(env, (var), &name##_type, &name##_length, &name, NULL, NULL) == napi_ok); \
uint8_t name##_width = typedarray_width(name##_type); \
SN_THROWS(name##_width == 0, "Unexpected TypedArray type") \
#define SN_BUFFER_CAST(type, name, val) \
type name; \
size_t name##_size; \
SN_STATUS_THROWS(napi_get_buffer_info(env, val, (void**) &name, &name##_size), "")
#define SN_ARGV_UINT8(name, index) \
napi_value name##_argv = argv[index]; \
SN_TYPE_ASSERT(name, name##_argv, napi_number, #name " must be an instance of Number") \
SN_UINT8(name, name##_argv)
#define SN_ARGV_UINT32(name, index) \
napi_value name##_argv = argv[index]; \
SN_TYPE_ASSERT(name, name##_argv, napi_number, #name " must be an instance of Number") \
SN_UINT32(name, name##_argv)
#define SN_ARGV_TYPEDARRAY(name, index) \
napi_value name##_argv = argv[index]; \
SN_TYPEDARRAY_ASSERT(name, name##_argv, #name " must be an instance of TypedArray") \
SN_TYPEDARRAY(name, name##_argv)
#define SN_ARGV_TYPEDARRAY_PTR(name, index) \
napi_value name##_argv = argv[index]; \
SN_TYPEDARRAY_ASSERT(name, name##_argv, #name " must be an instance of TypedArray") \
SN_TYPEDARRAY_PTR(name, name##_argv)
#define SN_ARGV_OPTS_TYPEDARRAY(name, index) \
napi_valuetype name##_valuetype; \
void *name##_data = NULL; \
size_t name##_size = 0; \
SN_STATUS_THROWS(napi_typeof(env, argv[index], &name##_valuetype), "") \
if (name##_valuetype != napi_null) { \
napi_value name##_argv = argv[index]; \
SN_TYPEDARRAY_ASSERT(name, name##_argv, #name " must be an instance of TypedArray") \
SN_OPT_TYPEDARRAY(name, name##_argv) \
}
#define SN_ARGV_OPTS_TYPEDARRAY_PTR(name, index) \
napi_valuetype name##_valuetype; \
void *name = NULL; \
SN_STATUS_THROWS(napi_typeof(env, argv[index], &name##_valuetype), "") \
if (name##_valuetype != napi_null) { \
napi_value name##_argv = argv[index]; \
SN_TYPEDARRAY_ASSERT(name, name##_argv, #name " must be an instance of TypedArray") \
SN_OPT_TYPEDARRAY_PTR(name, name##_argv) \
}
#define SN_ARGV_BUFFER_CAST(type, name, index) \
napi_value name##_argv = argv[index]; \
SN_BUFFER_CAST(type, name, name##_argv)
#define SN_EXPORT_UINT32(name, num) \
{ \
napi_value name##_num; \
SN_STATUS_THROWS(napi_create_uint32(env, (uint32_t) num, &name##_num), "") \
SN_STATUS_THROWS(napi_set_named_property(env, exports, #name, name##_num), "") \
}
#define SN_EXPORT_FUNCTION(name, cb) \
{ \
napi_value name##_fn; \
SN_STATUS_THROWS(napi_create_function(env, #name, NAPI_AUTO_LENGTH, cb, NULL, &name##_fn), "") \
SN_STATUS_THROWS(napi_set_named_property(env, exports, #name, name##_fn), "") \
}
#define SN_CALL(call, message) \
int success = call; \
SN_THROWS(success != 1, message)
#define SN_RETURN(call, message) \
int success = call; \
SN_THROWS(success != 1, message) \
return NULL;
#define SN_RETURN_BOOLEAN(call) \
int success = call; \
napi_value result; \
SN_STATUS_THROWS(napi_get_boolean(env, success, &result), "result not boolean") \
return result;
#define SN_RETURN_BOOLEAN_FROM_1(call) \
int success = call; \
napi_value result; \
SN_STATUS_THROWS(napi_get_boolean(env, success == 1, &result), "result not boolean") \
return result;