static void nashornCompiledScript(String code) throws ScriptException, NoSuchMethodException {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("nashorn");

    CompiledScript compiled = ((Compilable) engine).compile(code);

    Object result = null;
    ScriptContext context = new SimpleScriptContext();
    Bindings engineScope = context.getBindings(ScriptContext.ENGINE_SCOPE);
    long total = 0;
    for (int i = 0; i < RUNS; ++i) {
      long start = System.nanoTime();
      for (int j = 0; j < BATCH; ++j) {
        engineScope.put("value", "12345678");
        result = compiled.eval(context);
      }
      long stop = System.nanoTime();
      System.out.println(
          "Run "
              + (i * BATCH + 1)
              + "-"
              + ((i + 1) * BATCH)
              + ": "
              + Math.round((stop - start) / BATCH / 1000)
              + " us");
      total += (stop - start);
    }

    System.out.println("Average run: " + Math.round(total / RUNS / BATCH / 1000) + " us");
    System.out.println(
        "Data is " + ((Invocable) engine).invokeMethod(result, "toString").toString());
  }
  /* Utility functions for the binding classes */
  protected static ScriptEngine createScriptEngine(String language) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName(language);

    ScriptContext scriptContext = engine.getContext();
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    scriptContext.setBindings(new JSFBindings(bindings), ScriptContext.ENGINE_SCOPE);

    return engine;
  }
Пример #3
0
 private static GremlinScriptContext convertContext(final ScriptContext context) {
   if (context instanceof GremlinScriptContext) return (GremlinScriptContext) context;
   else {
     GremlinScriptContext context2 = new GremlinScriptContext();
     for (int scope : context.getScopes()) {
       context2.setBindings(context.getBindings(scope), scope);
     }
     return context2;
   }
 }
Пример #4
0
 private static void typeCastContextBindings(final ScriptContext context) {
   for (int scope : context.getScopes()) {
     Bindings bindings = context.getBindings(scope);
     if (!(bindings instanceof VariableLibrary) && null != bindings) {
       for (String key : bindings.keySet()) {
         Object object = bindings.get(key);
         if (object instanceof Atom) {
           bindings.put(key, ((Atom) object).getValue());
         }
       }
     }
   }
 }
Пример #5
0
  /**
   * Executes the program stored in the <code>CompiledScript</code> object using the supplied <code>
   * Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the associated <code>
   * ScriptEngine</code> during script execution. If bindings is null, then the effect of calling
   * this method is same as that of eval(getEngine().getContext()).
   *
   * <p>. The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer
   * </code> associated with the default <code>ScriptContext</code> of the associated <code>
   * ScriptEngine</code> are used.
   *
   * @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE</code>.
   * @return The return value from the script execution
   * @throws ScriptException if an error occurs.
   */
  public Object eval(Bindings bindings) throws ScriptException {

    ScriptContext ctxt = getEngine().getContext();

    if (bindings != null) {
      SimpleScriptContext tempctxt = new SimpleScriptContext();
      tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
      tempctxt.setBindings(
          ctxt.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE);
      tempctxt.setWriter(ctxt.getWriter());
      tempctxt.setReader(ctxt.getReader());
      tempctxt.setErrorWriter(ctxt.getErrorWriter());
      ctxt = tempctxt;
    }

    return eval(ctxt);
  }
Пример #6
0
 public Object eval(ScriptContext context) throws ScriptException {
   return eval(((LuajContext) context).globals, context.getBindings(ScriptContext.ENGINE_SCOPE));
 }