Message5417

Author tiago182
Recipients tiago182
Date 2010-01-07.14:09:11
SpamBayes Score 2.097742e-06
Marked as misclassified No
Message-id <1262873352.9.0.00221238394014.issue1536@psf.upfronthosting.co.za>
In-reply-to
Content
Here the test:

import org.junit.Test;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;
import java.io.FileReader;
import java.io.Reader;

public class TestJSR223EvalWithReader {

  @Test public void should_reuse_reader_in_eval() throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();

    final String engineType = "jython";
    final ScriptEngine engine = manager.getEngineByName(engineType);

    final Bindings bindings = new SimpleBindings();
    bindings.put("firstLevelNodes", 10);
    bindings.put("secondLevelNodes", 5);

    engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

    final Reader dfsScript = new FileReader(getDepthFirstSearchPath(engineType));

    for (int i = 1; i <= 10; i++)
      engine.eval(dfsScript);
  }

  private String getDepthFirstSearchPath(final String engineType) {
    if ("jython".equalsIgnoreCase(engineType))
      return "src/test/scripts/py/DepthFirstSearch.py";

    else if ("jruby".equalsIgnoreCase(engineType))
      return "src/test/scripts/rb/DepthFirstSearch.rb";

    else if ("rhino".equalsIgnoreCase(engineType))
      return "src/test/scripts/js/DepthFirstSearch.js";

    else if ("groovy".equalsIgnoreCase(engineType))
      return "src/test/scripts/groovy/DepthFirstSearch.groovy";

    throw new IllegalArgumentException("Invalid engine: " + engineType);
  }
}

= = =

Here the script:

class DFS:

  def __init__(self):
    self.visited_node_counter = 0

  def visitor(self):
    self.visited_node_counter += 1

  def visit(self, node):
    node.accept_visitor(self.visitor)
    for child in node.children: self.visit(child)

class Node:

  def __init__(self):
    self.children = []

  def add_child(self, node):
    self.children.append(node)

  def accept_visitor(self, visitor):
    visitor()

root = Node()

for i in xrange(0, firstLevelNodes):
  root.add_child(Node())

for child in root.children:
  for i in xrange(0, secondLevelNodes): child.add_child(Node())

dfs = DFS()
dfs.visit(root)

result = dfs.visited_node_counter

= = =

Just for the records, the same evaluation works well with other scripting engines, e.g. JRuby, Groovy, Rhino.
History
Date User Action Args
2010-01-07 14:09:13tiago182setrecipients: + tiago182
2010-01-07 14:09:12tiago182setmessageid: <1262873352.9.0.00221238394014.issue1536@psf.upfronthosting.co.za>
2010-01-07 14:09:12tiago182linkissue1536 messages
2010-01-07 14:09:11tiago182create