1

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.

MarzSocks
  • 4,041
  • 3
  • 21
  • 32

2 Answers2

3

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.

Matías Fidemraizer
  • 61,574
  • 17
  • 117
  • 195
1

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.

Oliver Bock
  • 4,535
  • 4
  • 36
  • 58
Matthew Layton
  • 35,375
  • 44
  • 163
  • 278