-
Notifications
You must be signed in to change notification settings - Fork 0
feat(codegen): generate wasm bridge callbacks #265
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
Open
pgherveou
wants to merge
13
commits into
rust-core/03-server-runtime
Choose a base branch
from
rust-core/04-wasm-bridge-codegen
base: rust-core/03-server-runtime
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f22b4b5
feat(codegen): generate wasm bridge callbacks
pgherveou 460dc77
Merge branch 'rust-core/03-server-runtime' into rust-core/04-wasm-bri…
pgherveou cddce3e
Merge branch 'rust-core/03-server-runtime' into rust-core/04-wasm-bri…
pgherveou a253874
fix(codegen): satisfy wasm bridge clippy
pgherveou 01ddadc
Merge remote-tracking branch 'origin/rust-core/03-server-runtime' int…
pgherveou 2fc5be9
Merge remote-tracking branch 'origin/rust-core/03-server-runtime' int…
pgherveou c3c243b
Merge remote-tracking branch 'origin/rust-core/03-server-runtime' int…
pgherveou 07a3a1c
Merge branch 'rust-core/03-server-runtime' into rust-core/04-wasm-bri…
pgherveou 9f0a7ef
Merge branch 'rust-core/03-server-runtime' into rust-core/04-wasm-bri…
pgherveou 5f21721
Merge branch 'rust-core/03-server-runtime' into rust-core/04-wasm-bri…
pgherveou c4f7c02
Merge branch 'rust-core/03-server-runtime' into rust-core/04-wasm-bri…
pgherveou ee797ed
Merge branch 'rust-core/03-server-runtime' into rust-core/04-wasm-bri…
pgherveou 95ce2e9
fixup! feat(codegen): generate wasm bridge callbacks
pgherveou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| //! Shared callback naming and selection rules for generated platform bridges. | ||
|
|
||
| use std::collections::BTreeSet; | ||
|
|
||
| use crate::platform::{PlatformDefinition, PlatformInner, PlatformMethod, PlatformTrait}; | ||
| use crate::rustdoc::TypeRef; | ||
|
|
||
| pub(crate) fn composed_traits(definition: &PlatformDefinition) -> Vec<&PlatformTrait> { | ||
| let composed: BTreeSet<String> = match &definition.super_trait { | ||
| Some(s) => s.composes.iter().cloned().collect(), | ||
| None => definition.traits.iter().map(|t| t.name.clone()).collect(), | ||
| }; | ||
| definition | ||
| .traits | ||
| .iter() | ||
| .filter(|t| composed.contains(&t.name)) | ||
| .collect() | ||
| } | ||
|
|
||
| pub(crate) fn raw_callback_name(method: &PlatformMethod) -> String { | ||
| to_camel_case(&method.name) | ||
| } | ||
|
|
||
| pub(crate) fn platform_trait_names(definition: &PlatformDefinition) -> BTreeSet<String> { | ||
| definition.traits.iter().map(|t| t.name.clone()).collect() | ||
| } | ||
|
|
||
| pub(crate) fn trait_object_return_name<'a>( | ||
| method: &'a PlatformMethod, | ||
| platform_trait_names: &BTreeSet<String>, | ||
| ) -> Option<&'a str> { | ||
| match &method.return_shape.inner { | ||
| PlatformInner::TraitObject(name) => Some(name.as_str()), | ||
| PlatformInner::Result { ok, .. } | PlatformInner::Plain(ok) => { | ||
| named_platform_trait(ok, platform_trait_names) | ||
| } | ||
| PlatformInner::Unit | PlatformInner::Stream(_) => None, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn raw_callback_wire_name( | ||
| trait_def: &PlatformTrait, | ||
| method: &PlatformMethod, | ||
| platform_trait_names: &BTreeSet<String>, | ||
| ) -> String { | ||
| let raw = raw_callback_name(method); | ||
| if trait_object_return_name(method, platform_trait_names).is_some() { | ||
| return format!( | ||
| "{}{}", | ||
| callback_namespace(&trait_def.name), | ||
| upper_first(&raw) | ||
| ); | ||
| } | ||
| raw | ||
| } | ||
|
|
||
| pub(crate) fn raw_callback_field_name( | ||
| trait_def: &PlatformTrait, | ||
| method: &PlatformMethod, | ||
| platform_trait_names: &BTreeSet<String>, | ||
| ) -> String { | ||
| snake_case(&raw_callback_wire_name( | ||
| trait_def, | ||
| method, | ||
| platform_trait_names, | ||
| )) | ||
| } | ||
|
|
||
| pub(crate) fn raw_callback_type_name( | ||
| trait_def: &PlatformTrait, | ||
| method: &PlatformMethod, | ||
| platform_trait_names: &BTreeSet<String>, | ||
| ) -> String { | ||
| upper_first(&raw_callback_wire_name( | ||
| trait_def, | ||
| method, | ||
| platform_trait_names, | ||
| )) | ||
| } | ||
|
|
||
| pub(crate) fn raw_callback_adapter_name( | ||
| trait_def: &PlatformTrait, | ||
| method: &PlatformMethod, | ||
| platform_trait_names: &BTreeSet<String>, | ||
| ) -> String { | ||
| format!( | ||
| "{}Adapter", | ||
| raw_callback_wire_name(trait_def, method, platform_trait_names) | ||
| ) | ||
| } | ||
|
|
||
| pub(crate) fn callback_namespace(trait_name: &str) -> String { | ||
| let stem = ["Provider", "Presenter", "Host"] | ||
| .into_iter() | ||
| .find_map(|suffix| trait_name.strip_suffix(suffix)) | ||
| .unwrap_or(trait_name); | ||
| lower_pascal_case(stem) | ||
| } | ||
|
|
||
| fn named_platform_trait<'a>( | ||
| ty: &'a TypeRef, | ||
| platform_trait_names: &BTreeSet<String>, | ||
| ) -> Option<&'a str> { | ||
| let TypeRef::Named { name, args } = ty else { | ||
| return None; | ||
| }; | ||
| if args.is_empty() && platform_trait_names.contains(name) { | ||
| return Some(name.as_str()); | ||
| } | ||
| None | ||
| } | ||
|
|
||
| /// Unwrap a `Result<T, E>` stream item to its `T`; other item types pass | ||
| /// through. Streams carry `Result`s on the Rust side but the JS raw bridge | ||
| /// already unwraps them before handing each item to the WASM callback sink. | ||
| pub(crate) fn stream_item(item: &TypeRef) -> &TypeRef { | ||
| if let TypeRef::Named { name, args } = item | ||
| && name == "Result" | ||
| && let Some(ok) = args.first() | ||
| { | ||
| return ok; | ||
| } | ||
| item | ||
| } | ||
|
|
||
| pub(crate) fn to_camel_case(name: &str) -> String { | ||
| let mut out = String::with_capacity(name.len()); | ||
| let mut upper_next = false; | ||
| for (idx, ch) in name.chars().enumerate() { | ||
| if ch == '_' { | ||
| upper_next = idx != 0; | ||
| continue; | ||
| } | ||
| if upper_next { | ||
| out.extend(ch.to_uppercase()); | ||
| upper_next = false; | ||
| } else { | ||
| out.push(ch); | ||
| } | ||
| } | ||
| out | ||
| } | ||
|
|
||
| fn lower_pascal_case(name: &str) -> String { | ||
| let mut chars = name.chars(); | ||
| let Some(first) = chars.next() else { | ||
| return String::new(); | ||
| }; | ||
| format!( | ||
| "{}{}", | ||
| first.to_ascii_lowercase(), | ||
| chars.collect::<String>() | ||
| ) | ||
| } | ||
|
|
||
| fn upper_first(name: &str) -> String { | ||
| let mut chars = name.chars(); | ||
| let Some(first) = chars.next() else { | ||
| return String::new(); | ||
| }; | ||
| format!( | ||
| "{}{}", | ||
| first.to_ascii_uppercase(), | ||
| chars.collect::<String>() | ||
| ) | ||
| } | ||
|
|
||
| pub(crate) fn snake_case(name: &str) -> String { | ||
| let mut out = String::with_capacity(name.len() + 4); | ||
| for (idx, ch) in name.chars().enumerate() { | ||
| if ch.is_ascii_uppercase() { | ||
| if idx != 0 { | ||
| out.push('_'); | ||
| } | ||
| out.push(ch.to_ascii_lowercase()); | ||
| } else { | ||
| out.push(ch); | ||
| } | ||
| } | ||
| out | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No Duplicate protection, meaning that a
FooHostandFooProviderwould silently emit duplicate keys.Also some tests on this file would help catch edge cases like this one