Nashorn 脚本引擎。如何将 "org" 对象从全局范围共享到本地范围?

Nashorn ScriptEngine. How to share "org" object from Global scope to Local scope?

Nashorn 脚本引擎不共享从全局范围到引擎范围的 "com" 和 "org" objects/namespaces。因为 "com" 和 "org" 是引擎可用的 Java 包。

在示例中,我将 aorg 变量放入 engine2 的全局范围。

代码:

public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    Bindings bindings = engine.createBindings();
    bindings.put("a", 3);
    bindings.put("org", 10);
    log("bingings-org", bindings.get("org"));
    manager.setBindings(bindings);

    ScriptEngine engine2 = manager.getEngineByName("JavaScript");
    log("global-a", engine2.getBindings(ScriptContext.GLOBAL_SCOPE).get("a"));
    log("local-a", engine2.get("a"));
    log("global-org", engine2.getBindings(ScriptContext.GLOBAL_SCOPE).get("org"));
    log("local-org", engine2.get("org"));
}
  1. 有没有办法将 "org" 和 "com" 包从全局范围共享到本地范围?
  2. 是否存在描述情况的文档?

搜索脚本全局变量时,搜索顺序为先ENGINE_SCOPE再GLOBAL_SCOPE。 Nashorn 引擎的 ENGINE_SCOPE 绑定是 ECMAScript "global" 对象包装为 javax.script.Bindings 对象。如您所见,ENGINE_SCOPE 具有 "org"、"com" 等的映射 - 它们是 Java 包前缀对象。因此,这些将从 GLOBAL_SCOPE 绑定中隐藏任何 "org"、"com" 等。两种解决方案:

  1. 您有 "context" 变量暴露给脚本。因此,您可以使用相同的方式获取 GLOBAL_SCOPE 映射。

    private static void log(String msg, Object obj) {
        System.out.println(msg + " " + obj);
    }
    
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        Bindings bindings = new SimpleBindings();
        bindings.put("a", 3);
        bindings.put("org", 10);
        log("bingings-org", bindings.get("org"));
        manager.setBindings(bindings);
    
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        log("global-a", engine.getBindings(ScriptContext.GLOBAL_SCOPE).get("a"));
        log("local-a", engine.get("a"));
        log("global-org", engine.getBindings(ScriptContext.GLOBAL_SCOPE).get("org"));
        log("local-org", engine.get("org"));
    
        // Use "context" to access GLOBAL_SCOPE "org" from script
        engine.eval(
            "var GLOBAL_SCOPE = javax.script.ScriptContext.GLOBAL_SCOPE;\n" +
            "var g = context.getBindings(GLOBAL_SCOPE);\n" +
            "print(g.get('org'))"); 
    }
    
  2. 您可以在ENGINE_SCOPE绑定中删除"org"、"com"等。

    private static void log(String msg, Object obj) {
        System.out.println(msg + " " + obj);
    }
    
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        Bindings bindings = new SimpleBindings();
        bindings.put("a", 3);
        bindings.put("org", 10);
        log("bingings-org", bindings.get("org"));
        manager.setBindings(bindings);
    
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        log("global-a", engine.getBindings(ScriptContext.GLOBAL_SCOPE).get("a"));
        log("local-a", engine.get("a"));
        log("global-org", engine.getBindings(ScriptContext.GLOBAL_SCOPE).get("org"));
        log("local-org", engine.get("org"));
        // "org" in ENGINE_SCOPE hides GLOBAL_SCOPE "org".
        // delete the "org" in ENGINE_SCOPE
        // you could delete "org" from Java code as well using
        // "remove" method of Bindings object
        engine.eval("delete org");
        System.out.println("after deleting ENGINE_SCOPE org");
        log("local-org", engine.get("org"));
    }