forked from bellard/quickjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshims.c
59 lines (45 loc) · 1.45 KB
/
shims.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
#include <wasi/api.h>
#include <stdint.h>
#include <stdio.h>
#include "quickjs.h"
typedef uint32_t u32;
#ifdef CUSTOM_SHIMS
__wasi_errno_t __shim_clock_time_get(__wasi_clockid_t id, u32 prec_hi, u32 prec_low, u32 *time)
__attribute__((
__import_module__("shims"),
__import_name__("clock_time_get"),
__warn_unused_result__,
));
__wasi_errno_t __wasi_clock_time_get(__wasi_clockid_t id, __wasi_timestamp_t precision, __wasi_timestamp_t *time) {
u32 prec_low = precision;
u32 prec_hi = precision >> 32;
u32 time_u32[2];
__wasi_errno_t err = __shim_clock_time_get(id, prec_hi, prec_low, &time_u32[0]);
__wasi_timestamp_t time_out = 0;
time_out += time_u32[0];
time_out <<= 32;
time_out += time_u32[1];
*time = time_out;
return err;
}
#endif
int eval_code(JSContext *context, const void *code, int code_len) {
const char *filename = "<eval>";
JSValue val = JS_Eval(context, code, code_len, filename, 0);
int is_exc = JS_IsException(val);
if (is_exc > 0) {
JSValue exc = JS_GetException(context);
JSValue toString = JS_GetPropertyStr(context, exc, "toString");
JSValue errorString = JS_Call(context, toString, exc, 0, NULL);
const char *str = JS_ToCString(context, errorString);
fprintf(stderr, "%s", str);
JS_FreeValue(context, errorString);
JS_FreeValue(context, toString);
JS_FreeValue(context, exc);
} else {
const char *str = JS_ToCString(context, val);
fprintf(stderr, "%s", str);
}
JS_FreeValue(context, val);
return is_exc;
}