Skip to content

Commit 8551bdb

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

File tree

2 files changed

+103
-42
lines changed

2 files changed

+103
-42
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: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,63 @@ static void js_debugger_get_variable_type(JSContext* ctx,
295295
sprintf(buffer, "ƒ %s ()", func_name);
296296
variable_type->value = copy_string(buffer, strlen(buffer));
297297
JS_FreeCString(ctx, func_name);
298+
} else if (JS_IsArray(ctx, var_val)) {
299+
variable_type->type = "array";
300+
char buf[12];
301+
int64_t length;
302+
js_get_length64(ctx, &length, var_val);
303+
sprintf(buf, "Array(%lld)", length);
304+
variable_type->value = copy_string(buf, strlen(buf));
298305
} else {
299306
JSValue object_proto = JS_GetPrototype(ctx, var_val);
300307
JSValue constructor_func = JS_GetPropertyStr(ctx, object_proto, "constructor");
301308
char buffer[64];
302309
sprintf(buffer, "%s", get_func_name(ctx, constructor_func));
303-
variable_type->value = copy_string(buffer, strlen(buffer));
310+
311+
if (strcmp(buffer, "Object") == 0) {
312+
if (is_short) {
313+
variable_type->value = "{..}";
314+
} else {
315+
JSPropertyEnum* property_enum;
316+
uint32_t property_len;
317+
if (!JS_GetOwnPropertyNames(ctx, &property_enum, &property_len, var_val, JS_GPN_SYMBOL_MASK | JS_GPN_STRING_MASK)) {
318+
size_t buf_len = 256;
319+
char* buf = js_malloc(ctx, 256);
320+
buf[0] = '{';
321+
size_t index = 1;
322+
for(int i = 0; i < property_len; i ++) {
323+
JSValue v = JS_GetProperty(ctx, var_val, property_enum[i].atom);
324+
const char* key = atom_to_string(ctx, property_enum[i].atom);
325+
VariableType object_var_type;
326+
js_debugger_get_variable_type(ctx, state, &object_var_type, v, depth + 1, 1);
327+
const char* tmp = object_var_type.value;
328+
size_t tmp_len = strlen(tmp);
329+
if (index + tmp_len > buf_len) {
330+
buf_len = buf_len * 2;
331+
js_realloc(ctx, buf, buf_len);
332+
}
333+
strcpy(buf + index, key);
334+
index += strlen(key);
335+
strcpy(buf + index, ": ");
336+
index += 2;
337+
strcpy(buf + index, tmp);
338+
index += tmp_len;
339+
340+
if (i + 1 < property_len) {
341+
strcpy(buf + index, ", ");
342+
index += 2;
343+
}
344+
JS_FreeValue(ctx, v);
345+
}
346+
buf[index] = '}';
347+
buf[index + 1] = 0;
348+
variable_type->value = buf;
349+
}
350+
}
351+
} else {
352+
variable_type->value = copy_string(buffer, strlen(buffer));
353+
}
354+
304355
JS_FreeValue(ctx, object_proto);
305356
JS_FreeValue(ctx, constructor_func);
306357
}
@@ -366,7 +417,7 @@ static void process_request(JSDebuggerInfo* info, struct DebuggerSuspendedState*
366417
const char* evaluate_result = JS_ToCStringLen(ctx, &len, result);
367418
response->body->result = copy_string(evaluate_result, len);
368419
VariableType variable_type;
369-
js_debugger_get_variable_type(ctx, state, &variable_type, result);
420+
js_debugger_get_variable_type(ctx, state, &variable_type, result, 0, 0);
370421
response->body->type = variable_type.type;
371422
response->body->variablesReference = variable_type.variablesReference;
372423

@@ -489,7 +540,7 @@ static void process_request(JSDebuggerInfo* info, struct DebuggerSuspendedState*
489540
for (uint32_t i = 0; i < count; i++) {
490541
JSValue value = JS_GetPropertyUint32(ctx, variable, start + i);
491542
VariableType variable_type;
492-
js_debugger_get_variable_type(ctx, state, &variable_type, value);
543+
js_debugger_get_variable_type(ctx, state, &variable_type, value, 0, 0);
493544

494545
sprintf(name_buf, "%d", i);
495546
init_variable(&variables[i]);
@@ -506,49 +557,50 @@ static void process_request(JSDebuggerInfo* info, struct DebuggerSuspendedState*
506557
}
507558

508559
unfiltered:
509-
if (!JS_GetOwnPropertyNames(ctx, &tab_atom, &tab_atom_count, variable, JS_GPN_ENUM_ONLY | JS_GPN_STRING_MASK)) {
560+
if (!JS_GetOwnPropertyNames(ctx, &tab_atom, &tab_atom_count, variable, JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK)) {
510561
if (tab_atom_count == 0) {
511562
variables = NULL;
512563
variableLen = 0;
513564
goto done;
514565
}
515566

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

569+
for (int i = 0; i < tab_atom_count; i++) {
570+
JSValue value = JS_GetProperty(ctx, variable, tab_atom[i].atom);
571+
VariableType variable_type;
572+
js_debugger_get_variable_type(ctx, state, &variable_type, value, 0, 0);
573+
init_variable(&variables[i]);
574+
variables[i].name = atom_to_string(ctx, tab_atom[i].atom);
575+
variables[i].type = variable_type.type;
576+
variables[i].variablesReference = variable_type.variablesReference;
577+
variables[i].value = variable_type.value;
578+
assert(variables[i].name != NULL);
579+
assert(variables[i].value != NULL);
580+
JS_FreeValue(ctx, value);
581+
}
582+
519583
if (!skip_proto) {
520584
const JSValue proto = JS_GetPrototype(ctx, variable);
521585
if (!JS_IsException(proto)) {
522586
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);
587+
js_debugger_get_variable_type(ctx, state, &variable_type, proto, 0, 0);
588+
init_variable(&variables[tab_atom_count]);
589+
variables[tab_atom_count].name = "[[Prototype]]";
590+
variables[tab_atom_count].value = variable_type.type;
591+
variables[tab_atom_count].type = variable_type.value;
592+
variables[tab_atom_count].variablesReference = variable_type.variablesReference;
593+
VariablePresentationHint* presentation_hint = js_malloc(ctx, sizeof(VariablePresentationHint));
594+
presentation_hint->visibility = "internal";
595+
presentation_hint->lazy = 0;
596+
variables[tab_atom_count].presentationHint = presentation_hint;
597+
assert(variables[tab_atom_count].name != NULL);
598+
assert(variables[tab_atom_count].value != NULL);
532599
}
533600
JS_FreeValue(ctx, proto);
534601
}
535602

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;
603+
variableLen = tab_atom_count + (skip_proto ? 0 : 1);
552604
js_free_prop_enum(ctx, tab_atom, tab_atom_count);
553605
}
554606

@@ -614,7 +666,7 @@ static void js_process_debugger_messages(JSDebuggerInfo* info, const uint8_t* cu
614666
Request* request = js_malloc(ctx, sizeof(Request));
615667
parse_request(ctx, request, item.buf, item.length);
616668
process_request(info, &state, request);
617-
free_request(ctx, request);
669+
// free_request(ctx, request);
618670

619671
js_free(ctx, item.buf);
620672
} while (info->is_paused);

0 commit comments

Comments
 (0)