scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

JSR223 - Accommodate types of bindings #11691

Open jmetertea opened 4 years ago

jmetertea commented 4 years ago

I try to use JSR223 code using bindings parameters (using JMeter):

 Bindings bindings = engine.createBindings();
 final Logger logger = LoggerFactory.getLogger(JSR223_INIT_FILE);
 bindings.put("log", logger); // $NON-NLS-1$ (this name is fixed)       
 engine.eval(reader, bindings);

bindings parameters can't be used when using Scala engine (in other engines as beanshell,groovy,velocity,... it works)

I expect one of syntax to work in script

log.info("Hello Scala");
$log.info("Hello Scala");

First asked in https://stackoverflow.com/questions/57266997/scala-jsr223-script-using-jmeter-java-context

som-snytt commented 4 years ago

The linked answer is right that downcasting is required. This was discussed when the minimal scripting support was introduced. It probably would not be too difficult for a motivated individual to add a cast in the code text generated to access the bound object. Alternatively, wrap the bound object as Dynamic. The set of bindings is already modeled as Dynamic selection (instead of just a Map). Maybe the original impetus was for it to supply Dynamic objects you could drill into reflectively.

With current and future changes to REPL support, it's an appropriate moment to commit to an improved scripting experience.

nmondal commented 9 months ago

There is a bit more. If we try to use Compile the scala source code as script it would not even compile.

import javax.script.*;

final String code  = "x" ;
Bindings  b = new SimpleBindings();
b.put("x", 42 );
final ScriptEngine engine = ScriptEngineManager.getEngineByName("scala");
CompiledScript compiled = ((Compilable) engine).compile(code); // boom --> compile issue right here...

This will state that variable "x" is not found.