Procházet zdrojové kódy

Fixing all unit tests for Array.constructor

Sebastien Ros před 12 roky
rodič
revize
61f9f074e1

+ 1 - 1
Jint/Native/Array/ArrayConstructor.cs

@@ -33,7 +33,7 @@ namespace Jint.Native.Array
 
         public void Configure()
         {
-            FastAddProperty("isArray", new ClrFunctionInstance<object, object>(Engine, IsArray), false, false, false);
+            FastAddProperty("isArray", new ClrFunctionInstance<object, object>(Engine, IsArray), true, false, true);
         }
 
         private object IsArray(object thisObj, object[] arguments)

+ 21 - 21
Jint/Native/Array/ArrayPrototype.cs

@@ -30,27 +30,27 @@ namespace Jint.Native.Array
 
         public void Configure()
         {
-            FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToString), false, false, false);
-            FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), false, false, false);
-            FastAddProperty("concat", new ClrFunctionInstance<object, object>(Engine, Concat), false, false, false);
-            FastAddProperty("join", new ClrFunctionInstance<object, object>(Engine, Join), false, false, false);
-            FastAddProperty("pop", new ClrFunctionInstance<ArrayInstance, object>(Engine, Pop), false, false, false);
-            FastAddProperty("push", new ClrFunctionInstance<ArrayInstance, object>(Engine, Push), false, false, false);
-            FastAddProperty("reverse", new ClrFunctionInstance<object, object>(Engine, Reverse), false, false, false);
-            FastAddProperty("shift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Shift), false, false, false);
-            FastAddProperty("slice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Slice), false, false, false);
-            FastAddProperty("sort", new ClrFunctionInstance<ArrayInstance, object>(Engine, Sort), false, false, false);
-            FastAddProperty("splice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Splice), false, false, false);
-            FastAddProperty("unshift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Unshift), false, false, false);
-            FastAddProperty("indexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, IndexOf), false, false, false);
-            FastAddProperty("lastIndexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, LastIndexOf), false, false, false);
-            FastAddProperty("every", new ClrFunctionInstance<ArrayInstance, object>(Engine, Every), false, false, false);
-            FastAddProperty("some", new ClrFunctionInstance<ArrayInstance, object>(Engine, Some), false, false, false);
-            FastAddProperty("forEach", new ClrFunctionInstance<ArrayInstance, object>(Engine, ForEach), false, false, false);
-            FastAddProperty("map", new ClrFunctionInstance<ArrayInstance, object>(Engine, Map), false, false, false);
-            FastAddProperty("filter", new ClrFunctionInstance<ArrayInstance, object>(Engine, Filter), false, false, false);
-            FastAddProperty("reduce", new ClrFunctionInstance<ArrayInstance, object>(Engine, Reduce), false, false, false);
-            FastAddProperty("reduceRight", new ClrFunctionInstance<ArrayInstance, object>(Engine, ReduceRight), false, false, false);
+            FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToString), true, false, true);
+            FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), true, false, true);
+            FastAddProperty("concat", new ClrFunctionInstance<object, object>(Engine, Concat), true, false, true);
+            FastAddProperty("join", new ClrFunctionInstance<object, object>(Engine, Join), true, false, true);
+            FastAddProperty("pop", new ClrFunctionInstance<ArrayInstance, object>(Engine, Pop), true, false, true);
+            FastAddProperty("push", new ClrFunctionInstance<ArrayInstance, object>(Engine, Push), true, false, true);
+            FastAddProperty("reverse", new ClrFunctionInstance<object, object>(Engine, Reverse), true, false, true);
+            FastAddProperty("shift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Shift), true, false, true);
+            FastAddProperty("slice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Slice), true, false, true);
+            FastAddProperty("sort", new ClrFunctionInstance<ArrayInstance, object>(Engine, Sort), true, false, true);
+            FastAddProperty("splice", new ClrFunctionInstance<ArrayInstance, object>(Engine, Splice), true, false, true);
+            FastAddProperty("unshift", new ClrFunctionInstance<ArrayInstance, object>(Engine, Unshift), true, false, true);
+            FastAddProperty("indexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, IndexOf), true, false, true);
+            FastAddProperty("lastIndexOf", new ClrFunctionInstance<ArrayInstance, object>(Engine, LastIndexOf), true, false, true);
+            FastAddProperty("every", new ClrFunctionInstance<ArrayInstance, object>(Engine, Every), true, false, true);
+            FastAddProperty("some", new ClrFunctionInstance<ArrayInstance, object>(Engine, Some), true, false, true);
+            FastAddProperty("forEach", new ClrFunctionInstance<ArrayInstance, object>(Engine, ForEach), true, false, true);
+            FastAddProperty("map", new ClrFunctionInstance<ArrayInstance, object>(Engine, Map), true, false, true);
+            FastAddProperty("filter", new ClrFunctionInstance<ArrayInstance, object>(Engine, Filter), true, false, true);
+            FastAddProperty("reduce", new ClrFunctionInstance<ArrayInstance, object>(Engine, Reduce), true, false, true);
+            FastAddProperty("reduceRight", new ClrFunctionInstance<ArrayInstance, object>(Engine, ReduceRight), true, false, true);
         }
 
         private object LastIndexOf(ArrayInstance arg1, object[] arg2)

+ 2 - 2
Jint/Native/Boolean/BooleanPrototype.cs

@@ -25,8 +25,8 @@ namespace Jint.Native.Boolean
 
         public void Configure()
         {
-            FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToBooleanString), false, false, false);
-            FastAddProperty("valueOf", new ClrFunctionInstance<object, object>(Engine, ValueOf), false, false, false);
+            FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToBooleanString), true, false, true);
+            FastAddProperty("valueOf", new ClrFunctionInstance<object, object>(Engine, ValueOf), true, false, true);
         }
 
         private object ValueOf(object thisObj, object[] arguments)

+ 12 - 5
Jint/Native/Function/FunctionPrototype.cs

@@ -31,10 +31,10 @@ namespace Jint.Native.Function
         public void Configure()
         {
             FastAddProperty("constructor", Engine.Function, false, false, false);
-            FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToString), false, false, false);
-            FastAddProperty("apply", new ClrFunctionInstance<object, object>(Engine, Apply), false, false, false);
-            FastAddProperty("call", new ClrFunctionInstance<object, object>(Engine, Call, 1), false, false, false);
-            FastAddProperty("bind", new ClrFunctionInstance<object, object>(Engine, Bind), false, false, false);
+            FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToString), true, false, true);
+            FastAddProperty("apply", new ClrFunctionInstance<object, object>(Engine, Apply), true, false, true);
+            FastAddProperty("call", new ClrFunctionInstance<object, object>(Engine, Call, 1), true, false, true);
+            FastAddProperty("bind", new ClrFunctionInstance<object, object>(Engine, Bind), true, false, true);
         }
 
         private object Bind(object thisObj, object[] arguments)
@@ -44,7 +44,14 @@ namespace Jint.Native.Function
 
         private object ToString(object thisObj, object[] arguments)
         {
-            throw new NotImplementedException();
+            var func = thisObj as FunctionInstance;
+
+            if (func == null)
+            {
+                throw new JavaScriptException(Engine.TypeError, "Function object expected.");       
+            }
+
+            return System.String.Format("function() {{ ... }}");
         }
 
         public object Apply(object thisObject, object[] arguments)

+ 2 - 2
Jint/Native/Json/JsonInstance.cs

@@ -30,8 +30,8 @@ namespace Jint.Native.Json
 
         public void Configure()
         {
-            FastAddProperty("parse", new ClrFunctionInstance<JsonInstance, object>(Engine, Parse), false, false, false);
-            FastAddProperty("stringify", new ClrFunctionInstance<JsonInstance, object>(Engine, Stringify), false, false, false);
+            FastAddProperty("parse", new ClrFunctionInstance<JsonInstance, object>(Engine, Parse), true, false, true);
+            FastAddProperty("stringify", new ClrFunctionInstance<JsonInstance, object>(Engine, Stringify), true, false, true);
         }
 
         public object Parse(JsonInstance thisObject, object[] arguments)

+ 18 - 18
Jint/Native/Math/MathInstance.cs

@@ -31,24 +31,24 @@ namespace Jint.Native.Math
 
         public void Configure()
         {
-            FastAddProperty("abs", new ClrFunctionInstance<object, double>(Engine, Abs), false, false, false);
-            FastAddProperty("acos", new ClrFunctionInstance<object, double>(Engine, Acos), false, false, false);
-            FastAddProperty("asin", new ClrFunctionInstance<object, double>(Engine, Asin), false, false, false);
-            FastAddProperty("atan", new ClrFunctionInstance<object, double>(Engine, Atan), false, false, false);
-            FastAddProperty("atan2", new ClrFunctionInstance<object, double>(Engine, Atan2), false, false, false);
-            FastAddProperty("ceil", new ClrFunctionInstance<object, double>(Engine, Ceil), false, false, false);
-            FastAddProperty("cos", new ClrFunctionInstance<object, double>(Engine, Cos), false, false, false);
-            FastAddProperty("exp", new ClrFunctionInstance<object, double>(Engine, Exp), false, false, false);
-            FastAddProperty("floor", new ClrFunctionInstance<object, double>(Engine, Floor), false, false, false);
-            FastAddProperty("log", new ClrFunctionInstance<object, double>(Engine, Log), false, false, false);
-            FastAddProperty("max", new ClrFunctionInstance<object, double>(Engine, Max), false, false, false);
-            FastAddProperty("min", new ClrFunctionInstance<object, double>(Engine, Min), false, false, false);
-            FastAddProperty("pow", new ClrFunctionInstance<object, double>(Engine, Pow), false, false, false);
-            FastAddProperty("random", new ClrFunctionInstance<object, double>(Engine, Random), false, false, false);
-            FastAddProperty("round", new ClrFunctionInstance<object, double>(Engine, Round), false, false, false);
-            FastAddProperty("sin", new ClrFunctionInstance<object, double>(Engine, Sin), false, false, false);
-            FastAddProperty("sqrt", new ClrFunctionInstance<object, double>(Engine, Sqrt), false, false, false);
-            FastAddProperty("tan", new ClrFunctionInstance<object, double>(Engine, Tan), false, false, false);
+            FastAddProperty("abs", new ClrFunctionInstance<object, double>(Engine, Abs), true, false, true);
+            FastAddProperty("acos", new ClrFunctionInstance<object, double>(Engine, Acos), true, false, true);
+            FastAddProperty("asin", new ClrFunctionInstance<object, double>(Engine, Asin), true, false, true);
+            FastAddProperty("atan", new ClrFunctionInstance<object, double>(Engine, Atan), true, false, true);
+            FastAddProperty("atan2", new ClrFunctionInstance<object, double>(Engine, Atan2), true, false, true);
+            FastAddProperty("ceil", new ClrFunctionInstance<object, double>(Engine, Ceil), true, false, true);
+            FastAddProperty("cos", new ClrFunctionInstance<object, double>(Engine, Cos), true, false, true);
+            FastAddProperty("exp", new ClrFunctionInstance<object, double>(Engine, Exp), true, false, true);
+            FastAddProperty("floor", new ClrFunctionInstance<object, double>(Engine, Floor), true, false, true);
+            FastAddProperty("log", new ClrFunctionInstance<object, double>(Engine, Log), true, false, true);
+            FastAddProperty("max", new ClrFunctionInstance<object, double>(Engine, Max), true, false, true);
+            FastAddProperty("min", new ClrFunctionInstance<object, double>(Engine, Min), true, false, true);
+            FastAddProperty("pow", new ClrFunctionInstance<object, double>(Engine, Pow), true, false, true);
+            FastAddProperty("random", new ClrFunctionInstance<object, double>(Engine, Random), true, false, true);
+            FastAddProperty("round", new ClrFunctionInstance<object, double>(Engine, Round), true, false, true);
+            FastAddProperty("sin", new ClrFunctionInstance<object, double>(Engine, Sin), true, false, true);
+            FastAddProperty("sqrt", new ClrFunctionInstance<object, double>(Engine, Sqrt), true, false, true);
+            FastAddProperty("tan", new ClrFunctionInstance<object, double>(Engine, Tan), true, false, true);
 
             FastAddProperty("E", System.Math.E, false, false, false);
             FastAddProperty("LN10", System.Math.Log(10), false, false, false);

+ 6 - 6
Jint/Native/Number/NumberPrototype.cs

@@ -26,12 +26,12 @@ namespace Jint.Native.Number
 
         public void Configure()
         {
-            FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToNumberString), false, false, false);
-            FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), false, false, false);
-            FastAddProperty("valueOf", new ClrFunctionInstance<object, object>(Engine, ValueOf), false, false, false);
-            FastAddProperty("toFixed", new ClrFunctionInstance<object, object>(Engine, ToFixed), false, false, false);
-            FastAddProperty("toExponential", new ClrFunctionInstance<object, object>(Engine, ToExponential), false, false, false);
-            FastAddProperty("toPrecision", new ClrFunctionInstance<object, object>(Engine, ToPrecision), false, false, false);
+            FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToNumberString), true, false, true);
+            FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), true, false, true);
+            FastAddProperty("valueOf", new ClrFunctionInstance<object, object>(Engine, ValueOf), true, false, true);
+            FastAddProperty("toFixed", new ClrFunctionInstance<object, object>(Engine, ToFixed), true, false, true);
+            FastAddProperty("toExponential", new ClrFunctionInstance<object, object>(Engine, ToExponential), true, false, true);
+            FastAddProperty("toPrecision", new ClrFunctionInstance<object, object>(Engine, ToPrecision), true, false, true);
         }
 
         private object ToLocaleString(object thisObj, object[] arguments)

+ 2 - 2
Jint/Native/Object/ObjectInstance.cs

@@ -337,7 +337,7 @@ namespace Jint.Native.Object
                 if (callable != null)
                 {
                     var val = callable.Call(this, Arguments.Empty);
-                    if (val is IPrimitiveType)
+                    if (TypeConverter.IsPrimitiveValue(val))
                     {
                         return val;
                     }
@@ -348,7 +348,7 @@ namespace Jint.Native.Object
                 if (callable != null)
                 {
                     var str = callable.Call(this, Arguments.Empty);
-                    if (str is IPrimitiveType)
+                    if (TypeConverter.IsPrimitiveValue(str))
                     {
                         return str;
                     }

+ 14 - 7
Jint/Native/Object/ObjectPrototype.cs

@@ -22,17 +22,24 @@ namespace Jint.Native.Object
 
         public void Configure()
         {
-            FastAddProperty("toString", new ClrFunctionInstance<object, string>(Engine, ToString), false, false, false);
-            FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), false, false, false);
-            FastAddProperty("valueOf", new ClrFunctionInstance<object, object>(Engine, ValueOf), false, false, false);
-            FastAddProperty("hasOwnProperty", new ClrFunctionInstance<object, bool>(Engine, HasOwnProperty), false, false, false);
-            FastAddProperty("isPrototypeOf", new ClrFunctionInstance<object, bool>(Engine, IsPrototypeOf), false, false, false);
-            FastAddProperty("propertyIsEnumerable", new ClrFunctionInstance<object, bool>(Engine, PropertyIsEnumerable), false, false, false);
+            FastAddProperty("toString", new ClrFunctionInstance<object, string>(Engine, ToString), true, false, true);
+            FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), true, false, true);
+            FastAddProperty("valueOf", new ClrFunctionInstance<object, object>(Engine, ValueOf), true, false, true);
+            FastAddProperty("hasOwnProperty", new ClrFunctionInstance<object, bool>(Engine, HasOwnProperty), true, false, true);
+            FastAddProperty("isPrototypeOf", new ClrFunctionInstance<object, bool>(Engine, IsPrototypeOf), true, false, true);
+            FastAddProperty("propertyIsEnumerable", new ClrFunctionInstance<object, bool>(Engine, PropertyIsEnumerable), true, false, true);
         }
 
         private bool PropertyIsEnumerable(object thisObject, object[] arguments)
         {
-            throw new NotImplementedException();
+            var p = TypeConverter.ToString(arguments[0]);
+            var o = TypeConverter.ToObject(Engine, thisObject);
+            var desc = o.GetOwnProperty(p);
+            if (desc == PropertyDescriptor.Undefined)
+            {
+                return false;
+            }
+            return desc.Enumerable;
         }
 
         private object ValueOf(object thisObject, object[] arguments)

+ 1 - 1
Jint/Runtime/Interop/ClrFunctionInstance.cs

@@ -26,7 +26,7 @@ namespace Jint.Runtime.Interop
         public override object Call(object thisObject, object[] arguments)
         {
             var result = _func((TObject) thisObject, arguments);
-            return new Completion(Completion.Normal, result, null);
+            return result;
         }
     }
 }

+ 9 - 0
Jint/Runtime/TypeConverter.cs

@@ -36,6 +36,15 @@ namespace Jint.Runtime
             return o.DefaultValue(preferredType);
         }
 
+        public static bool IsPrimitiveValue(object o)
+        {
+            return o is string
+                   || o is double
+                   || o is bool
+                   || o is Undefined
+                   || o is Null;
+        }
+    
         /// <summary>
         /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
         /// </summary>