Skip to content

Commit fc3b2bd

Browse files
committed
Revert some changes for separate PRs:
- merge `JS_DumpValue(ctx, val)` and `JS_DumpValueShort(rt, val)` as `JS_DumpValue(rt, val)` - remove unused `JS_PrintValue(ctx, val)` - fix DUMP_MEM: avoid crashing on invalid atoms in `JS_AtomGetStrRT` - fix crash in `print_lines` on null source
1 parent 5eee96a commit fc3b2bd

File tree

1 file changed

+73
-68
lines changed

1 file changed

+73
-68
lines changed

quickjs.c

+73-68
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@
7272
#define CONFIG_ATOMICS
7373
#endif
7474

75+
// Debug trace system:
7576
// uncomment one or more DUMP_XXX definition to produce debug output.
7677
// define the DUMP_XXX symbol as empty or 0 for unconditional output
77-
// otherwhise the debug outout will be produced to the dump stream (currently
78+
// otherwhise the debug output will be produced to the dump stream (currently
7879
// stdout) if qjs is invoked with -d<bitmask> with the corresponding bit set.
7980

8081
//#define DUMP_BYTECODE_FINAL 0x01 /* dump pass 3 final byte code */
@@ -1028,15 +1029,19 @@ static __exception int JS_ToArrayLengthFree(JSContext *ctx, uint32_t *plen,
10281029
static JSValue JS_EvalObject(JSContext *ctx, JSValue this_obj,
10291030
JSValue val, int flags, int scope_idx);
10301031
JSValue __attribute__((format(printf, 2, 3))) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...);
1031-
1032-
static __maybe_unused void JS_DumpString(JSRuntime *rt, const JSString *p);
1032+
static __maybe_unused void JS_DumpAtoms(JSRuntime *rt);
1033+
static __maybe_unused void JS_DumpString(JSRuntime *rt,
1034+
const JSString *p);
10331035
static __maybe_unused void JS_DumpObjectHeader(JSRuntime *rt);
10341036
static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p);
10351037
static __maybe_unused void JS_DumpGCObject(JSRuntime *rt, JSGCObjectHeader *p);
1036-
static __maybe_unused void JS_DumpValue(JSRuntime *rt, JSValue val);
1037-
static __maybe_unused void JS_DumpAtoms(JSRuntime *rt);
1038+
static __maybe_unused void JS_DumpValueShort(JSRuntime *rt,
1039+
JSValue val);
1040+
static __maybe_unused void JS_DumpValue(JSContext *ctx, JSValue val);
1041+
static __maybe_unused void JS_PrintValue(JSContext *ctx,
1042+
const char *str,
1043+
JSValue val);
10381044
static __maybe_unused void JS_DumpShapes(JSRuntime *rt);
1039-
10401045
static JSValue js_function_apply(JSContext *ctx, JSValue this_val,
10411046
int argc, JSValue *argv, int magic);
10421047
static void js_array_finalizer(JSRuntime *rt, JSValue val);
@@ -3025,22 +3030,19 @@ static const char *JS_AtomGetStrRT(JSRuntime *rt, char *buf, int buf_size,
30253030
{
30263031
if (__JS_AtomIsTaggedInt(atom)) {
30273032
snprintf(buf, buf_size, "%u", __JS_AtomToUInt32(atom));
3028-
} else if (atom == JS_ATOM_NULL) {
3029-
snprintf(buf, buf_size, "<null>");
3030-
} else if (atom >= rt->atom_size) {
3031-
assert(atom < rt->atom_size);
3032-
snprintf(buf, buf_size, "<invalid %x>", atom);
30333033
} else {
3034-
JSAtomStruct *p = rt->atom_array[atom];
3035-
if (atom_is_free(p)) {
3036-
assert(!atom_is_free(p));
3037-
snprintf(buf, buf_size, "<free %x>", atom);
3034+
JSAtomStruct *p;
3035+
assert(atom < rt->atom_size);
3036+
if (atom == JS_ATOM_NULL) {
3037+
snprintf(buf, buf_size, "<null>");
30383038
} else {
30393039
int i, c;
30403040
char *q;
30413041
JSString *str;
30423042

30433043
q = buf;
3044+
p = rt->atom_array[atom];
3045+
assert(!atom_is_free(p));
30443046
str = p;
30453047
if (str) {
30463048
if (!str->is_wide_char) {
@@ -5479,16 +5481,12 @@ void __JS_FreeValueRT(JSRuntime *rt, JSValue v)
54795481

54805482
#ifdef DUMP_FREE
54815483
if (check_dump_flag(rt, DUMP_FREE)) {
5482-
/* Prevent invalid object access during GC */
5483-
if ((rt->gc_phase != JS_GC_PHASE_REMOVE_CYCLES)
5484-
|| (tag != JS_TAG_OBJECT && tag != JS_TAG_FUNCTION_BYTECODE)) {
5485-
printf("Freeing ");
5486-
if (tag == JS_TAG_OBJECT) {
5487-
JS_DumpObject(rt, JS_VALUE_GET_OBJ(v));
5488-
} else {
5489-
JS_DumpValue(rt, v);
5490-
printf("\n");
5491-
}
5484+
printf("Freeing ");
5485+
if (tag == JS_TAG_OBJECT) {
5486+
JS_DumpObject(rt, JS_VALUE_GET_OBJ(v));
5487+
} else {
5488+
JS_DumpValueShort(rt, v);
5489+
printf("\n");
54925490
}
54935491
}
54945492
#endif
@@ -11585,7 +11583,7 @@ static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p)
1158511583
switch (p->class_id) {
1158611584
case JS_CLASS_ARRAY:
1158711585
case JS_CLASS_ARGUMENTS:
11588-
JS_DumpValue(rt, p->u.array.u.values[i]);
11586+
JS_DumpValueShort(rt, p->u.array.u.values[i]);
1158911587
break;
1159011588
case JS_CLASS_UINT8C_ARRAY:
1159111589
case JS_CLASS_INT8_ARRAY:
@@ -11630,7 +11628,7 @@ static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p)
1163011628
js_autoinit_get_id(pr),
1163111629
(void *)pr->u.init.opaque);
1163211630
} else {
11633-
JS_DumpValue(rt, pr->u.value);
11631+
JS_DumpValueShort(rt, pr->u.value);
1163411632
}
1163511633
is_first = FALSE;
1163611634
}
@@ -11646,11 +11644,11 @@ static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p)
1164611644
printf(" Closure:");
1164711645
for(i = 0; i < b->closure_var_count; i++) {
1164811646
printf(" ");
11649-
JS_DumpValue(rt, var_refs[i]->value);
11647+
JS_DumpValueShort(rt, var_refs[i]->value);
1165011648
}
1165111649
if (p->u.func.home_object) {
1165211650
printf(" HomeObject: ");
11653-
JS_DumpValue(rt, JS_MKPTR(JS_TAG_OBJECT, p->u.func.home_object));
11651+
JS_DumpValueShort(rt, JS_MKPTR(JS_TAG_OBJECT, p->u.func.home_object));
1165411652
}
1165511653
}
1165611654
}
@@ -11689,7 +11687,8 @@ static __maybe_unused void JS_DumpGCObject(JSRuntime *rt, JSGCObjectHeader *p)
1168911687
}
1169011688
}
1169111689

11692-
static __maybe_unused void JS_DumpValue(JSRuntime *rt, JSValue val)
11690+
static __maybe_unused void JS_DumpValueShort(JSRuntime *rt,
11691+
JSValue val)
1169311692
{
1169411693
uint32_t tag = JS_VALUE_GET_NORM_TAG(val);
1169511694
const char *str;
@@ -11742,11 +11741,7 @@ static __maybe_unused void JS_DumpValue(JSRuntime *rt, JSValue val)
1174211741
{
1174311742
JSFunctionBytecode *b = JS_VALUE_GET_PTR(val);
1174411743
char buf[ATOM_GET_STR_BUF_SIZE];
11745-
if (b->func_name) {
11746-
printf("[bytecode %s]", JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name));
11747-
} else {
11748-
printf("[bytecode (anonymous)]");
11749-
}
11744+
printf("[bytecode %s]", JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name));
1175011745
}
1175111746
break;
1175211747
case JS_TAG_OBJECT:
@@ -11775,6 +11770,21 @@ static __maybe_unused void JS_DumpValue(JSRuntime *rt, JSValue val)
1177511770
}
1177611771
}
1177711772

11773+
static __maybe_unused void JS_DumpValue(JSContext *ctx,
11774+
JSValue val)
11775+
{
11776+
JS_DumpValueShort(ctx->rt, val);
11777+
}
11778+
11779+
static __maybe_unused void JS_PrintValue(JSContext *ctx,
11780+
const char *str,
11781+
JSValue val)
11782+
{
11783+
printf("%s=", str);
11784+
JS_DumpValueShort(ctx->rt, val);
11785+
printf("\n");
11786+
}
11787+
1177811788
/* return -1 if exception (proxy case) or TRUE/FALSE */
1177911789
int JS_IsArray(JSContext *ctx, JSValue val)
1178011790
{
@@ -14519,7 +14529,8 @@ typedef enum {
1451914529
defined(DUMP_BYTECODE_PASS2) || \
1452014530
defined(DUMP_BYTECODE_PASS1) || \
1452114531
defined(DUMP_BYTECODE_STACK) || \
14522-
defined(DUMP_BYTECODE_STEP)
14532+
defined(DUMP_BYTECODE_STEP) || \
14533+
defined(DUMP_READ_OBJECT)
1452314534
#define DUMP_BYTECODE
1452414535
#endif
1452514536

@@ -27506,7 +27517,7 @@ static void js_free_function_def(JSContext *ctx, JSFunctionDef *fd)
2750627517

2750727518
#ifdef DUMP_BYTECODE
2750827519
static const char *skip_lines(const char *p, int n) {
27509-
while (p && n-- > 0 && *p) {
27520+
while (n-- > 0 && *p) {
2751027521
while (*p && *p++ != '\n')
2751127522
continue;
2751227523
}
@@ -27516,7 +27527,7 @@ static const char *skip_lines(const char *p, int n) {
2751627527
static void print_lines(const char *source, int line, int line1) {
2751727528
const char *s = source;
2751827529
const char *p = skip_lines(s, line);
27519-
if (p && *p) {
27530+
if (*p) {
2752027531
while (line++ < line1) {
2752127532
p = skip_lines(s = p, 1);
2752227533
printf(";; %.*s", (int)(p - s), s);
@@ -27644,7 +27655,7 @@ static void dump_byte_code(JSContext *ctx, int pass,
2764427655
} else {
2764527656
printf(" ");
2764627657
}
27647-
printf("%-15s", oi->name); /* align opcode arguments */
27658+
printf("%s", oi->name);
2764827659
pos++;
2764927660
switch(oi->fmt) {
2765027661
case OP_FMT_none_int:
@@ -27712,9 +27723,9 @@ static void dump_byte_code(JSContext *ctx, int pass,
2771227723
idx = get_u32(tab + pos);
2771327724
goto has_pool_idx;
2771427725
has_pool_idx:
27715-
printf(" %-4u ; ", idx);
27726+
printf(" %u: ", idx);
2771627727
if (idx < cpool_count) {
27717-
JS_DumpValue(ctx->rt, cpool[idx]);
27728+
JS_DumpValue(ctx, cpool[idx]);
2771827729
}
2771927730
break;
2772027731
case OP_FMT_atom:
@@ -27756,7 +27767,7 @@ static void dump_byte_code(JSContext *ctx, int pass,
2775627767
case OP_FMT_loc:
2775727768
idx = get_u16(tab + pos);
2775827769
has_loc:
27759-
printf(" %-4d ; ", idx);
27770+
printf(" %d: ", idx);
2776027771
if (idx < var_count) {
2776127772
print_atom(ctx, vars[idx].var_name);
2776227773
}
@@ -27767,7 +27778,7 @@ static void dump_byte_code(JSContext *ctx, int pass,
2776727778
case OP_FMT_arg:
2776827779
idx = get_u16(tab + pos);
2776927780
has_arg:
27770-
printf(" %-4d ; ", idx);
27781+
printf(" %d: ", idx);
2777127782
if (idx < arg_count) {
2777227783
print_atom(ctx, args[idx].var_name);
2777327784
}
@@ -27778,7 +27789,7 @@ static void dump_byte_code(JSContext *ctx, int pass,
2777827789
case OP_FMT_var_ref:
2777927790
idx = get_u16(tab + pos);
2778027791
has_var_ref:
27781-
printf(" %-4d ; ", idx);
27792+
printf(" %d: ", idx);
2778227793
if (idx < closure_var_count) {
2778327794
print_atom(ctx, closure_var[idx].var_name);
2778427795
}
@@ -31370,9 +31381,8 @@ static JSValue js_create_function(JSContext *ctx, JSFunctionDef *fd)
3137031381
add_gc_object(ctx->rt, &b->header, JS_GC_OBJ_TYPE_FUNCTION_BYTECODE);
3137131382

3137231383
#ifdef DUMP_BYTECODE_FINAL
31373-
if (check_dump_flag(ctx->rt, DUMP_BYTECODE_FINAL)) {
31384+
if (check_dump_flag(ctx->rt, DUMP_BYTECODE_FINAL))
3137431385
js_dump_function_bytecode(ctx, b);
31375-
}
3137631386
#endif
3137731387

3137831388
if (fd->parent) {
@@ -33466,9 +33476,10 @@ typedef struct BCReaderState {
3346633476
int objects_count;
3346733477
int objects_size;
3346833478

33469-
/* used for DUMP_READ_OBJECT */
33479+
#ifdef DUMP_READ_OBJECT
3347033480
const uint8_t *ptr_last;
3347133481
int level;
33482+
#endif
3347233483
} BCReaderState;
3347333484

3347433485
#ifdef DUMP_READ_OBJECT
@@ -33736,7 +33747,9 @@ static int JS_ReadFunctionBytecode(BCReaderState *s, JSFunctionBytecode *b,
3373633747
put_u32(bc_buf + pos + 1, atom);
3373733748
#ifdef DUMP_READ_OBJECT
3373833749
if (check_dump_flag(s->ctx->rt, DUMP_READ_OBJECT)) {
33739-
bc_read_trace(s, "at %d, fixup atom: ", pos + 1); print_atom(s->ctx, atom); printf("\n");
33750+
bc_read_trace(s, "at %d, fixup atom: ", pos + 1);
33751+
print_atom(s->ctx, atom);
33752+
printf("\n");
3374033753
}
3374133754
#endif
3374233755
break;
@@ -33931,7 +33944,9 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
3393133944
#ifdef DUMP_READ_OBJECT
3393233945
if (check_dump_flag(s->ctx->rt, DUMP_READ_OBJECT)) {
3393333946
if (b->func_name) {
33934-
bc_read_trace(s, "name: "); print_atom(s->ctx, b->func_name); printf("\n");
33947+
bc_read_trace(s, "name: ");
33948+
print_atom(s->ctx, b->func_name);
33949+
printf("\n");
3393533950
}
3393633951
}
3393733952
#endif
@@ -33943,7 +33958,6 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
3394333958

3394433959
if (local_count != 0) {
3394533960
bc_read_trace(s, "vars {\n");
33946-
bc_read_trace(s, "off flags scope name\n");
3394733961
for(i = 0; i < local_count; i++) {
3394833962
JSVarDef *vd = &b->vardefs[i];
3394933963
if (bc_get_atom(s, &vd->var_name))
@@ -33962,12 +33976,7 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
3396233976
vd->is_captured = bc_get_flags(v8, &idx, 1);
3396333977
#ifdef DUMP_READ_OBJECT
3396433978
if (check_dump_flag(s->ctx->rt, DUMP_READ_OBJECT)) {
33965-
bc_read_trace(s, "%3d %d%c%c%c %4d ",
33966-
i, vd->var_kind,
33967-
vd->is_const ? 'C' : '.',
33968-
vd->is_lexical ? 'L' : '.',
33969-
vd->is_captured ? 'X' : '.',
33970-
vd->scope_level);
33979+
bc_read_trace(s, "name: ");
3397133980
print_atom(s->ctx, vd->var_name);
3397233981
printf("\n");
3397333982
}
@@ -33977,7 +33986,6 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
3397733986
}
3397833987
if (b->closure_var_count != 0) {
3397933988
bc_read_trace(s, "closure vars {\n");
33980-
bc_read_trace(s, "off flags idx name\n");
3398133989
for(i = 0; i < b->closure_var_count; i++) {
3398233990
JSClosureVar *cv = &b->closure_var[i];
3398333991
int var_idx;
@@ -33996,13 +34004,7 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
3399634004
cv->var_kind = bc_get_flags(v8, &idx, 4);
3399734005
#ifdef DUMP_READ_OBJECT
3399834006
if (check_dump_flag(s->ctx->rt, DUMP_READ_OBJECT)) {
33999-
bc_read_trace(s, "%3d %d%c%c%c%c %3d ",
34000-
i, cv->var_kind,
34001-
cv->is_local ? 'L' : '.',
34002-
cv->is_arg ? 'A' : '.',
34003-
cv->is_const ? 'C' : '.',
34004-
cv->is_lexical ? 'X' : '.',
34005-
cv->var_idx);
34007+
bc_read_trace(s, "name: ");
3400634008
print_atom(s->ctx, cv->var_name);
3400734009
printf("\n");
3400834010
}
@@ -34045,7 +34047,6 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
3404534047
goto fail;
3404634048
if (b->source_len) {
3404734049
bc_read_trace(s, "source: %d bytes\n", b->source_len);
34048-
s->ptr_last += b->source_len; // omit source code hex dump
3404934050
b->source = js_mallocz(ctx, b->source_len);
3405034051
if (!b->source)
3405134052
goto fail;
@@ -34084,7 +34085,9 @@ static JSValue JS_ReadModule(BCReaderState *s)
3408434085
goto fail;
3408534086
#ifdef DUMP_READ_OBJECT
3408634087
if (check_dump_flag(s->ctx->rt, DUMP_READ_OBJECT)) {
34087-
bc_read_trace(s, "name: "); print_atom(s->ctx, module_name); printf("\n");
34088+
bc_read_trace(s, "name: ");
34089+
print_atom(s->ctx, module_name);
34090+
printf("\n");
3408834091
}
3408934092
#endif
3409034093
m = js_new_module_def(ctx, module_name);
@@ -34193,7 +34196,9 @@ static JSValue JS_ReadObjectTag(BCReaderState *s)
3419334196
goto fail;
3419434197
#ifdef DUMP_READ_OBJECT
3419534198
if (check_dump_flag(s->ctx->rt, DUMP_READ_OBJECT)) {
34196-
bc_read_trace(s, "propname: "); print_atom(s->ctx, atom); printf("\n");
34199+
bc_read_trace(s, "propname: ");
34200+
print_atom(s->ctx, atom);
34201+
printf("\n");
3419734202
}
3419834203
#endif
3419934204
val = JS_ReadObjectRec(s);
@@ -45789,7 +45794,7 @@ static JSValue js_promise_resolve_function_call(JSContext *ctx,
4578945794
#ifdef DUMP_PROMISE
4579045795
if (check_dump_flag(ctx->rt, DUMP_PROMISE)) {
4579145796
printf("js_promise_resolving_function_call: is_reject=%d resolution=", is_reject);
45792-
JS_DumpValue(ctx->rt, resolution);
45797+
JS_DumpValue(ctx, resolution);
4579345798
printf("\n");
4579445799
}
4579545800
#endif

0 commit comments

Comments
 (0)