groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jochen Theodorou" <blackd...@gmx.org>
Subject Aw: Re: Remembering imports between script invocations
Date Mon, 05 Nov 2018 12:24:26 GMT
<html><head></head><body><div style="font-family: Verdana;font-size:
12.0px;"><div>let us assume we add a setConfig method to GroovyScriptEngineImpl...
how would you call it?</div>

<div>&nbsp;</div>

<div>
<pre><code data-lang="java">ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName(&quot;groovy&quot;);
</code>GroovyScriptEngineImpl groovyEngine = (GroovyScriptEngineImpl) engine;
groovyEngine.setConfig(myConfig);</pre>

<div>which means you will require Groovy specific code to do this. And in that case
there is always the question why you are not using the Groovy classes directly then.</div>

<div>&nbsp;</div>

<div>But frankly... how about instead writing a small wrapper for GroovyShell or GroovyScriptEngine,
that does implement ScriptEngine and register a factory? If you do not need support for ScriptContext,
then this would be easy.</div>

<div>&nbsp;</div>

<div>And then there would be also the option to do it similar&nbsp;to&nbsp;#jsr223.groovy.engine.keep.globals
and make a special attribute in the engine context for GroovyScriptEngineImpl to let it set
the additional imports.</div>

<div>&nbsp;</div>

<div>bye Jochen</div>
</div>

<div>&nbsp;</div>

<div>
<div name="quote" style="margin:10px 5px 5px 10px; padding: 10px 0 10px 10px; border-left:2px
solid #C3D9E5; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="margin:0 0 10px 0;"><b>Gesendet:</b>&nbsp;Donnerstag, 01.
November 2018 um 16:19 Uhr<br/>
<b>Von:</b>&nbsp;&quot;Keith Suderman&quot; &lt;suderman@cs.vassar.edu&gt;<br/>
<b>An:</b>&nbsp;dev@groovy.apache.org<br/>
<b>Betreff:</b>&nbsp;Re: Remembering imports between script invocations</div>

<div name="quoted-content">
<div>Hi David,
<div>&nbsp;</div>

<div>I am not a Groovy committer and I am not that familiar with the Groovy code base
so _I_ can&#39;t revise the JSR-223 implementation. &nbsp;I did take a quick look
at the code, and other than having &quot;GroovyScriptEngine&quot; in their names the
two classes (GroovyScriptEngine and GroovyScriptEngineImpl) have nothing in common and parse/load
Groovy code in completely different ways. &nbsp;I suspect adding a feature like you want
would be a non-trivial undertaking. &nbsp;Maybe one of the active committers (Paul, Daniel,
et al) &nbsp;can comment.</div>

<div>&nbsp;</div>

<div>However, I suspect that the GroovyScriptEngineImpl class is &quot;crippled&quot;
by JSR-223 itself as it caters to the lowest common denominator</div>

<div>&nbsp;</div>

<div>- Keith
<div>&nbsp;
<blockquote>
<div>On Oct 31, 2018, at 12:12 PM, David Ekholm &lt;<a href="mailto:david@jalbum.net"
onclick="parent.window.location.href=&#39;mailto:david@jalbum.net&#39;; return false;"
target="_blank">david@jalbum.net</a>&gt; wrote:</div>
&nbsp;

<div>
<div>Hi Keith,
<div>&nbsp;</div>

<div>I&#39;m &quot;resurrecting&quot; this old thread in an attept to see
if we can use Groovy as the next scritpting language in jAlbum. What I&#39;d <i>really</i>&nbsp;want
to achive is to supply a number of default star imports. This is expecially important as Groovy
sadly doesn&#39;t remember imports between script invocations.</div>

<div>&nbsp;</div>

<div>I looked at your code suggetion below and tried to fit it to JSR-223 (javax.script
API) but it fails as the GroovyScriptEngineImpl class I get from that API doesn&#39;t
extend the GroovyScriptEngine class. It&#39;s only the&nbsp;GroovyScriptEngine class
that supplies the important&nbsp;setConfig method.</div>

<div>&nbsp;</div>

<div>Could you revise the JSR-223 implementation so it isn&#39;t unnecessary crippled
compared to the GroovyShell API, at very least add the setConfig method to the GroovyScriptEngineImpl
class? If we&#39;re to use the GroovyShell API, then we need to introduce our own abstraction
layer in order to continue supporting multiple scripting languages in jAlbum.</div>

<div>&nbsp;</div>

<div>Regards</div>

<div>/David, jAlbum founder</div>

<div>&nbsp;
<div>
<div>
<blockquote>
<div>On 10 Feb 2018, at 21:25, Keith Suderman &lt;<a href="mailto:suderman@anc.org"
onclick="parent.window.location.href=&#39;mailto:suderman@anc.org&#39;; return false;"
target="_blank">suderman@anc.org</a>&gt; wrote:</div>
&nbsp;

<div>
<div>
<div style="margin: 0.0px;line-height: normal;">Import statements are really just shortcuts
to tell the compiler how to resolve class names so there is nothing to &quot;remember&quot;
between invocations, that is, nothing gets added to the Binding.</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp;</div>

<div style="margin: 0.0px;line-height: normal;">I am not familiar with the javax.script
API, but I suspect that you will have to provide your own ScriptEngine implementation as you
will need to modify the CompilerConfiguration object the GroovyShell is using to compile the
Groovy code.&nbsp; For example:</div>

<div style="margin: 0.0px;line-height: normal;min-height: 17.0px;">&nbsp;</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
CompilerConfiguration configuration = new CompilerConfiguration()</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
GroovyShell compiler = new GroovyShell(configuration)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
println compiler.evaluate(&#39;json=&#92;&#39;{&quot;foo&quot;:&quot;bar&quot;}&#92;&#39;&#39;)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
println compiler.evaluate(&quot;groovy.json.JsonOutput.prettyPrint(json)&quot;)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp;</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
/* Fails: MissingPropertyException */</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
/* println compiler.evaluate(&quot;JsonOutput.prettyPrint(json)&quot;) */</div>

<div style="margin: 0.0px;line-height: normal;min-height: 17.0px;">&nbsp;</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
ImportCustomizer imports = new ImportCustomizer()</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
imports.addStarImports(&quot;groovy.json&quot;)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
configuration.addCompilationCustomizers(imports)</div>

<div style="margin: 0.0px;line-height: normal;">// Works, the compiler can now resolve
groovy.json.JsonOutput</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
println compiler.evaluate(&quot;JsonOutput.prettyPrint(json)&quot;)</div>

<div style="margin: 0.0px;line-height: normal;min-height: 17.0px;">&nbsp;</div>

<div style="margin: 0.0px;line-height: normal;">The difficult part will be &quot;hooking&quot;
into the compiler to know when an import statement is used so you can update your CompilerConfiguration
object.&nbsp; I am sure the really clever programmers here could come up with some sort
of AST transform that would do this.&nbsp; However, depending on what you are allowed
to do one option would be to tell your users that you have a &quot;Groovy DSL&quot;
and then implement an &quot;include&quot; method that users would use to &quot;import&quot;
Java packages.</div>

<div style="margin: 0.0px;line-height: normal;min-height: 17.0px;">&nbsp;</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
println eval(&#39;json=&#92;&#39;{&quot;foo&quot;:&quot;bar&quot;}&#92;&#39;&#39;)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
println eval(&#39;include &quot;groovy.json&quot;&#39;)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
println eval(&#39;JsonOutput.prettyPrint(json)&#39;)</div>

<div style="margin: 0.0px;line-height: normal;min-height: 17.0px;">&nbsp;</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; Object eval(String
code) {</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
Script script = compiler.parse(code)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
ExpandoMetaClass metaClass = new ExpandoMetaClass(script.class, false)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
metaClass.include = { String name -&gt;</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; ImportCustomizer includes = new ImportCustomizer()</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; includes.addStarImports(name)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; configuration.addCompilationCustomizers(includes)</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &quot;Imported &#36;name&quot;</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
}</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
metaClass.initialize()</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
script.metaClass = metaClass</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;
script.run()</div>

<div style="margin: 0.0px;line-height: normal;">&nbsp; &nbsp; }</div>

<div style="margin: 0.0px;line-height: normal;min-height: 17.0px;">&nbsp;</div>

<div style="margin: 0.0px;line-height: normal;">Hopefully that gives you some ideas.</div>

<div style="margin: 0.0px;line-height: normal;min-height: 17.0px;">&nbsp;</div>

<div style="margin: 0.0px;line-height: normal;">Cheers,</div>

<div style="margin: 0.0px;line-height: normal;">Keith</div>

<div>&nbsp;</div>

<div>
<blockquote>
<div>On Feb 8, 2018, at 3:47 PM, David Ekholm &lt;<a href="mailto:david@jalbum.net"
onclick="parent.window.location.href=&#39;mailto:david@jalbum.net&#39;; return false;"
target="_blank">david@jalbum.net</a>&gt; wrote:</div>
&nbsp;

<div>
<div>How do I do that via the javax.script API?
<div>&nbsp;</div>

<div>Even if this is possible via the javax.script API, chances are that a user wishes
to ad-hoc add another import, but as they are forgotten between script invocations, it makes
it hard to use Groovy to interactively create, say a Swing or JavaFX UI one line at a time.
With BeanShell, the user can add the needed imports, execute that &quot;script&quot;
and then continue to refer to the imported classes in the following script invocations. Making
Groovy remember imports would make it behave in as nice fashion as the new JShell tool in
Java 9. JShell unfortunately cannot run embedded via the javax.script API :-(</div>

<div>&nbsp;</div>

<div>Regards</div>

<div>/David</div>

<div>&nbsp;
<div>
<blockquote>
<div>On 8 Feb 2018, at 21:34, <a href="mailto:eric.milles@thomsonreuters.com" onclick="parent.window.location.href=&#39;mailto:eric.milles@thomsonreuters.com&#39;;
return false;" target="_blank">eric.milles@thomsonreuters.com</a> wrote:</div>
&nbsp;

<div>
<div class="WordSection1" style="page: WordSection1;font-family: Helvetica;font-size: 16.0px;font-style:
normal;font-weight: normal;letter-spacing: normal;text-indent: 0.0px;text-transform: none;white-space:
normal;word-spacing: 0.0px;">
<div style="font-size: 12.0pt;font-family: &quot;Times New Roman&quot; , serif;"><span
style="font-size: 11.0pt;font-family: Calibri , sans-serif;color: rgb(31,73,125);">You
can add all the imports you want to your compiler configuration and they will be consistently
available for all scripts.</span></div>

<div style="font-size: 12.0pt;font-family: &quot;Times New Roman&quot; , serif;"><span
style="font-size: 11.0pt;font-family: Calibri , sans-serif;color: rgb(31,73,125);">&nbsp;</span></div>

<div>
<div style="border-style: solid none none;border-top-width: 1.0pt;border-top-color: rgb(181,196,223);padding:
3.0pt 0.0in 0.0in;">
<div style="font-size: 12.0pt;font-family: &quot;Times New Roman&quot; , serif;"><b><span
style="font-size: 10.0pt;font-family: Tahoma , sans-serif;">From:</span></b><span
style="font-size: 10.0pt;font-family: Tahoma , sans-serif;"><span class="Apple-converted-space">&nbsp;</span>David
Ekholm [<a href="mailto:david@jalbum.net" onclick="parent.window.location.href=&#39;mailto:david@jalbum.net&#39;;
return false;" style="color: purple;text-decoration: underline;" target="_blank">mailto:david@jalbum.net</a>]<span
class="Apple-converted-space">&nbsp;</span><br/>
<b>Sent:</b><span class="Apple-converted-space">&nbsp;</span>Thursday,
February 08, 2018 2:12 PM<br/>
<b>To:</b><span class="Apple-converted-space">&nbsp;</span><a
href="mailto:dev@groovy.apache.org" onclick="parent.window.location.href=&#39;mailto:dev@groovy.apache.org&#39;;
return false;" style="color: purple;text-decoration: underline;" target="_blank">dev@groovy.apache.org</a><br/>
<b>Subject:</b><span class="Apple-converted-space">&nbsp;</span>Remembering
imports between script invocations<span class="Apple-converted-space">&nbsp;</span></span></div>
</div>
</div>

<div style="font-size: 12.0pt;font-family: &quot;Times New Roman&quot; , serif;">&nbsp;</div>

<div style="font-size: 12.0pt;font-family: &quot;Times New Roman&quot; , serif;">We&#39;re
considering supporting Groovy as an additional scripting language to our web gallery software
jAlbum (<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__jalbum.net&amp;d=DwMFAg&amp;c=4ZIZThykDLcoWk-GVjSLmy8-1Cr1I4FWIvbLFebwKgY&amp;r=tPJuIuL_GkTEazjQW7vvl7mNWVGXn3yJD5LGBHYYHww&amp;m=39n_uU4e7-n_s_WwrNC_8tQYLfhWKgmT_dDWwJw8ctA&amp;s=jGwsu2zf5Pm3kG3GKLFFxalyi30aoXq_-izsMrEy_iQ&amp;e="
style="color: purple;text-decoration: underline;" target="_blank">http://jalbum.net</a>),
but one aspect bugs me: It doesn&#39;t seem like import statements are remembered between
script invocations. This makes it far harder to use Groovy to prototype UIs within jAlbum&#39;s
scripting console than for instance BeanShell (using the javax.script API). We currently support
the slow BeanShell scripting language and JavaScript. BeanShell behaves well in this regard,
remembering earlier imported packages between script invocations. Can this be added to Groovy
or is there some API flag we can set?</div>

<div>
<div style="font-size: 12.0pt;font-family: &quot;Times New Roman&quot; , serif;">&nbsp;</div>
</div>

<div>
<div style="font-size: 12.0pt;font-family: &quot;Times New Roman&quot; , serif;">Regards</div>
</div>

<div>
<div style="font-size: 12.0pt;font-family: &quot;Times New Roman&quot; , serif;">/David,
jAlbum founder and client lead developer.</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote>
</div>
&nbsp;

<div>
<div>
<div style="font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight: normal;letter-spacing:
normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing: 0.0px;">----------------------</div>

<div style="font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight: normal;letter-spacing:
normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing: 0.0px;">Keith
Suderman</div>

<div style="font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight: normal;letter-spacing:
normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing: 0.0px;">Research
Associate</div>

<div style="font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight: normal;letter-spacing:
normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing: 0.0px;">Department
of Computer Science</div>

<div style="font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight: normal;letter-spacing:
normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing: 0.0px;">Vassar
College, Poughkeepsie NY</div>

<div style="font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight: normal;letter-spacing:
normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing: 0.0px;"><a
href="mailto:suderman@cs.vassar.edu" onclick="parent.window.location.href=&#39;mailto:suderman@cs.vassar.edu&#39;;
return false;" target="_blank">suderman@cs.vassar.edu</a></div>

<div style="font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight: normal;letter-spacing:
normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing: 0.0px;">&nbsp;</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
&nbsp;

<div>
<div>
<div style="color: rgb(0,0,0);font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight:
normal;letter-spacing: normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing:
0.0px;">----------------------</div>

<div style="color: rgb(0,0,0);font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight:
normal;letter-spacing: normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing:
0.0px;">Keith Suderman</div>

<div style="color: rgb(0,0,0);font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight:
normal;letter-spacing: normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing:
0.0px;">Research Associate</div>

<div style="color: rgb(0,0,0);font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight:
normal;letter-spacing: normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing:
0.0px;">Department of Computer Science</div>

<div style="color: rgb(0,0,0);font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight:
normal;letter-spacing: normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing:
0.0px;">Vassar College, Poughkeepsie NY</div>

<div style="color: rgb(0,0,0);font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight:
normal;letter-spacing: normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing:
0.0px;"><a href="mailto:suderman@cs.vassar.edu" onclick="parent.window.location.href=&#39;mailto:suderman@cs.vassar.edu&#39;;
return false;" target="_blank">suderman@cs.vassar.edu</a></div>

<div style="color: rgb(0,0,0);font-family: Helvetica;font-size: 14.0px;font-style: normal;font-weight:
normal;letter-spacing: normal;text-indent: 0.0px;text-transform: none;white-space: normal;word-spacing:
0.0px;">&nbsp;</div>
</div>
</div>
</div>
</div>
</div>
</div></div></body></html>

Mime
View raw message