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

always set default js #280 #292

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

Conversation

artlog
Copy link
Contributor

@artlog artlog commented Jun 4, 2024

No description provided.

- avoid NullPointerException
- always default to a non null defaut scripting implementation
  - default to ErrorMissingScriptEvalauator to fail a first unsupported scripting usage
- try to uee rhino if no js is
   - avoid NullPointerException even if rhino and js are missing
Copy link
Contributor

@rouazana rouazana 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 this contribution!

I understand that this solution should work, but it seems a little too much for me to create an error implementation just to handle the case when the default implementation is null.

If you really think that defaultImplementation can be null in some cases, I think replacing it by an Optional<ScriptableEvaluator> would do the job.
If the optional is empty, failing fast at this point is the good solution IMO.

On the other hand if you think that defaultImplementation should never be null it would be better to ensure it's well initialized in every cases, and also to fail fast if it's not the case.

if ( defaultImplementation != null ) {
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If understand well, this change means if you don't find any "js" implementation, you defaults on "rjs" which is in fact Rhino.
It's hard to understand that when reading your code at first sight. Wouldn't it be possible to make it more obvious?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes you understand well, trying to get the better default implementation.

it might be possible to make it more obvious, but making things obvious is not as obvious as it sound ;-)
Does just a comment would help ?
Or {"js","rjs"} set in a variable named 'preferredDefaultImplementations' ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I would just be more explicit, even if it means repeating myself:

defaultImplementation = instancesTypeCache.get("js");
if (defaultImplementation == null) {
  defaultImplementation = instancesTypeCache.get("rjs");
}

with the optional solution it's even easier to write it:

			defaultImplementation = Optional.ofNullable(instancesTypeCache.get("js"))
					.or(() -> Optional.ofNullable(instancesTypeCache.get("rjs")));

It's very easy and clear (to me at least ^^) how to add new cases if needed later (I guess it's the purpose of your array).

@artlog
Copy link
Contributor Author

artlog commented Jun 14, 2024

Hi @rouazana ,

Thanks for your review.

If you really think that defaultImplementation can be null in some cases, I think replacing it by an Optional would do the job.
If the optional is empty, failing fast at this point is the good solution IMO.

I didn't though about Optional usage when i wrote this change.
I agree this would allow to obtain a NoSuchElementException instead of NullPointer, what is better.
Now i am wondering if replacing it with Optional would require more change at other places like using isPresent() and replacing direct call on implementation method to get() on it. Or do i miss something here ?

On the other hand if you think that defaultImplementation should never be null it would be better to ensure it's well initialized in every cases, and also to fail fast if it's not the case.

Yes i think it should never be null but it should not stop on exception if never used.
Protecting against NullPointerException with a right error when used but not stop at initialisation if never used, what we can't reasonably know at initialisation time.

Script evaluation can occur in very simple case that don't seems to be related to scripting at first sight sometimes even for constants, delegating it to an implementation is there to display error at usage and not at initialisation. In the long run we could even provide a very simple default implementation instead of this MissingError that can handle very simple cases.

It was a catch all implementation, if ever default implementation was not used, failing at first initialisation might be too much.

By example if another scripting language is added that can't fit into default implementation, if all script usages specify explicitly a implementation then it is working smoothly even if no js implementation is found.

@rouazana
Copy link
Contributor

rouazana commented Jul 9, 2024

Hello,

Sorry for the late answer.

If you really think that defaultImplementation can be null in some cases, I think replacing it by an Optional would do the job.
If the optional is empty, failing fast at this point is the good solution IMO.

I didn't though about Optional usage when i wrote this change. I agree this would allow to obtain a NoSuchElementException instead of NullPointer, what is better. Now i am wondering if replacing it with Optional would require more change at other places like using isPresent() and replacing direct call on implementation method to get() on it. Or do i miss something here ?

You can keep the Optional as an internal implementation details and never expose it. I've attached an example.
292-optional-example.patch.txt

With this method you'll keep the right exception you want while not changing the API and not having to implement a dummy class.

On the other hand if you think that defaultImplementation should never be null it would be better to ensure it's well initialized in every cases, and also to fail fast if it's not the case.

Yes i think it should never be null but it should not stop on exception if never used. Protecting against NullPointerException with a right error when used but not stop at initialisation if never used, what we can't reasonably know at initialisation time.

Good point, thank you for the explanation.

@artlog
Copy link
Contributor Author

artlog commented Sep 4, 2024

see #304

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Java 21: GraalJSEngineFactory could not be instantiated
4 participants