Leveraging JavaScript's eval method in C#

February 7th, 2010 | 1 comment

One thing I want to support in Shader Toy is the idea of formulaic parameters. For example, any float parameter might define itself in the UI as:

time * 2 + otherParameter^2

Where ‘time’ is a value supplied by my editor for the total running time of the editor and ‘otherParameter’ is some other parameter in the shader code. I want this to work. It’s fairly trivial to do value substitution, replacing those names with other numbers, but to evaluate that at runtime is something that, using just C#, is quite non-trivial.

I started off doing some internet searches to see what can be done. I found that Mono has an Evaluator class built in to leverage the C# compiler and evaluate arbitrary C# code at runtime without the mess of handling your own compiler instance and so forth. Really cool (and I wish Microsoft’s .NET supported that), but a bit overkill for my needs, plus I’d rather not switch runtimes.

Then I found a quick blurb on leveraging JavaScript from C# and using the ‘eval’ method in that language. I gave this a try and am surprised at the simplicity of the solution when you realize what you’re able to do. I decided to test this out by making a command line calculator.

Calculator

First thing you want to do is create an Evaluator.js file and fill it with this code:

package Evaluator
{
  class JSEvaluator
  {
    public static function Eval(expr : String) : String
    {
      return eval(expr);
    }
  }
}

That’s it. It’s the most simplistic wrapper one could imagine. Next, open up a Visual Studio command line or XNA Game Studio command line (both are found in the Start menu in their respective folders). Navigate to your js file and run this command:

jsc /target:library Evaluator.js

That will compile your JavaScript file into a .NET usable DLL file.

Now create a new Windows Console Application project in Visual Studio. First add a reference to Microsoft.JScript.dll which gives us the JavaScript runtime. Then add a reference to the Evaluator.dll file we produced earlier. Next, drop this code into your Program.cs file:

using System;
using Evaluator;

namespace Calculator
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Calculator");
			Console.WriteLine("Enter a mathematical formula for evaluation.");
			Console.WriteLine("Type 'exit' to quit.");
			Console.WriteLine();

			string line = string.Empty;

			do
			{
				Console.Write("> ");

				line = Console.ReadLine();

				if (line == "exit")
					break;

				Console.WriteLine(JSEvaluator.Eval(line));

			} while (true);
		}
	}
}

That’s it. We just call into our JSEvaluator class’s static Eval method and write out the results. Now give it a try and play around, it’s really quite fun. I’m not sure exactly how much it handles, and of course I do no exception handling so it’ll likely break if you give it weird stuff, but it’s fun as a little calculator.

Now I just need to implement it into my Shader Toy. :P


Possibly Related Posts

(Automatically Generated)
sspack your images
Expanding animated Gif images
Sprite Sheet Packer Tool – XNA GS Example
Dynamically Refreshed Assets in XNA
Editing The Default XNA Game Studio Project Templates

  1. February 7th, 2010 at 13:57

    Javascript is just too ubiquitous to be ignored

You must be logged in to post a comment.