72
72
#define CONFIG_ATOMICS
73
73
#endif
74
74
75
+ // Debug trace system:
75
76
// uncomment one or more DUMP_XXX definition to produce debug output.
76
77
// 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
78
79
// stdout) if qjs is invoked with -d<bitmask> with the corresponding bit set.
79
80
80
81
//#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,
1028
1029
static JSValue JS_EvalObject(JSContext *ctx, JSValue this_obj,
1029
1030
JSValue val, int flags, int scope_idx);
1030
1031
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);
1033
1035
static __maybe_unused void JS_DumpObjectHeader(JSRuntime *rt);
1034
1036
static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p);
1035
1037
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);
1038
1044
static __maybe_unused void JS_DumpShapes(JSRuntime *rt);
1039
-
1040
1045
static JSValue js_function_apply(JSContext *ctx, JSValue this_val,
1041
1046
int argc, JSValue *argv, int magic);
1042
1047
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,
3025
3030
{
3026
3031
if (__JS_AtomIsTaggedInt(atom)) {
3027
3032
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);
3033
3033
} 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>" );
3038
3038
} else {
3039
3039
int i, c;
3040
3040
char *q;
3041
3041
JSString *str;
3042
3042
3043
3043
q = buf;
3044
+ p = rt->atom_array[atom];
3045
+ assert(!atom_is_free(p));
3044
3046
str = p;
3045
3047
if (str) {
3046
3048
if (!str->is_wide_char) {
@@ -5479,16 +5481,12 @@ void __JS_FreeValueRT(JSRuntime *rt, JSValue v)
5479
5481
5480
5482
#ifdef DUMP_FREE
5481
5483
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");
5492
5490
}
5493
5491
}
5494
5492
#endif
@@ -11585,7 +11583,7 @@ static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p)
11585
11583
switch (p->class_id) {
11586
11584
case JS_CLASS_ARRAY:
11587
11585
case JS_CLASS_ARGUMENTS:
11588
- JS_DumpValue (rt, p->u.array.u.values[i]);
11586
+ JS_DumpValueShort (rt, p->u.array.u.values[i]);
11589
11587
break;
11590
11588
case JS_CLASS_UINT8C_ARRAY:
11591
11589
case JS_CLASS_INT8_ARRAY:
@@ -11630,7 +11628,7 @@ static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p)
11630
11628
js_autoinit_get_id(pr),
11631
11629
(void *)pr->u.init.opaque);
11632
11630
} else {
11633
- JS_DumpValue (rt, pr->u.value);
11631
+ JS_DumpValueShort (rt, pr->u.value);
11634
11632
}
11635
11633
is_first = FALSE;
11636
11634
}
@@ -11646,11 +11644,11 @@ static __maybe_unused void JS_DumpObject(JSRuntime *rt, JSObject *p)
11646
11644
printf(" Closure:");
11647
11645
for(i = 0; i < b->closure_var_count; i++) {
11648
11646
printf(" ");
11649
- JS_DumpValue (rt, var_refs[i]->value);
11647
+ JS_DumpValueShort (rt, var_refs[i]->value);
11650
11648
}
11651
11649
if (p->u.func.home_object) {
11652
11650
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));
11654
11652
}
11655
11653
}
11656
11654
}
@@ -11689,7 +11687,8 @@ static __maybe_unused void JS_DumpGCObject(JSRuntime *rt, JSGCObjectHeader *p)
11689
11687
}
11690
11688
}
11691
11689
11692
- static __maybe_unused void JS_DumpValue(JSRuntime *rt, JSValue val)
11690
+ static __maybe_unused void JS_DumpValueShort(JSRuntime *rt,
11691
+ JSValue val)
11693
11692
{
11694
11693
uint32_t tag = JS_VALUE_GET_NORM_TAG(val);
11695
11694
const char *str;
@@ -11742,11 +11741,7 @@ static __maybe_unused void JS_DumpValue(JSRuntime *rt, JSValue val)
11742
11741
{
11743
11742
JSFunctionBytecode *b = JS_VALUE_GET_PTR(val);
11744
11743
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));
11750
11745
}
11751
11746
break;
11752
11747
case JS_TAG_OBJECT:
@@ -11775,6 +11770,21 @@ static __maybe_unused void JS_DumpValue(JSRuntime *rt, JSValue val)
11775
11770
}
11776
11771
}
11777
11772
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
+
11778
11788
/* return -1 if exception (proxy case) or TRUE/FALSE */
11779
11789
int JS_IsArray(JSContext *ctx, JSValue val)
11780
11790
{
@@ -14519,7 +14529,8 @@ typedef enum {
14519
14529
defined(DUMP_BYTECODE_PASS2) || \
14520
14530
defined(DUMP_BYTECODE_PASS1) || \
14521
14531
defined(DUMP_BYTECODE_STACK) || \
14522
- defined(DUMP_BYTECODE_STEP)
14532
+ defined(DUMP_BYTECODE_STEP) || \
14533
+ defined(DUMP_READ_OBJECT)
14523
14534
#define DUMP_BYTECODE
14524
14535
#endif
14525
14536
@@ -27506,7 +27517,7 @@ static void js_free_function_def(JSContext *ctx, JSFunctionDef *fd)
27506
27517
27507
27518
#ifdef DUMP_BYTECODE
27508
27519
static const char *skip_lines(const char *p, int n) {
27509
- while (p && n-- > 0 && *p) {
27520
+ while (n-- > 0 && *p) {
27510
27521
while (*p && *p++ != '\n')
27511
27522
continue;
27512
27523
}
@@ -27516,7 +27527,7 @@ static const char *skip_lines(const char *p, int n) {
27516
27527
static void print_lines(const char *source, int line, int line1) {
27517
27528
const char *s = source;
27518
27529
const char *p = skip_lines(s, line);
27519
- if (p && *p) {
27530
+ if (*p) {
27520
27531
while (line++ < line1) {
27521
27532
p = skip_lines(s = p, 1);
27522
27533
printf(";; %.*s", (int)(p - s), s);
@@ -27644,7 +27655,7 @@ static void dump_byte_code(JSContext *ctx, int pass,
27644
27655
} else {
27645
27656
printf(" ");
27646
27657
}
27647
- printf("%-15s ", oi->name); /* align opcode arguments */
27658
+ printf("%s ", oi->name);
27648
27659
pos++;
27649
27660
switch(oi->fmt) {
27650
27661
case OP_FMT_none_int:
@@ -27712,9 +27723,9 @@ static void dump_byte_code(JSContext *ctx, int pass,
27712
27723
idx = get_u32(tab + pos);
27713
27724
goto has_pool_idx;
27714
27725
has_pool_idx:
27715
- printf(" %-4u ; ", idx);
27726
+ printf(" %u: ", idx);
27716
27727
if (idx < cpool_count) {
27717
- JS_DumpValue(ctx->rt , cpool[idx]);
27728
+ JS_DumpValue(ctx, cpool[idx]);
27718
27729
}
27719
27730
break;
27720
27731
case OP_FMT_atom:
@@ -27756,7 +27767,7 @@ static void dump_byte_code(JSContext *ctx, int pass,
27756
27767
case OP_FMT_loc:
27757
27768
idx = get_u16(tab + pos);
27758
27769
has_loc:
27759
- printf(" %-4d ; ", idx);
27770
+ printf(" %d: ", idx);
27760
27771
if (idx < var_count) {
27761
27772
print_atom(ctx, vars[idx].var_name);
27762
27773
}
@@ -27767,7 +27778,7 @@ static void dump_byte_code(JSContext *ctx, int pass,
27767
27778
case OP_FMT_arg:
27768
27779
idx = get_u16(tab + pos);
27769
27780
has_arg:
27770
- printf(" %-4d ; ", idx);
27781
+ printf(" %d: ", idx);
27771
27782
if (idx < arg_count) {
27772
27783
print_atom(ctx, args[idx].var_name);
27773
27784
}
@@ -27778,7 +27789,7 @@ static void dump_byte_code(JSContext *ctx, int pass,
27778
27789
case OP_FMT_var_ref:
27779
27790
idx = get_u16(tab + pos);
27780
27791
has_var_ref:
27781
- printf(" %-4d ; ", idx);
27792
+ printf(" %d: ", idx);
27782
27793
if (idx < closure_var_count) {
27783
27794
print_atom(ctx, closure_var[idx].var_name);
27784
27795
}
@@ -31370,9 +31381,8 @@ static JSValue js_create_function(JSContext *ctx, JSFunctionDef *fd)
31370
31381
add_gc_object(ctx->rt, &b->header, JS_GC_OBJ_TYPE_FUNCTION_BYTECODE);
31371
31382
31372
31383
#ifdef DUMP_BYTECODE_FINAL
31373
- if (check_dump_flag(ctx->rt, DUMP_BYTECODE_FINAL)) {
31384
+ if (check_dump_flag(ctx->rt, DUMP_BYTECODE_FINAL))
31374
31385
js_dump_function_bytecode(ctx, b);
31375
- }
31376
31386
#endif
31377
31387
31378
31388
if (fd->parent) {
@@ -33466,9 +33476,10 @@ typedef struct BCReaderState {
33466
33476
int objects_count;
33467
33477
int objects_size;
33468
33478
33469
- /* used for DUMP_READ_OBJECT */
33479
+ #ifdef DUMP_READ_OBJECT
33470
33480
const uint8_t *ptr_last;
33471
33481
int level;
33482
+ #endif
33472
33483
} BCReaderState;
33473
33484
33474
33485
#ifdef DUMP_READ_OBJECT
@@ -33736,7 +33747,9 @@ static int JS_ReadFunctionBytecode(BCReaderState *s, JSFunctionBytecode *b,
33736
33747
put_u32(bc_buf + pos + 1, atom);
33737
33748
#ifdef DUMP_READ_OBJECT
33738
33749
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");
33740
33753
}
33741
33754
#endif
33742
33755
break;
@@ -33931,7 +33944,9 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
33931
33944
#ifdef DUMP_READ_OBJECT
33932
33945
if (check_dump_flag(s->ctx->rt, DUMP_READ_OBJECT)) {
33933
33946
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");
33935
33950
}
33936
33951
}
33937
33952
#endif
@@ -33943,7 +33958,6 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
33943
33958
33944
33959
if (local_count != 0) {
33945
33960
bc_read_trace(s, "vars {\n");
33946
- bc_read_trace(s, "off flags scope name\n");
33947
33961
for(i = 0; i < local_count; i++) {
33948
33962
JSVarDef *vd = &b->vardefs[i];
33949
33963
if (bc_get_atom(s, &vd->var_name))
@@ -33962,12 +33976,7 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
33962
33976
vd->is_captured = bc_get_flags(v8, &idx, 1);
33963
33977
#ifdef DUMP_READ_OBJECT
33964
33978
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: ");
33971
33980
print_atom(s->ctx, vd->var_name);
33972
33981
printf("\n");
33973
33982
}
@@ -33977,7 +33986,6 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
33977
33986
}
33978
33987
if (b->closure_var_count != 0) {
33979
33988
bc_read_trace(s, "closure vars {\n");
33980
- bc_read_trace(s, "off flags idx name\n");
33981
33989
for(i = 0; i < b->closure_var_count; i++) {
33982
33990
JSClosureVar *cv = &b->closure_var[i];
33983
33991
int var_idx;
@@ -33996,13 +34004,7 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
33996
34004
cv->var_kind = bc_get_flags(v8, &idx, 4);
33997
34005
#ifdef DUMP_READ_OBJECT
33998
34006
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: ");
34006
34008
print_atom(s->ctx, cv->var_name);
34007
34009
printf("\n");
34008
34010
}
@@ -34045,7 +34047,6 @@ static JSValue JS_ReadFunctionTag(BCReaderState *s)
34045
34047
goto fail;
34046
34048
if (b->source_len) {
34047
34049
bc_read_trace(s, "source: %d bytes\n", b->source_len);
34048
- s->ptr_last += b->source_len; // omit source code hex dump
34049
34050
b->source = js_mallocz(ctx, b->source_len);
34050
34051
if (!b->source)
34051
34052
goto fail;
@@ -34084,7 +34085,9 @@ static JSValue JS_ReadModule(BCReaderState *s)
34084
34085
goto fail;
34085
34086
#ifdef DUMP_READ_OBJECT
34086
34087
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");
34088
34091
}
34089
34092
#endif
34090
34093
m = js_new_module_def(ctx, module_name);
@@ -34193,7 +34196,9 @@ static JSValue JS_ReadObjectTag(BCReaderState *s)
34193
34196
goto fail;
34194
34197
#ifdef DUMP_READ_OBJECT
34195
34198
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");
34197
34202
}
34198
34203
#endif
34199
34204
val = JS_ReadObjectRec(s);
@@ -45789,7 +45794,7 @@ static JSValue js_promise_resolve_function_call(JSContext *ctx,
45789
45794
#ifdef DUMP_PROMISE
45790
45795
if (check_dump_flag(ctx->rt, DUMP_PROMISE)) {
45791
45796
printf("js_promise_resolving_function_call: is_reject=%d resolution=", is_reject);
45792
- JS_DumpValue(ctx->rt , resolution);
45797
+ JS_DumpValue(ctx, resolution);
45793
45798
printf("\n");
45794
45799
}
45795
45800
#endif
0 commit comments