Example #1
0
        public JsInstance Reverse(JsObject target, JsInstance[] parameters)
        {
            int length = target.Length;
            int num    = length / 2;

            for (int index1 = 0; index1 != num; ++index1)
            {
                string     index2    = (length - index1 - 1).ToString();
                string     index3    = index1.ToString();
                JsInstance result1   = (JsInstance)null;
                JsInstance result2   = (JsInstance)null;
                bool       property1 = target.TryGetProperty(index3, out result1);
                bool       property2 = target.TryGetProperty(index2, out result2);
                if (property1)
                {
                    target[index2] = result1;
                }
                else
                {
                    target.Delete(index2);
                }
                if (property2)
                {
                    target[index3] = result2;
                }
                else
                {
                    target.Delete(index3);
                }
            }
            return((JsInstance)target);
        }
Example #2
0
        public JsInstance UnShift(JsObject target, JsInstance[] parameters)
        {
            for (int length = target.Length; length > 0; --length)
            {
                JsInstance result = (JsInstance)null;
                int        num    = length - 1;
                string     index1 = num.ToString();
                num = length + parameters.Length - 1;
                string index2 = num.ToString();
                if (target.TryGetProperty(index1, out result))
                {
                    target[index2] = result;
                }
                else
                {
                    target.Delete(index2);
                }
            }
            List <JsInstance> jsInstanceList = new List <JsInstance>((IEnumerable <JsInstance>)parameters);
            int num1 = 0;

            while (jsInstanceList.Count > 0)
            {
                JsInstance jsInstance = jsInstanceList[0];
                jsInstanceList.RemoveAt(0);
                target[num1.ToString()] = jsInstance;
                ++num1;
            }
            return((JsInstance)this.Global.NumberClass.New((double)target.Length));
        }
        /// <summary>
        /// 15.4.4.13
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance UnShift(JsObject target, JsInstance[] parameters)
        {
            for (int k = target.Length; k > 0; k--)
            {
                JsInstance result = null;
                string     from   = (k - 1).ToString();
                string     to     = (k + parameters.Length - 1).ToString();
                if (target.TryGetProperty(from, out result))
                {
                    target[to] = result;
                }
                else
                {
                    target.Delete(to);
                }
            }
            List <JsInstance> items = new List <JsInstance>(parameters);

            for (int j = 0; items.Count > 0; j++)
            {
                JsInstance e = items[0];
                items.RemoveAt(0);
                target[j.ToString()] = e;
            }
            return(Global.NumberClass.New(target.Length));
        }
        /// <summary>
        /// 15.4.4.6
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Pop(JsObject target, JsInstance[] parameters)
        {
            var length = Convert.ToUInt32(target.Length);

            if (length == 0)
            {
                return(JsUndefined.Instance);
            }
            var key    = (length - 1).ToString();
            var result = target[key];

            target.Delete(key);
            return(result);
        }
        /// <summary>
        /// 15.4.4.8
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Reverse(JsObject target, JsInstance[] parameters)
        {
            int len    = target.Length;
            int middle = len / 2;

            for (int lower = 0; lower != middle; lower++)
            {
                int    upper  = len - lower - 1;
                string upperP = upper.ToString();
                string lowerP = lower.ToString();

                JsInstance lowerValue  = null;
                JsInstance upperValue  = null;
                bool       lowerExists = target.TryGetProperty(lowerP, out lowerValue);
                bool       upperExists = target.TryGetProperty(upperP, out upperValue);

                if (lowerExists)
                {
                    target[upperP] = lowerValue;
                }
                else
                {
                    target.Delete(upperP);
                }

                if (upperExists)
                {
                    target[lowerP] = upperValue;
                }
                else
                {
                    target.Delete(lowerP);
                }
            }
            return(target);
        }
Example #6
0
        public JsInstance Pop(JsObject target, JsInstance[] parameters)
        {
            uint uint32 = Convert.ToUInt32(target.Length);

            if (uint32 == 0U)
            {
                return((JsInstance)JsUndefined.Instance);
            }
            string     index      = (uint32 - 1U).ToString();
            JsInstance jsInstance = target[index];

            target.Delete(index);
            --target.Length;
            return(jsInstance);
        }
Example #7
0
 private JsInstance SetLengthImpl(JsObject that, JsInstance[] parameters)
 {
     if (that is JsArray)
     {
         that.Length = (int)parameters[0].ToNumber();
     }
     else
     {
         int length1 = that.Length;
         that.Length = (int)parameters[0].ToNumber();
         for (int length2 = that.Length; length2 < length1; ++length2)
         {
             that.Delete((JsInstance)this.Global.NumberClass.New((double)length2));
         }
     }
     return(parameters[0]);
 }
Example #8
0
        JsInstance SetLengthImpl(JsObject that, JsInstance[] parameters)
        {
            if (that is JsArray)
            {
                that.Length = (int)parameters[0].ToNumber();
            }
            else
            {
                int oldLen = that.Length;
                that.Length = (int)parameters[0].ToNumber();

                for (int i = that.Length; i < oldLen; i++)
                {
                    that.Delete(Global.NumberClass.New(i));
                }
            }

            return(parameters[0]);
        }
        /// <summary>
        /// 15.4.4.12
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Splice(JsObject target, JsInstance[] parameters)
        {
            JsArray array             = Global.ArrayClass.New();
            int     relativeStart     = Convert.ToInt32(parameters[0].ToNumber());
            int     actualStart       = relativeStart < 0 ? Math.Max(target.Length + relativeStart, 0) : Math.Min(relativeStart, target.Length);
            int     actualDeleteCount = Math.Min(Math.Max(Convert.ToInt32(parameters[1].ToNumber()), 0), target.Length - actualStart);
            int     len = target.Length;

            for (int k = 0; k < actualDeleteCount; k++)
            {
                string     from   = (relativeStart + k).ToString();
                JsInstance result = null;
                if (target.TryGetProperty(from, out result))
                {
                    array.DefineOwnProperty(k.ToString(), new ValueDescriptor(target, k.ToString(), result));
                }
            }

            List <JsInstance> items = new List <JsInstance>();

            items.AddRange(parameters);
            items.RemoveAt(0);
            items.RemoveAt(0);
            if (items.Count < actualDeleteCount)
            {
                for (int k = actualStart; k < len - actualDeleteCount; k++)
                {
                    JsInstance result = null;
                    string     from   = (k + actualDeleteCount).ToString();
                    string     to     = (k + items.Count).ToString();
                    if (target.TryGetProperty(from, out result))
                    {
                        target[to] = result;
                    }
                    else
                    {
                        target.Delete(to);
                    }
                }
                for (int k = target.Length; k > len - actualDeleteCount + items.Count; k--)
                {
                    target.Delete((k - 1).ToString());
                }
            }
            else
            {
                for (int k = len - actualDeleteCount; k > actualStart; k--)
                {
                    JsInstance result = null;
                    string     from   = (k + actualDeleteCount - 1).ToString();
                    string     to     = (k + items.Count - 1).ToString();
                    if (target.TryGetProperty(from, out result))
                    {
                        target[to] = result;
                    }
                    else
                    {
                        target.Delete(to);
                    }
                }
                if (target.length != int.MinValue && target.Prototype != Prototype)
                {
                    target.length += len - actualDeleteCount + items.Count;
                }

                for (int k = 0; items.Count > 0; k++)
                {
                    JsInstance e = items[0];
                    items.RemoveAt(0);
                    target[k.ToString()] = e;
                }
            }
            return(array);
        }
Example #10
0
        JsInstance SetLengthImpl(JsObject that, JsInstance[] parameters) {
            if (that is JsArray) {
                that.Length = (int)parameters[0].ToNumber();
            }
            else {
                int oldLen = that.Length;
                that.Length = (int)parameters[0].ToNumber();

                for (int i = that.Length; i < oldLen; i++)
                    that.Delete(Global.NumberClass.New(i));
            }

            return parameters[0];
        }
Example #11
0
 /// <summary>
 /// 15.4.4.13
 /// </summary>
 /// <param name="target"></param>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public JsInstance UnShift(JsObject target, JsInstance[] parameters) {
     for (int k = target.Length; k > 0; k--) {
         JsInstance result = null;
         string from = (k - 1).ToString();
         string to = (k + parameters.Length - 1).ToString();
         if (target.TryGetProperty(from, out result)) {
             target[to] = result;
         }
         else {
             target.Delete(to);
         }
     }
     List<JsInstance> items = new List<JsInstance>(parameters);
     for (int j = 0; items.Count > 0; j++) {
         JsInstance e = items[0];
         items.RemoveAt(0);
         target[j.ToString()] = e;
     }
     return Global.NumberClass.New(target.Length);
 }
Example #12
0
        /// <summary>
        /// 15.4.4.12
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Splice(JsObject target, JsInstance[] parameters) {
            JsArray array = Global.ArrayClass.New();
            int relativeStart = Convert.ToInt32(parameters[0].ToNumber());
            int actualStart = relativeStart < 0 ? Math.Max(target.Length + relativeStart, 0) : Math.Min(relativeStart, target.Length);
            int actualDeleteCount = Math.Min(Math.Max(Convert.ToInt32(parameters[1].ToNumber()), 0), target.Length - actualStart);
            int len = target.Length;

            for (int k = 0; k < actualDeleteCount; k++) {
                string from = (relativeStart + k).ToString();
                JsInstance result = null;
                if (target.TryGetProperty(from, out result)) {
                    array.put(k, result);
                }
            }

            List<JsInstance> items = new List<JsInstance>();
            items.AddRange(parameters);
            items.RemoveAt(0);
            items.RemoveAt(0);

            // use non-distructional copy, determine direction
            if (items.Count < actualDeleteCount) {
                for (int k = actualStart; k < len - actualDeleteCount; k++) {
                    JsInstance result = null;
                    string from = (k + actualDeleteCount).ToString();
                    string to = (k + items.Count).ToString();
                    if (target.TryGetProperty(from, out result)) {
                        target[to] = result;
                    }
                    else {
                        target.Delete(to);
                    }
                }

                for (int k = target.Length; k > len - actualDeleteCount + items.Count; k--) {
                    target.Delete((k - 1).ToString());
                }

                target.Length = len - actualDeleteCount + items.Count;
            }
            else {
                for (int k = len - actualDeleteCount; k > actualStart; k--) {
                    JsInstance result = null;
                    string from = (k + actualDeleteCount - 1).ToString();
                    string to = (k + items.Count - 1).ToString();
                    if (target.TryGetProperty(from, out result)) {
                        target[to] = result;
                    }
                    else {
                        target.Delete(to);
                    }
                }


            }
            for (int k = 0; k < items.Count; k++)
                target[k.ToString()] = items[k];

            return array;
        }
Example #13
0
 /// <summary>
 /// 15.4.4.6
 /// </summary>
 /// <param name="target"></param>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public JsInstance Pop(JsObject target, JsInstance[] parameters) {
     var length = Convert.ToUInt32(target.Length);
     if (length == 0)
         return JsUndefined.Instance;
     var key = (length - 1).ToString();
     var result = target[key];
     target.Delete(key);
     target.Length--;
     return result;
 }
Example #14
0
        /// <summary>
        /// 15.4.4.12
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Splice(JsObject target, JsInstance[] parameters)
        {
            JsArray array             = Global.ArrayClass.New();
            int     relativeStart     = Convert.ToInt32(parameters[0].ToNumber());
            int     actualStart       = relativeStart < 0 ? Math.Max(target.Length + relativeStart, 0) : Math.Min(relativeStart, target.Length);
            int     actualDeleteCount = Math.Min(Math.Max(Convert.ToInt32(parameters[1].ToNumber()), 0), target.Length - actualStart);
            int     len = target.Length;

            for (int k = 0; k < actualDeleteCount; k++)
            {
                string     from   = (relativeStart + k).ToString();
                JsInstance result = null;
                if (target.TryGetProperty(from, out result))
                {
                    array.put(k, result);
                }
            }

            List <JsInstance> items = new List <JsInstance>();

            items.AddRange(parameters);
            items.RemoveAt(0);
            items.RemoveAt(0);

            // use non-distructional copy, determine direction
            if (items.Count < actualDeleteCount)
            {
                for (int k = actualStart; k < len - actualDeleteCount; k++)
                {
                    JsInstance result = null;
                    string     from   = (k + actualDeleteCount).ToString();
                    string     to     = (k + items.Count).ToString();
                    if (target.TryGetProperty(from, out result))
                    {
                        target[to] = result;
                    }
                    else
                    {
                        target.Delete(to);
                    }
                }

                for (int k = target.Length; k > len - actualDeleteCount + items.Count; k--)
                {
                    target.Delete((k - 1).ToString());
                }

                target.Length = len - actualDeleteCount + items.Count;
            }
            else
            {
                for (int k = len - actualDeleteCount; k > actualStart; k--)
                {
                    JsInstance result = null;
                    string     from   = (k + actualDeleteCount - 1).ToString();
                    string     to     = (k + items.Count - 1).ToString();
                    if (target.TryGetProperty(from, out result))
                    {
                        target[to] = result;
                    }
                    else
                    {
                        target.Delete(to);
                    }
                }
            }
            for (int k = 0; k < items.Count; k++)
            {
                target[k.ToString()] = items[k];
            }

            return(array);
        }
Example #15
0
        /// <summary>
        /// 15.4.4.12
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Splice(JsObject target, JsInstance[] parameters)
        {
            JsArray array = Global.ArrayClass.New();
            int relativeStart = Convert.ToInt32(parameters[0].ToNumber());
            int actualStart = relativeStart < 0 ? Math.Max(target.Length + relativeStart, 0) : Math.Min(relativeStart, target.Length);
            int actualDeleteCount = Math.Min(Math.Max(Convert.ToInt32(parameters[1].ToNumber()), 0), target.Length - actualStart);
            int len = target.Length;

            for (int k = 0; k < actualDeleteCount; k++)
            {
                string from = (relativeStart + k).ToString();
                JsInstance result = null;
                if (target.TryGetProperty(from, out result))
                {
                    array.DefineOwnProperty(k.ToString(), new ValueDescriptor(target, k.ToString(), result));
                }
            }

            List<JsInstance> items = new List<JsInstance>();
            items.AddRange(parameters);
            items.RemoveAt(0);
            items.RemoveAt(0);
            if (items.Count < actualDeleteCount)
            {
                for (int k = actualStart; k < len - actualDeleteCount; k++)
                {
                    JsInstance result = null;
                    string from = (k + actualDeleteCount).ToString();
                    string to = (k + items.Count).ToString();
                    if (target.TryGetProperty(from, out result))
                    {
                        target[to] = result;
                    }
                    else
                    {
                        target.Delete(to);
                    }
                }
                for (int k = target.Length; k > len - actualDeleteCount + items.Count; k--)
                {
                    target.Delete((k - 1).ToString());
                }
            }
            else
            {
                for (int k = len - actualDeleteCount; k > actualStart; k--)
                {
                    JsInstance result = null;
                    string from = (k + actualDeleteCount - 1).ToString();
                    string to = (k + items.Count - 1).ToString();
                    if (target.TryGetProperty(from, out result))
                    {
                        target[to] = result;
                    }
                    else
                    {
                        target.Delete(to);
                    }
                }
                if (target.length != int.MinValue && target.Prototype != Prototype)
                    target.length += len - actualDeleteCount + items.Count;

                for (int k = 0; items.Count > 0; k++)
                {
                    JsInstance e = items[0];
                    items.RemoveAt(0);
                    target[k.ToString()] = e;
                }
            }
            return array;
        }
Example #16
0
        public JsInstance Splice(JsObject target, JsInstance[] parameters)
        {
            JsArray jsArray = this.Global.ArrayClass.New();
            int     int32   = Convert.ToInt32(parameters[0].ToNumber());
            int     num1    = int32 < 0 ? Math.Max(target.Length + int32, 0) : Math.Min(int32, target.Length);
            int     num2    = Math.Min(Math.Max(Convert.ToInt32(parameters[1].ToNumber()), 0), target.Length - num1);
            int     length1 = target.Length;
            int     num3;

            for (int i = 0; i < num2; ++i)
            {
                num3 = int32 + i;
                string     index  = num3.ToString();
                JsInstance result = (JsInstance)null;
                if (target.TryGetProperty(index, out result))
                {
                    jsArray.put(i, result);
                }
            }
            List <JsInstance> jsInstanceList = new List <JsInstance>();

            jsInstanceList.AddRange((IEnumerable <JsInstance>)parameters);
            jsInstanceList.RemoveAt(0);
            jsInstanceList.RemoveAt(0);
            if (jsInstanceList.Count < num2)
            {
                for (int index1 = num1; index1 < length1 - num2; ++index1)
                {
                    JsInstance result = (JsInstance)null;
                    num3 = index1 + num2;
                    string index2 = num3.ToString();
                    num3 = index1 + jsInstanceList.Count;
                    string index3 = num3.ToString();
                    if (target.TryGetProperty(index2, out result))
                    {
                        target[index3] = result;
                    }
                    else
                    {
                        target.Delete(index3);
                    }
                }
                for (int length2 = target.Length; length2 > length1 - num2 + jsInstanceList.Count; --length2)
                {
                    JsObject jsObject = target;
                    num3 = length2 - 1;
                    string index = num3.ToString();
                    jsObject.Delete(index);
                }
                target.Length = length1 - num2 + jsInstanceList.Count;
            }
            else
            {
                for (int index1 = length1 - num2; index1 > num1; --index1)
                {
                    JsInstance result = (JsInstance)null;
                    num3 = index1 + num2 - 1;
                    string index2 = num3.ToString();
                    num3 = index1 + jsInstanceList.Count - 1;
                    string index3 = num3.ToString();
                    if (target.TryGetProperty(index2, out result))
                    {
                        target[index3] = result;
                    }
                    else
                    {
                        target.Delete(index3);
                    }
                }
            }
            for (int index = 0; index < jsInstanceList.Count; ++index)
            {
                target[index.ToString()] = jsInstanceList[index];
            }
            return((JsInstance)jsArray);
        }
Example #17
0
	    private void PutDocument(string key, JsObject doc, JsObject meta)
	    {
	        if (doc == null)
	        {
	            throw new InvalidOperationException(
	                string.Format("Created document cannot be null or empty. Document key: '{0}'", key));
	        }

            var newDocument = new JsonDocument
            {
                Key = key,
                DataAsJson = ToRavenJObject(doc)
            };
            
	        if (meta == null)
	        {
	            RavenJToken value;
	            if (newDocument.DataAsJson.TryGetValue("@metadata", out value))
	            {
	                newDocument.DataAsJson.Remove("@metadata");
	                newDocument.Metadata = (RavenJObject) value;
	            }
	        }
	        else
	        {
	            foreach (var etagKeyName in EtagKeyNames)
	            {
	                JsInstance result;
	                if (!meta.TryGetProperty(etagKeyName, out result))
	                    continue;
	                string etag = result.ToString();
	                meta.Delete(etagKeyName);
	                if (string.IsNullOrEmpty(etag))
	                    continue;
	                Etag newDocumentEtag;
	                if (Etag.TryParse(etag, out newDocumentEtag) == false)
	                {
	                    throw new InvalidOperationException(string.Format("Invalid ETag value '{0}' for document '{1}'",
	                                                                      etag, key));
	                }
	                newDocument.Etag = newDocumentEtag;
	            }
	            newDocument.Metadata = ToRavenJObject(meta);
	        }
	        ValidateDocument(newDocument);

            if(key == null || key.EndsWith("/"))
                createdDocIdentity.Add(newDocument);
            else 
                createdDocDict[key] = newDocument;
	    }
Example #18
0
        /// <summary>
        /// 15.4.4.8
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance Reverse(JsObject target, JsInstance[] parameters) {
            int len = target.Length;
            int middle = len / 2;

            for (int lower = 0; lower != middle; lower++) {
                int upper = len - lower - 1;
                string upperP = upper.ToString();
                string lowerP = lower.ToString();

                JsInstance lowerValue = null;
                JsInstance upperValue = null;
                bool lowerExists = target.TryGetProperty(lowerP, out lowerValue);
                bool upperExists = target.TryGetProperty(upperP, out upperValue);

                if (lowerExists) {
                    target[upperP] = lowerValue;
                }
                else {
                    target.Delete(upperP);
                }

                if (upperExists) {
                    target[lowerP] = upperValue;
                }
                else {
                    target.Delete(lowerP);
                }
            }
            return target;
        }