@@ -501,6 +501,55 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
501501 static String _dynamicModuleConstantExportName (int id) => '#c$id ' ;
502502 static String _dynamicModuleInitFunctionExportName (int id) => '#cf$id ' ;
503503
504+ static int _nextGlobalId = 0 ;
505+ String _constantName (Constant constant) {
506+ final id = _nextGlobalId++ ;
507+ if (constant is StringConstant ) {
508+ var value = constant.value;
509+ final newline = value.indexOf ('\n ' );
510+ if (newline != - 1 ) value = value.substring (0 , newline);
511+ if (value.length > 30 ) value = '${value .substring (0 , 30 )}<...>' ;
512+ return 'C$id "$value "' ;
513+ }
514+ if (constant is BoolConstant ) {
515+ return 'C$id ${constant .value }' ;
516+ }
517+ if (constant is IntConstant ) {
518+ return 'C$id ${constant .value }' ;
519+ }
520+ if (constant is DoubleConstant ) {
521+ return 'C$id ${constant .value }' ;
522+ }
523+ if (constant is InstanceConstant ) {
524+ final klass = constant.classNode;
525+ final name = klass.name;
526+ if (constant.typeArguments.isEmpty) {
527+ return 'C$id $name ' ;
528+ }
529+ final typeArguments = constant.typeArguments.map (_nameType).join (', ' );
530+ if (klass == translator.wasmArrayClass ||
531+ klass == translator.immutableWasmArrayClass) {
532+ final entries =
533+ (constant.fieldValues.values.single as ListConstant ).entries;
534+ return 'C$id $name <$typeArguments >[${entries .length }]' ;
535+ }
536+ return 'C$id $name <$typeArguments >' ;
537+ }
538+ if (constant is TearOffConstant ) {
539+ return 'C$id ${constant .target .name } tear-off' ;
540+ }
541+ return 'C$id $constant ' ;
542+ }
543+
544+ String _nameType (DartType type) {
545+ if (type is InterfaceType ) {
546+ final name = type.classNode.name;
547+ if (type.typeArguments.isEmpty) return name;
548+ return '$name <${type .typeArguments .map ((t ) => _nameType (t )).join (', ' )}>' ;
549+ }
550+ return '$type ' ;
551+ }
552+
504553 ConstantInfo createConstant (
505554 Constant constant, w.RefType type, ConstantCodeGenerator generator,
506555 {bool lazy = false }) {
@@ -535,12 +584,14 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
535584 _dynamicModuleInitFunctionExportName (mainModuleExportId),
536585 ftype);
537586 } else {
538- final definedGlobal = global = targetModule.globals.define (globalType);
587+ final name = _constantName (constant);
588+ final definedGlobal =
589+ global = targetModule.globals.define (globalType, name);
539590 definedGlobal.initializer.ref_null (w.HeapType .none);
540591 definedGlobal.initializer.end ();
541592
542- final function =
543- initFunction = targetModule.functions.define (ftype, "$ constant " );
593+ final function = initFunction =
594+ targetModule.functions.define (ftype, '$ name (lazy initializer)}' );
544595
545596 if (isShareableAcrossModules) {
546597 final exportId = dynamicModuleConstantIdMap[constant] =
@@ -575,7 +626,8 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
575626 _dynamicModuleConstantExportName (mainModuleExportId), globalType);
576627 } else {
577628 constants.currentlyCreating = true ;
578- final definedGlobal = global = targetModule.globals.define (globalType);
629+ final definedGlobal = global =
630+ targetModule.globals.define (globalType, _constantName (constant));
579631 generator (definedGlobal.initializer);
580632 definedGlobal.initializer.end ();
581633 constants.currentlyCreating = false ;
@@ -830,48 +882,21 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
830882
831883 @override
832884 ConstantInfo ? visitListConstant (ListConstant constant) {
833- Constant typeArgConstant =
834- constants._lowerTypeConstant (constant.typeArgument);
835- ensureConstant (typeArgConstant);
836- bool lazy = constant.entries.length > maxArrayNewFixedLength;
837- for (Constant subConstant in constant.entries) {
838- lazy | = ensureConstant (subConstant)? .isLazy ?? false ;
839- }
840-
841- ClassInfo info = translator.classInfo[translator.immutableListClass]! ;
842- translator.functions.recordClassAllocation (info.classId);
843- w.RefType type = info.nonNullableType;
844- return createConstant (constant, type, lazy: lazy, (b) {
845- w.ArrayType arrayType = translator.listArrayType;
846- w.ValueType elementType = arrayType.elementType.type.unpacked;
847- int length = constant.entries.length;
848- b.pushObjectHeaderFields (translator, info);
849- constants.instantiateConstant (
850- b, typeArgConstant, constants.typeInfo.nullableType);
851- b.i64_const (length);
852- if (lazy) {
853- // Allocate array and set each entry to the corresponding sub-constant.
854- w.Local arrayLocal =
855- b.addLocal (w.RefType .def (arrayType, nullable: false ));
856- b.i32_const (length);
857- b.array_new_default (arrayType);
858- b.local_set (arrayLocal);
859- for (int i = 0 ; i < length; i++ ) {
860- b.local_get (arrayLocal);
861- b.i32_const (i);
862- constants.instantiateConstant (b, constant.entries[i], elementType);
863- b.array_set (arrayType);
864- }
865- b.local_get (arrayLocal);
866- } else {
867- // Push all sub-constants on the stack and initialize array from them.
868- for (int i = 0 ; i < length; i++ ) {
869- constants.instantiateConstant (b, constant.entries[i], elementType);
870- }
871- b.array_new_fixed (arrayType, length);
872- }
873- b.struct_new (info.struct);
885+ final instanceConstant =
886+ InstanceConstant (translator.immutableListClass.reference, [
887+ constant.typeArgument,
888+ ], {
889+ translator.listBaseLengthField.fieldReference:
890+ IntConstant (constant.entries.length),
891+ translator.listBaseDataField.fieldReference:
892+ InstanceConstant (translator.wasmArrayClass.reference, [
893+ translator.coreTypes.objectNullableRawType
894+ ], {
895+ translator.wasmArrayValueField.fieldReference: ListConstant (
896+ translator.coreTypes.objectNullableRawType, constant.entries)
897+ }),
874898 });
899+ return ensureConstant (instanceConstant);
875900 }
876901
877902 @override
0 commit comments