Skip to content

[ffigen] ObjC id<Foo> -> Dart Foo #1959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 3, 2025
Merged

[ffigen] ObjC id<Foo> -> Dart Foo #1959

merged 14 commits into from
Feb 3, 2025

Conversation

liamappelbe
Copy link
Contributor

@liamappelbe liamappelbe commented Jan 31, 2025

This is the second part of #1462. API methods that accept id<Foo> are now code-genned as taking Foo instead of ObjCObjectBase. The protocol's implement method also returns that protocol instead of ObjCObjectBase.

There's a big caveat with this solution. I couldn't think of a way of capturing the semantics of id<Foo, Bar>, so I'm treating it as if it's just id<Foo>. This means that there are still cases where you can pass the wrong type of object to these methods. But I think these cases are pretty rare, and the failure case is just that the compiler is a bit too permissive, so it's probably not a big deal.

Details

  • Added a new type to the ffigen type system: ObjCObjectPointerWithProtocols.
  • Added logic to parse id into either ObjCObjectPointer or ObjCObjectPointerWithProtocols depending on whether it has any conforming protocols.
  • I had to add the same castFrom and castFromPointer methods that interfaces have, as an escape hatch, and to support more advanced use cases that use ObjCProtocolBuilder to implement multiple methods.
  • That meant that I had to make these protocols instantiatable, so now they're interface class instead of abstract interface class.
  • I also added a protocol conformsTo method, analogous to interface's isInstance method. This method doesn't work for Dart constructed protocols, but I can add support for that if a user needs it.
  • When parsing protocol Foo from id<Foo>, the AST has a weird shape. Instead of the AST looking like (decl -> methods/etc), it looks like (stubDecl --superProto-> decl -> methods/etc). So I had to add some logic in the protocol parser to detect this case and parse the true decl instead.

Fixes #1462

Copy link

github-actions bot commented Jan 31, 2025

PR Health

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
objective_c Breaking 4.1.0 5.0.0-wip 5.0.0 ✔️
API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbols
License Headers ✔️
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/jni/lib/src/third_party/generated_bindings.dart
pkgs/objective_c/lib/src/ns_input_stream.dart

@liamappelbe liamappelbe changed the title WIP: [ffigen] ObjC id<Foo> -> Dart Foo [ffigen] ObjC id<Foo> -> Dart Foo Jan 31, 2025
@coveralls
Copy link

coveralls commented Feb 3, 2025

Coverage Status

coverage: 88.099% (+0.5%) from 87.598%
when pulling 567fc79 on protoid
into a36bc4a on main.

@liamappelbe liamappelbe marked this pull request as ready for review February 3, 2025 03:06
@liamappelbe liamappelbe merged commit b053b7b into main Feb 3, 2025
23 checks passed
@liamappelbe liamappelbe deleted the protoid branch February 3, 2025 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Obj-C protocol conformance is dropped from types
3 participants