use of com.bluenimble.platform.scripting.Scriptable 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);
}
}
Aggregations