Skip to content

Commit 7ecf961

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 c7f55e0 commit 7ecf961

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
@@ -2609,6 +2609,14 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
26092609
return ABIDifference::NeedsThunk;
26102610
}
26112611

2612+
// There is no ABI compatibility between non-throws and throws on WebAssembly,
2613+
// so need thunk.
2614+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2615+
if (!fnTy1->hasErrorResult() && fnTy2->hasErrorResult()) {
2616+
return ABIDifference::NeedsThunk;
2617+
}
2618+
}
2619+
26122620
auto rep1 = fnTy1->getRepresentation(), rep2 = fnTy2->getRepresentation();
26132621
if (rep1 != rep2) {
26142622
if (rep1 == SILFunctionTypeRepresentation::Thin &&
@@ -2620,8 +2628,14 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
26202628
} else {
26212629
return ABIDifference::CompatibleRepresentation_ThinToThick;
26222630
}
2623-
}
26242631

2632+
// There is no ABI compatibility between thin and thick on WebAssembly,
2633+
// so need thunk.
2634+
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
2635+
return ABIDifference::NeedsThunk;
2636+
}
2637+
return ABIDifference::ThinToThick;
2638+
}
26252639
return ABIDifference::NeedsThunk;
26262640
}
26272641

0 commit comments

Comments
 (0)