Integer comparison fails?

Feb 15, 2010 at 6:36 PM

Hi all,

The following code, which I would think compares 4 to itself three times, does not always indicate that 4 == 4.  I tried to remove any opportunity for copy/paste errors by using formatting to a ridiculous degree, but I got the same results.

static void Main(string[] args)
{
    Jint.JintEngine engine = new Jint.JintEngine();
    int integerObject = 4;
    string name = "IntegerObject";
    engine.SetParameter(name, integerObject);
    Console.Out.WriteLine("{0} == {1} : {2}", engine.Run(name), engine.Run("4"), engine.Run(String.Format("{0} == {1}", name, 4)));
    Console.Out.WriteLine("{0} == {1} : {2}", engine.Run("4"), engine.Run("4"), engine.Run(String.Format("{0} == {1}", 4, 4)));
    Console.Out.WriteLine("{0} == {1} : {2}", engine.Run(name), engine.Run(name), engine.Run(String.Format("{0} == {1}", name, name)));

    Console.Out.WriteLine("{0} == {1} : {2}", engine.Run("IntegerObject"), engine.Run("4"), engine.Run("IntegerObject == 4"));
    Console.Out.WriteLine("{0} == {1} : {2}", engine.Run("4"), engine.Run("4"), engine.Run("4 == 4"));
    Console.Out.WriteLine("{0} == {1} : {2}", engine.Run("IntegerObject"), engine.Run("IntegerObject"), engine.Run("IntegerObject == IntegerObject"));

    Console.Out.Write("Press the [Any] key to continue.");
    Console.ReadKey();
}

I get the following output:

 

4 == 4 : False
4 == 4 : True
4 == 4 : True
4 == 4 : False
4 == 4 : True
4 == 4 : True
Press the [Any] key to continue.

If I use the interactive debugger to see what engine.Run("IntegerObject") is, I get object{int}.

 

Any ideas?

Thanks again for this excellent tool!

Feb 15, 2010 at 6:41 PM
Edited Feb 15, 2010 at 6:42 PM

As a work-around, I am running "IntegerObject + 0".  I've also discovered that, in C#, (object) 4 == (object) 4 evaluates to false.  Can Jint be set up to unbox instead of autobox?

 

Coordinator
Feb 16, 2010 at 5:54 AM

Thanks for your feedback,. I have corrected it and it's now available on the svn repository.

Feb 16, 2010 at 1:57 PM

Thank you again for your speedy response!