Skip to content

Commit 163b1d4

Browse files
committed
Don't use rt->user_opaque in quickjs-libc.c
JS_SetRuntimeOpaque() and JS_GetRuntimeOpaque() are intended for embedders. Stop using them in quickjs-libc.c
1 parent 55b829e commit 163b1d4

File tree

3 files changed

+72
-27
lines changed

3 files changed

+72
-27
lines changed

quickjs-libc.c

+44-27
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ static BOOL my_isdigit(int c)
181181
return (c >= '0' && c <= '9');
182182
}
183183

184+
static JSThreadState *js_get_thread_state(JSRuntime *rt)
185+
{
186+
return (JSThreadState *)js_std_cmd(/*GetOpaque*/0, rt);
187+
}
188+
189+
static void js_set_thread_state(JSRuntime *rt, JSThreadState *ts)
190+
{
191+
js_std_cmd(/*SetOpaque*/1, rt, ts);
192+
}
193+
184194
static JSValue js_printf_internal(JSContext *ctx,
185195
int argc, JSValue *argv, FILE *fp)
186196
{
@@ -831,7 +841,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
831841
int argc, JSValue *argv)
832842
{
833843
JSRuntime *rt = JS_GetRuntime(ctx);
834-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
844+
JSThreadState *ts = js_get_thread_state(rt);
835845
const char *str = NULL;
836846
size_t len;
837847
JSValue ret, obj;
@@ -905,7 +915,7 @@ static BOOL is_stdio(FILE *f)
905915

906916
static void js_std_file_finalizer(JSRuntime *rt, JSValue val)
907917
{
908-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
918+
JSThreadState *ts = js_get_thread_state(rt);
909919
JSSTDFile *s = JS_GetOpaque(val, ts->std_file_class_id);
910920
if (s) {
911921
if (s->f && !is_stdio(s->f)) {
@@ -939,7 +949,7 @@ static JSValue js_std_strerror(JSContext *ctx, JSValue this_val,
939949
static JSValue js_new_std_file(JSContext *ctx, FILE *f, BOOL is_popen)
940950
{
941951
JSRuntime *rt = JS_GetRuntime(ctx);
942-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
952+
JSThreadState *ts = js_get_thread_state(rt);
943953
JSSTDFile *s;
944954
JSValue obj;
945955
obj = JS_NewObjectClass(ctx, ts->std_file_class_id);
@@ -1099,7 +1109,7 @@ static JSValue js_std_printf(JSContext *ctx, JSValue this_val,
10991109
static FILE *js_std_file_get(JSContext *ctx, JSValue obj)
11001110
{
11011111
JSRuntime *rt = JS_GetRuntime(ctx);
1102-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
1112+
JSThreadState *ts = js_get_thread_state(rt);
11031113
JSSTDFile *s = JS_GetOpaque2(ctx, obj, ts->std_file_class_id);
11041114
if (!s)
11051115
return NULL;
@@ -1140,7 +1150,7 @@ static JSValue js_std_file_close(JSContext *ctx, JSValue this_val,
11401150
int argc, JSValue *argv)
11411151
{
11421152
JSRuntime *rt = JS_GetRuntime(ctx);
1143-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
1153+
JSThreadState *ts = js_get_thread_state(rt);
11441154
JSSTDFile *s = JS_GetOpaque2(ctx, this_val, ts->std_file_class_id);
11451155
int err;
11461156
if (!s)
@@ -1657,7 +1667,7 @@ static int js_std_init(JSContext *ctx, JSModuleDef *m)
16571667
{
16581668
JSValue proto;
16591669
JSRuntime *rt = JS_GetRuntime(ctx);
1660-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
1670+
JSThreadState *ts = js_get_thread_state(rt);
16611671

16621672
/* FILE class */
16631673
/* the class ID is created once */
@@ -1945,7 +1955,7 @@ static JSValue js_os_rename(JSContext *ctx, JSValue this_val,
19451955

19461956
static BOOL is_main_thread(JSRuntime *rt)
19471957
{
1948-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
1958+
JSThreadState *ts = js_get_thread_state(rt);
19491959
return !ts->recv_pipe;
19501960
}
19511961

@@ -1976,7 +1986,7 @@ static JSValue js_os_setReadHandler(JSContext *ctx, JSValue this_val,
19761986
int argc, JSValue *argv, int magic)
19771987
{
19781988
JSRuntime *rt = JS_GetRuntime(ctx);
1979-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
1989+
JSThreadState *ts = js_get_thread_state(rt);
19801990
JSOSRWHandler *rh;
19811991
int fd;
19821992
JSValue func;
@@ -2046,7 +2056,7 @@ static JSValue js_os_signal(JSContext *ctx, JSValue this_val,
20462056
int argc, JSValue *argv)
20472057
{
20482058
JSRuntime *rt = JS_GetRuntime(ctx);
2049-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
2059+
JSThreadState *ts = js_get_thread_state(rt);
20502060
JSOSSignalHandler *sh;
20512061
uint32_t sig_num;
20522062
JSValue func;
@@ -2128,7 +2138,7 @@ static JSValue js_os_setTimeout(JSContext *ctx, JSValue this_val,
21282138
int argc, JSValue *argv, int magic)
21292139
{
21302140
JSRuntime *rt = JS_GetRuntime(ctx);
2131-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
2141+
JSThreadState *ts = js_get_thread_state(rt);
21322142
int64_t delay;
21332143
JSValue func;
21342144
JSOSTimer *th;
@@ -2171,7 +2181,7 @@ static JSValue js_os_clearTimeout(JSContext *ctx, JSValue this_val,
21712181
int argc, JSValue *argv)
21722182
{
21732183
JSRuntime *rt = JS_GetRuntime(ctx);
2174-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
2184+
JSThreadState *ts = js_get_thread_state(rt);
21752185
JSOSTimer *th;
21762186
int64_t timer_id;
21772187

@@ -2189,7 +2199,7 @@ static JSValue js_os_sleepAsync(JSContext *ctx, JSValueConst this_val,
21892199
int argc, JSValueConst *argv)
21902200
{
21912201
JSRuntime *rt = JS_GetRuntime(ctx);
2192-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
2202+
JSThreadState *ts = js_get_thread_state(rt);
21932203
int64_t delay;
21942204
JSOSTimer *th;
21952205
JSValue promise, resolving_funcs[2];
@@ -2228,7 +2238,7 @@ static int call_handler(JSContext *ctx, JSValue func)
22282238
r = 0;
22292239
if (JS_IsException(ret)) {
22302240
JSRuntime *rt = JS_GetRuntime(ctx);
2231-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
2241+
JSThreadState *ts = js_get_thread_state(rt);
22322242
ts->exc = JS_GetException(ctx);
22332243
r = -1;
22342244
}
@@ -2278,7 +2288,7 @@ static int js_os_run_timers(JSRuntime *rt, JSContext *ctx, JSThreadState *ts, in
22782288
static int js_os_poll(JSContext *ctx)
22792289
{
22802290
JSRuntime *rt = JS_GetRuntime(ctx);
2281-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
2291+
JSThreadState *ts = js_get_thread_state(rt);
22822292
int min_delay, console_fd;
22832293
JSOSRWHandler *rh;
22842294
struct list_head *el;
@@ -2407,7 +2417,7 @@ static int handle_posted_message(JSRuntime *rt, JSContext *ctx,
24072417
static int js_os_poll(JSContext *ctx)
24082418
{
24092419
JSRuntime *rt = JS_GetRuntime(ctx);
2410-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
2420+
JSThreadState *ts = js_get_thread_state(rt);
24112421
int ret, fd_max, min_delay;
24122422
fd_set rfds, wfds;
24132423
JSOSRWHandler *rh;
@@ -3415,7 +3425,7 @@ static void js_free_port(JSRuntime *rt, JSWorkerMessageHandler *port)
34153425

34163426
static void js_worker_finalizer(JSRuntime *rt, JSValue val)
34173427
{
3418-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
3428+
JSThreadState *ts = js_get_thread_state(rt);
34193429
JSWorkerData *worker = JS_GetOpaque(val, ts->worker_class_id);
34203430
if (worker) {
34213431
js_free_message_pipe(worker->recv_pipe);
@@ -3448,7 +3458,7 @@ static void *worker_func(void *opaque)
34483458
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
34493459

34503460
/* set the pipe to communicate with the parent */
3451-
ts = JS_GetRuntimeOpaque(rt);
3461+
ts = js_get_thread_state(rt);
34523462
ts->recv_pipe = args->recv_pipe;
34533463
ts->send_pipe = args->send_pipe;
34543464

@@ -3485,7 +3495,7 @@ static JSValue js_worker_ctor_internal(JSContext *ctx, JSValue new_target,
34853495
JSWorkerMessagePipe *send_pipe)
34863496
{
34873497
JSRuntime *rt = JS_GetRuntime(ctx);
3488-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
3498+
JSThreadState *ts = js_get_thread_state(rt);
34893499
JSValue obj = JS_UNDEFINED, proto;
34903500
JSWorkerData *s;
34913501

@@ -3602,7 +3612,7 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
36023612
int argc, JSValue *argv)
36033613
{
36043614
JSRuntime *rt = JS_GetRuntime(ctx);
3605-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
3615+
JSThreadState *ts = js_get_thread_state(rt);
36063616
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, ts->worker_class_id);
36073617
JSWorkerMessagePipe *ps;
36083618
size_t data_len, i;
@@ -3681,7 +3691,7 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValue this_val,
36813691
JSValue func)
36823692
{
36833693
JSRuntime *rt = JS_GetRuntime(ctx);
3684-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
3694+
JSThreadState *ts = js_get_thread_state(rt);
36853695
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, ts->worker_class_id);
36863696
JSWorkerMessageHandler *port;
36873697

@@ -3715,7 +3725,7 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValue this_val,
37153725
static JSValue js_worker_get_onmessage(JSContext *ctx, JSValue this_val)
37163726
{
37173727
JSRuntime *rt = JS_GetRuntime(ctx);
3718-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
3728+
JSThreadState *ts = js_get_thread_state(rt);
37193729
JSWorkerData *worker = JS_GetOpaque2(ctx, this_val, ts->worker_class_id);
37203730
JSWorkerMessageHandler *port;
37213731
if (!worker)
@@ -3862,7 +3872,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
38623872
static int js_os_init(JSContext *ctx, JSModuleDef *m)
38633873
{
38643874
JSRuntime *rt = JS_GetRuntime(ctx);
3865-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
3875+
JSThreadState *ts = js_get_thread_state(rt);
38663876

38673877
ts->can_js_os_poll = TRUE;
38683878

@@ -3989,6 +3999,13 @@ void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
39893999
JS_FreeValue(ctx, global_obj);
39904000
}
39914001

4002+
static void js_std_finalize(JSRuntime *rt, void *arg)
4003+
{
4004+
JSThreadState *ts = arg;
4005+
js_set_thread_state(rt, NULL);
4006+
js_free_rt(rt, ts);
4007+
}
4008+
39924009
void js_std_init_handlers(JSRuntime *rt)
39934010
{
39944011
JSThreadState *ts;
@@ -4006,8 +4023,8 @@ void js_std_init_handlers(JSRuntime *rt)
40064023
ts->next_timer_id = 1;
40074024
ts->exc = JS_UNDEFINED;
40084025

4009-
JS_SetRuntimeOpaque(rt, ts);
4010-
JS_AddRuntimeFinalizer(rt, js_free_rt, ts);
4026+
js_set_thread_state(rt, ts);
4027+
JS_AddRuntimeFinalizer(rt, js_std_finalize, ts);
40114028

40124029
#ifdef USE_WORKER
40134030
/* set the SharedArrayBuffer memory handlers */
@@ -4024,7 +4041,7 @@ void js_std_init_handlers(JSRuntime *rt)
40244041

40254042
void js_std_free_handlers(JSRuntime *rt)
40264043
{
4027-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
4044+
JSThreadState *ts = js_get_thread_state(rt);
40284045
struct list_head *el, *el1;
40294046

40304047
list_for_each_safe(el, el1, &ts->os_rw_handlers) {
@@ -4101,7 +4118,7 @@ void js_std_promise_rejection_tracker(JSContext *ctx, JSValue promise,
41014118
JSValue js_std_loop(JSContext *ctx)
41024119
{
41034120
JSRuntime *rt = JS_GetRuntime(ctx);
4104-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
4121+
JSThreadState *ts = js_get_thread_state(rt);
41054122
JSContext *ctx1;
41064123
int err;
41074124

@@ -4131,7 +4148,7 @@ JSValue js_std_loop(JSContext *ctx)
41314148
JSValue js_std_await(JSContext *ctx, JSValue obj)
41324149
{
41334150
JSRuntime *rt = JS_GetRuntime(ctx);
4134-
JSThreadState *ts = JS_GetRuntimeOpaque(rt);
4151+
JSThreadState *ts = js_get_thread_state(rt);
41354152
JSValue ret;
41364153
int state;
41374154

quickjs.c

+25
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ struct JSRuntime {
298298
JSShape **shape_hash;
299299
bf_context_t bf_ctx;
300300
void *user_opaque;
301+
void *libc_opaque;
301302
JSRuntimeFinalizerState *finalizers;
302303
};
303304

@@ -55424,6 +55425,30 @@ BOOL JS_DetectModule(const char *input, size_t input_len)
5542455425
return is_module;
5542555426
}
5542655427

55428+
uintptr_t js_std_cmd(int cmd, ...) {
55429+
JSRuntime *rt;
55430+
uintptr_t rv;
55431+
va_list ap;
55432+
55433+
rv = 0;
55434+
va_start(ap, cmd);
55435+
switch (cmd) {
55436+
case 0: // GetOpaque
55437+
rt = va_arg(ap, JSRuntime *);
55438+
rv = (uintptr_t)rt->libc_opaque;
55439+
break;
55440+
case 1: // SetOpaque
55441+
rt = va_arg(ap, JSRuntime *);
55442+
rt->libc_opaque = va_arg(ap, void *);
55443+
break;
55444+
default:
55445+
rv = -1;
55446+
}
55447+
va_end(ap);
55448+
55449+
return rv;
55450+
}
55451+
5542755452
#undef malloc
5542855453
#undef free
5542955454
#undef realloc

quickjs.h

+3
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,9 @@ JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
10311031

10321032
JS_EXTERN const char* JS_GetVersion(void);
10331033

1034+
/* Integration point for quickjs-libc.c, not for public use. */
1035+
JS_EXTERN uintptr_t js_std_cmd(int cmd, ...);
1036+
10341037
#undef JS_EXTERN
10351038
#undef js_force_inline
10361039
#undef __js_printf_like

0 commit comments

Comments
 (0)