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

Extensibility: Adding new commands, new functions #1

Open
buko opened this issue Jun 15, 2019 · 5 comments
Open

Extensibility: Adding new commands, new functions #1

buko opened this issue Jun 15, 2019 · 5 comments

Comments

@buko
Copy link

buko commented Jun 15, 2019

Not sure this project is still under active development but it's certainly piqued some interest. (Nobody likes javascript but that's the alternative.) One question is how to implement extensibility. We'd like to give the user the ability to load packages which would introduce new commands and functions into the shell. Logically, this would involve dynamically defining a new base class in which to evaluate commands and functions. The question is what happens when the user loads another package. Lets say you have two classes that both extend JshBase:

class ShellPackageA extends JshBase { }

class ShellPackageB extends JshBase { }

When the user loads package A we want to change the base class to Package A. But when the user loads Package B we don't want to simply switch over to Package B and lose Package A functionality. Really what we want to do is change ShellPackageB to extend ShelllPackageA. Does this make sense? Is something like this possible?

@aunkrig
Copy link
Member

aunkrig commented Jun 15, 2019 via email

@buko
Copy link
Author

buko commented Jun 16, 2019

In this example, shellPackageACode and shelPackageBCode are the sources for the actual PackageA and PackageB classes that both extend JshBase? So this code actually does change the base class for the existing classes?

So we can start off by writing:

class PackageA extends JshBase { }
class PackageB extends JshBase { }

And the CBE code will produce:

class ShellPackageA extends JshBase { }
class ShellPackageB extends ShellPackageA { }

This is very interesting and exactly what we wanted to do!

The other question that comes up are variables. My understanding from playing with the shell is that variables are basically fields. You can refer to variables like 'wd' as long as the generated class has a field called 'wd'. Is this correct?

I suppose if we then wanted to give the user the ability to assign variables we'd need to generate a new class with that protected field. I'm assuming Janino also has a mechanism for introducing such fields? Can it be done without introducing a whole new class but simply redefining the existing class?

@aunkrig
Copy link
Member

aunkrig commented Jun 19, 2019

Hey there,

I believe I mistook your issue a bit because I missed the fact that it relates to JSH, not JANINO. After all, the first JSH ever, congrats!

Currently, the InteractiveShell hardcodes that the extended class is JshBase. I could either make that configurable, or you could put

import static my.package.ShellPackageA.*;
Import static my.package.ShellPackageB.*;

in your .jshrc file. The only limitation then is that you can access only static members of your ShellPackageA/B classes.

Does that help? Let me know which solution you choose and why.

CU Arno

@buko
Copy link
Author

buko commented Jun 19, 2019 via email

@aunkrig
Copy link
Member

aunkrig commented Jun 19, 2019

I just updated JSH so that it uses more up-to-date versions of de.unkrig.commons and org.codehaus.janino.

My feeling is that it is not ideal to change the "extended class" from JshBase to some other class (which probably extends JshBase) because it is difficult to combine multiple such "packages".

Again, I'd recomment that you use IMPORT STATIC, like so:

$ emptyList();
Line 1, Column 1: A method named "emptyList" is not declared in any enclosing class nor any supertype, nor through a static import
$ import static java.util.Collections.*;
$ emptyList();
$

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

No branches or pull requests

2 participants