forked from Moddable-OpenSource/moddable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lin_xs_cli.c
93 lines (81 loc) · 2.56 KB
/
lin_xs_cli.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
#include "lin_xs.h"
#include "xsPlatform.h"
#include "xs.h"
#include "mc.xs.h"
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <unistd.h>
#define mxSeparator '/'
#ifdef mxDebug
#define xsElseThrow(_ASSERTION) \
((void)((_ASSERTION) || (fxThrowMessage(the,(char *)__FILE__,__LINE__,XS_UNKNOWN_ERROR,"%s",strerror(errno)), 1)))
#else
#define xsElseThrow(_ASSERTION) \
((void)((_ASSERTION) || (fxThrowMessage(the,NULL,0,XS_UNKNOWN_ERROR,"%s",strerror(errno)), 1)))
#endif
extern void fxRunPromiseJobs(void* machine);
extern txS1 fxPromiseIsPending(xsMachine* the, xsSlot* promise);
extern txS1 fxPromiseIsRejected(xsMachine* the, xsSlot* promise);
extern void worker_abort(xsMachine *the, void* worker);
void fxAbort(xsMachine* the, int status)
{
fprintf(stderr, "fxAbort(the=%p, status=%d).\n", the, status);
void *worker = xsGetContext(the);
if (!worker) {
fprintf(stderr, "exit(%d)\n", status);
exit(status);
}
worker_abort(the, worker);
}
int main(int argc, char* argv[]) // here
{
int error = 0;
xsMachine* machine = fxPrepareMachine(NULL, xsPreparation(), "tool", NULL, NULL);
xsBeginHost(machine);
{
xsVars(3);
{
xsTry {
int argi;
xsVar(0) = xsNewArray(0);
for (argi = 1; argi < argc; argi++) {
xsSetAt(xsVar(0), xsInteger(argi - 1), xsString(argv[argi]));
}
printf("lin_xs_cli: loading top-level main.js\n");
xsVar(1) = xsAwaitImport("main", XS_IMPORT_DEFAULT);
printf(" lin_xs_cli: loaded\n");
printf("lin_xs_cli: invoking main(argv)\n");
xsVar(2) = xsCallFunction1(xsVar(1), xsUndefined, xsVar(0));
if (!xsIsInstanceOf(xsVar(2), xsPromisePrototype)) {
fprintf(stderr, "main() returned immediate value (not a promise). exiting\n");
exit(xsToInteger(xsVar(2)));
}
printf(" lin_xs_cli: main() returned a promise; entering event loop\n");
GMainContext *mainctx = g_main_context_default();
while (the->promiseJobsFlag || fxPromiseIsPending(the, &xsVar(2))) {
while (the->promiseJobsFlag) {
the->promiseJobsFlag = 0;
fxRunPromiseJobs(the);
}
g_main_context_iteration(mainctx, TRUE);
}
if (fxPromiseIsRejected(the, &xsVar(2))) {
error = 1;
}
// ISSUE: g_main_context_unref(mainctx); causes xsDeleteMachine() below
// to hang in g_main_context_find_source_by_id() aquiring a lock.
}
xsCatch {
xsStringValue message = xsToString(xsException);
fprintf(stderr, "### %s\n", message);
error = 1;
}
}
}
xsEndHost(the);
xsDeleteMachine(machine);
return error;
}