-
Notifications
You must be signed in to change notification settings - Fork 10
/
bridge.c
63 lines (50 loc) · 1.88 KB
/
bridge.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
#include "_cgo_export.h"
#include "quickjs.h"
#include <time.h>
JSValue JS_NewNull() { return JS_NULL; }
JSValue JS_NewUndefined() { return JS_UNDEFINED; }
JSValue JS_NewUninitialized() { return JS_UNINITIALIZED; }
JSValue ThrowSyntaxError(JSContext *ctx, const char *fmt) { return JS_ThrowSyntaxError(ctx, "%s", fmt); }
JSValue ThrowTypeError(JSContext *ctx, const char *fmt) { return JS_ThrowTypeError(ctx, "%s", fmt); }
JSValue ThrowReferenceError(JSContext *ctx, const char *fmt) { return JS_ThrowReferenceError(ctx, "%s", fmt); }
JSValue ThrowRangeError(JSContext *ctx, const char *fmt) { return JS_ThrowRangeError(ctx, "%s", fmt); }
JSValue ThrowInternalError(JSContext *ctx, const char *fmt) { return JS_ThrowInternalError(ctx, "%s", fmt); }
int ValueGetTag(JSValueConst v) {
return JS_VALUE_GET_TAG(v);
}
JSValue InvokeProxy(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
return goProxy(ctx, this_val, argc, argv);
}
JSValue InvokeAsyncProxy(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
return goAsyncProxy(ctx, this_val, argc, argv);
}
int interruptHandler(JSRuntime *rt, void *handlerArgs) {
return goInterruptHandler(rt, handlerArgs);
}
void SetInterruptHandler(JSRuntime *rt, void *handlerArgs){
JS_SetInterruptHandler(rt, &interruptHandler, handlerArgs);
}
typedef struct {
time_t start;
time_t timeout;
} TimeoutStruct;
int timeoutHandler(JSRuntime *rt, void *opaque) {
TimeoutStruct* ts = (TimeoutStruct*)opaque;
time_t timeout = ts->timeout;
time_t start = ts->start;
if (timeout <= 0) {
return 0;
}
time_t now = time(NULL);
if (now - start > timeout) {
free(ts);
return 1;
}
return 0;
}
void SetExecuteTimeout(JSRuntime *rt, time_t timeout){
TimeoutStruct* ts = malloc(sizeof(TimeoutStruct));
ts->start = time(NULL);
ts->timeout = timeout;
JS_SetInterruptHandler(rt, &timeoutHandler, ts);
}