Skip to content

Commit 7bea7d1

Browse files
natebiggsCommit Queue
authored andcommitted
[dart2wasm] Add indirection for struct initialization.
For dynamic modules we will "adjust" the class ID at runtime to ensure each module gets independent class ID spaces. This initial change simply provides the point where we will eventually add that logic. Change-Id: Iad9c38d9e3e842be2e77c48b1755ebe57d02d023 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/400923 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Nate Biggs <[email protected]>
1 parent 8c1fa6d commit 7bea7d1

File tree

5 files changed

+24
-30
lines changed

5 files changed

+24
-30
lines changed

pkg/dart2wasm/lib/closures.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:vm/transformations/type_flow/utils.dart' show UnionFind;
1212
import 'package:wasm_builder/wasm_builder.dart' as w;
1313

1414
import 'class_info.dart';
15+
import 'code_generator.dart';
1516
import 'param_info.dart';
1617
import 'translator.dart';
1718

@@ -722,8 +723,7 @@ class ClosureLayouter extends RecursiveVisitor {
722723
w.Local typeParam(int i) => instantiationFunction.locals[1 + i];
723724

724725
// Header for the closure struct
725-
b.i32_const(translator.closureInfo.classId);
726-
b.i32_const(initialIdentityHash);
726+
b.pushObjectHeaderFields(translator.closureInfo);
727727

728728
// Context for the instantiated closure, containing the original closure and
729729
// the type arguments

pkg/dart2wasm/lib/code_generator.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,8 +2335,7 @@ abstract class AstCodeGenerator
23352335
ClassInfo info = translator.closureInfo;
23362336
translator.functions.recordClassAllocation(info.classId);
23372337

2338-
b.i32_const(info.classId);
2339-
b.i32_const(initialIdentityHash);
2338+
b.pushObjectHeaderFields(info);
23402339
pushContext();
23412340
translator.globals.readGlobal(b, closure.vtable);
23422341
types.makeType(this, functionType);
@@ -2941,8 +2940,7 @@ abstract class AstCodeGenerator
29412940
translator.getRecordClassInfo(node.recordType);
29422941
translator.functions.recordClassAllocation(recordClassInfo.classId);
29432942

2944-
b.i32_const(recordClassInfo.classId);
2945-
b.i32_const(initialIdentityHash);
2943+
b.pushObjectHeaderFields(recordClassInfo);
29462944
for (Expression positional in node.positional) {
29472945
translateExpression(positional, translator.topInfo.nullableType);
29482946
}
@@ -3256,8 +3254,7 @@ class TearOffCodeGenerator extends AstCodeGenerator {
32563254
ClassInfo info = translator.closureInfo;
32573255
translator.functions.recordClassAllocation(info.classId);
32583256

3259-
b.i32_const(info.classId);
3260-
b.i32_const(initialIdentityHash);
3257+
b.pushObjectHeaderFields(info);
32613258
b.local_get(paramLocals[0]); // `this` as context
32623259
translator.globals.readGlobal(b, closure.vtable);
32633260
types.makeType(this, functionType);
@@ -3731,8 +3728,7 @@ class ConstructorAllocatorCodeGenerator extends AstCodeGenerator {
37313728
}
37323729

37333730
// Set field values
3734-
b.i32_const(info.classId);
3735-
b.i32_const(initialIdentityHash);
3731+
b.pushObjectHeaderFields(info);
37363732

37373733
for (w.Local local in orderedFieldLocals.reversed) {
37383734
b.local_get(local);
@@ -4595,6 +4591,13 @@ extension MacroAssembler on w.InstructionsBuilder {
45954591

45964592
return target.signature.outputs;
45974593
}
4594+
4595+
/// Pushes fields common to all Dart objects (class id, id hash).
4596+
void pushObjectHeaderFields(ClassInfo classInfo) {
4597+
// TODO(natebiggs): Adjust class ID for dynamic module if appropriate.
4598+
i32_const(classInfo.classId);
4599+
i32_const(initialIdentityHash);
4600+
}
45984601
}
45994602

46004603
/// A call target that may be called with a direct call or may be inlined.

pkg/dart2wasm/lib/constants.dart

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:wasm_builder/wasm_builder.dart' as w;
1212

1313
import 'class_info.dart';
1414
import 'closures.dart';
15+
import 'code_generator.dart';
1516
import 'param_info.dart';
1617
import 'translator.dart';
1718
import 'types.dart';
@@ -532,8 +533,7 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
532533
if (translator.options.jsCompatibility) {
533534
ClassInfo info = translator.classInfo[translator.jsStringClass]!;
534535
return createConstant(constant, info.nonNullableType, (b) {
535-
b.i32_const(info.classId);
536-
b.i32_const(initialIdentityHash);
536+
b.pushObjectHeaderFields(info);
537537
translator.globals.readGlobal(b,
538538
translator.getInternalizedStringGlobal(b.module, constant.value));
539539
b.struct_new(info.struct);
@@ -551,8 +551,7 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
551551
(info.struct.fields[FieldIndex.stringArray].type as w.RefType)
552552
.heapType as w.ArrayType;
553553

554-
b.i32_const(info.classId);
555-
b.i32_const(initialIdentityHash);
554+
b.pushObjectHeaderFields(info);
556555
if (lazy) {
557556
// Initialize string contents from passive data segment.
558557
w.DataSegmentBuilder segment;
@@ -631,8 +630,7 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
631630
}
632631

633632
return createConstant(constant, type, lazy: lazy, (b) {
634-
b.i32_const(info.classId);
635-
b.i32_const(initialIdentityHash);
633+
b.pushObjectHeaderFields(info);
636634
for (int i = baseFieldCount; i < fieldCount; i++) {
637635
Constant subConstant = subConstants[i]!;
638636
constants.instantiateConstant(
@@ -740,8 +738,7 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
740738
w.ArrayType arrayType = translator.listArrayType;
741739
w.ValueType elementType = arrayType.elementType.type.unpacked;
742740
int length = constant.entries.length;
743-
b.i32_const(info.classId);
744-
b.i32_const(initialIdentityHash);
741+
b.pushObjectHeaderFields(info);
745742
constants.instantiateConstant(
746743
b, typeArgConstant, constants.typeInfo.nullableType);
747744
b.i64_const(length);
@@ -865,8 +862,7 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
865862
ClassInfo info = translator.closureInfo;
866863
translator.functions.recordClassAllocation(info.classId);
867864

868-
b.i32_const(info.classId);
869-
b.i32_const(initialIdentityHash);
865+
b.pushObjectHeaderFields(info);
870866
translator.globals.readGlobal(b, dummyStructGlobal); // Dummy context
871867
translator.globals.readGlobal(b, closure.vtable);
872868
constants.instantiateConstant(
@@ -1026,8 +1022,7 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
10261022
b.struct_new(instantiationOfTearOffRepresentation.vtableStruct);
10271023
}
10281024

1029-
b.i32_const(info.classId);
1030-
b.i32_const(initialIdentityHash);
1025+
b.pushObjectHeaderFields(info);
10311026

10321027
// Context is not used by the vtable functions, but it's needed for
10331028
// closure equality checks to work (`_Closure._equals`).
@@ -1059,8 +1054,7 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
10591054
StringConstant nameConstant = StringConstant(constant.name);
10601055
bool lazy = ensureConstant(nameConstant)?.isLazy ?? false;
10611056
return createConstant(constant, info.nonNullableType, lazy: lazy, (b) {
1062-
b.i32_const(info.classId);
1063-
b.i32_const(initialIdentityHash);
1057+
b.pushObjectHeaderFields(info);
10641058
constants.instantiateConstant(b, nameConstant, stringType);
10651059
b.struct_new(info.struct);
10661060
});
@@ -1082,8 +1076,7 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?>
10821076

10831077
return createConstant(constant, recordClassInfo.nonNullableType, lazy: lazy,
10841078
(b) {
1085-
b.i32_const(recordClassInfo.classId);
1086-
b.i32_const(initialIdentityHash);
1079+
b.pushObjectHeaderFields(recordClassInfo);
10871080
for (Constant argument in arguments) {
10881081
constants.instantiateConstant(
10891082
b, argument, translator.topInfo.nullableType);

pkg/dart2wasm/lib/sync_star.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ mixin SyncStarCodeGeneratorMixin on StateMachineEntryAstCodeGenerator {
2727
// function for this `sync*` function.
2828
DartType elementType = functionNode.emittedValueType!;
2929
translator.functions.recordClassAllocation(syncStarIterableInfo.classId);
30-
b.i32_const(syncStarIterableInfo.classId);
31-
b.i32_const(initialIdentityHash);
30+
b.pushObjectHeaderFields(syncStarIterableInfo);
3231
types.makeType(this, elementType);
3332
if (context != null) {
3433
assert(!context.isEmpty);

pkg/dart2wasm/lib/types.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,7 @@ class Types {
346346
}
347347

348348
translator.functions.recordClassAllocation(info.classId);
349-
b.i32_const(info.classId);
350-
b.i32_const(initialIdentityHash);
349+
b.pushObjectHeaderFields(info);
351350
if (type is InterfaceType) {
352351
_makeInterfaceType(codeGen, type);
353352
} else if (type is FunctionType) {

0 commit comments

Comments
 (0)