Browse Source

Calling GetValue when needed

Sebastien Ros 12 years ago
parent
commit
87e4e0fc81

+ 18 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -288,6 +288,24 @@ namespace Jint.Tests.Runtime
             ");
         }
 
+        [Fact]
+        public void MathAbsReturnsAbsolute()
+        {
+            RunTest(@"
+                assert(1 == Math.abs(-1));
+            ");
+        }
+
+        [Fact]
+        public void NaNIsNan()
+        {
+            RunTest(@"
+                var x = NaN; 
+                assert(isNaN(NaN));
+                assert(isNaN(Math.abs(x)));
+            ");
+        }
+
         [Fact]
         public void Scratch()
         {

+ 8 - 0
Jint/Native/Global/GlobalObject.cs

@@ -1,5 +1,6 @@
 using Jint.Native.Object;
 using Jint.Runtime;
+using Jint.Runtime.Descriptors;
 using Jint.Runtime.Descriptors.Specialized;
 
 namespace Jint.Native.Global
@@ -14,6 +15,13 @@ namespace Jint.Native.Global
         public static GlobalObject CreateGlobalObject(Engine engine, ObjectInstance prototype)
         {
             var global = new GlobalObject(prototype);
+            
+            // Global object properties
+            global.DefineOwnProperty("NaN", new DataDescriptor(double.NaN), false);
+            global.DefineOwnProperty("Infinity", new DataDescriptor(double.PositiveInfinity), false);
+            global.DefineOwnProperty("undefined", new DataDescriptor(Undefined.Instance), false);
+
+            // Global object functions
             global.DefineOwnProperty("isNaN", new ClrDataDescriptor<object, bool>(engine, IsNaN), false);
 
             return global;

+ 6 - 11
Jint/Runtime/ExpressionIntepreter.cs

@@ -264,11 +264,6 @@ namespace Jint.Runtime
 
         public object EvaluateIdentifier(Identifier identifier)
         {
-            if (identifier.Name == "undefined")
-            {
-                return Undefined.Instance;
-            }
-
             return _engine.CurrentExecutionContext.LexicalEnvironment.GetIdentifierReference(identifier.Name, _engine.Options.IsStrict());
         }
 
@@ -332,20 +327,20 @@ namespace Jint.Runtime
         {
             /// todo: read the spec as this is made up
             
-            var arguments = callExpression.Arguments.Select(EvaluateExpression);
+            var arguments = callExpression.Arguments.Select(EvaluateExpression).Select(_engine.GetValue).ToArray();
             var result = EvaluateExpression(callExpression.Callee);
             var r = result as Reference;
             if (r != null)
             {
                 // x.hasOwnProperty
                 var callee = (FunctionInstance)_engine.GetValue(r);
-                return callee.Call(r.GetBase(), arguments.ToArray());
+                return callee.Call(r.GetBase(), arguments);
             }
             else
             {
                 // assert(...)
                 var callee = (FunctionInstance)_engine.GetValue(result);
-                return callee.Call(_engine.CurrentExecutionContext.ThisBinding, arguments.ToArray());
+                return callee.Call(_engine.CurrentExecutionContext.ThisBinding, arguments);
             }
 
         }
@@ -458,14 +453,14 @@ namespace Jint.Runtime
                     return TypeConverter.ToNumber(_engine.GetValue(value));
                     
                 case "-":
-                    var n = TypeConverter.ToNumber(value);
+                    var n = TypeConverter.ToNumber(_engine.GetValue(value));
                     return double.IsNaN(n) ? double.NaN : n*-1;
                 
                 case "~":
-                    return ~TypeConverter.ToInt32(value);
+                    return ~TypeConverter.ToInt32(_engine.GetValue(value));
                 
                 case "!":
-                    return !TypeConverter.ToBoolean(value);
+                    return !TypeConverter.ToBoolean(_engine.GetValue(value));
                 
                 case "delete":
                     r = value as Reference;

+ 1 - 1
Jint/Runtime/StatementInterpreter.cs

@@ -43,7 +43,7 @@ namespace Jint.Runtime
 
                 if (declaration.Init != null)
                 {
-                    result = value = _engine.EvaluateExpression(declaration.Init);
+                    result = value = _engine.GetValue(_engine.EvaluateExpression(declaration.Init));
                 }
 
                 var dn = declaration.Id.Name;