|
Any hints on how to get the underlying GroovyShell instance to remember things? Seems like it doesn't do that at all right now. Perhaps the SourceUnit needs to be inspected and then use that information to pull out bits from the compiled script... er something? As you'll evaluate new scripts with GroovyShell, you can pass a binding containing variables. So in this binding, you can put MethodClosures wrapping methods defined in previous scripts. Okay, and I'm guessing that previously defined classes should already stick na? What about things like: def foo = "bar"
And more so... how does one figure out what methods (and or simple defs like above) were defined? Seems that the binding does not capture those (unfortunately IMO), so I'm probably going to have to use the SourceUnit to track those down? def foo = "bar"
is a local variable, so we won't be able to capture it and put into the binding, but: foo = "bar"
will go in the binding of the script by default. For methods, as if you evaluate this script: def err(msg) { System.err.println(msg) }
you will have to introspect the Script class that is compiled to retrieve the methods that have been defined on it. Then, you wrap that in a MethodClosure that you keep inside the binding as 'err' (for the key), and the method closure for the value, so that later on, people can call err('hi') (in fact, the script will retrieve the err property in the binding, and as it's a closure, will call it) The trickier thing is probably how to memorize the imports :-/ The last thing also that would be needed would be to remember class definitions, so that you can define a class in a first script: class A {}
and then in another script execution being able to create new instances: new A()
There might be some tricks with classloaders to make that possible though, and I'm not sure yet how to best do that. This is done, enable with NEWSHELL=t ./bin/groovysh atm. |
|||||||||||||||||||||||||||||||||||||||||
Requirements: