Skip to content

Commit 480f2c1

Browse files
Emit thunk function for specific ABI on WebAssembly. (#6)
Changed to make thunk to convert thin-to-thick and non-throws-to-throws. We needs it on WebAssembly host because WASM checks number of arguments strictly for indirect function call. This patch allows you to run the Swift code below. ```swift func f(_ a: (Int) -> Void) { g(a) } func g(_ b: (Int) throws -> Void) { try! b(1) } f { _ in } ```
1 parent f995fb0 commit 480f2c1

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Diff for: lib/SIL/TypeLowering.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,14 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
26252625
return ABIDifference::NeedsThunk;
26262626
}
26272627

2628+
// There is no ABI compatibility between non-throws and throws on WebAssembly,
2629+
// so need thunk.
2630+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2631+
if (!fnTy1->hasErrorResult() && fnTy2->hasErrorResult()) {
2632+
return ABIDifference::NeedsThunk;
2633+
}
2634+
}
2635+
26282636
auto rep1 = fnTy1->getRepresentation(), rep2 = fnTy2->getRepresentation();
26292637
if (rep1 != rep2) {
26302638
if (rep1 == SILFunctionTypeRepresentation::Thin &&
@@ -2636,8 +2644,14 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
26362644
} else {
26372645
return ABIDifference::CompatibleRepresentation_ThinToThick;
26382646
}
2639-
}
26402647

2648+
// There is no ABI compatibility between thin and thick on WebAssembly,
2649+
// so need thunk.
2650+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2651+
return ABIDifference::NeedsThunk;
2652+
}
2653+
return ABIDifference::ThinToThick;
2654+
}
26412655
return ABIDifference::NeedsThunk;
26422656
}
26432657

0 commit comments

Comments
 (0)