Finish bindings generation for (implements ..)#13513
Open
alexcrichton wants to merge 1 commit into
Open
Conversation
This commit is the final step in finishing up the bindings generation
story for `bindgen!` w.r.t. the `(implement ...)` component model
feature. Namely `bindgen!` now has the option `named_imports: { ... }`
which generates a new top-level `named_imports` module with nested
modules within it. These nested modules are similar to the
default-generated ones except that they additionally take an extra "id"
parameter in all trait methods. The `add_to_linker` function
additionally takes both a `&Component` and a `lookup` function.
Putting all this together the expected flow is:
* Host decide they want to accept any number of `import a: b:c/d;` from
components at runtime. Bindings are generated with
`named_imports: { ... }` in the `bindgen!` invocation.
* Hosts switch from linker-per-engine to instead using a
linker-per-component. After the typical population of items within the
`Linker` next the host uses the
`named_imports::b::c::d::add_to_linker`, for example. Here the host
performs lookup from whatever string was found in the component to an
ID key that the host knows about. Upon success this key will be closed
over in all `add_to_linker` functions. Upon failure the
`add_to_linker` closure fails.
* This per-component linker is used to instantiate and create instances
as usual. At runtime when a named import is invoked the closed-over ID
is passed to the trait implementation as a parameter. Hosts can thus
disambiguate through which interface the component made a call through.
Some light tests are added throughout this showcasing the various
capabilities. This notably added a new `ComponentExtern::is_implements`
method to peform semver-matching automatically so hosts can generate
bindings with one version and still accept `implements` with other
slightly different but compatible versions.
Member
One thing I didn't think about when we discussed this yesterday: how much overhead does this introduce for hosts with large numbers of components loaded? (I'm not sure how much of a real alternative to this design there is, but I'd at least like to understand the costs a bit better.) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit is the final step in finishing up the bindings generation story for
bindgen!w.r.t. the(implement ...)component model feature. Namelybindgen!now has the optionnamed_imports: { ... }which generates a new top-levelnamed_importsmodule with nested modules within it. These nested modules are similar to the default-generated ones except that they additionally take an extra "id" parameter in all trait methods. Theadd_to_linkerfunction additionally takes both a&Componentand alookupfunction.Putting all this together the expected flow is:
Host decide they want to accept any number of
import a: b:c/d;from components at runtime. Bindings are generated withnamed_imports: { ... }in thebindgen!invocation.Hosts switch from linker-per-engine to instead using a linker-per-component. After the typical population of items within the
Linkernext the host uses thenamed_imports::b::c::d::add_to_linker, for example. Here the host performs lookup from whatever string was found in the component to an ID key that the host knows about. Upon success this key will be closed over in alladd_to_linkerfunctions. Upon failure theadd_to_linkerclosure fails.This per-component linker is used to instantiate and create instances as usual. At runtime when a named import is invoked the closed-over ID is passed to the trait implementation as a parameter. Hosts can thus disambiguate through which interface the component made a call through.
Some light tests are added throughout this showcasing the various capabilities. This notably added a new
ComponentExtern::is_implementsmethod to peform semver-matching automatically so hosts can generate bindings with one version and still acceptimplementswith other slightly different but compatible versions.