Skip to content

Commit 5cf1bed

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 8b49c06 commit 5cf1bed

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
@@ -2633,6 +2633,14 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
26332633
return ABIDifference::NeedsThunk;
26342634
}
26352635

2636+
// There is no ABI compatibility between non-throws and throws on WebAssembly,
2637+
// so need thunk.
2638+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2639+
if (!fnTy1->hasErrorResult() && fnTy2->hasErrorResult()) {
2640+
return ABIDifference::NeedsThunk;
2641+
}
2642+
}
2643+
26362644
auto rep1 = fnTy1->getRepresentation(), rep2 = fnTy2->getRepresentation();
26372645
if (rep1 != rep2) {
26382646
if (rep1 == SILFunctionTypeRepresentation::Thin &&
@@ -2644,8 +2652,14 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
26442652
} else {
26452653
return ABIDifference::CompatibleRepresentation_ThinToThick;
26462654
}
2647-
}
26482655

2656+
// There is no ABI compatibility between thin and thick on WebAssembly,
2657+
// so need thunk.
2658+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2659+
return ABIDifference::NeedsThunk;
2660+
}
2661+
return ABIDifference::ThinToThick;
2662+
}
26492663
return ABIDifference::NeedsThunk;
26502664
}
26512665

0 commit comments

Comments
 (0)