groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jochen Theodorou (JIRA)" <j...@apache.org>
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

    [ https://issues.apache.org/jira/browse/GROOVY-8221?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16045444#comment-16045444
] 

Jochen Theodorou commented on GROOVY-8221:
------------------------------------------

{code:Java}
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)
t.bar()
{code}
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
that.

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: https://issues.apache.org/jira/browse/GROOVY-8221
>             Project: Groovy
>          Issue Type: Bug
>          Components: GroovyScriptEngine
>    Affects Versions: 2.4.11
>         Environment: Probably not relevant, but problem at least confirmed on Mac OS
X
>            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
object):
> 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
object):
> class Test {
>   void bar() {
>     println(foo); // Reference to missing variable
>   }
> }
> Test t = new Test()
> t.bar() // Fails due to missing variable



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Mime
View raw message