@@ -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 is_popen ;
@@ -888,7 +888,8 @@ static BOOL is_stdio(FILE *f)
888
888
889
889
static void js_std_file_finalizer (JSRuntime * rt , JSValue val )
890
890
{
891
- JSSTDFile * s = JS_GetOpaque (val , js_std_file_class_id );
891
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
892
+ JSSTDFile * s = JS_GetOpaque (val , ts -> std_file_class_id );
892
893
if (s ) {
893
894
if (s -> f && !is_stdio (s -> f )) {
894
895
#if !defined(__wasi__ )
@@ -920,9 +921,11 @@ static JSValue js_std_strerror(JSContext *ctx, JSValue this_val,
920
921
921
922
static JSValue js_new_std_file (JSContext * ctx , FILE * f , BOOL is_popen )
922
923
{
924
+ JSRuntime * rt = JS_GetRuntime (ctx );
925
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
923
926
JSSTDFile * s ;
924
927
JSValue obj ;
925
- obj = JS_NewObjectClass (ctx , js_std_file_class_id );
928
+ obj = JS_NewObjectClass (ctx , ts -> std_file_class_id );
926
929
if (JS_IsException (obj ))
927
930
return obj ;
928
931
s = js_mallocz (ctx , sizeof (* s ));
@@ -1078,7 +1081,9 @@ static JSValue js_std_printf(JSContext *ctx, JSValue this_val,
1078
1081
1079
1082
static FILE * js_std_file_get (JSContext * ctx , JSValue obj )
1080
1083
{
1081
- JSSTDFile * s = JS_GetOpaque2 (ctx , obj , js_std_file_class_id );
1084
+ JSRuntime * rt = JS_GetRuntime (ctx );
1085
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1086
+ JSSTDFile * s = JS_GetOpaque2 (ctx , obj , ts -> std_file_class_id );
1082
1087
if (!s )
1083
1088
return NULL ;
1084
1089
if (!s -> f ) {
@@ -1117,7 +1122,9 @@ static JSValue js_std_file_puts(JSContext *ctx, JSValue this_val,
1117
1122
static JSValue js_std_file_close (JSContext * ctx , JSValue this_val ,
1118
1123
int argc , JSValue * argv )
1119
1124
{
1120
- JSSTDFile * s = JS_GetOpaque2 (ctx , this_val , js_std_file_class_id );
1125
+ JSRuntime * rt = JS_GetRuntime (ctx );
1126
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1127
+ JSSTDFile * s = JS_GetOpaque2 (ctx , this_val , ts -> std_file_class_id );
1121
1128
int err ;
1122
1129
if (!s )
1123
1130
return JS_EXCEPTION ;
@@ -1633,16 +1640,17 @@ static int js_std_init(JSContext *ctx, JSModuleDef *m)
1633
1640
{
1634
1641
JSValue proto ;
1635
1642
JSRuntime * rt = JS_GetRuntime (ctx );
1643
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
1636
1644
1637
1645
/* FILE class */
1638
1646
/* the class ID is created once */
1639
- JS_NewClassID (rt , & js_std_file_class_id );
1647
+ JS_NewClassID (rt , & ts -> std_file_class_id );
1640
1648
/* the class is created once per runtime */
1641
- JS_NewClass (rt , js_std_file_class_id , & js_std_file_class );
1649
+ JS_NewClass (rt , ts -> std_file_class_id , & js_std_file_class );
1642
1650
proto = JS_NewObject (ctx );
1643
1651
JS_SetPropertyFunctionList (ctx , proto , js_std_file_proto_funcs ,
1644
1652
countof (js_std_file_proto_funcs ));
1645
- JS_SetClassProto (ctx , js_std_file_class_id , proto );
1653
+ JS_SetClassProto (ctx , ts -> std_file_class_id , proto );
1646
1654
1647
1655
JS_SetModuleExportList (ctx , m , js_std_funcs ,
1648
1656
countof (js_std_funcs ));
@@ -3278,7 +3286,6 @@ typedef struct {
3278
3286
uint64_t buf [];
3279
3287
} JSSABHeader ;
3280
3288
3281
- static JSClassID js_worker_class_id ;
3282
3289
static JSContext * (* js_worker_new_context_func )(JSRuntime * rt );
3283
3290
3284
3291
static int atomic_add_int (int * ptr , int v )
@@ -3391,7 +3398,8 @@ static void js_free_port(JSRuntime *rt, JSWorkerMessageHandler *port)
3391
3398
3392
3399
static void js_worker_finalizer (JSRuntime * rt , JSValue val )
3393
3400
{
3394
- JSWorkerData * worker = JS_GetOpaque (val , js_worker_class_id );
3401
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3402
+ JSWorkerData * worker = JS_GetOpaque (val , ts -> worker_class_id );
3395
3403
if (worker ) {
3396
3404
js_free_message_pipe (worker -> recv_pipe );
3397
3405
js_free_message_pipe (worker -> send_pipe );
@@ -3459,18 +3467,20 @@ static JSValue js_worker_ctor_internal(JSContext *ctx, JSValue new_target,
3459
3467
JSWorkerMessagePipe * recv_pipe ,
3460
3468
JSWorkerMessagePipe * send_pipe )
3461
3469
{
3470
+ JSRuntime * rt = JS_GetRuntime (ctx );
3471
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3462
3472
JSValue obj = JS_UNDEFINED , proto ;
3463
3473
JSWorkerData * s ;
3464
3474
3465
3475
/* create the object */
3466
3476
if (JS_IsUndefined (new_target )) {
3467
- proto = JS_GetClassProto (ctx , js_worker_class_id );
3477
+ proto = JS_GetClassProto (ctx , ts -> worker_class_id );
3468
3478
} else {
3469
3479
proto = JS_GetPropertyStr (ctx , new_target , "prototype" );
3470
3480
if (JS_IsException (proto ))
3471
3481
goto fail ;
3472
3482
}
3473
- obj = JS_NewObjectProtoClass (ctx , proto , js_worker_class_id );
3483
+ obj = JS_NewObjectProtoClass (ctx , proto , ts -> worker_class_id );
3474
3484
JS_FreeValue (ctx , proto );
3475
3485
if (JS_IsException (obj ))
3476
3486
goto fail ;
@@ -3574,7 +3584,9 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValue new_target,
3574
3584
static JSValue js_worker_postMessage (JSContext * ctx , JSValue this_val ,
3575
3585
int argc , JSValue * argv )
3576
3586
{
3577
- JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , js_worker_class_id );
3587
+ JSRuntime * rt = JS_GetRuntime (ctx );
3588
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3589
+ JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , ts -> worker_class_id );
3578
3590
JSWorkerMessagePipe * ps ;
3579
3591
size_t data_len , i ;
3580
3592
uint8_t * data ;
@@ -3653,7 +3665,7 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValue this_val,
3653
3665
{
3654
3666
JSRuntime * rt = JS_GetRuntime (ctx );
3655
3667
JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3656
- JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , js_worker_class_id );
3668
+ JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , ts -> worker_class_id );
3657
3669
JSWorkerMessageHandler * port ;
3658
3670
3659
3671
if (!worker )
@@ -3685,7 +3697,9 @@ static JSValue js_worker_set_onmessage(JSContext *ctx, JSValue this_val,
3685
3697
3686
3698
static JSValue js_worker_get_onmessage (JSContext * ctx , JSValue this_val )
3687
3699
{
3688
- JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , js_worker_class_id );
3700
+ JSRuntime * rt = JS_GetRuntime (ctx );
3701
+ JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3702
+ JSWorkerData * worker = JS_GetOpaque2 (ctx , this_val , ts -> worker_class_id );
3689
3703
JSWorkerMessageHandler * port ;
3690
3704
if (!worker )
3691
3705
return JS_EXCEPTION ;
@@ -3838,16 +3852,16 @@ static int js_os_init(JSContext *ctx, JSModuleDef *m)
3838
3852
JSThreadState * ts = JS_GetRuntimeOpaque (rt );
3839
3853
JSValue proto , obj ;
3840
3854
/* Worker class */
3841
- JS_NewClassID (rt , & js_worker_class_id );
3842
- JS_NewClass (rt , js_worker_class_id , & js_worker_class );
3855
+ JS_NewClassID (rt , & ts -> worker_class_id );
3856
+ JS_NewClass (rt , ts -> worker_class_id , & js_worker_class );
3843
3857
proto = JS_NewObject (ctx );
3844
3858
JS_SetPropertyFunctionList (ctx , proto , js_worker_proto_funcs , countof (js_worker_proto_funcs ));
3845
3859
3846
3860
obj = JS_NewCFunction2 (ctx , js_worker_ctor , "Worker" , 1 ,
3847
3861
JS_CFUNC_constructor , 0 );
3848
3862
JS_SetConstructor (ctx , obj , proto );
3849
3863
3850
- JS_SetClassProto (ctx , js_worker_class_id , proto );
3864
+ JS_SetClassProto (ctx , ts -> worker_class_id , proto );
3851
3865
3852
3866
/* set 'Worker.parent' if necessary */
3853
3867
if (ts -> recv_pipe && ts -> send_pipe ) {
@@ -3961,7 +3975,7 @@ void js_std_init_handlers(JSRuntime *rt)
3961
3975
{
3962
3976
JSThreadState * ts ;
3963
3977
3964
- ts = malloc ( sizeof (* ts ));
3978
+ ts = js_malloc_rt ( rt , sizeof (* ts ));
3965
3979
if (!ts ) {
3966
3980
fprintf (stderr , "Could not allocate memory for the worker" );
3967
3981
exit (1 );
@@ -3976,6 +3990,7 @@ void js_std_init_handlers(JSRuntime *rt)
3976
3990
ts -> exc = JS_UNDEFINED ;
3977
3991
3978
3992
JS_SetRuntimeOpaque (rt , ts );
3993
+ JS_AddRuntimeFinalizer (rt , js_free_rt , ts );
3979
3994
3980
3995
#ifdef USE_WORKER
3981
3996
/* set the SharedArrayBuffer memory handlers */
@@ -4015,9 +4030,6 @@ void js_std_free_handlers(JSRuntime *rt)
4015
4030
js_free_message_pipe (ts -> recv_pipe );
4016
4031
js_free_message_pipe (ts -> send_pipe );
4017
4032
#endif
4018
-
4019
- free (ts );
4020
- JS_SetRuntimeOpaque (rt , NULL ); /* fail safe */
4021
4033
}
4022
4034
4023
4035
static void js_dump_obj (JSContext * ctx , FILE * f , JSValue val )
0 commit comments