Browse Source

Fixing some more unit tests

Sebastien Ros 11 years ago
parent
commit
f591dea25e

+ 1 - 1
Jint.Tests.Ecma/EcmaTest.cs

@@ -55,7 +55,7 @@ namespace Jint.Tests.Ecma
                 }
                 }
                 catch (JavaScriptException j)
                 catch (JavaScriptException j)
                 {
                 {
-                    _lastError = j.Error.ToString();
+                    _lastError = TypeConverter.ToString(j.Error).AsString();
                 }
                 }
                 catch (Exception e)
                 catch (Exception e)
                 {
                 {

+ 13 - 23
Jint.Tests/Runtime/EngineTests.cs

@@ -14,7 +14,7 @@ namespace Jint.Tests.Runtime
             var engine = new Engine(cfg => cfg
             var engine = new Engine(cfg => cfg
                 .WithDelegate("log", new Action<object>(Console.WriteLine))
                 .WithDelegate("log", new Action<object>(Console.WriteLine))
                 .WithDelegate("assert", new Action<bool>(Assert.True))
                 .WithDelegate("assert", new Action<bool>(Assert.True))
-            );
+                );
 
 
             engine.Execute(source);
             engine.Execute(source);
 
 
@@ -45,8 +45,8 @@ namespace Jint.Tests.Runtime
         public void ShouldInterpretLiterals(object expected, string source)
         public void ShouldInterpretLiterals(object expected, string source)
         {
         {
             var engine = new Engine();
             var engine = new Engine();
-            var result = engine.GetValue(engine.Execute(source));
-            
+            var result = engine.Execute(source).ToObject();
+
             Assert.Equal(expected, result);
             Assert.Equal(expected, result);
         }
         }
 
 
@@ -64,16 +64,16 @@ namespace Jint.Tests.Runtime
         [InlineData(-2d, "1 - 3")]
         [InlineData(-2d, "1 - 3")]
         [InlineData(3d, "1 * 3")]
         [InlineData(3d, "1 * 3")]
         [InlineData(2d, "6 / 3")]
         [InlineData(2d, "6 / 3")]
-        [InlineData(9, "15 & 9")]
-        [InlineData(15, "15 | 9")]
-        [InlineData(6, "15 ^ 9")]
-        [InlineData(36, "9 << 2")]
-        [InlineData(2, "9 >> 2")]
-        [InlineData((uint)4, "19 >>> 2")]
+        [InlineData(9d, "15 & 9")]
+        [InlineData(15d, "15 | 9")]
+        [InlineData(6d, "15 ^ 9")]
+        [InlineData(36d, "9 << 2")]
+        [InlineData(2d, "9 >> 2")]
+        [InlineData(4d, "19 >>> 2")]
         public void ShouldInterpretBinaryExpression(object expected, string source)
         public void ShouldInterpretBinaryExpression(object expected, string source)
         {
         {
             var engine = new Engine();
             var engine = new Engine();
-            var result = engine.GetValue(engine.Execute(source));
+            var result = engine.Execute(source).ToObject();
 
 
             Assert.Equal(expected, result);
             Assert.Equal(expected, result);
         }
         }
@@ -527,7 +527,7 @@ namespace Jint.Tests.Runtime
         public void OperatorsPrecedence(object expected, string source)
         public void OperatorsPrecedence(object expected, string source)
         {
         {
             var engine = new Engine();
             var engine = new Engine();
-            var result = engine.GetValue(engine.Execute(source));
+            var result = engine.Execute(source).ToObject();
 
 
             Assert.Equal(expected, result);
             Assert.Equal(expected, result);
         }
         }
@@ -553,19 +553,9 @@ namespace Jint.Tests.Runtime
         public void ShouldEvaluateParseInt(object expected, string source)
         public void ShouldEvaluateParseInt(object expected, string source)
         {
         {
             var engine = new Engine();
             var engine = new Engine();
-            var result = engine.GetValue(engine.Execute(source));
+            var result = engine.Execute(source).ToObject();
 
 
             Assert.Equal(expected, result);
             Assert.Equal(expected, result);
         }
         }
-
-        /*
-                        [Fact]
-                        public void ()
-                        {
-                            RunTest(@"
-                            ");
-                        }
-                */
-
-        }
+    }
 }
 }

+ 2 - 2
Jint/Engine.cs

@@ -169,13 +169,13 @@ namespace Jint
             _executionContexts.Pop();
             _executionContexts.Pop();
         }
         }
 
 
-        public object Execute(string source)
+        public JsValue Execute(string source)
         {
         {
             var parser = new JavaScriptParser();
             var parser = new JavaScriptParser();
             return Execute(parser.Parse(source));
             return Execute(parser.Parse(source));
         }
         }
 
 
-        public object Execute(Program program)
+        public JsValue Execute(Program program)
         {
         {
             using (new StrictModeScope(Options.IsStrict() || program.Strict))
             using (new StrictModeScope(Options.IsStrict() || program.Strict))
             {
             {

+ 0 - 3
Jint/Jint.csproj

@@ -37,9 +37,6 @@
     <ErrorReport>prompt</ErrorReport>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   </PropertyGroup>
-  <ItemGroup>
-    <!-- A reference to the entire .NET Framework is automatically included -->
-  </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <Compile Include="DeclarationBindingType.cs" />
     <Compile Include="DeclarationBindingType.cs" />
     <Compile Include="EvalCodeScope.cs" />
     <Compile Include="EvalCodeScope.cs" />

+ 1 - 1
Jint/Native/Function/BindFunctionInstance.cs

@@ -36,7 +36,7 @@ namespace Jint.Native.Function
             return target.Construct(BoundArgs.Union(arguments).ToArray());
             return target.Construct(BoundArgs.Union(arguments).ToArray());
         }
         }
 
 
-        public override bool HasInstance(object v)
+        public override bool HasInstance(JsValue v)
         {
         {
             var f = TargetFunction.TryCast<FunctionInstance>(x =>
             var f = TargetFunction.TryCast<FunctionInstance>(x =>
             {
             {

+ 2 - 2
Jint/Native/Function/FunctionInstance.cs

@@ -29,9 +29,9 @@ namespace Jint.Native.Function
         public string[] FormalParameters { get; private set; }
         public string[] FormalParameters { get; private set; }
         public bool Strict { get; private set; }
         public bool Strict { get; private set; }
 
 
-        public virtual bool HasInstance(object v)
+        public virtual bool HasInstance(JsValue v)
         {
         {
-            var vObj = v as ObjectInstance;
+            var vObj = v.TryCast<ObjectInstance>();
             if (vObj == null)
             if (vObj == null)
             {
             {
                 return false;
                 return false;

+ 5 - 2
Jint/Native/Function/ScriptFunctionInstance.cs

@@ -38,8 +38,11 @@ namespace Jint.Native.Function
             var proto = engine.Object.Construct(Arguments.Empty);
             var proto = engine.Object.Construct(Arguments.Empty);
             proto.DefineOwnProperty("constructor", new PropertyDescriptor(this, true, false, true), false);
             proto.DefineOwnProperty("constructor", new PropertyDescriptor(this, true, false, true), false);
             DefineOwnProperty("prototype", new PropertyDescriptor(proto, true, false, false ), false);
             DefineOwnProperty("prototype", new PropertyDescriptor(proto, true, false, false ), false);
-            DefineOwnProperty("name", new PropertyDescriptor(_functionDeclaration.Id.Name, null, null, null), false);
-            
+            if (_functionDeclaration.Id != null)
+            {
+                DefineOwnProperty("name", new PropertyDescriptor(_functionDeclaration.Id.Name, null, null, null), false);
+            }
+
             if (strict)
             if (strict)
             {
             {
                 var thrower = engine.Function.ThrowTypeError;
                 var thrower = engine.Function.ThrowTypeError;

+ 31 - 1
Jint/Native/JsValue.cs

@@ -145,7 +145,7 @@ namespace Jint.Native
             return null;
             return null;
         }
         }
 
 
-        public bool Is<T>() where T : ObjectInstance
+        public bool Is<T>()
         {
         {
             return IsObject() && AsObject() is T;
             return IsObject() && AsObject() is T;
         }
         }
@@ -239,6 +239,11 @@ namespace Jint.Native
         private static readonly Type[] NumberTypes = { typeof(double), typeof(int), typeof(float), typeof(uint), typeof(byte), typeof(short), typeof(ushort), typeof(long), typeof(ulong) };
         private static readonly Type[] NumberTypes = { typeof(double), typeof(int), typeof(float), typeof(uint), typeof(byte), typeof(short), typeof(ushort), typeof(long), typeof(ulong) };
         public static JsValue FromObject(object value)
         public static JsValue FromObject(object value)
         {
         {
+            if (value == null)
+            {
+                return Null;
+            }
+
             var s = value as string;
             var s = value as string;
             if (s != null)
             if (s != null)
             {
             {
@@ -258,6 +263,31 @@ namespace Jint.Native
             return Undefined;
             return Undefined;
         }
         }
 
 
+        /// <summary>
+        /// Converts a <see cref="JsValue"/> to its underlying CLR value.
+        /// </summary>
+        /// <returns>The underlying CLR value of the <see cref="JsValue"/> instance.</returns>
+        public object ToObject()
+        {
+            switch (_type)
+            {
+                case Types.None:
+                case Types.Undefined:
+                case Types.Null:
+                    return null;
+                case Types.Boolean:
+                    return _bool;
+                case Types.String:
+                    return _string;
+                case Types.Number:
+                    return _double;
+                case Types.Object:
+                    return _object;
+                default:
+                    throw new ArgumentOutOfRangeException();
+            }
+        }
+
         public static bool operator ==(JsValue a, JsValue b)
         public static bool operator ==(JsValue a, JsValue b)
         {
         {
             return a.Equals(b);
             return a.Equals(b);

+ 1 - 1
Jint/Native/Json/JsonSerializer.cs

@@ -29,7 +29,7 @@ namespace Jint.Native.Json
 
 
             if (replacer.IsObject())
             if (replacer.IsObject())
             {
             {
-                if (replacer.AsObject() is ICallable)
+                if (replacer.Is<ICallable>())
                 {
                 {
                     _replacerFunction = replacer;
                     _replacerFunction = replacer;
                 }
                 }

+ 20 - 24
Jint/Native/Object/ObjectInstance.cs

@@ -58,7 +58,7 @@ namespace Jint.Native.Object
                 return desc.Value.Value;
                 return desc.Value.Value;
             }
             }
 
 
-            var getter = desc.Get.Value;
+            var getter = desc.Get.HasValue ? desc.Get.Value : Undefined.Instance;
 
 
             if (getter.IsUndefined())
             if (getter.IsUndefined())
             {
             {
@@ -66,7 +66,7 @@ namespace Jint.Native.Object
             }
             }
 
 
             // if getter is not undefined it must be ICallable
             // if getter is not undefined it must be ICallable
-            var callable = (ICallable)(getter.AsObject());
+            var callable = getter.TryCast<ICallable>();
             return callable.Call(this, Arguments.Empty);
             return callable.Call(this, Arguments.Empty);
         }
         }
 
 
@@ -173,7 +173,7 @@ namespace Jint.Native.Object
 
 
             if (desc.IsAccessorDescriptor())
             if (desc.IsAccessorDescriptor())
             {
             {
-                var setter = (ICallable)desc.Set.Value.AsObject();
+                var setter = desc.Set.Value.TryCast<ICallable>();
                 setter.Call(new JsValue(this), new [] {value});
                 setter.Call(new JsValue(this), new [] {value});
             }
             }
             else
             else
@@ -199,7 +199,7 @@ namespace Jint.Native.Object
             {
             {
                 if (desc.IsAccessorDescriptor())
                 if (desc.IsAccessorDescriptor())
                 {
                 {
-                    if (desc.Set.Value.IsUndefined())
+                    if (!desc.Set.HasValue || desc.Set.Value.IsUndefined())
                     {
                     {
                         return false;
                         return false;
                     }
                     }
@@ -207,7 +207,7 @@ namespace Jint.Native.Object
                     return true;
                     return true;
                 }
                 }
 
 
-                return desc.Writable.Value.AsBoolean();
+                return desc.Writable.HasValue && desc.Writable.Value.AsBoolean();
             }
             }
 
 
             if (Prototype == null)
             if (Prototype == null)
@@ -224,7 +224,7 @@ namespace Jint.Native.Object
 
 
             if (inherited.IsAccessorDescriptor())
             if (inherited.IsAccessorDescriptor())
             {
             {
-                if (inherited.Set.Value.IsUndefined())
+                if (!inherited.Set.HasValue || inherited.Set.Value.IsUndefined())
                 {
                 {
                     return false;
                     return false;
                 }
                 }
@@ -238,7 +238,7 @@ namespace Jint.Native.Object
             }
             }
             else
             else
             {
             {
-                return inherited.Writable.Value.AsBoolean();
+                return inherited.Writable.HasValue && inherited.Writable.Value.AsBoolean();
             }
             }
         }
         }
 
 
@@ -271,7 +271,7 @@ namespace Jint.Native.Object
                 return true;
                 return true;
             }
             }
 
 
-            if (desc.Configurable.Value.AsBoolean())
+            if (desc.Configurable.HasValue && desc.Configurable.Value.AsBoolean())
             {
             {
                 Properties.Remove(propertyName);
                 Properties.Remove(propertyName);
                 return true;
                 return true;
@@ -297,22 +297,20 @@ namespace Jint.Native.Object
         {
         {
             if ((hint == Types.String) || (hint == Types.None && this is StringInstance) || this is DateInstance)
             if ((hint == Types.String) || (hint == Types.None && this is StringInstance) || this is DateInstance)
             {
             {
-                var toString = Get("toString").AsObject();
-                var callable = toString as ICallable;
-                if (callable != null)
+                var toString = Get("toString").TryCast<ICallable>();
+                if (toString != null)
                 {
                 {
-                    var str = callable.Call(new JsValue(this), Arguments.Empty);
+                    var str = toString.Call(new JsValue(this), Arguments.Empty);
                     if (str.IsPrimitive())
                     if (str.IsPrimitive())
                     {
                     {
                         return str;
                         return str;
                     }
                     }
                 }
                 }
 
 
-                var valueOf = Get("valueOf").AsObject();
-                callable = valueOf as ICallable;
-                if (callable != null)
+                var valueOf = Get("valueOf").TryCast<ICallable>();
+                if (valueOf != null)
                 {
                 {
-                    var val = callable.Call(new JsValue(this), Arguments.Empty);
+                    var val = valueOf.Call(new JsValue(this), Arguments.Empty);
                     if (val.IsPrimitive())
                     if (val.IsPrimitive())
                     {
                     {
                         return val;
                         return val;
@@ -324,22 +322,20 @@ namespace Jint.Native.Object
 
 
             if ((hint == Types.Number) || (hint == Types.None))
             if ((hint == Types.Number) || (hint == Types.None))
             {
             {
-                var valueOf = Get("valueOf").AsObject();
-                var callable = valueOf as ICallable;
-                if (callable != null)
+                var valueOf = Get("valueOf").TryCast<ICallable>();
+                if (valueOf != null)
                 {
                 {
-                    var val = callable.Call(new JsValue(this), Arguments.Empty);
+                    var val = valueOf.Call(new JsValue(this), Arguments.Empty);
                     if (val.IsPrimitive())
                     if (val.IsPrimitive())
                     {
                     {
                         return val;
                         return val;
                     }
                     }
                 }
                 }
 
 
-                var toString = Get("toString").AsObject();
-                callable = toString as ICallable;
-                if (callable != null)
+                var toString = Get("toString").TryCast<ICallable>();
+                if (toString != null)
                 {
                 {
-                    var str = callable.Call(new JsValue(this), Arguments.Empty);
+                    var str = toString.Call(new JsValue(this), Arguments.Empty);
                     if (str.IsPrimitive())
                     if (str.IsPrimitive())
                     {
                     {
                         return str;
                         return str;

+ 4 - 5
Jint/Runtime/Descriptors/PropertyDescriptor.cs

@@ -103,10 +103,9 @@ namespace Jint.Runtime.Descriptors
             return !IsDataDescriptor() && !IsAccessorDescriptor();
             return !IsDataDescriptor() && !IsAccessorDescriptor();
         }
         }
 
 
-        public static PropertyDescriptor ToPropertyDescriptor(Engine engine, object o)
+        public static PropertyDescriptor ToPropertyDescriptor(Engine engine, JsValue o)
         {
         {
-            var obj = o as ObjectInstance;
-
+            var obj = o.TryCast<ObjectInstance>();
             if (obj == null)
             if (obj == null)
             {
             {
                 throw new JavaScriptException(engine.TypeError);
                 throw new JavaScriptException(engine.TypeError);
@@ -144,7 +143,7 @@ namespace Jint.Runtime.Descriptors
             if (obj.HasProperty("get"))
             if (obj.HasProperty("get"))
             {
             {
                 var getter = obj.Get("get");
                 var getter = obj.Get("get");
-                if (getter != JsValue.Undefined && !(getter is ICallable))
+                if (getter != JsValue.Undefined && getter.TryCast<ICallable>() == null)
                 {
                 {
                     throw new JavaScriptException(engine.TypeError);
                     throw new JavaScriptException(engine.TypeError);
                 }
                 }
@@ -154,7 +153,7 @@ namespace Jint.Runtime.Descriptors
             if (obj.HasProperty("set"))
             if (obj.HasProperty("set"))
             {
             {
                 var setter = obj.Get("set");
                 var setter = obj.Get("set");
-                if (setter != Native.Undefined.Instance && !(setter is ICallable))
+                if (setter != Native.Undefined.Instance && setter.TryCast<ICallable>() == null)
                 {
                 {
                     throw new JavaScriptException(engine.TypeError);
                     throw new JavaScriptException(engine.TypeError);
                 }
                 }

+ 2 - 1
Jint/Runtime/Interop/DelegateWrapper.cs

@@ -1,4 +1,5 @@
 using System;
 using System;
+using System.Linq;
 using Jint.Native;
 using Jint.Native;
 using Jint.Native.Function;
 using Jint.Native.Function;
 
 
@@ -21,7 +22,7 @@ namespace Jint.Runtime.Interop
 
 
         public override JsValue Call(JsValue thisObject, JsValue[] arguments)
         public override JsValue Call(JsValue thisObject, JsValue[] arguments)
         {
         {
-            return JsValue.FromObject(_d.DynamicInvoke(arguments));
+            return JsValue.FromObject(_d.DynamicInvoke(arguments.Select(x => x.ToObject()).ToArray()));
         }
         }
     }
     }
 }
 }