@@ -164,6 +164,8 @@ typedef struct JSThreadState {
164
164
JSValue exc ; /* current exception from one of our handlers */
165
165
/* not used in the main thread */
166
166
JSWorkerMessagePipe * recv_pipe , * send_pipe ;
167
+ JSClassID std_file_class_id ;
168
+ JSClassID worker_class_id ;
167
169
} JSThreadState ;
168
170
169
171
static uint64_t os_pending_signals ;
@@ -874,8 +876,6 @@ static JSValue js_evalScript(JSContext *ctx, JSValue this_val,
874
876
return ret ;
875
877
}
876
878
877
- static JSClassID js_std_file_class_id ;
878
-
879
879
typedef struct {
880
880
FILE * f ;
881
881
BOOL close_in_finalizer ;
@@ -884,7 +884,8 @@ typedef struct {
884
884
885
885
static void js_std_file_finalizer (JSRuntime * rt , JSValue val )
886
886
{
887
- JSSTDFile * s = JS_GetOpaque (val , js_std_file_class_id );
887
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
888
+ JSSTDFile * s = JS_GetOpaque (val , ts -> std_file_class_id );
888
889
if (s ) {
889
890
if (s -> f && s -> close_in_finalizer ) {
890
891
#if !defined(__wasi__ )
@@ -918,9 +919,11 @@ static JSValue js_new_std_file(JSContext *ctx, FILE *f,
918
919
BOOL close_in_finalizer ,
919
920
BOOL is_popen )
920
921
{
922
+ JSRuntime * rt = JS_GetRuntime (ctx );
923
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
921
924
JSSTDFile * s ;
922
925
JSValue obj ;
923
- obj = JS_NewObjectClass (ctx , js_std_file_class_id );
926
+ obj = JS_NewObjectClass (ctx , ts -> std_file_class_id );
924
927
if (JS_IsException (obj ))
925
928
return obj ;
926
929
s = js_mallocz (ctx , sizeof (* s ));
@@ -1077,7 +1080,9 @@ static JSValue js_std_printf(JSContext *ctx, JSValue this_val,
1077
1080
1078
1081
static FILE * js_std_file_get (JSContext * ctx , JSValue obj )
1079
1082
{
1080
- JSSTDFile * s = JS_GetOpaque2 (ctx , obj , js_std_file_class_id );
1083
+ JSRuntime * rt = JS_GetRuntime (ctx );
1084
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1085
+ JSSTDFile * s = JS_GetOpaque2 (ctx , obj , ts -> std_file_class_id );
1081
1086
if (!s )
1082
1087
return NULL ;
1083
1088
if (!s -> f ) {
@@ -1116,7 +1121,9 @@ static JSValue js_std_file_puts(JSContext *ctx, JSValue this_val,
1116
1121
static JSValue js_std_file_close (JSContext * ctx , JSValue this_val ,
1117
1122
int argc , JSValue * argv )
1118
1123
{
1119
- JSSTDFile * s = JS_GetOpaque2 (ctx , this_val , js_std_file_class_id );
1124
+ JSRuntime * rt = JS_GetRuntime (ctx );
1125
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1126
+ JSSTDFile * s = JS_GetOpaque2 (ctx , this_val , ts -> std_file_class_id );
1120
1127
int err ;
1121
1128
if (!s )
1122
1129
return JS_EXCEPTION ;
@@ -1630,16 +1637,17 @@ static int js_std_init(JSContext *ctx, JSModuleDef *m)
1630
1637
{
1631
1638
JSValue proto ;
1632
1639
JSRuntime * rt = JS_GetRuntime (ctx );
1640
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1633
1641
1634
1642
/* FILE class */
1635
1643
/* the class ID is created once */
1636
- JS_NewClassID (rt , & js_std_file_class_id );
1644
+ JS_NewClassID (rt , & ts -> std_file_class_id );
1637
1645
/* the class is created once per runtime */
1638
- JS_NewClass (rt , js_std_file_class_id , & js_std_file_class );
1646
+ JS_NewClass (rt , ts -> std_file_class_id , & js_std_file_class );
1639
1647
proto = JS_NewObject (ctx );
1640
1648
JS_SetPropertyFunctionList (ctx , proto , js_std_file_proto_funcs ,
1641
1649
countof (js_std_file_proto_funcs ));
1642
- JS_SetClassProto (ctx , js_std_file_class_id , proto );
1650
+ JS_SetClassProto (ctx , ts -> std_file_class_id , proto );
1643
1651
1644
1652
JS_SetModuleExportList (ctx , m , js_std_funcs ,
1645
1653
countof (js_std_funcs ));
@@ -3275,7 +3283,6 @@ typedef struct {
3275
3283
uint64_t buf [];
3276
3284
} JSSABHeader ;
3277
3285
3278
- static JSClassID js_worker_class_id ;
3279
3286
static JSContext * (* js_worker_new_context_func )(JSRuntime * rt );
3280
3287
3281
3288
static int atomic_add_int (int * ptr , int v )
@@ -3388,7 +3395,8 @@ static void js_free_port(JSRuntime *rt, JSWorkerMessageHandler *port)
3388
3395
3389
3396
static void js_worker_finalizer (JSRuntime * rt , JSValue val )
3390
3397
{
3391
- JSWorkerData * worker = JS_GetOpaque (val , js_worker_class_id );
3398
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3399
+ JSWorkerData * worker = JS_GetOpaque (val , ts -> worker_class_id );
3392
3400
if (worker ) {
3393
3401
js_free_message_pipe (worker -> recv_pipe );
3394
3402
js_free_message_pipe (worker -> send_pipe );
@@ -3447,7 +3455,6 @@ static void *worker_func(void *opaque)
3447
3455
js_std_loop (ctx );
3448
3456
3449
3457
JS_FreeContext (ctx );
3450
- js_std_free_handlers (rt );
3451
3458
JS_FreeRuntime (rt );
3452
3459
return NULL ;
3453
3460
}
@@ -3456,18 +3463,20 @@ static JSValue js_worker_ctor_internal(JSContext *ctx, JSValue new_target,
3456
3463
JSWorkerMessagePipe * recv_pipe ,
3457
3464
JSWorkerMessagePipe * send_pipe )
3458
3465
{
3466
+ JSRuntime * rt = JS_GetRuntime (ctx );
3467
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3459
3468
JSValue obj = JS_UNDEFINED , proto ;
3460
3469
JSWorkerData * s ;
3461
3470
3462
3471
/* create the object */
3463
3472
if (JS_IsUndefined (new_target )) {
3464
- proto = JS_GetClassProto (ctx , js_worker_class_id );
3473
+ proto = JS_GetClassProto (ctx , ts -> worker_class_id );
3465
3474
} else {
3466
3475
proto = JS_GetPropertyStr (ctx , new_target , "prototype" );
3467
3476
if (JS_IsException (proto ))
3468
3477
goto fail ;
3469
3478
}
3470
- obj = JS_NewObjectProtoClass (ctx , proto , js_worker_class_id );
3479
+ obj = JS_NewObjectProtoClass (ctx , proto , ts -> worker_class_id );
3471
3480
JS_FreeValue (ctx , proto );
3472
3481
if (JS_IsException (obj ))
3473
3482
goto fail ;
@@ -3571,7 +3580,9 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValue new_target,
3571
3580
static JSValue js_worker_postMessage (JSContext * ctx , JSValue this_val ,
3572
3581
int argc , JSValue * argv )
3573
3582
{
3574
- JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , js_worker_class_id );
3583
+ JSRuntime * rt = JS_GetRuntime (ctx );
3584
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3585
+ JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , ts -> worker_class_id );
3575
3586
JSWorkerMessagePipe * ps ;
3576
3587
size_t data_len , i ;
3577
3588
uint8_t * data ;
@@ -3650,7 +3661,7 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValue this_val,
3650
3661
{
3651
3662
JSRuntime * rt = JS_GetRuntime (ctx );
3652
3663
JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3653
- JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , js_worker_class_id );
3664
+ JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , ts -> worker_class_id );
3654
3665
JSWorkerMessageHandler * port ;
3655
3666
3656
3667
if (!worker )
@@ -3682,7 +3693,9 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValue this_val,
3682
3693
3683
3694
static JSValue js_worker_get_onmessage (JSContext * ctx , JSValue this_val )
3684
3695
{
3685
- JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , js_worker_class_id );
3696
+ JSRuntime * rt = JS_GetRuntime (ctx );
3697
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3698
+ JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , ts -> worker_class_id );
3686
3699
JSWorkerMessageHandler * port ;
3687
3700
if (!worker )
3688
3701
return JS_EXCEPTION ;
@@ -3835,16 +3848,16 @@ static int js_os_init(JSContext *ctx, JSModuleDef *m)
3835
3848
JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3836
3849
JSValue proto , obj ;
3837
3850
/* Worker class */
3838
- JS_NewClassID (rt , & js_worker_class_id );
3839
- JS_NewClass (rt , js_worker_class_id , & js_worker_class );
3851
+ JS_NewClassID (rt , & ts -> worker_class_id );
3852
+ JS_NewClass (rt , ts -> worker_class_id , & js_worker_class );
3840
3853
proto = JS_NewObject (ctx );
3841
3854
JS_SetPropertyFunctionList (ctx , proto , js_worker_proto_funcs , countof (js_worker_proto_funcs ));
3842
3855
3843
3856
obj = JS_NewCFunction2 (ctx , js_worker_ctor , "Worker" , 1 ,
3844
3857
JS_CFUNC_constructor , 0 );
3845
3858
JS_SetConstructor (ctx , obj , proto );
3846
3859
3847
- JS_SetClassProto (ctx , js_worker_class_id , proto );
3860
+ JS_SetClassProto (ctx , ts -> worker_class_id , proto );
3848
3861
3849
3862
/* set 'Worker.parent' if necessary */
3850
3863
if (ts -> recv_pipe && ts -> send_pipe ) {
@@ -3954,6 +3967,36 @@ void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
3954
3967
JS_FreeValue (ctx , global_obj );
3955
3968
}
3956
3969
3970
+ static void js_std_rt_finalizer (JSRuntime * rt , void * arg )
3971
+ {
3972
+ JSThreadState * ts = arg ;
3973
+ struct list_head * el , * el1 ;
3974
+
3975
+ list_for_each_safe (el , el1 , & ts -> os_rw_handlers ) {
3976
+ JSOSRWHandler * rh = list_entry (el , JSOSRWHandler , link );
3977
+ free_rw_handler (rt , rh );
3978
+ }
3979
+
3980
+ list_for_each_safe (el , el1 , & ts -> os_signal_handlers ) {
3981
+ JSOSSignalHandler * sh = list_entry (el , JSOSSignalHandler , link );
3982
+ free_sh (rt , sh );
3983
+ }
3984
+
3985
+ list_for_each_safe (el , el1 , & ts -> os_timers ) {
3986
+ JSOSTimer * th = list_entry (el , JSOSTimer , link );
3987
+ free_timer (rt , th );
3988
+ }
3989
+
3990
+ #ifdef USE_WORKER
3991
+ /* XXX: free port_list ? */
3992
+ js_free_message_pipe (ts -> recv_pipe );
3993
+ js_free_message_pipe (ts -> send_pipe );
3994
+ #endif
3995
+
3996
+ free (ts );
3997
+ JS_SetRuntimeOpaque (rt , NULL ); /* fail safe */
3998
+ }
3999
+
3957
4000
void js_std_init_handlers (JSRuntime * rt )
3958
4001
{
3959
4002
JSThreadState * ts ;
@@ -3973,6 +4016,7 @@ void js_std_init_handlers(JSRuntime *rt)
3973
4016
ts -> exc = JS_UNDEFINED ;
3974
4017
3975
4018
JS_SetRuntimeOpaque (rt , ts );
4019
+ JS_AddRuntimeFinalizer (rt , js_std_rt_finalizer , ts );
3976
4020
3977
4021
#ifdef USE_WORKER
3978
4022
/* set the SharedArrayBuffer memory handlers */
@@ -3989,32 +4033,6 @@ void js_std_init_handlers(JSRuntime *rt)
3989
4033
3990
4034
void js_std_free_handlers (JSRuntime * rt )
3991
4035
{
3992
- JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3993
- struct list_head * el , * el1 ;
3994
-
3995
- list_for_each_safe (el , el1 , & ts -> os_rw_handlers ) {
3996
- JSOSRWHandler * rh = list_entry (el , JSOSRWHandler , link );
3997
- free_rw_handler (rt , rh );
3998
- }
3999
-
4000
- list_for_each_safe (el , el1 , & ts -> os_signal_handlers ) {
4001
- JSOSSignalHandler * sh = list_entry (el , JSOSSignalHandler , link );
4002
- free_sh (rt , sh );
4003
- }
4004
-
4005
- list_for_each_safe (el , el1 , & ts -> os_timers ) {
4006
- JSOSTimer * th = list_entry (el , JSOSTimer , link );
4007
- free_timer (rt , th );
4008
- }
4009
-
4010
- #ifdef USE_WORKER
4011
- /* XXX: free port_list ? */
4012
- js_free_message_pipe (ts -> recv_pipe );
4013
- js_free_message_pipe (ts -> send_pipe );
4014
- #endif
4015
-
4016
- free (ts );
4017
- JS_SetRuntimeOpaque (rt , NULL ); /* fail safe */
4018
4036
}
4019
4037
4020
4038
static void js_dump_obj (JSContext * ctx , FILE * f , JSValue val )
0 commit comments