-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathutils.c
397 lines (348 loc) · 10.6 KB
/
utils.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* txiki.js
*
* Copyright (c) 2019-present Saúl Ibarra Corretgé <s@saghul.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "utils.h"
#include "private.h"
#include "tjs.h"
#include <stdlib.h>
#include <string.h>
void tjs_assert(const struct AssertionInfo info) {
fprintf(stderr,
"%s:%s%s Assertion `%s' failed.\n",
info.file_line,
info.function,
*info.function ? ":" : "",
info.message);
fflush(stderr);
abort();
}
uv_loop_t *tjs_get_loop(JSContext *ctx) {
TJSRuntime *qrt = JS_GetContextOpaque(ctx);
CHECK_NOT_NULL(qrt);
return TJS_GetLoop(qrt);
}
int tjs_obj2addr(JSContext *ctx, JSValue obj, struct sockaddr_storage *ss) {
JSValue js_ip;
JSValue js_port;
const char *ip;
uint32_t port = 0;
int r;
int ret = 0;
js_ip = JS_GetPropertyStr(ctx, obj, "ip");
ip = JS_ToCString(ctx, js_ip);
JS_FreeValue(ctx, js_ip);
if (!ip) {
return -1;
}
js_port = JS_GetPropertyStr(ctx, obj, "port");
r = JS_ToUint32(ctx, &port, js_port);
JS_FreeValue(ctx, js_port);
if (r != 0) {
ret = -1;
goto end;
}
memset(ss, 0, sizeof(*ss));
if (uv_inet_pton(AF_INET, ip, &((struct sockaddr_in *) ss)->sin_addr) == 0) {
ss->ss_family = AF_INET;
((struct sockaddr_in *) ss)->sin_port = htons(port);
} else if (uv_inet_pton(AF_INET6, ip, &((struct sockaddr_in6 *) ss)->sin6_addr) == 0) {
ss->ss_family = AF_INET6;
((struct sockaddr_in6 *) ss)->sin6_port = htons(port);
} else {
tjs_throw_errno(ctx, UV_EAFNOSUPPORT);
ret = -1;
}
end:
JS_FreeCString(ctx, ip);
return ret;
}
void tjs_addr2obj(JSContext *ctx, JSValue obj, const struct sockaddr *sa, bool skip_port) {
char buf[INET6_ADDRSTRLEN + 1];
switch (sa->sa_family) {
case AF_INET: {
struct sockaddr_in *addr4 = (struct sockaddr_in *) sa;
uv_ip4_name(addr4, buf, sizeof(buf));
JS_DefinePropertyValueStr(ctx, obj, "family", JS_NewInt32(ctx, 4), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ip", JS_NewString(ctx, buf), JS_PROP_C_W_E);
if (!skip_port) {
JS_DefinePropertyValueStr(ctx, obj, "port", JS_NewInt32(ctx, ntohs(addr4->sin_port)), JS_PROP_C_W_E);
}
break;
}
case AF_INET6: {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) sa;
uv_ip6_name(addr6, buf, sizeof(buf));
JS_DefinePropertyValueStr(ctx, obj, "family", JS_NewInt32(ctx, 6), JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "ip", JS_NewString(ctx, buf), JS_PROP_C_W_E);
if (!skip_port) {
JS_DefinePropertyValueStr(ctx, obj, "port", JS_NewInt32(ctx, ntohs(addr6->sin6_port)), JS_PROP_C_W_E);
}
JS_DefinePropertyValueStr(ctx,
obj,
"flowInfo",
JS_NewInt32(ctx, ntohl(addr6->sin6_flowinfo)),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, obj, "scopeId", JS_NewInt32(ctx, addr6->sin6_scope_id), JS_PROP_C_W_E);
break;
}
}
}
static void tjs_dump_obj(JSContext *ctx, FILE *f, JSValue val) {
const char *str = JS_ToCString(ctx, val);
if (str) {
fprintf(f, "%s\n", str);
JS_FreeCString(ctx, str);
} else {
fprintf(f, "[exception]\n");
}
}
void tjs_dump_error(JSContext *ctx) {
JSValue exception_val = JS_GetException(ctx);
tjs_dump_error1(ctx, exception_val);
JS_FreeValue(ctx, exception_val);
}
void tjs_dump_error1(JSContext *ctx, JSValue exception_val) {
int is_error = JS_IsError(ctx, exception_val);
tjs_dump_obj(ctx, stderr, exception_val);
if (is_error) {
JSValue val = JS_GetPropertyStr(ctx, exception_val, "stack");
if (!JS_IsUndefined(val)) {
tjs_dump_obj(ctx, stderr, val);
}
JS_FreeValue(ctx, val);
}
fflush(stderr);
}
void tjs_call_handler(JSContext *ctx, JSValue func, int argc, JSValue *argv) {
JSValue ret, func1;
/* 'func' might be destroyed when calling itself (if it frees the
handler), so must take extra care */
func1 = JS_DupValue(ctx, func);
ret = JS_Call(ctx, func1, JS_UNDEFINED, argc, argv);
JS_FreeValue(ctx, func1);
if (JS_IsException(ret)) {
TJSRuntime *qrt = TJS_GetRuntime(ctx);
CHECK_NOT_NULL(qrt);
TJS_Stop(qrt);
}
JS_FreeValue(ctx, ret);
}
JSValue TJS_InitPromise(JSContext *ctx, TJSPromise *p) {
JSValue rfuncs[2];
p->p = JS_NewPromiseCapability(ctx, rfuncs);
if (JS_IsException(p->p)) {
return JS_EXCEPTION;
}
p->rfuncs[0] = JS_DupValue(ctx, rfuncs[0]);
p->rfuncs[1] = JS_DupValue(ctx, rfuncs[1]);
return JS_DupValue(ctx, p->p);
}
bool TJS_IsPromisePending(JSContext *ctx, TJSPromise *p) {
return !JS_IsUndefined(p->p);
}
void TJS_FreePromise(JSContext *ctx, TJSPromise *p) {
JS_FreeValue(ctx, p->rfuncs[0]);
JS_FreeValue(ctx, p->rfuncs[1]);
JS_FreeValue(ctx, p->p);
}
void TJS_FreePromiseRT(JSRuntime *rt, TJSPromise *p) {
JS_FreeValueRT(rt, p->rfuncs[0]);
JS_FreeValueRT(rt, p->rfuncs[1]);
JS_FreeValueRT(rt, p->p);
}
void TJS_ClearPromise(JSContext *ctx, TJSPromise *p) {
p->p = JS_UNDEFINED;
p->rfuncs[0] = JS_UNDEFINED;
p->rfuncs[1] = JS_UNDEFINED;
}
void TJS_MarkPromise(JSRuntime *rt, TJSPromise *p, JS_MarkFunc *mark_func) {
JS_MarkValue(rt, p->p, mark_func);
JS_MarkValue(rt, p->rfuncs[0], mark_func);
JS_MarkValue(rt, p->rfuncs[1], mark_func);
}
void TJS_SettlePromise(JSContext *ctx, TJSPromise *p, bool is_reject, int argc, JSValue *argv) {
JSValue ret = JS_Call(ctx, p->rfuncs[is_reject], JS_UNDEFINED, argc, argv);
for (int i = 0; i < argc; i++) {
JS_FreeValue(ctx, argv[i]);
}
JS_FreeValue(ctx, ret); /* XXX: what to do if exception ? */
JS_FreeValue(ctx, p->rfuncs[0]);
JS_FreeValue(ctx, p->rfuncs[1]);
TJS_FreePromise(ctx, p);
}
void TJS_ResolvePromise(JSContext *ctx, TJSPromise *p, int argc, JSValue *argv) {
TJS_SettlePromise(ctx, p, false, argc, argv);
}
void TJS_RejectPromise(JSContext *ctx, TJSPromise *p, int argc, JSValue *argv) {
TJS_SettlePromise(ctx, p, true, argc, argv);
}
static inline JSValue tjs__settled_promise(JSContext *ctx, bool is_reject, int argc, JSValue *argv) {
JSValue promise, resolving_funcs[2], ret;
promise = JS_NewPromiseCapability(ctx, resolving_funcs);
if (JS_IsException(promise)) {
return JS_EXCEPTION;
}
ret = JS_Call(ctx, resolving_funcs[is_reject], JS_UNDEFINED, argc, argv);
for (int i = 0; i < argc; i++) {
JS_FreeValue(ctx, argv[i]);
}
JS_FreeValue(ctx, ret);
JS_FreeValue(ctx, resolving_funcs[0]);
JS_FreeValue(ctx, resolving_funcs[1]);
return promise;
}
JSValue TJS_NewResolvedPromise(JSContext *ctx, int argc, JSValue *argv) {
return tjs__settled_promise(ctx, false, argc, argv);
}
JSValue TJS_NewRejectedPromise(JSContext *ctx, int argc, JSValue *argv) {
return tjs__settled_promise(ctx, true, argc, argv);
}
static void tjs__buf_free(JSRuntime *rt, void *opaque, void *ptr) {
js_free_rt(rt, ptr);
}
JSValue TJS_NewUint8Array(JSContext *ctx, uint8_t *data, size_t size) {
return JS_NewUint8Array(ctx, data, size, tjs__buf_free, NULL, false);
}
const char *tjs_signal_map[] = {
#ifdef SIGHUP
[SIGHUP] = "SIGHUP",
#endif
#ifdef SIGINT
[SIGINT] = "SIGINT",
#endif
#ifdef SIGQUIT
[SIGQUIT] = "SIGQUIT",
#endif
#ifdef SIGILL
[SIGILL] = "SIGILL",
#endif
#ifdef SIGTRAP
[SIGTRAP] = "SIGTRAP",
#endif
#ifdef SIGABRT
[SIGABRT] = "SIGABRT",
#endif
#ifdef SIGBUS
[SIGBUS] = "SIGBUS",
#endif
#ifdef SIGFPE
[SIGFPE] = "SIGFPE",
#endif
#ifdef SIGKILL
[SIGKILL] = "SIGKILL",
#endif
#ifdef SIGUSR1
[SIGUSR1] = "SIGUSR1",
#endif
#ifdef SIGSEGV
[SIGSEGV] = "SIGSEGV",
#endif
#ifdef SIGUSR2
[SIGUSR2] = "SIGUSR2",
#endif
#ifdef SIGPIPE
[SIGPIPE] = "SIGPIPE",
#endif
#ifdef SIGALRM
[SIGALRM] = "SIGALRM",
#endif
#ifdef SIGTERM
[SIGTERM] = "SIGTERM",
#endif
#ifdef SIGSTKFLT
[SIGSTKFLT] = "SIGSTKFLT",
#endif
#ifdef SIGCHLD
[SIGCHLD] = "SIGCHLD",
#endif
#ifdef SIGCONT
[SIGCONT] = "SIGCONT",
#endif
#ifdef SIGSTOP
[SIGSTOP] = "SIGSTOP",
#endif
#ifdef SIGTSTP
[SIGTSTP] = "SIGTSTP",
#endif
#ifdef SIGBREAK
[SIGBREAK] = "SIGBREAK",
#endif
#ifdef SIGTTIN
[SIGTTIN] = "SIGTTIN",
#endif
#ifdef SIGTTOU
[SIGTTOU] = "SIGTTOU",
#endif
#ifdef SIGURG
[SIGURG] = "SIGURG",
#endif
#ifdef SIGXCPU
[SIGXCPU] = "SIGXCPU",
#endif
#ifdef SIGXFSZ
[SIGXFSZ] = "SIGXFSZ",
#endif
#ifdef SIGVTALRM
[SIGVTALRM] = "SIGVTALRM",
#endif
#ifdef SIGPROF
[SIGPROF] = "SIGPROF",
#endif
#ifdef SIGWINCH
[SIGWINCH] = "SIGWINCH",
#endif
#ifdef SIGPOLL
[SIGPOLL] = "SIGPOLL",
#endif
#ifdef SIGLOST
[SIGLOST] = "SIGLOST",
#endif
#ifdef SIGPWR
[SIGPWR] = "SIGPWR",
#endif
#ifdef SIGINFO
[SIGINFO] = "SIGINFO",
#endif
#ifdef SIGSYS
[SIGSYS] = "SIGSYS",
#endif
};
size_t tjs_signal_map_count = ARRAY_SIZE(tjs_signal_map);
const char *tjs_getsig(int sig) {
if (sig < 0 || sig >= tjs_signal_map_count || !tjs_signal_map[sig]) {
return NULL;
}
return tjs_signal_map[sig];
}
int tjs_getsignum(const char *sig_str) {
for (int i = 0; i < tjs_signal_map_count; i++) {
const char *s = tjs_signal_map[i];
if (s && strcmp(sig_str, s) == 0) {
return i;
}
}
return -1;
}
void tjs_dbuf_init(JSContext *ctx, DynBuf *s) {
dbuf_init2(s, JS_GetRuntime(ctx), (DynBufReallocFunc *) js_realloc_rt);
}