Search in sources :

Example 1 with Referenceable

use of com.bluenimble.platform.Referenceable in project serverless by bluenimble.

the class DefaultScriptingEngine method invoke.

@Override
public Object invoke(Object scriptable, String function, Object... args) throws ScriptingEngineException {
    if (scriptable == null) {
        scriptable = platform;
    }
    if (!(scriptable instanceof ScriptObjectMirror)) {
        throw new ScriptingEngineException("object is not a valid scriptable object");
    }
    ScriptObjectMirror som = (ScriptObjectMirror) scriptable;
    if (args != null && args.length > 0) {
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            if (arg == null || arg instanceof ScriptObjectMirror) {
                continue;
            }
            Class<?> type = arg.getClass();
            Scriptable ann = type.getAnnotation(Scriptable.class);
            if (ann == null) {
                continue;
            }
            Object jsArg = null;
            if (Referenceable.class.isAssignableFrom(arg.getClass())) {
                jsArg = ((Referenceable) arg).getReference();
            }
            if (jsArg == null) {
                jsArg = platform.callMember(ann.name(), arg);
                if (Referenceable.class.isAssignableFrom(arg.getClass())) {
                    ((Referenceable) arg).setReference(jsArg);
                }
            }
            args[i] = jsArg;
        }
    }
    try {
        return som.callMember(function, args);
    } catch (Throwable err) {
        throw new ScriptingEngineException(err.getMessage(), err);
    }
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) Referenceable(com.bluenimble.platform.Referenceable) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) Scriptable(com.bluenimble.platform.scripting.Scriptable)

Aggregations

Referenceable (com.bluenimble.platform.Referenceable)1 Scriptable (com.bluenimble.platform.scripting.Scriptable)1 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)1 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1