Rhino到Nashorn - 添加可用于范围

时间:2016-06-16 05:25:03

标签: javascript java rhino nashorn

在Rhino中,我有一个Scriptable bean,如下所示

 /**
 * 
 */
package test.rhino;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.mozilla.javascript.FunctionObject;
import org.mozilla.javascript.Scriptable;

/**
 * @author 
 * 
 */
public class SomeBean implements Scriptable, Map<String,Object> {

    /**
     * The current values for this object.
     */
    private HashMap<String, Object> values = new HashMap<>();

    /**
     * 
     */
    public SomeBean() {
        System.out.println("SomeBean();");
    }

    /*
     * @see org.mozilla.javascript.Scriptable#getClassName()
     */
    @Override
    public String getClassName() {
        return "SomeBean";
    }

    /*
     * @see org.mozilla.javascript.Scriptable#get(java.lang.String,
     * org.mozilla.javascript.Scriptable)
     */
    @Override
    public Object get(String name, Scriptable start) {

        System.out.println("Get is called.");
        System.out.println("Called for this" + name + " and returned :" + values.get(name));

        return values.get(name);
    }

    /*
     * @see org.mozilla.javascript.Scriptable#put(java.lang.String,
     * org.mozilla.javascript.Scriptable, java.lang.Object)
     */
    @Override
    public void put(String name, Scriptable start, Object value) {
        System.out.println("Put is called. Input name: " + name + "\n Input values: " + value);

        values.put(name, value);

    }

    @Override
    public Object get(int index, Scriptable start) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean has(String name, Scriptable start) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean has(int index, Scriptable start) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void put(int index, Scriptable start, Object value) {
        // TODO Auto-generated method stub

    }

    @Override
    public void delete(String name) {
        // TODO Auto-generated method stub

    }

    @Override
    public void delete(int index) {
        // TODO Auto-generated method stub

    }

    @Override
    public Scriptable getPrototype() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setPrototype(Scriptable prototype) {
        // TODO Auto-generated method stub

    }

    @Override
    public Scriptable getParentScope() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setParentScope(Scriptable parent) {
        // TODO Auto-generated method stub

    }

    @Override
    public Object[] getIds() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getDefaultValue(Class<?> hint) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean hasInstance(Scriptable instance) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean containsKey(Object key) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean containsValue(Object value) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Object get(Object key) {
        System.out.println("MAP -- Get is called.");
        // TODO Auto-generated method stub
        return values.get(key);
    }

    @Override
    public Object put(String key, Object value) {
        values.put(key, value);
        System.out.println("MAP -- Put is called.");
        // TODO Auto-generated method stub
        return value;
    }

    @Override
    public Object remove(Object key) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void putAll(Map<? extends String, ? extends Object> m) {
        // TODO Auto-generated method stub

    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    @Override
    public Set<String> keySet() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Collection<Object> values() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Set<java.util.Map.Entry<String, Object>> entrySet() {
        // TODO Auto-generated method stub
        return null;
    }

    private static Scriptable globalPrototype;

    public static void finishInit(Scriptable scope, FunctionObject constructor, Scriptable prototype) {
        System.out.println("finishInit is called.");
        globalPrototype = prototype;
    }

}

我可以将此bean检索为Scriptable对象,并将其传递给已编译脚本的eval()函数,如下所示。

String src5 = "(function(){return Name;})();\n";
Script sc = getCompiledScript(src5);
SomeBean sb = new SomeBean();
sb.put("Name", "Matt Murdock");
Scriptable sp = (Scriptable) sb;
result = script.exec(context, sp);

结果打印Matt Murdock。我们可以在这里直接引用属性,因为SomeBean实例作为范围传递给脚本执行。

我无法在Nashorn中找到相同的方法。我可以使用source来获取CompiledScript实例,但是我不能将SomeBean添加到绑定而不给它一个键,我不能直接在我的JavaScript函数中调用它的成员(Name)。下面的代码抛出错误,这是显而易见的,因为我们无法访问没有前缀键的属性。

NSomeOtherBean nsob = new NSomeOtherBean(); // extends AbstractJSObject
nsob.setMember("Name", "Bruce Wayne");

Bindings binding = engine.createBindings();
binding.put("nsob", nsob);
engine.setBindings(binding, ScriptContext.GLOBAL_SCOPE);
result = engine.eval("(function(){return Name;})();\n",binding);

在Nashorn中是否有办法将NSomeOtherBean实例的属性添加到范围并在不使用前缀的情况下访问它们?就像我们如何在Rhino中做到这一点。