@@ -256,7 +256,9 @@ static inline JS_BOOL JS_IsInteger(JSValueConst v) {
256
256
static void js_debugger_get_variable_type (JSContext * ctx ,
257
257
struct DebuggerSuspendedState * state ,
258
258
VariableType * variable_type ,
259
- JSValue var_val ) {
259
+ JSValue var_val ,
260
+ size_t depth ,
261
+ int8_t is_short ) {
260
262
// 0 means not expandible
261
263
uint32_t reference = 0 ;
262
264
if (JS_IsString (var_val )) {
@@ -295,12 +297,63 @@ static void js_debugger_get_variable_type(JSContext* ctx,
295
297
sprintf (buffer , "ƒ %s ()" , func_name );
296
298
variable_type -> value = copy_string (buffer , strlen (buffer ));
297
299
JS_FreeCString (ctx , func_name );
300
+ } else if (JS_IsArray (ctx , var_val )) {
301
+ variable_type -> type = "array" ;
302
+ char buf [12 ];
303
+ int64_t length ;
304
+ js_get_length64 (ctx , & length , var_val );
305
+ sprintf (buf , "Array(%lld)" , length );
306
+ variable_type -> value = copy_string (buf , strlen (buf ));
298
307
} else {
299
308
JSValue object_proto = JS_GetPrototype (ctx , var_val );
300
309
JSValue constructor_func = JS_GetPropertyStr (ctx , object_proto , "constructor" );
301
310
char buffer [64 ];
302
311
sprintf (buffer , "%s" , get_func_name (ctx , constructor_func ));
303
- variable_type -> value = copy_string (buffer , strlen (buffer ));
312
+
313
+ if (strcmp (buffer , "Object" ) == 0 ) {
314
+ if (is_short ) {
315
+ variable_type -> value = "{..}" ;
316
+ } else {
317
+ JSPropertyEnum * property_enum ;
318
+ uint32_t property_len ;
319
+ if (!JS_GetOwnPropertyNames (ctx , & property_enum , & property_len , var_val , JS_GPN_SYMBOL_MASK | JS_GPN_STRING_MASK )) {
320
+ size_t buf_len = 256 ;
321
+ char * buf = js_malloc (ctx , 256 );
322
+ buf [0 ] = '{' ;
323
+ size_t index = 1 ;
324
+ for (int i = 0 ; i < property_len ; i ++ ) {
325
+ JSValue v = JS_GetProperty (ctx , var_val , property_enum [i ].atom );
326
+ const char * key = atom_to_string (ctx , property_enum [i ].atom );
327
+ VariableType object_var_type ;
328
+ js_debugger_get_variable_type (ctx , state , & object_var_type , v , depth + 1 , 1 );
329
+ const char * tmp = object_var_type .value ;
330
+ size_t tmp_len = strlen (tmp );
331
+ if (index + tmp_len > buf_len ) {
332
+ buf_len = buf_len * 2 ;
333
+ js_realloc (ctx , buf , buf_len );
334
+ }
335
+ strcpy (buf + index , key );
336
+ index += strlen (key );
337
+ strcpy (buf + index , ": " );
338
+ index += 2 ;
339
+ strcpy (buf + index , tmp );
340
+ index += tmp_len ;
341
+
342
+ if (i + 1 < property_len ) {
343
+ strcpy (buf + index , ", " );
344
+ index += 2 ;
345
+ }
346
+ JS_FreeValue (ctx , v );
347
+ }
348
+ buf [index ] = '}' ;
349
+ buf [index + 1 ] = 0 ;
350
+ variable_type -> value = buf ;
351
+ }
352
+ }
353
+ } else {
354
+ variable_type -> value = copy_string (buffer , strlen (buffer ));
355
+ }
356
+
304
357
JS_FreeValue (ctx , object_proto );
305
358
JS_FreeValue (ctx , constructor_func );
306
359
}
@@ -366,7 +419,7 @@ static void process_request(JSDebuggerInfo* info, struct DebuggerSuspendedState*
366
419
const char * evaluate_result = JS_ToCStringLen (ctx , & len , result );
367
420
response -> body -> result = copy_string (evaluate_result , len );
368
421
VariableType variable_type ;
369
- js_debugger_get_variable_type (ctx , state , & variable_type , result );
422
+ js_debugger_get_variable_type (ctx , state , & variable_type , result , 0 , 0 );
370
423
response -> body -> type = variable_type .type ;
371
424
response -> body -> variablesReference = variable_type .variablesReference ;
372
425
@@ -489,7 +542,7 @@ static void process_request(JSDebuggerInfo* info, struct DebuggerSuspendedState*
489
542
for (uint32_t i = 0 ; i < count ; i ++ ) {
490
543
JSValue value = JS_GetPropertyUint32 (ctx , variable , start + i );
491
544
VariableType variable_type ;
492
- js_debugger_get_variable_type (ctx , state , & variable_type , value );
545
+ js_debugger_get_variable_type (ctx , state , & variable_type , value , 0 , 0 );
493
546
494
547
sprintf (name_buf , "%d" , i );
495
548
init_variable (& variables [i ]);
@@ -506,49 +559,50 @@ static void process_request(JSDebuggerInfo* info, struct DebuggerSuspendedState*
506
559
}
507
560
508
561
unfiltered :
509
- if (!JS_GetOwnPropertyNames (ctx , & tab_atom , & tab_atom_count , variable , JS_GPN_ENUM_ONLY | JS_GPN_STRING_MASK )) {
562
+ if (!JS_GetOwnPropertyNames (ctx , & tab_atom , & tab_atom_count , variable , JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK )) {
510
563
if (tab_atom_count == 0 ) {
511
564
variables = NULL ;
512
565
variableLen = 0 ;
513
566
goto done ;
514
567
}
515
568
516
- int offset = 0 ;
517
569
variables = js_malloc (ctx , sizeof (Variable ) * (tab_atom_count + (skip_proto ? 0 : 1 )));
518
570
571
+ for (int i = 0 ; i < tab_atom_count ; i ++ ) {
572
+ JSValue value = JS_GetProperty (ctx , variable , tab_atom [i ].atom );
573
+ VariableType variable_type ;
574
+ js_debugger_get_variable_type (ctx , state , & variable_type , value , 0 , 0 );
575
+ init_variable (& variables [i ]);
576
+ variables [i ].name = atom_to_string (ctx , tab_atom [i ].atom );
577
+ variables [i ].type = variable_type .type ;
578
+ variables [i ].variablesReference = variable_type .variablesReference ;
579
+ variables [i ].value = variable_type .value ;
580
+ assert (variables [i ].name != NULL );
581
+ assert (variables [i ].value != NULL );
582
+ JS_FreeValue (ctx , value );
583
+ }
584
+
519
585
if (!skip_proto ) {
520
586
const JSValue proto = JS_GetPrototype (ctx , variable );
521
587
if (!JS_IsException (proto )) {
522
588
VariableType variable_type ;
523
- js_debugger_get_variable_type (ctx , state , & variable_type , proto );
524
- int i = offset ++ ;
525
- init_variable (& variables [i ]);
526
- variables [i ].name = "[[Prototype]]" ;
527
- variables [i ].value = variable_type .type ;
528
- variables [i ].type = variable_type .value ;
529
- variables [i ].variablesReference = variable_type .variablesReference ;
530
- assert (variables [i ].name != NULL );
531
- assert (variables [i ].value != NULL );
589
+ js_debugger_get_variable_type (ctx , state , & variable_type , proto , 0 , 0 );
590
+ init_variable (& variables [tab_atom_count ]);
591
+ variables [tab_atom_count ].name = "[[Prototype]]" ;
592
+ variables [tab_atom_count ].value = variable_type .type ;
593
+ variables [tab_atom_count ].type = variable_type .value ;
594
+ variables [tab_atom_count ].variablesReference = variable_type .variablesReference ;
595
+ VariablePresentationHint * presentation_hint = js_malloc (ctx , sizeof (VariablePresentationHint ));
596
+ presentation_hint -> visibility = "internal" ;
597
+ presentation_hint -> lazy = 0 ;
598
+ variables [tab_atom_count ].presentationHint = presentation_hint ;
599
+ assert (variables [tab_atom_count ].name != NULL );
600
+ assert (variables [tab_atom_count ].value != NULL );
532
601
}
533
602
JS_FreeValue (ctx , proto );
534
603
}
535
604
536
- for (int i = 0 ; i < tab_atom_count ; i ++ ) {
537
- JSValue value = JS_GetProperty (ctx , variable , tab_atom [i ].atom );
538
- printf ("atom %s %p\n" , info -> runtime -> atom_array [tab_atom [i ].atom ]-> u .str8 , JS_VALUE_GET_PTR (value ));
539
- VariableType variable_type ;
540
- js_debugger_get_variable_type (ctx , state , & variable_type , value );
541
- init_variable (& variables [i + offset ]);
542
- variables [i + offset ].name = atom_to_string (ctx , tab_atom [i ].atom );
543
- variables [i + offset ].type = variable_type .type ;
544
- variables [i + offset ].variablesReference = variable_type .variablesReference ;
545
- variables [i + offset ].value = variable_type .value ;
546
- assert (variables [i + offset ].name != NULL );
547
- assert (variables [i + offset ].value != NULL );
548
- JS_FreeValue (ctx , value );
549
- }
550
-
551
- variableLen = offset + tab_atom_count ;
605
+ variableLen = tab_atom_count + (skip_proto ? 0 : 1 );
552
606
js_free_prop_enum (ctx , tab_atom , tab_atom_count );
553
607
}
554
608
@@ -614,7 +668,7 @@ static void js_process_debugger_messages(JSDebuggerInfo* info, const uint8_t* cu
614
668
Request * request = js_malloc (ctx , sizeof (Request ));
615
669
parse_request (ctx , request , item .buf , item .length );
616
670
process_request (info , & state , request );
617
- free_request (ctx , request );
671
+ // free_request(ctx, request);
618
672
619
673
js_free (ctx , item .buf );
620
674
} while (info -> is_paused );
0 commit comments