dart-lang / native

Dart packages related to FFI and native assets bundling.
BSD 3-Clause "New" or "Revised" License
155 stars 43 forks source link

[swift2objc] Parsing is flaky #1670

Open liamappelbe opened 2 weeks ago

liamappelbe commented 2 weeks ago

I'm running https://github.com/dart-lang/native/pull/1367 (with https://github.com/dart-lang/native/pull/1668 patched) on the AVFAudio module to try to see if I can migrate the ffigen objc example to swift. That'll be a good milestone to demonstrate binding a simple, but realistic, Apple API.

The most important class for this example is AVAudioPlayer, but currently it's flaky whether that class even exists in the list of declarations returned by parseAst.

liamappelbe commented 2 weeks ago

Fixed in https://github.com/dart-lang/native/pull/1668.

The main problem was that we're indicating errors by throwing exceptions. So if, for example, a method returned an enum, which we don't support yet, parseDeclaration would throw. Then since the method parser wasn't catching that exception, it also propagated up through _parseCompoundDeclaration, effectively removing that declaration. The fix is to catch the error in _parseCompoundDeclaration and just omit that method. I added tryParseDeclaration that wraps parseDeclaration, logs the exception and returns null.

More generally, any time we're calling parseDeclaration, if we want to gracefully handle a problem with the declaration, we should instead call tryParseDeclaration and handle the nullable result. I had a quick look at all the call sites and I don't think there are any other cases like this at the moment.