Skip to content

Commit 3e4e71e

Browse files
kateinoigakukunMaxDesiatov
authored andcommitted
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 cb3c0eb commit 3e4e71e

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/SIL/TypeLowering.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -2644,12 +2644,25 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
26442644
return ABIDifference::NeedsThunk;
26452645
}
26462646

2647+
// There is no ABI compatibility between non-throws and throws on WebAssembly,
2648+
// so need thunk.
2649+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2650+
if (!fnTy1->hasErrorResult() && fnTy2->hasErrorResult()) {
2651+
return ABIDifference::NeedsThunk;
2652+
}
2653+
}
2654+
26472655
auto rep1 = fnTy1->getRepresentation(), rep2 = fnTy2->getRepresentation();
26482656
if (rep1 != rep2) {
26492657
if (rep1 == SILFunctionTypeRepresentation::Thin &&
2650-
rep2 == SILFunctionTypeRepresentation::Thick)
2658+
rep2 == SILFunctionTypeRepresentation::Thick) {
2659+
// There is no ABI compatibility between thin and thick on WebAssembly,
2660+
// so need thunk.
2661+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2662+
return ABIDifference::NeedsThunk;
2663+
}
26512664
return ABIDifference::ThinToThick;
2652-
2665+
}
26532666
return ABIDifference::NeedsThunk;
26542667
}
26552668

0 commit comments

Comments
 (0)