} // sort // When the splice method is called with two or more arguments start, // deleteCount and (optionally) item1, item2, etc., the deleteCount // elements of the array starting at array index start are replaced by // the arguments item1, item2, etc. // // NOTE: we return a new array containing the deleted elements. This // is not explicitly mentioned in the description in the spec, but it // is part of the algorithm contained in the spec. public static JArrayObject splice( object _this, int start, int deleteCount, params object[] args ) { JArrayObject newArray = new JArrayObject(); uint length = JConvert.ToUInt32(Support.GetProperty(_this, "length")); uint argCount = (uint)args.Length; uint adjustedStart = (start < 0) ? Math.Max((uint)((int)length+start), 0) : Math.Min((uint)start, length); uint adjustedDel = Math.Min( (uint)Math.Max(deleteCount,0), (uint)(length-adjustedStart) ); uint newLength = length - adjustedDel + argCount; for (int k=0; k < adjustedDel; k++) { object entry = Support.GetProperty(_this, JArrayObject.ArrayIndexToString((uint)(adjustedStart+k)) ); if (entry is JUndefinedObject) newArray.IncrementLength(); else newArray.Add(entry); } if (argCount < adjustedDel) { for (uint k = adjustedStart; k < length - adjustedDel; k++) { object tempEntry = Support.GetProperty( _this, JArrayObject.ArrayIndexToString(k+adjustedDel) ); string kArgString = JArrayObject.ArrayIndexToString(k+argCount); if (tempEntry is JUndefinedObject) Support.DeleteProperty(_this, kArgString); else Support.AssignProperty(_this, kArgString, tempEntry); } for (uint k = length; k > newLength; k--) Support.DeleteProperty( _this, JArrayObject.ArrayIndexToString(k-1) ); } else if (argCount > adjustedDel) { for (uint k = length - adjustedDel; k > adjustedStart; k--) { object tempEntry = Support.GetProperty( _this, JArrayObject.ArrayIndexToString(k+adjustedDel-1) ); string kArgString = JArrayObject.ArrayIndexToString(k+argCount-1); if (tempEntry is JUndefinedObject) Support.DeleteProperty(_this, kArgString); else Support.AssignProperty(_this, kArgString, tempEntry); } } uint argIndex = adjustedStart; foreach (object curArg in args) { Support.AssignProperty( _this, JArrayObject.ArrayIndexToString(argIndex), curArg ); argIndex++; } Support.AssignProperty(_this, "length", newLength); return newArray; // HACK snewman 10/2/01: the spec states that "the length property of // the splice method is 2". Need to implement this. } // splice
} // toLocaleString // If obj is a JArrayObject, append its contents to array. Otherwise, // append obj to array. private static void AppendToArray(JArrayObject array, object obj) { if (obj is JArrayObject) { JArrayObject objAsArray = (JArrayObject)obj; uint arrayLen = objAsArray.ArrayLength; for (uint i=0; i<arrayLen; i++) { object curEntry = objAsArray.Get(JArrayObject.ArrayIndexToString(i)); if (!(curEntry is JUndefinedObject)) array.Add(curEntry); else array.IncrementLength(); } } else array.Add(obj); } // AppendToArray
} // join // The slice method takes two arguments, start and end, and returns an array // containing the elements of the array from element start up to, but not // including, element end (or through the end of the array if end is undefined). // If start is negative, it is treated as (length+start) where length is the // length of the array. If end is negative, it is treated as (length+end) // where length is the length of the array. public static JArrayObject slice(object _this, int start, int end) { JArrayObject newArray = new JArrayObject(); uint length = JConvert.ToUInt32(Support.GetProperty(_this, "length")); int adjustedStart = (start < 0) ? Math.Max((int)(length+start), 0) : Math.Min(start, (int)length); int adjustedEnd = (end < 0) ? Math.Max((int)(length+end), 0) : Math.Min(end, (int)length); uint n = 0; for (int k = adjustedStart; k < adjustedEnd; k++) { string kString = JArrayObject.ArrayIndexToString((uint)k); object entryK = Support.GetProperty(_this, kString); if (entryK is JUndefinedObject) newArray.IncrementLength(); else newArray.Add(entryK); n++; } return newArray; // HACK snewman 10/2/01: the spec states that "the length property of // the join method is 2". Need to implement this. } // slice