Skip to content

Commit 76aec02

Browse files
committed
Added a compile-time macro QJS_ENABLE_DEBUGGER to gate all the debug-related code
1 parent afc287f commit 76aec02

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ xoption(QJS_BUILD_CLI_STATIC "Build a static qjs executable" OFF)
3636
xoption(QJS_BUILD_CLI_WITH_MIMALLOC "Build the qjs executable with mimalloc" OFF)
3737
xoption(QJS_BUILD_CLI_WITH_STATIC_MIMALLOC "Build the qjs executable with mimalloc (statically linked)" OFF)
3838
xoption(QJS_DISABLE_PARSER "Disable JS source code parser" OFF)
39+
xoption(QJS_ENABLE_DEBUGGER "Enable debugging interface" OFF)
3940
xoption(QJS_ENABLE_ASAN "Enable AddressSanitizer (ASan)" OFF)
4041
xoption(QJS_ENABLE_MSAN "Enable MemorySanitizer (MSan)" OFF)
4142
xoption(QJS_ENABLE_TSAN "Enable ThreadSanitizer (TSan)" OFF)

quickjs.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#include "libregexp.h"
4949
#include "dtoa.h"
5050

51-
#if defined(EMSCRIPTEN) || defined(_MSC_VER)
51+
#if defined(EMSCRIPTEN) || defined(_MSC_VER) || defined(QJS_ENABLE_DEBUGGER)
5252
#define DIRECT_DISPATCH 0
5353
#else
5454
#define DIRECT_DISPATCH 1
@@ -535,9 +535,10 @@ struct JSContext {
535535
const char *input, size_t input_len,
536536
const char *filename, int line, int flags, int scope_idx);
537537
void *user_opaque;
538-
538+
#ifdef QJS_ENABLE_DEBUGGER
539539
JSOPChangedHandler *operation_changed;
540540
void *oc_opaque;
541+
#endif
541542
};
542543

543544
typedef union JSFloat64Union {
@@ -2560,6 +2561,8 @@ JSValue JS_GetFunctionProto(JSContext *ctx)
25602561
return js_dup(ctx->function_proto);
25612562
}
25622563

2564+
#ifdef QJS_ENABLE_DEBUGGER
2565+
25632566
void JS_SetOPChangedHandler(JSContext *ctx, JSOPChangedHandler *cb, void *opaque)
25642567
{
25652568
ctx->operation_changed = cb;
@@ -2595,7 +2598,7 @@ int JS_GetStackDepth(JSContext *ctx)
25952598
}
25962599

25972600
/* Get local variables at a specific stack level */
2598-
JSLocalVar *JS_GetLocalVariablesAtLevel(JSContext *ctx, int level, int *pcount)
2601+
JSDebugLocalVar *JS_GetLocalVariablesAtLevel(JSContext *ctx, int level, int *pcount)
25992602
{
26002603
if (pcount)
26012604
*pcount = 0;
@@ -2618,7 +2621,7 @@ JSLocalVar *JS_GetLocalVariablesAtLevel(JSContext *ctx, int level, int *pcount)
26182621
if (total_vars == 0)
26192622
return NULL;
26202623

2621-
JSLocalVar *vars = js_malloc(ctx, sizeof(JSLocalVar) * total_vars);
2624+
JSDebugLocalVar *vars = js_malloc(ctx, sizeof(JSDebugLocalVar) * total_vars);
26222625
if (!vars)
26232626
return NULL;
26242627

@@ -2648,7 +2651,7 @@ JSLocalVar *JS_GetLocalVariablesAtLevel(JSContext *ctx, int level, int *pcount)
26482651
}
26492652

26502653
/* Free local variables array */
2651-
void JS_FreeLocalVariables(JSContext *ctx, JSLocalVar *vars, int count)
2654+
void JS_FreeLocalVariables(JSContext *ctx, JSDebugLocalVar *vars, int count)
26522655
{
26532656
if (!vars)
26542657
return;
@@ -2659,6 +2662,8 @@ void JS_FreeLocalVariables(JSContext *ctx, JSLocalVar *vars, int count)
26592662
js_free(ctx, vars);
26602663
}
26612664

2665+
#endif /* QJS_ENABLE_DEBUGGER */
2666+
26622667
typedef enum JSFreeModuleEnum {
26632668
JS_FREE_MODULE_ALL,
26642669
JS_FREE_MODULE_NOT_RESOLVED,
@@ -17573,34 +17578,29 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1757317578
int call_argc;
1757417579
JSValue *call_argv;
1757517580

17576-
{
17577-
if (b && ctx->operation_changed != NULL) {
17578-
int col_num = 0;
17579-
int line_num = -1;
17580-
const char *filename = NULL;
17581-
const char *funcname = NULL;
17581+
#ifdef QJS_ENABLE_DEBUGGER
17582+
if (b && ctx->operation_changed != NULL) {
17583+
int col_num = 0;
17584+
int line_num = -1;
1758217585

17583-
uint32_t pc_index = (uint32_t)(pc - b->byte_code_buf);
17584-
if (b->pc2line_buf) {
17585-
line_num = find_line_num(ctx, b, pc_index, &col_num);
17586-
}
17587-
filename = b->filename ? JS_AtomToCString(ctx, b->filename) : NULL;
17588-
funcname = b->func_name ? JS_AtomToCString(ctx, b->func_name) : NULL;
17586+
uint32_t pc_index = (uint32_t)(pc - b->byte_code_buf);
17587+
if (b->pc2line_buf) {
17588+
line_num = find_line_num(ctx, b, pc_index, &col_num);
17589+
}
17590+
const char *filename = b->filename ? JS_AtomToCString(ctx, b->filename) : NULL;
17591+
const char *funcname = b->func_name ? JS_AtomToCString(ctx, b->func_name) : NULL;
1758917592

17590-
int ret = 0;
17591-
ret = ctx->operation_changed(ctx, *pc, filename, funcname, line_num, col_num, ctx->oc_opaque);
17592-
if (filename) {
17593-
// fprintf(stderr, "op:%d %d at %s %s:%d:%d\n", *pc, OP_return, funcname, filename, line_num, col_num);
17594-
JS_FreeCString(ctx, filename);
17595-
JS_FreeCString(ctx, funcname);
17596-
}
17593+
int ret = ctx->operation_changed(ctx, *pc, filename, funcname,
17594+
line_num, col_num, ctx->oc_opaque);
17595+
if (filename)
17596+
JS_FreeCString(ctx, filename);
17597+
if (funcname)
17598+
JS_FreeCString(ctx, funcname);
1759717599

17598-
if(ret != 0)
17599-
{
17600-
goto exception;
17601-
}
17602-
}
17600+
if (ret != 0)
17601+
goto exception;
1760317602
}
17603+
#endif /* QJS_ENABLE_DEBUGGER */
1760417604

1760517605
SWITCH(pc) {
1760617606
CASE(OP_push_i32):

quickjs.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,10 @@ JS_EXTERN void JS_SetClassProto(JSContext *ctx, JSClassID class_id, JSValue obj)
542542
JS_EXTERN JSValue JS_GetClassProto(JSContext *ctx, JSClassID class_id);
543543
JS_EXTERN JSValue JS_GetFunctionProto(JSContext *ctx);
544544

545-
/* Debug callback - called for each bytecode operation during execution */
545+
#ifdef QJS_ENABLE_DEBUGGER
546+
547+
/* Debug callback - called for each bytecode operation during execution.
548+
Return 0 to continue, non-zero to raise an exception. */
546549
typedef int JSOPChangedHandler(JSContext *ctx,
547550
uint8_t op,
548551
const char *filename,
@@ -553,23 +556,26 @@ typedef int JSOPChangedHandler(JSContext *ctx,
553556
JS_EXTERN void JS_SetOPChangedHandler(JSContext *ctx, JSOPChangedHandler *cb, void *opaque);
554557

555558
/* Debug API: Get local variables in stack frames */
556-
typedef struct JSLocalVar {
559+
typedef struct JSDebugLocalVar {
557560
const char *name; /* variable name (must be freed with JS_FreeCString) */
558561
JSValue value; /* variable value (must be freed with JS_FreeValue) */
559562
int is_arg; /* 1 if argument, 0 if local variable */
560563
int scope_level; /* scope level of the variable */
561-
} JSLocalVar;
564+
} JSDebugLocalVar;
562565

563566
/* Get the call stack depth */
564567
JS_EXTERN int JS_GetStackDepth(JSContext *ctx);
565568

566569
/* Get local variables at a specific stack level (0 = current frame, 1 = caller, etc.)
567570
*pcount: output, number of variables returned
568-
Returns allocated array of JSLocalVar (must be freed with JS_FreeLocalVariables), or NULL on error */
569-
JS_EXTERN JSLocalVar *JS_GetLocalVariablesAtLevel(JSContext *ctx, int level, int *pcount);
571+
Returns allocated array of JSDebugLocalVar (must be freed with JS_FreeLocalVariables),
572+
or NULL on error */
573+
JS_EXTERN JSDebugLocalVar *JS_GetLocalVariablesAtLevel(JSContext *ctx, int level, int *pcount);
570574

571575
/* Free local variables array returned by JS_GetLocalVariablesAtLevel */
572-
JS_EXTERN void JS_FreeLocalVariables(JSContext *ctx, JSLocalVar *vars, int count);
576+
JS_EXTERN void JS_FreeLocalVariables(JSContext *ctx, JSDebugLocalVar *vars, int count);
577+
578+
#endif /* QJS_ENABLE_DEBUGGER */
573579

574580
/* the following functions are used to select the intrinsic object to
575581
save memory */

0 commit comments

Comments
 (0)