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,229
  • 3
  • 22
  • 35

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
  • 63,804
  • 18
  • 124
  • 206
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,829
  • 5
  • 38
  • 62
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313