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 committed Dec 14, 2019
1 parent c7f55e0 commit 7ecf961
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/SIL/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2609,6 +2609,14 @@ 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 &&
Expand All @@ -2620,8 +2628,14 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M,
} else {
return ABIDifference::CompatibleRepresentation_ThinToThick;
}
}

// 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 7ecf961

Please sign in to comment.