groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jochen Theodorou (JIRA)" <>
Subject [jira] [Commented] (GROOVY-8221) Not being able to refer to global variables via scripted methods
Date Sat, 10 Jun 2017 07:39:18 GMT


Jochen Theodorou commented on GROOVY-8221:

trait ConnectWithBinding {
  Binding binding
  def propertyMissing(String name) { binding.getVariable(name) }
  def propertyMissing(String name, value) { binding.setVariable(name,value) }
class Test implements ConnectWithBinding {
   void bar() { println(foo); }
foo = 1
Test t = new Test(binding:binding)
As you see there is a change needed to be done to the class, as well as the constructor. But
then the class can access the binding. 

I think the biggest difference between Groovy and a normal scripting language is that we do
not have a global interpreter state. Which means each eval call is basically independent and
you have to transition the state from one eval to the next. This is also due to the fact that
we have no interpreter in Groovy at all. That means you cannot stop the interpreter at one
point and then continue. That would normally be the case at an input for example. 

Most of the state transitions are done in the context of groovysh (with :set interpreterMode
true).Of course that still  does not provide a real interpreter mode, but it comes near to

then about imports... in general you can write a transform that collects the imports from
one eval call and adds them in the next call. Of course that is not the normal eval call anymore.
groovysh is doing things like that.

> Not being able to refer to global variables via scripted methods
> ----------------------------------------------------------------
>                 Key: GROOVY-8221
>                 URL:
>             Project: Groovy
>          Issue Type: Bug
>          Components: GroovyScriptEngine
>    Affects Versions: 2.4.11
>         Environment: Probably not relevant, but problem at least confirmed on Mac OS
>            Reporter: David Ekholm
> We wish to switch from BeanShell to Groovy as script language (JSR-223) in our desktop
software jAlbum. jAlbum injects a set of global variables into the scriptlets via the Bindings
object of the JSR-223 API. These variables are directly accessible in simple scripts, but
contrary to scripting in BeanShell, these variables are not available to methods declared
within scripted classes.
> Here's what works (assuming that "foo" is passed to the GroovyScriptEngine via the Bindings
> println(foo) // foo being made available to the magically created Script class' run method
> Here's what won't work (foo again being passed to the GroovyScriptEngine via the Bindings
> class Test {
>   void bar() {
>     println(foo); // Reference to missing variable
>   }
> }
> Test t = new Test()
> // Fails due to missing variable

This message was sent by Atlassian JIRA

View raw message