Example #1
1
  public static void main(String[] args) {
    // Script Engine instantiation using ServiceProvider - this will
    // look in the classpath for a file
    //  /META-INF/services/javax.script.ScriptEngineFactory
    // where the AbclScriptEngineFactory is registered
    ScriptEngine lispEngine = new ScriptEngineManager().getEngineByExtension("lisp");

    // Alternatively, you can directly instantiate the script engine:

    // ScriptEngineManager scriptManager = new ScriptEngineManager();
    // scriptManager.registerEngineExtension("lisp", new AbclScriptEngineFactory());
    // ScriptEngine lispEngine = scriptManager.getEngineByExtension("lisp");

    // (thanks to Peter Tsenter for suggesting this)

    // Accessing variables
    System.out.println();
    System.out.println("*package* = " + lispEngine.get("*package*"));
    Object someValue = new Object();
    lispEngine.put("someVariable", someValue);
    System.out.println("someVariable = " + lispEngine.get("someVariable"));
    try {
      // Interpretation (also from streams)
      lispEngine.eval("(defun hello (arg) (print (list arg someVariable)) (terpri))");

      // Direct function invocation
      ((Invocable) lispEngine).invokeFunction("hello", "world");

      // Implementing a Java interface in Lisp
      lispEngine.eval("(defun compare-to (&rest args) 42)");
      Comparable c = ((Invocable) lispEngine).getInterface(java.lang.Comparable.class);
      System.out.println("compareTo: " + c.compareTo(null));

      // Compilation!
      lispEngine.eval(
          "(defmacro slow-compiling-macro (arg) (dotimes (i 1000000) (incf i)) `(print ,arg))");

      long millis = System.currentTimeMillis();
      lispEngine.eval("(slow-compiling-macro 42)");
      millis = System.currentTimeMillis() - millis;
      System.out.println("interpretation took " + millis);

      millis = System.currentTimeMillis();
      CompiledScript cs = ((Compilable) lispEngine).compile("(slow-compiling-macro 42)");
      millis = System.currentTimeMillis() - millis;
      System.out.println("compilation took " + millis);

      millis = System.currentTimeMillis();
      cs.eval();
      millis = System.currentTimeMillis() - millis;
      System.out.println("evaluation took " + millis);

      millis = System.currentTimeMillis();
      cs.eval();
      millis = System.currentTimeMillis() - millis;
      System.out.println("evaluation took " + millis);

      // Ecc. ecc.
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (ScriptException e) {
      e.printStackTrace();
    }
  }
  static void nashornInvokeMethod(String code) throws ScriptException, NoSuchMethodException {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("nashorn");

    engine.eval(code);
    Invocable inv = (Invocable) engine;
    JSObject propertiesDict = (JSObject) engine.get("properties");

    Object result = null;
    Object property;
    long total = 0;
    for (int i = 0; i < RUNS; ++i) {
      long start = System.nanoTime();
      for (int j = 0; j < BATCH; ++j) {
        property = propertiesDict.getMember("ssn");
        result = inv.invokeMethod(property, "clean", "12345678");
      }
      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());
  }
Example #3
0
 public static void main(String[] args) throws Exception {
   System.out.println("\nTest7\n");
   File file = new File(System.getProperty("test.src", "."), "Test7.js");
   Reader r = new FileReader(file);
   ScriptEngineManager m = new ScriptEngineManager();
   ScriptEngine eng = Helper.getJsEngine(m);
   if (eng == null) {
     System.out.println("Warning: No js engine found; test vacuously passes.");
     return;
   }
   eng.put("filename", file.getAbsolutePath());
   eng.eval(r);
   String str = (String) eng.get("firstLine");
   // do not change first line in Test7.js -- we check it here!
   if (!str.equals("//this is the first line of Test7.js")) {
     throw new RuntimeException("unexpected first line");
   }
 }
  private void initScript() {
    String scriptName = "src" + File.separator + "jscripts" + File.separator + "CreateWorld.js";
    List<ScriptEngineFactory> list = factory.getEngineFactories();
    File scriptFile = new File(scriptName);

    try {
      FileReader fileReader = new FileReader(scriptFile);
      engine.eval(fileReader);
      fileReader.close();
    } catch (FileNotFoundException e1) {
      System.out.println(scriptName + " not found " + e1);
    } catch (IOException e2) {
      System.out.println("IO issue detected " + scriptName + e2);
    } catch (ScriptException e3) {
      System.out.println("ScriptException in " + scriptName + e3);
    } catch (NullPointerException e4) {
      System.out.println("Null pointer in" + scriptName + e4);
    }
    addGameWorldObject((SceneNode) engine.get("worldAxis"));
  }