59
59
#define LOG_LEN 500
60
60
61
61
#ifdef QTS_DEBUG_MODE
62
- #define QTS_DEBUG (msg ) qts_log(msg);
63
- #define QTS_DUMP (value ) qts_dump(ctx, value);
62
+ #define IF_DEBUG if (QTS_GetContextData(ctx)->debug_log)
63
+ #define IF_DEBUG_RT if (QTS_GetRuntimeData(rt)->debug_log)
64
+
65
+ #define QTS_DEBUG (msg ) IF_DEBUG qts_log(msg);
66
+ #define QTS_DEBUG_RT (msg ) IF_DEBUG_RT qts_log(msg);
67
+ #define QTS_DUMP (value ) IF_DEBUG qts_dump(ctx, value);
68
+
64
69
#else
70
+ #define IF_DEBUG if (0)
71
+ #define IF_DEBUG_RT if (0)
65
72
#define QTS_DEBUG (msg ) ;
73
+ #define QTS_DEBUG_RT (msg ) ;
66
74
#define QTS_DUMP (value ) ;
67
75
#endif
68
76
93
101
#define IntrinsicsFlags enum QTS_Intrinsic
94
102
#define EvalDetectModule int
95
103
104
+ typedef struct QTS_RuntimeData {
105
+ bool debug_log ;
106
+ } QTS_RuntimeData ;
107
+
108
+ QTS_RuntimeData * QTS_GetRuntimeData (JSRuntime * rt ) {
109
+ QTS_RuntimeData * data = JS_GetRuntimeOpaque (rt );
110
+ if (data == NULL ) {
111
+ data = malloc (sizeof (QTS_RuntimeData ));
112
+ data -> debug_log = false;
113
+ JS_SetRuntimeOpaque (rt , data );
114
+ }
115
+ return data ;
116
+ }
117
+
118
+ QTS_RuntimeData * QTS_GetContextData (JSContext * ctx ) {
119
+ return QTS_GetRuntimeData (JS_GetRuntime (ctx ));
120
+ }
121
+
96
122
void qts_log (char * msg ) {
97
123
fputs (PKG , stderr );
98
124
fputs (msg , stderr );
@@ -273,6 +299,10 @@ JSRuntime *QTS_NewRuntime() {
273
299
}
274
300
275
301
void QTS_FreeRuntime (JSRuntime * rt ) {
302
+ void * data = JS_GetRuntimeOpaque (rt );
303
+ if (data ) {
304
+ free (data );
305
+ }
276
306
JS_FreeRuntime (rt );
277
307
}
278
308
@@ -533,11 +563,11 @@ MaybeAsync(JSValue *) QTS_ExecutePendingJob(JSRuntime *rt, int maxJobsToExecute,
533
563
executed ++ ;
534
564
}
535
565
}
536
- #ifdef QTS_DEBUG_MODE
537
- char msg [500 ];
538
- snprintf (msg , 500 , "QTS_ExecutePendingJob(executed: %d, pctx: %p, lastJobExecuted: %p)" , executed , pctx , * lastJobContext );
539
- QTS_DEBUG (msg )
540
- #endif
566
+ IF_DEBUG_RT {
567
+ char msg [LOG_LEN ];
568
+ snprintf (msg , LOG_LEN , "QTS_ExecutePendingJob(executed: %d, pctx: %p, lastJobExecuted: %p)" , executed , pctx , * lastJobContext );
569
+ qts_log (msg );
570
+ }
541
571
return jsvalue_to_heap (JS_NewFloat64 (pctx , executed ));
542
572
}
543
573
@@ -710,10 +740,10 @@ MaybeAsync(JSBorrowedChar *) QTS_Dump(JSContext *ctx, JSValueConst *obj) {
710
740
}
711
741
}
712
742
713
- #ifdef QTS_DEBUG_MODE
714
- qts_log ("Error dumping JSON:" );
715
- js_std_dump_error (ctx );
716
- #endif
743
+ IF_DEBUG {
744
+ qts_log ("Error dumping JSON:" );
745
+ js_std_dump_error (ctx );
746
+ }
717
747
718
748
// Fallback: convert to string
719
749
return QTS_GetString (ctx , obj );
@@ -731,7 +761,7 @@ JSValue qts_resolve_func_data(
731
761
732
762
MaybeAsync (JSValue * ) QTS_Eval (JSContext * ctx , BorrowedHeapChar * js_code , size_t js_code_length , const char * filename , EvalDetectModule detectModule , EvalFlags evalFlags ) {
733
763
#ifdef QTS_DEBUG_MODE
734
- char msg [500 ];
764
+ char msg [LOG_LEN ];
735
765
#endif
736
766
if (detectModule ) {
737
767
if (JS_DetectModule ((const char * )js_code , js_code_length )) {
@@ -778,10 +808,10 @@ MaybeAsync(JSValue *) QTS_Eval(JSContext *ctx, BorrowedHeapChar *js_code, size_t
778
808
QTS_DEBUG ("QTS_Eval: JS_EVAL_TYPE_GLOBAL eval_result" )
779
809
}
780
810
781
- #ifdef QTS_DEBUG_MODE
782
- snprintf (msg , 500 , "QTS_Eval: eval_result = %d" , eval_result );
783
- QTS_DEBUG (msg )
784
- #endif
811
+ IF_DEBUG {
812
+ snprintf (msg , LOG_LEN , "QTS_Eval: eval_result = %d" , eval_result );
813
+ qts_log (msg );
814
+ }
785
815
786
816
if (
787
817
// Error - nothing more to do.
@@ -795,10 +825,10 @@ MaybeAsync(JSValue *) QTS_Eval(JSContext *ctx, BorrowedHeapChar *js_code, size_t
795
825
// We eval'd a module.
796
826
// Make our return type `ModuleExports | Promise<ModuleExports>>`
797
827
JSPromiseStateEnum state = JS_PromiseState (ctx , eval_result );
798
- #ifdef QTS_DEBUG_MODE
799
- snprintf (msg , 500 , "QTS_Eval: eval_result JS_PromiseState = %i" , state );
800
- QTS_DEBUG (msg )
801
- #endif
828
+ IF_DEBUG {
829
+ snprintf (msg , LOG_LEN , "QTS_Eval: eval_result JS_PromiseState = %i" , state );
830
+ qts_log (msg );
831
+ }
802
832
if (
803
833
// quickjs@2024-01-14 evaluating module
804
834
// produced a promise
@@ -965,6 +995,19 @@ void QTS_TestStringArg(const char *string) {
965
995
// pass
966
996
}
967
997
998
+ int QTS_GetDebugLogEnabled (JSRuntime * rt ) {
999
+ IF_DEBUG_RT {
1000
+ return 1 ;
1001
+ }
1002
+ return 0 ;
1003
+ }
1004
+
1005
+ void QTS_SetDebugLogEnabled (JSRuntime * rt , int is_enabled ) {
1006
+ #ifdef QTS_DEBUG_MODE
1007
+ QTS_GetRuntimeData (rt )-> debug_log = (bool )is_enabled ;
1008
+ #endif
1009
+ }
1010
+
968
1011
int QTS_BuildIsDebug () {
969
1012
#ifdef QTS_DEBUG_MODE
970
1013
return 1 ;
@@ -1016,11 +1059,11 @@ JSValue qts_call_function(JSContext *ctx, JSValueConst this_val, int argc, JSVal
1016
1059
1017
1060
// Function: Host -> QuickJS
1018
1061
JSValue * QTS_NewFunction (JSContext * ctx , uint32_t func_id , const char * name ) {
1019
- #ifdef QTS_DEBUG_MODE
1020
- char msg [500 ];
1021
- snprintf (msg , 500 , "new_function(name: %s, magic: %d)" , name , func_id );
1022
- QTS_DEBUG (msg )
1023
- #endif
1062
+ IF_DEBUG {
1063
+ char msg [LOG_LEN ];
1064
+ snprintf (msg , LOG_LEN , "new_function(name: %s, magic: %d)" , name , func_id );
1065
+ qts_log (msg );
1066
+ }
1024
1067
JSValue func_obj = JS_NewCFunctionMagic (
1025
1068
/* context */ ctx ,
1026
1069
/* JSCFunctionMagic* */ & qts_call_function ,
@@ -1101,11 +1144,11 @@ loading, but (1) seems much easier to implement in the sort run.
1101
1144
*/
1102
1145
1103
1146
JSModuleDef * qts_compile_module (JSContext * ctx , const char * module_name , BorrowedHeapChar * module_body ) {
1104
- #ifdef QTS_DEBUG_MODE
1105
- char msg [500 ];
1106
- sprintf (msg , "QTS_CompileModule(ctx: %p, name: %s, bodyLength: %lu)" , ctx , module_name , strlen (module_body ));
1107
- QTS_DEBUG (msg )
1108
- #endif
1147
+ IF_DEBUG {
1148
+ char msg [LOG_LEN ];
1149
+ sprintf (msg , "QTS_CompileModule(ctx: %p, name: %s, bodyLength: %lu)" , ctx , module_name , strlen (module_body ));
1150
+ qts_log (msg );
1151
+ }
1109
1152
JSValue func_val = JS_Eval (ctx , module_body , strlen (module_body ), module_name , JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY );
1110
1153
if (JS_IsException (func_val )) {
1111
1154
return NULL ;
@@ -1146,11 +1189,11 @@ EM_JS(MaybeAsync(char *), qts_host_normalize_module, (JSRuntime * rt, JSContext
1146
1189
// See js_module_loader in quickjs/quickjs-libc.c:567
1147
1190
JSModuleDef * qts_load_module (JSContext * ctx , const char * module_name , void * _unused ) {
1148
1191
JSRuntime * rt = JS_GetRuntime (ctx );
1149
- #ifdef QTS_DEBUG_MODE
1150
- char msg [500 ];
1151
- sprintf (msg , "qts_load_module(rt: %p, ctx: %p, name: %s)" , rt , ctx , module_name );
1152
- QTS_DEBUG (msg )
1153
- #endif
1192
+ IF_DEBUG_RT {
1193
+ char msg [LOG_LEN ];
1194
+ sprintf (msg , "qts_load_module(rt: %p, ctx: %p, name: %s)" , rt , ctx , module_name );
1195
+ qts_log (msg );
1196
+ }
1154
1197
char * module_source = qts_host_load_module_source (rt , ctx , module_name );
1155
1198
if (module_source == NULL ) {
1156
1199
return NULL ;
@@ -1163,11 +1206,11 @@ JSModuleDef *qts_load_module(JSContext *ctx, const char *module_name, void *_unu
1163
1206
1164
1207
char * qts_normalize_module (JSContext * ctx , const char * module_base_name , const char * module_name , void * _unused ) {
1165
1208
JSRuntime * rt = JS_GetRuntime (ctx );
1166
- #ifdef QTS_DEBUG_MODE
1167
- char msg [500 ];
1168
- sprintf (msg , "qts_normalize_module(rt: %p, ctx: %p, base_name: %s, name: %s)" , rt , ctx , module_base_name , module_name );
1169
- QTS_DEBUG (msg )
1170
- #endif
1209
+ IF_DEBUG_RT {
1210
+ char msg [LOG_LEN ];
1211
+ sprintf (msg , "qts_normalize_module(rt: %p, ctx: %p, base_name: %s, name: %s)" , rt , ctx , module_base_name , module_name );
1212
+ qts_log (msg );
1213
+ }
1171
1214
char * em_module_name = qts_host_normalize_module (rt , ctx , module_base_name , module_name );
1172
1215
char * js_module_name = js_strdup (ctx , em_module_name );
1173
1216
free (em_module_name );
0 commit comments