Java Code Examples for jdk.nashorn.api.scripting.ScriptObjectMirror#isFunction()

The following examples show how to use jdk.nashorn.api.scripting.ScriptObjectMirror#isFunction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WithObject.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example 2
Source File: WithObject.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example 3
Source File: WithObject.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example 4
Source File: WithObject.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example 5
Source File: WithObject.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example 6
Source File: WithObject.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example 7
Source File: WithObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example 8
Source File: ScriptBridge.java    From karate with MIT License 6 votes vote down vote up
public Object filter(List list, ScriptObjectMirror som) {
    if (list == null) {
        return new ArrayList();
    }
    if (!som.isFunction()) {
        throw new RuntimeException("not a JS function: " + som);
    }
    List res = new ArrayList();
    for (int i = 0; i < list.size(); i++) {
        Object x = list.get(i);
        Object y = som.call(som, x, i);
        if (y instanceof Boolean) {
            if ((Boolean) y) {
                res.add(x);
            }
        } else if (y instanceof Number) { // support truthy numbers as a convenience
            String exp = y + " == 0";
            ScriptValue sv = Script.evalJsExpression(exp, null);
            if (!sv.isBooleanTrue()) {
                res.add(x);
            }
        }
    }
    return res;
}
 
Example 9
Source File: ScriptValueFactoryImpl.java    From purplejs with Apache License 2.0 5 votes vote down vote up
private ScriptValue newValueFromObjectMirror( final ScriptObjectMirror value )
{
    if ( value.isFunction() )
    {
        return new FunctionScriptValue( this, value, this.runtime );
    }

    if ( value.isArray() )
    {
        return new ArrayScriptValue( this, value );
    }

    return new ObjectScriptValue( this, value );
}
 
Example 10
Source File: ScriptBridge.java    From karate with MIT License 5 votes vote down vote up
public Object repeat(int n, ScriptObjectMirror som) {
    if (!som.isFunction()) {
        throw new RuntimeException("not a JS function: " + som);
    }
    List res = new ArrayList();
    for (int i = 0; i < n; i++) {
        Object o = som.call(som, i);
        res.add(o);
    }
    return res;
}
 
Example 11
Source File: ScriptBridge.java    From karate with MIT License 5 votes vote down vote up
public Object map(List list, ScriptObjectMirror som) {
    if (list == null) {
        return new ArrayList();
    }
    if (!som.isFunction()) {
        throw new RuntimeException("not a JS function: " + som);
    }
    List res = new ArrayList(list.size());
    for (int i = 0; i < list.size(); i++) {
        Object y = som.call(som, list.get(i), i);
        res.add(y);
    }
    return res;
}
 
Example 12
Source File: ScriptBridge.java    From karate with MIT License 5 votes vote down vote up
public void forEach(List list, ScriptObjectMirror som) {
    if (list == null) {
        return;
    }
    if (!som.isFunction()) {
        throw new RuntimeException("not a JS function: " + som);
    }
    for (int i = 0; i < list.size(); i++) {
        som.call(som, list.get(i), i);
    }
}
 
Example 13
Source File: ScriptBridge.java    From karate with MIT License 5 votes vote down vote up
public void forEach(Map<String, Object> map, ScriptObjectMirror som) {
    if (map == null) {
        return;
    }
    if (!som.isFunction()) {
        throw new RuntimeException("not a JS function: " + som);
    }
    AtomicInteger i = new AtomicInteger();
    map.forEach((k, v) -> som.call(som, k, v, i.getAndIncrement()));
}
 
Example 14
Source File: Tags.java    From karate with MIT License 5 votes vote down vote up
public boolean isEach(ScriptObjectMirror som) {
    if (!som.isFunction()) {
        return false;
    }            
    for (String s : values) {
        Object o = som.call(som, s);
        ScriptValue sv = new ScriptValue(o);
        if (!sv.isBooleanTrue()) {
            return false;
        }
    }
    return true;
}
 
Example 15
Source File: DFActorManagerJs.java    From dfactor with MIT License 5 votes vote down vote up
private boolean _initCustomScript(File dir){
	boolean bRet = false;
	do {
		LinkedList<File> lsFile = new LinkedList<>();
		_iteratorCustomScript(dir, lsFile);
		Iterator<File> it = lsFile.iterator();
		while(it.hasNext()){
			File f = it.next();
			if(!_checkScriptFileValid(f)){
				return false;
			}
			try {
				_jsEngine.eval(new FileReader(f));
			} catch (FileNotFoundException | ScriptException e) {
				e.printStackTrace();
				return false;
			}
		}
		Bindings bind = _jsEngine.getBindings(ScriptContext.ENGINE_SCOPE);
		Object objEntry = bind.get(_entryActor);
		if(objEntry == null){
			printError("EntryActor not found: "+_entryActor);
			break;
		}
		if(!(objEntry instanceof ScriptObjectMirror)){
			printError("invalid EntryActor class: "+objEntry.getClass());
			break;
		}
		ScriptObjectMirror mir = (ScriptObjectMirror) objEntry;
		if(!mir.isFunction()){
			printError("invalid EntryActor type: not function");
			break;
		}
		
		bRet = true;
	} while (false);
	return bRet;
}
 
Example 16
Source File: DFJsActor.java    From dfactor with MIT License 5 votes vote down vote up
private boolean _checkFunction(String name){
	Object obj = _js.getMember(name);
	if(obj != null && !ScriptObjectMirror.isUndefined(obj)){
		ScriptObjectMirror mir = (ScriptObjectMirror) obj;
		if(mir.isFunction()){
			return true;
		}
	}
	return false;
}
 
Example 17
Source File: DFJsUtil.java    From dfactor with MIT License 5 votes vote down vote up
public static boolean isJsFunction(Object func){
	if(func != null && func instanceof ScriptObjectMirror){
		ScriptObjectMirror mir = (ScriptObjectMirror) func;
		return mir.isFunction();
	}
	return false;
}
 
Example 18
Source File: ScriptBridge.java    From karate with MIT License 4 votes vote down vote up
public Object listen(long timeout, ScriptObjectMirror som) {
    if (!som.isFunction()) {
        throw new RuntimeException("not a JS function: " + som);
    }
    return context.listen(timeout, () -> Script.evalJsFunctionCall(som, null, context));
}
 
Example 19
Source File: ScriptValue.java    From karate with MIT License 4 votes vote down vote up
public ScriptValue(Object value, String source) {
    // pre-process and convert any nashorn js objects into vanilla Map / List
    if (value instanceof ScriptObjectMirror) {
        ScriptObjectMirror som = (ScriptObjectMirror) value;
        if (!som.isFunction()) {
            value = JsonUtils.toJsonDoc(value).read("$"); // results in Map or List
            if (value instanceof Map) {
                Map map = (Map) value;
                retainRootKeyValuesWhichAreFunctions(som, map, true);
            }
        }
    }
    this.value = value;
    this.source = source;
    if (value == null) {
        type = Type.NULL;
    } else if (value instanceof DocumentContext) {
        DocumentContext doc = (DocumentContext) value;
        listLike = doc.json() instanceof List;
        mapLike = !listLike;
        type = Type.JSON;
    } else if (value instanceof Node) {
        mapLike = true;
        type = Type.XML;
    } else if (value instanceof List) {
        listLike = true;
        type = Type.LIST;
    } else if (value instanceof ScriptObjectMirror) { // has to be before Map
        type = Type.JS_FUNCTION;
    } else if (value instanceof Map) {
        mapLike = true;
        type = Type.MAP;
    } else if (value instanceof String) {
        type = Type.STRING;
    } else if (value instanceof byte[]) {
        type = Type.BYTE_ARRAY;
    } else if (value instanceof InputStream) {
        type = Type.INPUT_STREAM;
    } else if (Script.isPrimitive(value.getClass())) {
        type = Type.PRIMITIVE;
    } else if (value instanceof Feature) {
        type = Type.FEATURE;
    } else if (value instanceof Function) {
        type = Type.JAVA_FUNCTION;
    } else {
        type = Type.UNKNOWN;
    }
}