Is it possible to sandbox a JavaScript function in C# code, execute it by passing in an argument such as an array, and return a result. No web browser involved here, this is not an Ajax or 'registerstartupscript' type of question.
2 Answers
From my own experience, for simple things Jurassic works like a charm and it's a JavaScript that can be used to run-time compile JavaScript and call functions, variables or whatever.
For example, taken from their own doc pages:
var engine = new Jurassic.ScriptEngine();
Console.WriteLine(engine.Evaluate("5 * 10 + 2"));
There're other options which involves embedding a full JavaScript engine, but AFAIK and reading your question's requirement, it seems like Jurassic should work in your scenario and you get the advantage of using a managed JavaScript compiler written in C# so there's no other dependency than the BCL from .NET and Jurassic itself.
2 Comments
Yes, you can!
JavaScript.NET is a .NET port of Google's V8 engine.
Another one I quite like is called Jurassic and it's available on nuget.