@@ -181,6 +181,16 @@ static BOOL my_isdigit(int c)
181
181
return (c >= '0' && c <= '9' );
182
182
}
183
183
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
+
184
194
static JSValue js_printf_internal (JSContext * ctx ,
185
195
int argc , JSValue * argv , FILE * fp )
186
196
{
@@ -831,7 +841,7 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
831
841
int argc , JSValue * argv )
832
842
{
833
843
JSRuntime * rt = JS_GetRuntime (ctx );
834
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
844
+ JSThreadState * ts = js_get_thread_state (rt );
835
845
const char * str = NULL ;
836
846
size_t len ;
837
847
JSValue ret , obj ;
@@ -905,7 +915,7 @@ static BOOL is_stdio(FILE *f)
905
915
906
916
static void js_std_file_finalizer (JSRuntime * rt , JSValue val )
907
917
{
908
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
918
+ JSThreadState * ts = js_get_thread_state (rt );
909
919
JSSTDFile * s = JS_GetOpaque (val , ts -> std_file_class_id );
910
920
if (s ) {
911
921
if (s -> f && !is_stdio (s -> f )) {
@@ -939,7 +949,7 @@ static JSValue js_std_strerror(JSContext *ctx, JSValue this_val,
939
949
static JSValue js_new_std_file (JSContext * ctx , FILE * f , BOOL is_popen )
940
950
{
941
951
JSRuntime * rt = JS_GetRuntime (ctx );
942
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
952
+ JSThreadState * ts = js_get_thread_state (rt );
943
953
JSSTDFile * s ;
944
954
JSValue obj ;
945
955
obj = JS_NewObjectClass (ctx , ts -> std_file_class_id );
@@ -1099,7 +1109,7 @@ static JSValue js_std_printf(JSContext *ctx, JSValue this_val,
1099
1109
static FILE * js_std_file_get (JSContext * ctx , JSValue obj )
1100
1110
{
1101
1111
JSRuntime * rt = JS_GetRuntime (ctx );
1102
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1112
+ JSThreadState * ts = js_get_thread_state (rt );
1103
1113
JSSTDFile * s = JS_GetOpaque2 (ctx , obj , ts -> std_file_class_id );
1104
1114
if (!s )
1105
1115
return NULL ;
@@ -1140,7 +1150,7 @@ static JSValue js_std_file_close(JSContext *ctx, JSValue this_val,
1140
1150
int argc , JSValue * argv )
1141
1151
{
1142
1152
JSRuntime * rt = JS_GetRuntime (ctx );
1143
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1153
+ JSThreadState * ts = js_get_thread_state (rt );
1144
1154
JSSTDFile * s = JS_GetOpaque2 (ctx , this_val , ts -> std_file_class_id );
1145
1155
int err ;
1146
1156
if (!s )
@@ -1657,7 +1667,7 @@ static int js_std_init(JSContext *ctx, JSModuleDef *m)
1657
1667
{
1658
1668
JSValue proto ;
1659
1669
JSRuntime * rt = JS_GetRuntime (ctx );
1660
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1670
+ JSThreadState * ts = js_get_thread_state (rt );
1661
1671
1662
1672
/* FILE class */
1663
1673
/* the class ID is created once */
@@ -1945,7 +1955,7 @@ static JSValue js_os_rename(JSContext *ctx, JSValue this_val,
1945
1955
1946
1956
static BOOL is_main_thread (JSRuntime * rt )
1947
1957
{
1948
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1958
+ JSThreadState * ts = js_get_thread_state (rt );
1949
1959
return !ts -> recv_pipe ;
1950
1960
}
1951
1961
@@ -1976,7 +1986,7 @@ static JSValue js_os_setReadHandler(JSContext *ctx, JSValue this_val,
1976
1986
int argc , JSValue * argv , int magic )
1977
1987
{
1978
1988
JSRuntime * rt = JS_GetRuntime (ctx );
1979
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1989
+ JSThreadState * ts = js_get_thread_state (rt );
1980
1990
JSOSRWHandler * rh ;
1981
1991
int fd ;
1982
1992
JSValue func ;
@@ -2046,7 +2056,7 @@ static JSValue js_os_signal(JSContext *ctx, JSValue this_val,
2046
2056
int argc , JSValue * argv )
2047
2057
{
2048
2058
JSRuntime * rt = JS_GetRuntime (ctx );
2049
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
2059
+ JSThreadState * ts = js_get_thread_state (rt );
2050
2060
JSOSSignalHandler * sh ;
2051
2061
uint32_t sig_num ;
2052
2062
JSValue func ;
@@ -2128,7 +2138,7 @@ static JSValue js_os_setTimeout(JSContext *ctx, JSValue this_val,
2128
2138
int argc , JSValue * argv , int magic )
2129
2139
{
2130
2140
JSRuntime * rt = JS_GetRuntime (ctx );
2131
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
2141
+ JSThreadState * ts = js_get_thread_state (rt );
2132
2142
int64_t delay ;
2133
2143
JSValue func ;
2134
2144
JSOSTimer * th ;
@@ -2171,7 +2181,7 @@ static JSValue js_os_clearTimeout(JSContext *ctx, JSValue this_val,
2171
2181
int argc , JSValue * argv )
2172
2182
{
2173
2183
JSRuntime * rt = JS_GetRuntime (ctx );
2174
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
2184
+ JSThreadState * ts = js_get_thread_state (rt );
2175
2185
JSOSTimer * th ;
2176
2186
int64_t timer_id ;
2177
2187
@@ -2189,7 +2199,7 @@ static JSValue js_os_sleepAsync(JSContext *ctx, JSValueConst this_val,
2189
2199
int argc , JSValueConst * argv )
2190
2200
{
2191
2201
JSRuntime * rt = JS_GetRuntime (ctx );
2192
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
2202
+ JSThreadState * ts = js_get_thread_state (rt );
2193
2203
int64_t delay ;
2194
2204
JSOSTimer * th ;
2195
2205
JSValue promise , resolving_funcs [2 ];
@@ -2228,7 +2238,7 @@ static int call_handler(JSContext *ctx, JSValue func)
2228
2238
r = 0 ;
2229
2239
if (JS_IsException (ret )) {
2230
2240
JSRuntime * rt = JS_GetRuntime (ctx );
2231
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
2241
+ JSThreadState * ts = js_get_thread_state (rt );
2232
2242
ts -> exc = JS_GetException (ctx );
2233
2243
r = -1 ;
2234
2244
}
@@ -2278,7 +2288,7 @@ static int js_os_run_timers(JSRuntime *rt, JSContext *ctx, JSThreadState *ts, in
2278
2288
static int js_os_poll (JSContext * ctx )
2279
2289
{
2280
2290
JSRuntime * rt = JS_GetRuntime (ctx );
2281
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
2291
+ JSThreadState * ts = js_get_thread_state (rt );
2282
2292
int min_delay , console_fd ;
2283
2293
JSOSRWHandler * rh ;
2284
2294
struct list_head * el ;
@@ -2407,7 +2417,7 @@ static int handle_posted_message(JSRuntime *rt, JSContext *ctx,
2407
2417
static int js_os_poll (JSContext * ctx )
2408
2418
{
2409
2419
JSRuntime * rt = JS_GetRuntime (ctx );
2410
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
2420
+ JSThreadState * ts = js_get_thread_state (rt );
2411
2421
int ret , fd_max , min_delay ;
2412
2422
fd_set rfds , wfds ;
2413
2423
JSOSRWHandler * rh ;
@@ -3415,7 +3425,7 @@ static void js_free_port(JSRuntime *rt, JSWorkerMessageHandler *port)
3415
3425
3416
3426
static void js_worker_finalizer (JSRuntime * rt , JSValue val )
3417
3427
{
3418
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3428
+ JSThreadState * ts = js_get_thread_state (rt );
3419
3429
JSWorkerData * worker = JS_GetOpaque (val , ts -> worker_class_id );
3420
3430
if (worker ) {
3421
3431
js_free_message_pipe (worker -> recv_pipe );
@@ -3448,7 +3458,7 @@ static void *worker_func(void *opaque)
3448
3458
JS_SetModuleLoaderFunc (rt , NULL , js_module_loader , NULL );
3449
3459
3450
3460
/* set the pipe to communicate with the parent */
3451
- ts = JS_GetRuntimeOpaque (rt );
3461
+ ts = js_get_thread_state (rt );
3452
3462
ts -> recv_pipe = args -> recv_pipe ;
3453
3463
ts -> send_pipe = args -> send_pipe ;
3454
3464
@@ -3485,7 +3495,7 @@ static JSValue js_worker_ctor_internal(JSContext *ctx, JSValue new_target,
3485
3495
JSWorkerMessagePipe * send_pipe )
3486
3496
{
3487
3497
JSRuntime * rt = JS_GetRuntime (ctx );
3488
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3498
+ JSThreadState * ts = js_get_thread_state (rt );
3489
3499
JSValue obj = JS_UNDEFINED , proto ;
3490
3500
JSWorkerData * s ;
3491
3501
@@ -3602,7 +3612,7 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
3602
3612
int argc , JSValue * argv )
3603
3613
{
3604
3614
JSRuntime * rt = JS_GetRuntime (ctx );
3605
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3615
+ JSThreadState * ts = js_get_thread_state (rt );
3606
3616
JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , ts -> worker_class_id );
3607
3617
JSWorkerMessagePipe * ps ;
3608
3618
size_t data_len , i ;
@@ -3681,7 +3691,7 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValue this_val,
3681
3691
JSValue func )
3682
3692
{
3683
3693
JSRuntime * rt = JS_GetRuntime (ctx );
3684
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3694
+ JSThreadState * ts = js_get_thread_state (rt );
3685
3695
JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , ts -> worker_class_id );
3686
3696
JSWorkerMessageHandler * port ;
3687
3697
@@ -3715,7 +3725,7 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValue this_val,
3715
3725
static JSValue js_worker_get_onmessage (JSContext * ctx , JSValue this_val )
3716
3726
{
3717
3727
JSRuntime * rt = JS_GetRuntime (ctx );
3718
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3728
+ JSThreadState * ts = js_get_thread_state (rt );
3719
3729
JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , ts -> worker_class_id );
3720
3730
JSWorkerMessageHandler * port ;
3721
3731
if (!worker )
@@ -3862,7 +3872,7 @@ static const JSCFunctionListEntry js_os_funcs[] = {
3862
3872
static int js_os_init (JSContext * ctx , JSModuleDef * m )
3863
3873
{
3864
3874
JSRuntime * rt = JS_GetRuntime (ctx );
3865
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3875
+ JSThreadState * ts = js_get_thread_state (rt );
3866
3876
3867
3877
ts -> can_js_os_poll = TRUE;
3868
3878
@@ -3989,6 +3999,13 @@ void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
3989
3999
JS_FreeValue (ctx , global_obj );
3990
4000
}
3991
4001
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
+
3992
4009
void js_std_init_handlers (JSRuntime * rt )
3993
4010
{
3994
4011
JSThreadState * ts ;
@@ -4006,8 +4023,8 @@ void js_std_init_handlers(JSRuntime *rt)
4006
4023
ts -> next_timer_id = 1 ;
4007
4024
ts -> exc = JS_UNDEFINED ;
4008
4025
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 );
4011
4028
4012
4029
#ifdef USE_WORKER
4013
4030
/* set the SharedArrayBuffer memory handlers */
@@ -4024,7 +4041,7 @@ void js_std_init_handlers(JSRuntime *rt)
4024
4041
4025
4042
void js_std_free_handlers (JSRuntime * rt )
4026
4043
{
4027
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
4044
+ JSThreadState * ts = js_get_thread_state (rt );
4028
4045
struct list_head * el , * el1 ;
4029
4046
4030
4047
list_for_each_safe (el , el1 , & ts -> os_rw_handlers ) {
@@ -4101,7 +4118,7 @@ void js_std_promise_rejection_tracker(JSContext *ctx, JSValue promise,
4101
4118
JSValue js_std_loop (JSContext * ctx )
4102
4119
{
4103
4120
JSRuntime * rt = JS_GetRuntime (ctx );
4104
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
4121
+ JSThreadState * ts = js_get_thread_state (rt );
4105
4122
JSContext * ctx1 ;
4106
4123
int err ;
4107
4124
@@ -4131,7 +4148,7 @@ JSValue js_std_loop(JSContext *ctx)
4131
4148
JSValue js_std_await (JSContext * ctx , JSValue obj )
4132
4149
{
4133
4150
JSRuntime * rt = JS_GetRuntime (ctx );
4134
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
4151
+ JSThreadState * ts = js_get_thread_state (rt );
4135
4152
JSValue ret ;
4136
4153
int state ;
4137
4154
0 commit comments