Skip to content

Commit 80733f9

Browse files
committed
refactor: change filename and funcname parameters to JSAtom in debug trace functions
suggestion form @jprendes at #1421 (comment)
1 parent 831130b commit 80733f9

3 files changed

Lines changed: 28 additions & 29 deletions

File tree

api-test.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,27 +1017,25 @@ static struct {
10171017
int call_count;
10181018
int last_line;
10191019
int last_col;
1020-
char last_filename[256];
1021-
char last_funcname[256];
1020+
JSAtom last_filename;
1021+
JSAtom last_funcname;
10221022
int stack_depth;
10231023
int max_local_count;
10241024
int abort_at; /* abort (return -1) on this call, 0 = never */
10251025
} trace_state;
10261026

10271027
static int debug_trace_cb(JSContext *ctx,
1028-
const char *filename,
1029-
const char *funcname,
1028+
JSAtom filename,
1029+
JSAtom funcname,
10301030
int line,
10311031
int col,
10321032
void *opaque)
10331033
{
10341034
trace_state.call_count++;
10351035
trace_state.last_line = line;
10361036
trace_state.last_col = col;
1037-
snprintf(trace_state.last_filename, sizeof(trace_state.last_filename),
1038-
"%s", filename);
1039-
snprintf(trace_state.last_funcname, sizeof(trace_state.last_funcname),
1040-
"%s", funcname);
1037+
trace_state.last_filename = filename;
1038+
trace_state.last_funcname = funcname;
10411039
trace_state.stack_depth = JS_GetStackDepth(ctx);
10421040
int count = 0;
10431041
JSDebugLocalVar *vars = NULL;
@@ -1072,7 +1070,11 @@ static void debug_trace(void)
10721070
assert(!JS_IsException(ret));
10731071
JS_FreeValue(ctx, ret);
10741072
assert(trace_state.call_count > 0);
1075-
assert(!strcmp(trace_state.last_filename, "<input>"));
1073+
{
1074+
const char *fn = JS_AtomToCString(ctx, trace_state.last_filename);
1075+
assert(fn && !strcmp(fn, "<input>"));
1076+
JS_FreeCString(ctx, fn);
1077+
}
10761078
}
10771079

10781080
{

quickjs.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17833,25 +17833,13 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1783317833
uint32_t pc_index = (uint32_t)(pc - b->byte_code_buf - 1);
1783417834
line_num = find_line_num(ctx, b, pc_index, &col_num);
1783517835

17836-
/* Use JS_AtomToCString to get the full filename / funcname
17837-
without the 63-byte truncation that a stack buffer would
17838-
impose. The pointers are only valid for the duration of
17839-
the callback. */
17840-
const char *filename = JS_AtomToCString(ctx, b->filename);
17841-
if (unlikely(!filename)) {
17842-
/* OOM: a pending exception has been raised */
17843-
goto exception;
17844-
}
17845-
const char *funcname = JS_AtomToCString(ctx, b->func_name);
17846-
if (unlikely(!funcname)) {
17847-
JS_FreeCString(ctx, filename);
17848-
goto exception;
17849-
}
17850-
int ret = ctx->debug_trace(ctx, filename, funcname,
17836+
/* Pass the JSAtom values directly — no heap allocation.
17837+
The atoms are valid for the lifetime of the bytecode
17838+
object, which outlives the callback. The embedder must
17839+
call JS_DupAtom() if it needs to retain them. */
17840+
int ret = ctx->debug_trace(ctx, b->filename, b->func_name,
1785117841
line_num, col_num,
1785217842
ctx->debug_trace_opaque);
17853-
JS_FreeCString(ctx, filename);
17854-
JS_FreeCString(ctx, funcname);
1785517843

1785617844
if (ret != 0 || JS_HasException(ctx)) {
1785717845
/* If the callback indicated failure but did not raise

quickjs.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,19 @@ JS_EXTERN JSValue JS_GetFunctionProto(JSContext *ctx);
540540
is parsed (e.g. by JS_Eval / JS_Compile) AFTER JS_SetDebugTraceHandler
541541
has been called will be instrumented; previously compiled bytecode
542542
will not invoke the callback. In practice, install the handler before
543-
evaluating any application code. */
543+
evaluating any application code.
544+
545+
'filename' and 'funcname' are JSAtom values identifying the source file
546+
and enclosing function name. They are valid only for the duration of
547+
the callback; call JS_DupAtom() if you need to retain them. Either
548+
may be JS_ATOM_NULL (0) for anonymous functions or eval code. Use
549+
JS_AtomToCString() / JS_AtomToString() to convert to a C string or
550+
JSValue when needed. Accepting JSAtom avoids a heap allocation on
551+
every instrumented statement when the embedder only needs to compare
552+
against a known set of breakpoint locations. */
544553
typedef int JSDebugTraceFunc(JSContext *ctx,
545-
const char *filename,
546-
const char *funcname,
554+
JSAtom filename,
555+
JSAtom funcname,
547556
int line,
548557
int col,
549558
void *opaque);

0 commit comments

Comments
 (0)