Skip to content
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

Allow external crates to write their own binding generators #1201

Merged
merged 6 commits into from
Apr 1, 2022

Conversation

tarikeshaq
Copy link
Contributor

These are the first bits needed to implement an external binding generator that lives in an outside crate.

Most of the changes are in the uniffi_bindgen/src/lib.rs to define a trait BindingGenerator that an external crate can use to take advantage of the logic in uniffi to parse the udl into a ComponentInterface.

This is not a breaking change, and only exposes the functionality (it doesn't change the existing crates)

cc @bendk @mhammond

Tarik Eshaq and others added 5 commits February 22, 2022 16:28
Use &Path rather than AsRef<Path>.  We can call `as_ref()` before
calling into `BindingGenerator` and keep the implementation code simple.

Added code to extract the binding-specific config from the `uniffi.toml`
config that contains it.

Instead of leveraging `From<&ComponentInterface>` and `MergeWith`, have
some `BindingGeneratorConfig` methods that explicitly do what we need
them to do: update the values read from TOML with defaults from
`ComponentInterface`.

When implementing custom types, I wasn't sure how to properly implement
the `MergeWith` trait.  For example, what if we have 2 configs, each
with a custom type map should we try to take entries from both?  Having
methods specific to the actual process can avoid issues like that.  In
the custom types case, we never get custom types from the component
interface, so we don't need to worry about merging them.
- Updated `BindingsGeneratorConfig` and added an empty implementation
  for bindings that don't have config values.
- Instead of having `generate_external_bindings` construct the
  `BindingsGenerator` it now inputs one instead.  The advantage here is
  that bindings generators can have whatever fields they want.  For the
  desktop JS project I want to make a generator store an enum that
  controls if it's generating the .webidl, .cpp, .h, or .jsm file.
- Have `write_bindings()` input the config since we're no longer passing
  that to the `BindingsGenerator` constructor.
- Removed the `compile_bindings()` and `run_script()` methods.  These
  are going to be replaced with some other system to run tests.
- Separated the trait bounds for the `AsRef<Path>` arguments to
  `write_bindings()`.  I think the previous code required all of them to
  be the same type.  Now one can be a `PathBuf` and another can be a
  `Path`.
- Made ComponentInterface public, since it's used in `BindingsGenerator`
- Added `ComponentInterface` method to get all FFI functions,
  except the `RustBuffer` ones
- Pass in an owned ComponentInterface rather than a reference
- Don't return an error when `uniffi.toml` is missing
- Added some anyhow context calls for better error tracking
@tarikeshaq tarikeshaq requested a review from a team March 31, 2022 21:47
@tarikeshaq tarikeshaq changed the title Allow external creates to write their own binding generators Allow external crates to write their own binding generators Mar 31, 2022
Copy link
Member

@mhammond mhammond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lovely, thanks Tarik!

uniffi/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@bendk bendk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me and it's been used for our desktop JS code for a while so I think we can have decent confidence in the code.

The one issue is the export hack should be removed since it's been implemented in a better way.

uniffi/src/lib.rs Outdated Show resolved Hide resolved
@tarikeshaq tarikeshaq requested a review from bendk April 1, 2022 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants