-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjs_commandlineparser.cpp
More file actions
279 lines (215 loc) · 7.49 KB
/
Copy pathjs_commandlineparser.cpp
File metadata and controls
279 lines (215 loc) · 7.49 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "js_commandlineparser.hpp"
#include "js_cv.hpp"
#include "include/js_alloc.hpp"
#include "include/js_array.hpp"
#include "include/jsbindings.hpp"
#include "include/util.hpp"
extern "C" {
thread_local JSValue commandlineparser_proto, commandlineparser_class;
thread_local JSClassID js_commandlineparser_class_id;
}
static JSValue
js_commandlineparser_constructor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst argv[]) {
JSCommandLineParserData *clp, *other;
JSValue obj = JS_UNDEFINED, proto;
if(!(clp = js_allocate<JSCommandLineParserData>(ctx)))
return JS_EXCEPTION;
if(argc == 0) {
JS_ThrowInternalError(ctx, "argument expected");
goto fail;
}
if((other = js_commandlineparser_data(argv[0]))) {
new(clp) JSCommandLineParserData(*other);
} else {
std::vector<cv::String> args;
cv::String keys;
int i = 0;
if(js_is_array(ctx, argv[i])) {
js_array_to(ctx, argv[i++], args);
} else {
JSValue sa = js_global_get(ctx, "scriptArgs");
js_array_to(ctx, sa, args);
JS_FreeValue(ctx, sa);
}
size_t argn = args.size();
std::vector<const char*> stra;
stra.resize(argn + 1);
std::transform(args.begin(), args.end(), stra.begin(), [](const std::string& str) -> const char* { return str.c_str(); });
stra[argn] = nullptr;
const char** strb = static_cast<const char**>(js_mallocz(ctx, sizeof(const char*) * (argn + 16)));
for(size_t j = 0; j < stra.size(); ++j)
strb[j] = stra[j] ? js_strdup(ctx, stra[j]) : nullptr;
if(i < argc)
js_value_to(ctx, argv[i], keys);
try {
new(clp) JSCommandLineParserData(argn, strb, keys);
} catch(const cv::Exception& e) {
js_cv_throw(ctx, e);
goto fail;
}
}
/* using new_target to get the prototype is necessary when the class is extended. */
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if(JS_IsException(proto))
goto fail;
obj = JS_NewObjectProtoClass(ctx, proto, js_commandlineparser_class_id);
JS_FreeValue(ctx, proto);
if(JS_IsException(obj))
goto fail;
JS_SetOpaque(obj, clp);
return obj;
fail:
js_deallocate(ctx, clp);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
JSCommandLineParserData*
js_commandlineparser_data(JSValueConst val) {
return static_cast<JSCommandLineParserData*>(JS_GetOpaque(val, js_commandlineparser_class_id));
}
JSCommandLineParserData*
js_commandlineparser_data2(JSContext* ctx, JSValueConst val) {
return static_cast<JSCommandLineParserData*>(JS_GetOpaque2(ctx, val, js_commandlineparser_class_id));
}
static JSValue
js_commandlineparser_get(JSContext* ctx, JSValueConst this_val, int magic) {
JSCommandLineParserData* clp;
JSValue ret = JS_UNDEFINED;
if(!(clp = js_commandlineparser_data2(ctx, this_val)))
return JS_EXCEPTION;
switch(magic) {}
return ret;
}
static JSValue
js_commandlineparser_set(JSContext* ctx, JSValueConst this_val, JSValueConst val, int magic) {
JSCommandLineParserData* clp;
if(!(clp = js_commandlineparser_data2(ctx, this_val)))
return JS_EXCEPTION;
switch(magic) {}
return JS_UNDEFINED;
}
enum {
METHOD_ABOUT,
METHOD_CHECK,
METHOD_GET,
METHOD_GET_PATH_TO_APPLICATION,
METHOD_HAS,
METHOD_PRINT_ERRORS,
METHOD_PRINT_MESSAGE,
};
static JSValue
js_commandlineparser_method(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
JSCommandLineParserData* clp;
JSValue ret = JS_UNDEFINED;
if(!(clp = js_commandlineparser_data2(ctx, this_val)))
return JS_EXCEPTION;
try {
switch(magic) {
case METHOD_ABOUT: {
cv::String message;
js_value_to(ctx, argv[0], message);
clp->about(message);
break;
}
case METHOD_CHECK: {
ret = JS_NewBool(ctx, clp->check());
break;
}
case METHOD_GET: {
BOOL space_delete = TRUE;
cv::String s;
if(argc > 1)
space_delete = JS_ToBool(ctx, argv[1]);
if(JS_IsNumber(argv[0])) {
int32_t index = -1;
JS_ToInt32(ctx, &index, argv[0]);
s = clp->get<cv::String>(index, space_delete);
} else {
const char* name;
name = JS_ToCString(ctx, argv[0]);
s = clp->get<cv::String>(name, space_delete);
JS_FreeCString(ctx, name);
}
ret = JS_NewString(ctx, s.c_str());
break;
}
case METHOD_GET_PATH_TO_APPLICATION: {
cv::String s = clp->getPathToApplication();
ret = JS_NewString(ctx, s.c_str());
break;
}
case METHOD_HAS: {
const char* name;
name = JS_ToCString(ctx, argv[0]);
ret = JS_NewBool(ctx, clp->has(name));
JS_FreeCString(ctx, name);
break;
}
case METHOD_PRINT_ERRORS: {
clp->printErrors();
break;
}
case METHOD_PRINT_MESSAGE: {
clp->printMessage();
break;
}
}
} catch(const cv::Exception& e) { return js_cv_throw(ctx, e); }
return ret;
}
void
js_commandlineparser_finalizer(JSRuntime* rt, JSValue val) {
JSCommandLineParserData* clp;
/* Note: 'clp' can be NULL in case JS_SetOpaque() was not called */
if((clp = js_commandlineparser_data(val))) {
clp->~JSCommandLineParserData();
js_deallocate(rt, clp);
}
}
JSClassDef js_commandlineparser_class = {
.class_name = "CommandLineParser",
.finalizer = js_commandlineparser_finalizer,
};
JSCFunctionListEntry js_commandlineparser_proto_funcs[] = {
JS_CFUNC_MAGIC_DEF("about", 1, js_commandlineparser_method, METHOD_ABOUT),
JS_CFUNC_MAGIC_DEF("check", 0, js_commandlineparser_method, METHOD_CHECK),
JS_CFUNC_MAGIC_DEF("get", 1, js_commandlineparser_method, METHOD_GET),
JS_CFUNC_MAGIC_DEF("getPathToApplication", 0, js_commandlineparser_method, METHOD_GET_PATH_TO_APPLICATION),
JS_CFUNC_MAGIC_DEF("has", 1, js_commandlineparser_method, METHOD_HAS),
JS_CFUNC_MAGIC_DEF("printErrors", 0, js_commandlineparser_method, METHOD_PRINT_ERRORS),
JS_CFUNC_MAGIC_DEF("printMessage", 0, js_commandlineparser_method, METHOD_PRINT_MESSAGE),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "CommandLineParser", JS_PROP_CONFIGURABLE),
};
extern "C" int
js_commandlineparser_init(JSContext* ctx, JSModuleDef* m) {
/* create the CommandLineParser class */
JS_NewClassID(&js_commandlineparser_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_commandlineparser_class_id, &js_commandlineparser_class);
commandlineparser_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, commandlineparser_proto, js_commandlineparser_proto_funcs, countof(js_commandlineparser_proto_funcs));
JS_SetClassProto(ctx, js_commandlineparser_class_id, commandlineparser_proto);
commandlineparser_class = JS_NewCFunction2(ctx, js_commandlineparser_constructor, "CommandLineParser", 0, JS_CFUNC_constructor, 0);
/* set proto.constructor and ctor.prototype */
JS_SetConstructor(ctx, commandlineparser_class, commandlineparser_proto);
if(m) {
JS_SetModuleExport(ctx, m, "CommandLineParser", commandlineparser_class);
}
return 0;
}
extern "C" void
js_commandlineparser_export(JSContext* ctx, JSModuleDef* m) {
JS_AddModuleExport(ctx, m, "CommandLineParser");
}
#ifdef JS_COMMANDLINEPARSER_MODULE
#define JS_INIT_MODULE VISIBLE js_init_module
#else
#define JS_INIT_MODULE js_init_module_commandlineparser
#endif
extern "C" JSModuleDef*
JS_INIT_MODULE(JSContext* ctx, const char* module_name) {
JSModuleDef* m;
if(!(m = JS_NewCModule(ctx, module_name, &js_commandlineparser_init)))
return NULL;
js_commandlineparser_export(ctx, m);
return m;
}