dart-lang / native

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

[ffigen] ObjC version compatibility runtime check #300

Open liamappelbe opened 2 years ago

liamappelbe commented 2 years ago

Not all APIs are available on all versions of iOS. There are annotations in Objective C for this, so we can turn that into version checks, and throw user friendly exceptions rather than crashing. Suggestion from Brian:

  - (NSURLSessionWebSocketTask *)webSocketTaskWithURL:(NSURL *)url protocols:(NSArray<NSString *>*)protocols API_AVAILABLE(macos(10.15), ios(13.0), watchos(6.0), tvos(13.0));

Could translate into:

webSocketTaskWithURL(NSURL url, NSArray<NSString> protocols) {
  if (platform.isIOS) {
    if (platform.version < 13.0) {
      throw UnsupportedError("webSocketTaskWithURL requires: macos > 10.15, ios >= 13.0, watchos >= 6.0 or tvos >= 13.0 [is ios ${platform.version}.")
    }
  } else ...
  <do the real work>
}
liamappelbe commented 2 years ago

cc @brianquinlan

dcharkes commented 2 years ago

Cool, reading those annotations and generating version checking code is a cool idea!

dcharkes commented 1 month ago

platform.version

Isn't that the Dart version instead of the OS version?

liamappelbe commented 1 month ago

Oops, yeah. Probably meant to use operatingSystemVersion, but I don't think we want to try parsing that.

liamappelbe commented 1 month ago

We're now parsing those API version annotations (#1403). So the only remaining issue is how do we get the OS version at runtime. Since a check like that is OS specific and requires native calls, it might make sense to add a native function to package:objective_c to do this on iOS and macOS. If OS version getters are available on every mobile and desktop platform, and always returns an answer that fits in a semver, then we could instead add this to dart:ffi or package:ffi.