Skip to content

Commit bb1b136

Browse files
committed
feat: optimize variable kind and presentation.
1 parent f35cee4 commit bb1b136

File tree

2 files changed

+106
-43
lines changed

2 files changed

+106
-43
lines changed

bridge/scripts/code_generator/src/ts_types/dap/generateSource.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,20 @@ function generateMemberInit(prop: PropsDeclaration, externalInitialize: string[]
214214
return initCode;
215215
}
216216

217-
function generateMemberStringifyCode(prop: PropsDeclaration, bodyName: string, externalInitialize: string[], info: DAPInfoCollector): string {
218-
function wrapIf(code: string) {
219-
if (prop.type.value === FunctionArgumentType.dom_string || typeof prop.type.value === 'string') {
220-
return `if (${bodyName}->${prop.name} != NULL) {
217+
function wrapIf(code: string, expression: string, type: ParameterType) {
218+
if (type.value === FunctionArgumentType.dom_string || typeof type.value === 'string') {
219+
return `if (${expression} != NULL) {
221220
${code}
222221
}`;
223-
} else if (prop.type.value === FunctionArgumentType.double || prop.type.value === FunctionArgumentType.int64) {
224-
return `if (!isnan(${bodyName}->${prop.name})) {
222+
} else if (type.value === FunctionArgumentType.double || type.value === FunctionArgumentType.int64) {
223+
return `if (!isnan(${expression})) {
225224
${code}
226225
}`
227-
}
228-
return code;
229226
}
227+
return code;
228+
}
229+
230+
function generateMemberStringifyCode(prop: PropsDeclaration, bodyName: string, externalInitialize: string[], info: DAPInfoCollector): string {
230231

231232
function generateQuickJSInitFromType(type: ParameterType) {
232233
if (type.value === FunctionArgumentType.double) {
@@ -260,9 +261,17 @@ function generateMemberStringifyCode(prop: PropsDeclaration, bodyName: string, e
260261
} else {
261262
if (type.isArray) {
262263
let isReference = typeof (prop.type.value as ParameterType).value === 'string';
264+
let arrCallCode = `JS_SetPropertyUint32(ctx, arr, i, ${generateQuickJSInitFromType(prop.type.value as ParameterType)}(ctx, ${isReference ? '&' : ''}${bodyName}->${prop.name}[i]));`;
265+
const typeKind = getTypeKind(type);
266+
if (prop.optional && typeKind === PropTypeKind.normalArray) {
267+
arrCallCode = `if (${bodyName}->${prop.name} != NULL) {
268+
${arrCallCode}
269+
}`;
270+
}
271+
263272
callCode = `JSValue arr = JS_NewArray(ctx);
264-
for(int i = 0; i < ${bodyName}->${prop.name}Len; i ++) {
265-
JS_SetPropertyUint32(ctx, arr, i, ${generateQuickJSInitFromType(prop.type.value as ParameterType)}(ctx, ${isReference ? '&' : ''}${bodyName}->${prop.name}[i]));
273+
for(int i = 0; i < ${bodyName}->${prop.name}Len; i ++) {
274+
${arrCallCode}
266275
}
267276
JS_SetPropertyStr(ctx, object, "${prop.name}", arr);`
268277
} else {
@@ -278,7 +287,7 @@ JS_SetPropertyStr(ctx, object, "${prop.name}", arr);`
278287

279288
let callCode = genCallCode(prop.type, prop);
280289

281-
return addIndent(prop.optional ? wrapIf(callCode) : callCode, 2);
290+
return addIndent(prop.optional ? wrapIf(callCode, `${bodyName}->${prop.name}`, prop.type) : callCode, 2);
282291
}
283292

284293
function generateRequestParser(info: DAPInfoCollector, requests: string[], externalInitialize: string[]) {

bridge/third_party/quickjs/src/core/debugger.c

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ static inline JS_BOOL JS_IsInteger(JSValueConst v) {
256256
static void js_debugger_get_variable_type(JSContext* ctx,
257257
struct DebuggerSuspendedState* state,
258258
VariableType* variable_type,
259-
JSValue var_val) {
259+
JSValue var_val,
260+
size_t depth,
261+
int8_t is_short) {
260262
// 0 means not expandible
261263
uint32_t reference = 0;
262264
if (JS_IsString(var_val)) {
@@ -295,12 +297,63 @@ static void js_debugger_get_variable_type(JSContext* ctx,
295297
sprintf(buffer, "ƒ %s ()", func_name);
296298
variable_type->value = copy_string(buffer, strlen(buffer));
297299
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));
298307
} else {
299308
JSValue object_proto = JS_GetPrototype(ctx, var_val);
300309
JSValue constructor_func = JS_GetPropertyStr(ctx, object_proto, "constructor");
301310
char buffer[64];
302311
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+
304357
JS_FreeValue(ctx, object_proto);
305358
JS_FreeValue(ctx, constructor_func);
306359
}
@@ -366,7 +419,7 @@ static void process_request(JSDebuggerInfo* info, struct DebuggerSuspendedState*
366419
const char* evaluate_result = JS_ToCStringLen(ctx, &len, result);
367420
response->body->result = copy_string(evaluate_result, len);
368421
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);
370423
response->body->type = variable_type.type;
371424
response->body->variablesReference = variable_type.variablesReference;
372425

@@ -489,7 +542,7 @@ static void process_request(JSDebuggerInfo* info, struct DebuggerSuspendedState*
489542
for (uint32_t i = 0; i < count; i++) {
490543
JSValue value = JS_GetPropertyUint32(ctx, variable, start + i);
491544
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);
493546

494547
sprintf(name_buf, "%d", i);
495548
init_variable(&variables[i]);
@@ -506,49 +559,50 @@ static void process_request(JSDebuggerInfo* info, struct DebuggerSuspendedState*
506559
}
507560

508561
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)) {
510563
if (tab_atom_count == 0) {
511564
variables = NULL;
512565
variableLen = 0;
513566
goto done;
514567
}
515568

516-
int offset = 0;
517569
variables = js_malloc(ctx, sizeof(Variable) * (tab_atom_count + (skip_proto ? 0 : 1)));
518570

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+
519585
if (!skip_proto) {
520586
const JSValue proto = JS_GetPrototype(ctx, variable);
521587
if (!JS_IsException(proto)) {
522588
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);
532601
}
533602
JS_FreeValue(ctx, proto);
534603
}
535604

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);
552606
js_free_prop_enum(ctx, tab_atom, tab_atom_count);
553607
}
554608

@@ -614,7 +668,7 @@ static void js_process_debugger_messages(JSDebuggerInfo* info, const uint8_t* cu
614668
Request* request = js_malloc(ctx, sizeof(Request));
615669
parse_request(ctx, request, item.buf, item.length);
616670
process_request(info, &state, request);
617-
free_request(ctx, request);
671+
// free_request(ctx, request);
618672

619673
js_free(ctx, item.buf);
620674
} while (info->is_paused);

0 commit comments

Comments
 (0)