javadelight / delight-rhino-sandbox

A sandbox to execute JavaScript code with Rhino in Java.
Other
38 stars 12 forks source link

Cast Object to String #7

Closed plippe closed 5 years ago

plippe commented 5 years ago

Hi,

Just started playing with your library, and I am wondering if there is a better way to cast the output from .eval to a String.

My current code is the follow:

import delight.rhinosandox.RhinoSandboxes
import org.mozilla.javascript.{Context => Rhino}

object Main extends App { 
    Rhino.enter
    try {
      val obj = RhinoSandboxes
        .create()
        .setInstructionLimit(1000)
        .setMaxDuration(1000)
        .eval("<cmd>", "['foo', 'bar']")

      Rhino.toString(obj)
    } finally {
      Rhino.exit
    }
}

Thanks for your help

mxro commented 5 years ago

Hi Philippe, thank you for your question.

.eval just returns whatever you would receive when using vanilla Rhino. So anything that works for Rhino, should work here as well.

One option might be if you already convert the result into a string in your JS code?

plippe commented 5 years ago

It is more on the Java/Scala side of things. Object, and asInstanceOf, isn't really "safe" code.

I was unable to find anything in the javadoc, and was wondering if I missed anything.

The code above works, but require Context to be entered, and exited. You might of had something magic I could have used.

Anyways, great work on the lib, works wonders.

On Wed, 24 Apr 2019, 23:15 Max Rohde, notifications@github.com wrote:

Hi Philippe, thank you for your question.

.eval just returns whatever you would receive when using vanilla Rhino. So anything that works for Rhino, should work here as well.

One option might be if you already convert the result into a string in your JS code?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/javadelight/delight-rhino-sandbox/issues/7#issuecomment-486448263, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKRKO7PP7TXJK2DWFTEL3TPSDLYLANCNFSM4HIFNCIQ .

mxro commented 5 years ago

Thank you, glad that it is helpful!