Can you not add the stop flag check to the observeInstructionCount callback method? That would be the most straight forward way to stop the script and really take very few lines of code to implement. Can you explain why this wouldn't work in your case? If you're concerned with finding a reference to the stop check flag you can always extend Context and add your own reference to whatever object contains the check you need to perform as nearly demonstrated in this example:
http://www.mozilla.org/rhino/apidocs/org/mozilla/javascript/ContextFactory.h tml David -----Original Message----- From: dev-tech-js-engine-rhino-bounces+davidparks21=yahoo....@lists.mozilla.org [mailto:dev-tech-js-engine-rhino-bounces+davidparks21=yahoo....@lists.mozill a.org] On Behalf Of Patrick Lightbody Sent: Thursday, February 19, 2009 1:28 AM To: [email protected] Subject: Stopping JavaScript Evaluation Is there any way to simply stop the execution of a running script? I've read the docs and archives and understand that the preferred methods are for the script to either throw an exception OR for me to use the observeInstructionCount capability of Rhino. My issue is that neither of those are ideal for my case. I provide a hosted service and I want to allow untrusted end-users to execute arbitrary code written in JavaScript. I've been able to lock down what objects they can talk to, thanks to ClassShutter. I can also use the instruction count trick as one method to stop runaway scripts. But I also have a need to simply stop a script on command at any given moment in time. Calling Thread.interrupt() works well for when the script is engaged in an interruptible operation (ie: Thread.sleep(), file I/O, network calls, etc). But what if the script is in while loop and isn't calling any interruptible code? Maybe I'm trivializing how Rhino works, but because it's evaluated on the fly could there not be some simple "check" that happens after each instruction and looks to see if a stop flag has been set to true? This is of course the simple-but-recommended way to stop things in Java, even since they deprecated Thread.stop(): http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.ht ml I found one email archive basically saying they bit the bullet and called Thread.stop(), but I have a LOT of synchronization going on and the risk that I'd break the multi-threaded code is too high for me to consider it right now. It also doesn't help that the thread is actually managed by an Executor, so I don't actually get a handle to the executing Thread from other threads and instead only get access to either the calling object (w/ references to the Rhino Context) and the Future.cancel(true), which causes an interrupt. I would really appreciate any thoughts on how I can achieve this. Thanks! Patrick _______________________________________________ dev-tech-js-engine-rhino mailing list [email protected] https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino _______________________________________________ dev-tech-js-engine-rhino mailing list [email protected] https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
