ScriptEngineContext continued

I’m using the JSR223 engine inside JRuby 1.6.2.

In the following example, I declare a method using a new context. I
would think that method would then only be useable when specifying
that context. However, I can execute that method:

  • using a completely separate context
  • just using the engine’s built-in context

Shouldn’t the declaration be isolated to contextA?

=============
import javax.script._

val scriptManager = new ScriptEngineManager
val engine = scriptManager.getEngineByName(“jruby”)

// Evaluate method declaration against contextA
val contextA = new SimpleScriptContext
engine.eval(“def helloWorld; “Hello world!”; end”, contextA)

// Should print “Hello World!”
println("Evaling withContextA: " + engine.eval(“helloWorld”, contextA))

// I would think this should blow up, since contextB shouldn’t have
this method definition
// since I evaluated it against contextA
val contextB = new SimpleScriptContext
println("Evaling withContextB: " + engine.eval(“helloWorld”, contextB))

// I would think this should blow up, since evaluting against just the
default engine context
println("Evaling withEngine: " + engine.eval(“helloWorld”))

For further information, this code does blow up as I expect when using
Javascript:

import javax.script._

val scriptManager = new ScriptEngineManager
val engine = scriptManager.getEngineByName(“javascript”)

// Evaluate method declaration against contextA
val contextA = new SimpleScriptContext
engine.eval(“function helloWorld() {return “Hello world!”;}”,
contextA)

// Should print “Hello World!”
println("Evaling withContextA: " + engine.eval(“helloWorld();”,
contextA))

// I would think this should blow up, since contextB shouldn’t have
this method definition
// since I evaluated it against contextA
val contextB = new SimpleScriptContext
println("Evaling withContextB: " + engine.eval(“helloWorld();”,
contextB))

Matt H. wrote in post #1004235:

For further information, this code does blow up as I expect when using
Javascript:

import javax.script._

val scriptManager = new ScriptEngineManager
val engine = scriptManager.getEngineByName(“javascript”)

// Evaluate method declaration against contextA
val contextA = new SimpleScriptContext
engine.eval(“function helloWorld() {return “Hello world!”;}”,
contextA)

// Should print “Hello World!”
println("Evaling withContextA: " + engine.eval(“helloWorld();”,
contextA))

Appears the old wiki has some example codes that the new doesn’t:

http://kenai.com/projects/redbridge/pages/Home

I’m not entirely sure but searching for
“org.jruby.embed.localcontext.scope” might be helpful.
-roger

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs