Discussion:
How to stop the Rhino javascript engine
l***@gmail.com
2007-05-18 21:29:39 UTC
Permalink
Hi,

Does the Rhino engine have an api that can stop the execution of a
script fie in the middle. For example, I have a script file in which
there is an infinte loop. How can I stop the execution in the middle?

Of course, I can stop the jvm which started the Rhino engine to
excecute the script. But maybe that's not a good idea if I started the
Rhino engine programmatically, in this case, I don't not want to stop
the jvm totally because I don't want my java application itself. How
can I keep my java app still running but just kill the Rhino engine's
execution of the script file?

Thanks

Linda
n***@gmail.com
2007-05-19 01:12:32 UTC
Permalink
Post by l***@gmail.com
Hi,
Does the Rhino engine have an api that can stop the execution of a
script fie in the middle. For example, I have a script file in which
there is an infinte loop. How can I stop the execution in the middle?
Of course, I can stop the jvm which started the Rhino engine to
excecute the script. But maybe that's not a good idea if I started the
Rhino engine programmatically, in this case, I don't not want to stop
the jvm totally because I don't want my java application itself. How
can I keep my java app still running but just kill the Rhino engine's
execution of the script file?
Thanks
Linda
Look at ContextFactory.observeInstructionCount (http://www.mozilla.org/
rhino/apidocs/org/mozilla/javascript/
ContextFactory.html#observeInstructionCount(org.mozilla.javascript.Context,
%20int))

--N
Linda Xu
2007-05-21 00:00:49 UTC
Permalink
Hi,

Thanks for your response.

This looks work for terminate a possible infinite loop. So the programmer programmatically preset the longest time between instructions.

But how about the scenario that my script code is suppose to be a long-live process, e.g. monitoring a system log file and process it when there are new data written to the log file; while there is no new data, the script just wait and keep polling after certain time interval. Then a programmer start the script for a debug session from a main java application, he runs the script for some time, collect enough information, then he is ready to stop debugging the script but without stopping his main java application (maybe because the main java app has many other features, starting and showing the debugger GUI is only one of them. So, when the debugger session ended, the main java app should still be running).

For this scenario, the solution shown in the http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/ContextFactory.html#call(org.mozilla.javascript.ContextAction) maybe doesn't work because we can't set the timeout limitation like the sample code:

MyContext mcx = (MyContext)cx;
long currentTime = System.currentTimeMillis();
if (currentTime - mcx.startTime > 10*1000) {
// More then 10 seconds from Context creation time:
// it is time to stop the script.
// Throw Error instance to ensure that script will never
// get control back through catch or finally.
throw new Error();
}

Because during the debugging session, user could purpose stop at a breakpoint for time longer than 10 seconds, at this situation, we don't want to terminate the script execution.

Is there anyway that we can terminate the script when user click a button to 'Stop debugging ...'?

Thank you very much for your assistance.

Linda

Continue reading on narkive:
Loading...