I'd like to run some arbitrary user code in a Silverlight application. Of course I want to embed a Turing-complete language (easy enough), but don't want to allow the user to lock up their browser if they write some bad (non-terminating) code. I'm not asking to solve the Halting Problem, just run this user code on a background thread and terminate it at the press of a button.
I can't use Thread.Abort in Silverlight even if I want to, so how can I interrupt the background interpreter thread?
My current ideas:
- If I interpret some language by hand, of course I can stop execution when I want
- I could use Reflection.Emit or compile an expression tree, and insert similar checks to do early termination
- Modify an existing compiler to generate code that does this (F# or IronPython maybe?)
- Postprocess IL generated by an existing tool (Does this rule out DLR based languages?)
I can't help think there has to be a simpler solution.
Modifying IronPython or IronRuby to do this wouldn't be too difficult. Ultimately you'll just need to write a ExpressionVisitor which inserts polling at back branches (loops, maybe gotos) and method calls. This should be pretty easy, for example handling loops could look like:
For IronPython you could then run this expression visitor on the lambda provided when FunctionCode objects are constructed.