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

Use ServiceLoader to load RegExp and XML implementations #1799

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gbrail
Copy link
Collaborator

@gbrail gbrail commented Jan 12, 2025

  • Use ServiceLoader to load RegExp and XML implementations
  • Don't forget new files

This is a more modern way than just classloading and could allow for some
more flexibility in the future.
@gbrail
Copy link
Collaborator Author

gbrail commented Jan 12, 2025

This PR uses the ServiceLoader to load the implementations of Regexp and XML objects, as opposed to what we do now, which is to load classes based on hard-coded class names. It also moves initialization of the specific objects required by XML to the XML implementation and gets that code out of ScriptRuntime.

I think that this is a bit cleaner than we what did before, and it theoretically lets us replace these implementations in future versions of Rhino by just changing the classpath. On the other hand, in both cases we just take the first service that we find, which is not how ServiceLoader is normally used, since if there is more than one implementation we run the risk of inconsistency.

I'm open to suggestions as to other ways to do this, or whether it's worth it at all, but I like the way that this gets the XML stuff out of the main codebase at least.

This is all leading up to another PR where I'm going to try and use a simpler "lazy loading" mechanism for most of the standard objects to try and keep the loading of the standard library objects as fast as we can.

@gbrail gbrail marked this pull request as ready for review January 13, 2025 06:19
@gbrail
Copy link
Collaborator Author

gbrail commented Jan 15, 2025

I recall that we discussed some of these issues with @rPraml back in #1699. I think that this PR is a simpler one that at least gets closer to the possibility of removing a few unnecessary XML dependencies from the main module.

Copy link
Contributor

@rPraml rPraml left a comment

Choose a reason for hiding this comment

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

This PR should not introduce any behaviour change, so it looks good to me.

XMLLoader loader = loadOneServiceImplementation(XMLLoader.class);
if (loader != null) {
loader.load(scope, sealed);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

CHECKME: Should we log some warning in future, if FEATURE_E4X is enabled (unfortunately, it is by default), but no XMLLoader found?

public XMLLib.Factory getE4xImplementationFactory() {
return getFactory().getE4xImplementationFactory();
XMLLoader loader = ScriptRuntime.loadOneServiceImplementation(XMLLoader.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

INFO: As far as I know, this will create a new instance of XMLLoader every time.

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