Skip to content

Commit

Permalink
Emit thunk function for specific ABI on WebAssembly. (#6)
Browse files Browse the repository at this point in the history
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 }
```
  • Loading branch information
kateinoigakukun authored and MaxDesiatov committed Dec 14, 2019
1 parent cb3c0eb commit 3e4e71e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/SIL/TypeLowering.cpp
Expand Up @@ -2644,12 +2644,25 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
return ABIDifference::NeedsThunk;
}

// There is no ABI compatibility between non-throws and throws on WebAssembly,
// so need thunk.
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
if (!fnTy1->hasErrorResult() && fnTy2->hasErrorResult()) {
return ABIDifference::NeedsThunk;
}
}

auto rep1 = fnTy1->getRepresentation(), rep2 = fnTy2->getRepresentation();
if (rep1 != rep2) {
if (rep1 == SILFunctionTypeRepresentation::Thin &&
rep2 == SILFunctionTypeRepresentation::Thick)
rep2 == SILFunctionTypeRepresentation::Thick) {
// There is no ABI compatibility between thin and thick on WebAssembly,
// so need thunk.
if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) {
return ABIDifference::NeedsThunk;
}
return ABIDifference::ThinToThick;

}
return ABIDifference::NeedsThunk;
}

Expand Down

0 comments on commit 3e4e71e

Please sign in to comment.