JavaScript Nashorn, compile once for multiple scopes, like for Rhino

I'm investigating the possibility of moving a huge project based on rhino to nashorn. That said project has thousands of scopes (~instances) running in one rhino compiled script (of 10k+ lines of javascript).

What I know how to do:

  • Multiple nashorn engines will give me multiple/indepedent scopes. Works but comsumes waaaayyyyy too much memory (1000 engines...)
  • eval a string on a nashorn CompiledScript. Works too but makes it nearly impossible to pass complex java objects/instances to the function i want to call.

Example of what I can do with Rhino and want to be able to do with Nashorn:

String SOURCE = "var i = 0; function add(a) { i += a; return i; }";
System.out.println("=== RHINO ===");
Context cx = Context.enter();
cx.setOptimizationLevel(Context.FEATURE_STRICT_EVAL); // optimization level 9

Script script = cx.compileString(SOURCE, "mySource", 1, null); // compile once

Scriptable scope1 = cx.initStandardObjects(); // 1st re-use
Scriptable scope2 = cx.initStandardObjects(); // 2nd re-use

script.exec(cx, scope1);
script.exec(cx, scope2);

Function add1 = (Function) scope1.get("add", scope1);
Function add2 = (Function) scope2.get("add", scope2);

try {
    Object r1 = add1.call(cx, scope1, scope1, new Object[]{6});
    System.out.println("result1=" + r1); // print 6
    r1 = add1.call(cx, scope1, scope1, new Object[]{4});
    System.out.println("result1=" + r1); // print 10
} catch (Exception ex) {
    ex.printStackTrace();
}

try {
    Object r2 = add2.call(cx, scope2, scope2, new Object[]{9});
    System.out.println("result2=" + r2); // prints 9
    r2 = add2.call(cx, scope2, scope2, new Object[]{3});
    System.out.println("result2=" + r2); // prints 12
} catch (Exception ex) {
    ex.printStackTrace();
}

Context.exit();

In the example above, I compile the script once and use multiple scopes to isolate my different execution instances. How can I do such a thing with Nashorn? Keep in mind that in my real project I want to invoke much more complex functions that would take javascript or java instances in parameters.

Thank you in advance for your help.

Answer:1

Consider a function in JavaScript: If val is not defined in the first call, it becomes 0 function someRecursiveFn (item, val) { val = val || 0; ... } How do I assign the same way in Python?...

Consider a function in JavaScript: If val is not defined in the first call, it becomes 0 function someRecursiveFn (item, val) { val = val || 0; ... } How do I assign the same way in Python?...

I'm following this tutorial, implementing Facebook authentication with Node and Passport. Both the tutorial and the docs say to do this: router.get('/auth/facebook/callback', passport.authenticate(...

I'm following this tutorial, implementing Facebook authentication with Node and Passport. Both the tutorial and the docs say to do this: router.get('/auth/facebook/callback', passport.authenticate(...

  1. show user mysql
  2. show user linux
  3. show user privileges mysql
  4. show user groups linux
  5. show user groups
  6. show user
  7. show user permissions mysql
  8. show user ubuntu
  9. show user postgres
  10. show user mariadb
  11. show user groups ubuntu
  12. show user agent
  13. show user oracle
  14. show_user_profile
  15. show user grants mysql
  16. show user library mac
  17. show user mongodb
  18. show user password linux
  19. show user centos
  20. show user postgresql

I want to replace single new lines with one space like if I have string like this: "foo\nbar\nbaz\n\nquux" I want to have "foo bar baz\n\nquux" I've tried this regex: /\n(?!\n)/ but it don't work ...

I want to replace single new lines with one space like if I have string like this: "foo\nbar\nbaz\n\nquux" I want to have "foo bar baz\n\nquux" I've tried this regex: /\n(?!\n)/ but it don't work ...

I am trying to build a real time notifications module that functions in a similar manner to facebook's realtime notifications for example if a user requests friendship of another user a push ...

I am trying to build a real time notifications module that functions in a similar manner to facebook's realtime notifications for example if a user requests friendship of another user a push ...