} // ArrayConstructorFunction // Implementation for calling the Array constructor as a constructor. public static object ArrayConstructor(object this_, params object[] args) { JArrayObject arrayObj = new JArrayObject(); if (args.Length == 1 && JObject.Typeof(args[0]) == "number") { // Treat the one argument as a length, and create an array of // that length. UInt32 newLength = JConvert.ToUInt32(args[0]); if (newLength != JConvert.ToNumber(args[0])) throw new RangeError("array length in constructor is outside the UInt32 range"); arrayObj.arrayLength = newLength; } else { // Create an array with the entries specified in the args array. foreach (object curArg in args) arrayObj.entries.Add(curArg); arrayObj.arrayLength = (uint) args.Length; } return arrayObj; } // ArrayConstructor
} // split (implicit limit) // Overload of split where the separator and limit parameters are not specified. public static JArrayObject split(string _this) { JArrayObject A = new JArrayObject(); A.Add(_this); return A; } // split (implicit separator, limit)
} // 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
} // slice (implicit end) // Returns an Array object into which substrings of the result of converting // this object to a string have been stored. The substrings are determined // by searching from left to right for occurrences of separator; these // occurrences are not part of any substring in the returned array, but // serve to divide up the string value. The value of separator may be a // string of any length or it may be a RegExp object. public static JArrayObject split(string _this, string separator, uint limit) { // HACK snewman 10/17/01: add support for RegExpr separators. JArrayObject A = new JArrayObject(); if (limit == 0) return A; int len = _this.Length; int matchEnd; if (len == 0) { matchEnd = SplitMatch(separator, _this, 0); if (matchEnd < 0) A.Add(_this); return A; } // Look for split positions. int fragmentStart = 0; while (true) { bool foundFrag = false; for (int searchPos = fragmentStart+1; searchPos < len; searchPos++) { matchEnd = SplitMatch(separator, _this, searchPos); if (matchEnd >= 0) { A.Add(_this.Substring(fragmentStart, searchPos-fragmentStart)); if (A.ArrayLength >= limit) return A; fragmentStart = matchEnd; foundFrag = true; break; } } if (!foundFrag) break; } // while (true) A.Add(_this.Substring(fragmentStart, len-fragmentStart)); return A; // HACK snewman 10/2/01: the spec states that "the length property of // the split method is 2". Need to implement this. } // split
} // 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
} // 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
} // toLocaleString // When the concat method is called with zero or more arguments item1, // item2, etc., it returns an array containing the array elements of // the object followed by the array elements of each argument in order. public static JArrayObject concat(object _this, params object[] args) { JArrayObject newArray = new JArrayObject(); AppendToArray(newArray, _this); foreach (object obj in args) AppendToArray(newArray, obj); return newArray; // HACK snewman 10/2/01: the spec states that "the length property of // the concat method is 1". Need to implement this. } // concat
} // toString // The elements of the array are converted to strings using their // toLocaleString methods, and these strings are then concatenated, // separated by occurrences of a separator string that has been derived in // an implementation-defined locale-specific way. The result of calling // this function is intended to be analogous to the result of toString, // except that the result of this function is intended to be locale-specific. public static string toLocaleString(JArrayObject _this) { return joinCore(_this, ",", true); } // toLocaleString
// The result of calling this function is the same as if the built-in // "join" method were invoked for this object with no argument. public static string toString(JArrayObject _this) { return join(_this); } // toString