On May 6, 11:21 pm, Vorxio Mister <[email protected]> wrote:
> Hi, I'm a Rhino newbie ...
> Suppose that I have successfully defined a function in javascript that
> returns a String:
>
> function myfun(s) {
>   s = s + " RECEIVED!";
>   return s;
>
> }
>
> I can call myfun from java using something like this:
>
> String res = (String)(cx.evaluateString(scope, "myfun(\"hello world
> \")", "bzzz", 1, null));
>
> Is there a fastest and optimized way to do this?!?!?
>
> For example retrieve the corresponding javascript FunctionObject and
> use a method "call" (if it exists) ?!?!?

Yes, you can retrieve the function from the scope and call it, which
should be much faster than compiling a new script to do that. The code
would look something like this (Callable being the
org.mozilla.javascript.Callable interface, not the one in
java.util.concurrent):

Callable fn = (Callable) ScriptableObject.getProperty(scope, "myfun");
Object result = fn.call(cx, scope, scope, new Object[] {"hello
world"});

Hannes

> Thank you in advance,
>   Vor

_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to