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

Plugin API Surface Refactor #879

Merged
merged 8 commits into from
Feb 13, 2025

Conversation

ianmarmour
Copy link
Contributor

@ianmarmour ianmarmour commented Jan 21, 2025

Description of the change

These changes clean up the interfaces around code generation and plugins so that they will be ready to spin off into their own crate without dramatically increasing the API surface of the new crate. The overview of the changes made are below,

  • Updated - Plugin is now a wrapper around a Vec<u8>.
  • Updated - Generator now stores plugin_kind internally to determine if it's linking to a User/Default/V2 Plugin.
  • Updated - Generator now exposes linking_v2_plugin or linking_default_plugin that can have their visibility controlled with a future feature flag to support the existing cli use cases.
  • Updated - Some files were moved around to ensure they're in the codegen crate rather then cli.
  • Added - CliPlugin wrapper that stores PluginKind information for use with config generation logic in the cli.
  • Removed - CodeGenBuilder was removed as it's behavior is provided by Generator.

Why am I making this change?

To lower the API surface of the plugin/code-generation components in preparation for a future split of that logic into a javy-codegen crate based on @jeffcharles request.

Checklist

  • I've updated the relevant CHANGELOG files if necessary. Changes to javy-cli and javy-plugin do not require updating CHANGELOG files.
  • I've updated the relevant crate versions if necessary. Versioning policy for library crates
  • I've updated documentation including crate documentation if necessary.

@ianmarmour ianmarmour changed the title Generator refactor Plugin API Surface Refactor Jan 21, 2025
@ianmarmour ianmarmour marked this pull request as ready for review January 21, 2025 14:42
@jeffcharles jeffcharles self-requested a review January 21, 2025 15:03
Copy link
Collaborator

@jeffcharles jeffcharles left a comment

Choose a reason for hiding this comment

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

Thanks for the work! I understand it takes time to do and I appreciate you taking the time to do it.

That said, I do have concerns about what's in here. I think there was some misunderstanding around the abstractions I would've liked to see for the Plugin. Specifically, I wanted a richer Plugin struct/abstraction for use in the CLI crate and a different and very slimmed down Plugin struct/abstraction for use in the future codegen crate (specifically in the Generator) with additional properties and gated setters on the Generator to preserve behaviour with a very slimmed down plugin abstraction.

Does that make sense?

crates/cli/src/codegen/builder.rs Outdated Show resolved Hide resolved
crates/cli/src/codegen/builder.rs Outdated Show resolved Hide resolved
crates/cli/src/codegen/mod.rs Outdated Show resolved Hide resolved
crates/cli/src/codegen/mod.rs Outdated Show resolved Hide resolved
crates/cli/src/commands.rs Show resolved Hide resolved
crates/cli/src/codegen/mod.rs Outdated Show resolved Hide resolved
crates/cli/src/codegen/mod.rs Outdated Show resolved Hide resolved
crates/cli/src/plugins.rs Outdated Show resolved Hide resolved
crates/cli/Cargo.toml Outdated Show resolved Hide resolved
}

impl ConfigSchema {
pub(crate) fn from_plugin(plugin: &Plugin) -> Result<Option<ConfigSchema>> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can keep this method on CliPlugin, just not the codegen plugin.

@ianmarmour ianmarmour reopened this Feb 9, 2025
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't see a use case where this was required over just implementing the generator itself as a builder pattern.

Comment on lines 158 to 185
/// Set true if linking with a default plugin module.
pub fn linking_default_plugin(&mut self, value: bool) -> &mut Self {
self.plugin_kind = if value {
plugin::PluginKind::Default
} else {
plugin::PluginKind::None
};

self
}

/// Set true if linking with a V2 plugin module.
pub fn linking_v2_plugin(&mut self, value: bool) -> &mut Self {
self.plugin_kind = if value {
plugin::PluginKind::V2
} else {
plugin::PluginKind::None
};

self
}

/// Runtime configuration options to pass to the module.
pub fn js_runtime_config(&mut self, js_runtime_config: Vec<u8>) -> &mut Self {
self.js_runtime_config = js_runtime_config;
self
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These will all sit behind the feature flag.

Comment on lines 254 to 257
let import_namespace = match self.plugin_kind {
plugin::PluginKind::V2 => "javy_quickjs_provider_v2".to_string(),
_ => self.plugin.import_namespace()?,
};
Copy link
Contributor Author

@ianmarmour ianmarmour Feb 9, 2025

Choose a reason for hiding this comment

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

So this kind of sucks... I'll admit it... but I couldn't think of a better way to keep the import_namespace function in this crate while supporting the existing behavior/tests.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a function like plugin_kind.import_namespace(&plugin) -> String to PluginKind with this implementation? It's moving logic around but at least that way the namespace will be in the plugin module as opposed to the generator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that quite a bit more.


/// Uses the plugin to generate QuickJS bytecode.
impl Plugin {
/// Uses a plugin to generate QuickJS bytecode.
pub fn compile_source(&self, js_source_code: &[u8]) -> Result<Vec<u8>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This probably doesn't matter quite yet but do we want/need this to be public? Do we not just want the Generators generate method to be public instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah a bunch of the methods that were public probably should be changed to pub(super) or pub(crate).

Copy link
Collaborator

@jeffcharles jeffcharles left a comment

Choose a reason for hiding this comment

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

Mostly looks good! Just would like to figure out where the plugin initialization logic should live and fix up some minor things but otherwise it's what I'm looking for structurally.

crates/cli/src/codegen/mod.rs Show resolved Hide resolved
crates/cli/src/codegen/plugin.rs Outdated Show resolved Hide resolved
crates/cli/src/codegen/plugin.rs Outdated Show resolved Hide resolved
crates/cli/src/main.rs Outdated Show resolved Hide resolved
crates/cli/src/codegen/plugin.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@jeffcharles jeffcharles left a comment

Choose a reason for hiding this comment

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

Thank you for your hard work and patience! LGTM!

@jeffcharles jeffcharles self-requested a review February 12, 2025 16:33
@jeffcharles
Copy link
Collaborator

Actually I do need you to run cargo fmt and cargo clippy on all crates. The tests passed but linting failed. And then this should be good to go.

Copy link
Collaborator

@jeffcharles jeffcharles left a comment

Choose a reason for hiding this comment

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

LGTM

@jeffcharles jeffcharles merged commit 3fd7fc9 into bytecodealliance:main Feb 13, 2025
4 checks passed
@ianmarmour ianmarmour deleted the generator-refactor branch February 14, 2025 20:00
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.

2 participants