Passing variable arguments to a java function from javascript using GraalVM

PoojaKamat :

I am trying to call a function available in Corda RPC called startTrackedFlowDynamic which accepts 2 arguments: startTrackedFlowDynamic(logicType: Class<out FlowLogic<T>>, vararg args: Any?) which is packaged in a JAR

The call to this function is made from a Javascript context (using GraalVM to achieve that), I want to call this function and pass the arguments to it obtained from a request object (say, coming from a REST API)

Example: if the request contains an array [::InitiatorA, iouValue], I would want to call startTrackedFlowDynamic like: startTrackedFlowDynamic(::InitiatorA, iouValue)

if the request contains an array [::InitiatorB, abc, xyz] I would want to call startTrackedFlowDynamic like: startTrackedFlowDynamic(::InitiatorB, abc, xyz)

if the request contains an array [::InitiatorC] I would want to call startTrackedFlowDynamic like: startTrackedFlowDynamic(::InitiatorC)

TLDR: I would like to make it as a generic API instead of re-writing for every different Flow call. I want to be able to pass dynamic number of arguments coming from the request object to this function instead of hard coding a fixed number of arguments and having to update it when the number of argument changes

An example of the behaviour I want to replicate:

var func = function () {
    console.log(arguments.length);
    for (var i = 0; i < arguments.length; i++) {
        console.log(arguments[i]);
    }
};

func.apply(null, ['::InitiatorA', 'abc', 'xyz'])

Any suggestions?

PoojaKamat :

It seems to work with the following JavaScript syntax

var argsArray = ['::InitiatorA', 'abc', 'xyz']
startTrackedFlowDynamic(...argsArray)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=83481&siteId=1