Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bindings support #5

Open
eed3si9n opened this issue Apr 24, 2023 · 6 comments
Open

bindings support #5

eed3si9n opened this issue Apr 24, 2023 · 6 comments

Comments

@eed3si9n
Copy link
Owner

Do you have any plans to add bindings support so that it is possible to pass values in addition to the simple code evaluation?

Originally posted by @krakadzeyau-clarabridge in #4 (comment)

@eed3si9n
Copy link
Owner Author

See the last example in https://eed3si9n.com/eval/.

Eval() by default takes the current classpath -

def apply(): Eval =
new Eval(Nil, currentClasspath, None, None)
, so you should be able to define an object with the bindings and import the object?

I don't see a big upside to implementing binding feature that would effectively to emulate the object creation, which depends on having some specific classpath.

@krakadzeyau-clarabridge

I am not a Scala programmer actually. I only need to pass some code to my Gatling-based performance tests, and this code requires access to objects in the scope in which it is executed. Below is a "model" of the use case:

import com.eed3si9n.eval.Eval

@main
def eed3si9n_eval(): Unit =
    val name = "evaluation"
    val code=s"""
println("Hello " + name)
"""
    val eval = Eval()
    eval.evalInfer(code)

This code throws com.eed3si9n.eval.EvalException: "Not found: name". If your library can help me, I would appreciate it.

@krakadzeyau-clarabridge
Copy link

krakadzeyau-clarabridge commented Apr 24, 2023

To be more specific. This is a hardcoded setUp() method call and another call on the SetUp object it creates:

    setUp(
        plot.inject(. . .))
            .maxDuration(inject_maxDuration)

To be able to inject the code from a Jenkins job parameter I need access to the plot variable during the code evaluation. Is it possible via the library?

@krakadzeyau-clarabridge
Copy link

krakadzeyau-clarabridge commented Apr 24, 2023

An example from Groovy:

int i = 42
Eval.x(i, "println x") // prints "42"

I believe something like this was possible in Scala 2 via the Script Engine, but Scala 3 does not support bindings (I hope it is a temporary issue).

@eed3si9n
Copy link
Owner Author

Here's what I mean:

build.sbt

ThisBuild / scalaVersion := "3.2.0"
libraryDependencies += ("com.eed3si9n.eval" % "eval" % "0.2.0").cross(CrossVersion.full)
run / fork := true

Test.scala

import com.eed3si9n.eval.Eval

object Binding:
  val name = "evaluation"

@main
def main(): Unit =
    val code=s"""
import Binding.*
"Hello " + name
"""
    val eval = Eval()
    val result = eval.evalInfer(code)
    println(result.getValue(this.getClass.getClassLoader))

output

sbt:foo> run
[info] compiling 1 Scala source to /private/tmp/bar/target/scala-3.2.0/classes ...
[info] running (fork) main
[info] Hello evaluation

@krakadzeyau-clarabridge

Thank you! I will try to use it in my test code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants