Browse Source

Implementing Array.prototype.every

Sebastien Ros 12 years ago
parent
commit
5125546765
1 changed files with 31 additions and 3 deletions
  1. 31 3
      Jint/Native/Array/ArrayPrototype.cs

+ 31 - 3
Jint/Native/Array/ArrayPrototype.cs

@@ -47,7 +47,7 @@ namespace Jint.Native.Array
             FastAddProperty("unshift", new ClrFunctionInstance<object, uint>(Engine, Unshift, 1), true, false, true);
             FastAddProperty("unshift", new ClrFunctionInstance<object, uint>(Engine, Unshift, 1), true, false, true);
             FastAddProperty("indexOf", new ClrFunctionInstance<object, int>(Engine, IndexOf, 1), true, false, true);
             FastAddProperty("indexOf", new ClrFunctionInstance<object, int>(Engine, IndexOf, 1), true, false, true);
             FastAddProperty("lastIndexOf", new ClrFunctionInstance<object, int>(Engine, LastIndexOf, 1), true, false, true);
             FastAddProperty("lastIndexOf", new ClrFunctionInstance<object, int>(Engine, LastIndexOf, 1), true, false, true);
-            FastAddProperty("every", new ClrFunctionInstance<ArrayInstance, object>(Engine, Every), true, false, true);
+            FastAddProperty("every", new ClrFunctionInstance<object, bool>(Engine, Every, 1), true, false, true);
             FastAddProperty("some", new ClrFunctionInstance<ArrayInstance, object>(Engine, Some), 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("forEach", new ClrFunctionInstance<ArrayInstance, object>(Engine, ForEach), true, false, true);
             FastAddProperty("map", new ClrFunctionInstance<ArrayInstance, object>(Engine, Map), true, false, true);
             FastAddProperty("map", new ClrFunctionInstance<ArrayInstance, object>(Engine, Map), true, false, true);
@@ -119,9 +119,37 @@ namespace Jint.Native.Array
             throw new NotImplementedException();
             throw new NotImplementedException();
         }
         }
 
 
-        private object Every(ArrayInstance arg1, object[] arg2)
+        private bool Every(object thisObj, object[] arguments)
         {
         {
-            throw new NotImplementedException();
+            var callbackfn = arguments.Length > 0 ? arguments[0] : Undefined.Instance;
+            var thisArg = arguments.Length > 1 ? arguments[1] : Undefined.Instance;
+
+            var o = TypeConverter.ToObject(Engine, thisObj);
+            var lenValue = o.Get("length");
+            var len = TypeConverter.ToUint32(lenValue);
+
+            var callable = callbackfn as ICallable;
+            if (callable == null)
+            {
+                throw new JavaScriptException(Engine.TypeError, "Argument must be callable");
+            }
+
+            for (var k = 0; k < len; k++)
+            {
+                var pk = k.ToString();
+                var kpresent = o.HasProperty(pk);
+                if (kpresent)
+                {
+                    var kvalue = o.Get(pk);
+                    var testResult = callable.Call(thisArg, new object[] { kvalue, k, o });
+                    if (!TypeConverter.ToBoolean(testResult))
+                    {
+                        return false;
+                    }
+                }
+            }
+
+            return true;
         }
         }
 
 
         private int IndexOf(object thisObj, object[] arguments)
         private int IndexOf(object thisObj, object[] arguments)