-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpl_eventloop.c
More file actions
36 lines (31 loc) · 1.1 KB
/
pl_eventloop.c
File metadata and controls
36 lines (31 loc) · 1.1 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
#include <stdio.h>
#include "pl_eventloop.h"
#include "c_eventloop.h"
int pl_register_eventloop(Duk* duk)
{
/* Register our event loop dispatcher, otherwise calls to */
/* dispatch_function_in_event_loop will not work. */
eventloop_register(duk->ctx);
return 0;
}
int pl_run_function_in_event_loop(Duk* duk, const char* func)
{
duk_context* ctx = duk->ctx;
duk_int_t rc = 0;
/* Start a zero timer which will call our function from the event loop. */
duk_push_sprintf(ctx, "setTimeout(function() { %s(); }, 0);", func);
rc = duk_peval(ctx);
if (rc != DUK_EXEC_SUCCESS) {
croak("Could not eval JS event loop dispatcher for %s: %d - %s\n",
func, rc, duk_safe_to_string(ctx, -1));
}
duk_pop(ctx); /* pop result / error */
/* Launch eventloop; this call only returns after the eventloop terminates. */
rc = duk_safe_call(ctx, eventloop_run, duk, 0 /*nargs*/, 1 /*nrets*/);
if (rc != DUK_EXEC_SUCCESS) {
croak("JS event loop run failed: %d - %s\n",
rc, duk_safe_to_string(ctx, -1));
}
duk_pop(ctx);
return 0;
}