internal static String Join(Object thisob, String separator, bool localize) { StringBuilder str = new StringBuilder(); uint thisLength = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); if (thisLength > int.MaxValue) { throw new JScriptException(JSError.OutOfMemory); } // Small optimization so we're not doing a bunch of reallocs for large arrays. if (thisLength > str.Capacity) { str.Capacity = (int)thisLength; } for (uint i = 0; i < thisLength; i++) { Object value = LateBinding.GetValueAtIndex(thisob, i); if (value != null && !(value is Missing)) { if (localize) { str.Append(Convert.ToLocaleString(value)); } else { str.Append(Convert.ToString(value)); } } if (i < thisLength - 1) { str.Append(separator); } } return(str.ToString()); }
internal static String Join(Object thisob, String separator, bool localize) { StringBuilder str = new StringBuilder(); uint thisLength = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); for (uint i = 0; i < thisLength; i++) { Object value = LateBinding.GetValueAtIndex(thisob, i); if (value != null && !(value is Missing)) { if (localize) { str.Append(Convert.ToLocaleString(value)); } else { str.Append(Convert.ToString(value)); } } if (i < thisLength - 1) { str.Append(separator); } } return(str.ToString()); }
public static Object unshift(Object thisob, params Object[] args) { if (args == null || args.Length == 0) { return(thisob); } if (thisob is ArrayObject) { return(((ArrayObject)thisob).Unshift(args)); } uint oldLength = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); long newLength = oldLength + args.Length; LateBinding.SetMemberValue(thisob, "length", newLength); // shift the array for (long i = oldLength - 1; i >= 0; i--) { Object val = LateBinding.GetValueAtIndex(thisob, (ulong)i); if (val is Missing) { LateBinding.DeleteValueAtIndex(thisob, (ulong)(i + args.Length)); } else { LateBinding.SetValueAtIndex(thisob, (ulong)(i + args.Length), val); } } // copy the input args for (uint i = 0; i < args.Length; i++) { LateBinding.SetValueAtIndex(thisob, i, args[i]); } return(thisob); }
public static Object shift(Object thisob) { Object res = null; if (thisob is ArrayObject) { return(((ArrayObject)thisob).Shift()); } uint length = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); if (length == 0) { LateBinding.SetMemberValue(thisob, "length", 0); return(res); } res = LateBinding.GetValueAtIndex(thisob, 0); for (uint i = 1; i < length; i++) { Object val = LateBinding.GetValueAtIndex(thisob, i); if (val is Missing) { LateBinding.DeleteValueAtIndex(thisob, i - 1); } else { LateBinding.SetValueAtIndex(thisob, i - 1, val); } } LateBinding.DeleteValueAtIndex(thisob, length - 1); LateBinding.SetMemberValue(thisob, "length", length - 1); return(res); }
internal ArrayObject(object o) { IConvertible ic = o as IConvertible; TypeCode tc = Convert.GetTypeCode(o, ic); try { if (Convert.IsNumberTypeCode(tc)) { uint size = Convert.ToUint32(o); if (size >= 0) { _length = size; return; } else { Console.WriteLine("size = {0}", size); throw new JScriptException(JSError.ArrayLengthConstructIncorrect); } } } catch (FormatException) { /* OK */ } elems = new Hashtable(); _length = (uint)1; elems.Add(0, o); }
public new ArrayObject CreateInstance(params Object[] args) { ArrayObject arrayObj = new ArrayObject(this.originalPrototype, typeof(ArrayObject)); if (args.Length != 0) { if (args.Length == 1) { Object arg0 = args[0]; IConvertible ic = Convert.GetIConvertible(arg0); switch (Convert.GetTypeCode(arg0, ic)) { case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: double d = Convert.ToNumber(arg0, ic); uint len = Convert.ToUint32(arg0, ic); if (d != (double)len) { throw new JScriptException(JSError.ArrayLengthConstructIncorrect); } arrayObj.length = len; return(arrayObj); } } if (args.Length == 1 && args[0] is Array) { Array array = (Array)args[0]; if (array.Rank != 1) { throw new JScriptException(JSError.TypeMismatch); } arrayObj.length = array.Length; for (int i = 0; i < array.Length; i++) { arrayObj.SetValueAtIndex((uint)i, array.GetValue(i)); } } else { arrayObj.length = args.Length; for (int i = 0; i < args.Length; i++) { arrayObj.SetValueAtIndex((uint)i, args[i]); } } } return(arrayObj); }
internal static bool IsArrayIndex(string key) { if (key == "" || NonDigitRegex.IsMatch(key)) { return(false); } uint index = Convert.ToUint32(key); return(Convert.ToString(index) == key); }
public static ArrayObject slice(object thisObj, VsaEngine engine, double start, object end) { // TODO: Shouldn't this be generic!? SemanticAnalyser.assert_type(thisObj, typeof(ArrayObject)); ArrayObject array_obj = (ArrayObject)thisObj; uint array_len = (uint)array_obj.length; uint _start, _end; if (start > array_len) { _start = array_len; } else { _start = (uint)start; if (_start < 0) { _start += array_len; } } if (end == null) { _end = array_len; } else { _end = Convert.ToUint32(end); if (_end < 0) { _end += array_len; } else if (_end > array_len) { _end = array_len; } } if (_end < _start) { _end = _start; } ArrayObject result = new ArrayObject(); result.length = _end - _start; for (uint i = _start; i < _end; i++) { result.elems [i - _start] = array_obj.elems [i]; } return(result); }
public static Object reverse(Object thisob) { uint thisLength = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); uint halfSize = thisLength / 2; for (uint low = 0, high = thisLength - 1; low < halfSize; low++, high--) { LateBinding.SwapValues(thisob, low, high); } return(thisob); }
public static ArrayObject slice(Object thisob, VsaEngine engine, double start, Object end) { ArrayObject array = engine.GetOriginalArrayConstructor().Construct(); uint length = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); // compute the start index long startIndex = Runtime.DoubleToInt64(Convert.ToInteger(start)); if (startIndex < 0) { startIndex = length + startIndex; if (startIndex < 0) { startIndex = 0; } } else if (startIndex > length) { startIndex = length; } // compute the end index long endIndex = length; if (end != null && !(end is Missing)) { endIndex = Runtime.DoubleToInt64(Convert.ToInteger(end)); if (endIndex < 0) { endIndex = length + endIndex; if (endIndex < 0) { endIndex = 0; } } else if (endIndex > length) { endIndex = length; } } // slice if (endIndex > startIndex) { array.length = endIndex - startIndex; for (ulong i = (ulong)startIndex, j = 0; i < (ulong)endIndex; i++, j++) { Object val = LateBinding.GetValueAtIndex(thisob, i); if (!(val is Missing)) { LateBinding.SetValueAtIndex(array, j, val); } } } return(array); }
public static long push(Object thisob, params Object[] args) { uint length = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); for (uint i = 0; i < args.Length; i++) { LateBinding.SetValueAtIndex(thisob, i + (ulong)length, args[i]); } long newLength = length + args.Length; LateBinding.SetMemberValue(thisob, "length", newLength); return(newLength); }
public static Object pop(Object thisob) { uint thisLength = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); if (thisLength == 0) { LateBinding.SetMemberValue(thisob, "length", 0); return(null); } Object result = LateBinding.GetValueAtIndex(thisob, thisLength - 1); LateBinding.DeleteValueAtIndex(thisob, thisLength - 1); LateBinding.SetMemberValue(thisob, "length", thisLength - 1); return(result); }
public static Object sort(Object thisob, Object function) { ScriptFunction func = null; if (function is ScriptFunction) { func = (ScriptFunction)function; } uint length = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); if (thisob is ArrayObject) { ((ArrayObject)thisob).Sort(func); } else if (length <= Int32.MaxValue) { QuickSort qs = new QuickSort(thisob, func); qs.SortObject(0, length); } return(thisob); }
public static ArrayObject splice(Object thisob, VsaEngine engine, double start, double deleteCnt, params Object[] args) { uint oldLength = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length")); // compute the start index long startIndex = Runtime.DoubleToInt64(Convert.ToInteger(start)); if (startIndex < 0) { startIndex = oldLength + startIndex; if (startIndex < 0) { startIndex = 0; } } else if (startIndex > oldLength) { startIndex = oldLength; } // compute the number of items to delete long deleteCount = Runtime.DoubleToInt64(Convert.ToInteger(deleteCnt)); if (deleteCount < 0) { deleteCount = 0; } else if (deleteCount > oldLength - startIndex) { deleteCount = oldLength - startIndex; } long newLength = oldLength + args.Length - deleteCount; // create an array for the result ArrayObject result = engine.GetOriginalArrayConstructor().Construct(); result.length = deleteCount; // special case array objects (nice speedup if dense) if (thisob is ArrayObject) { ((ArrayObject)thisob).Splice((uint)startIndex, (uint)deleteCount, args, result, (uint)oldLength, (uint)newLength); return(result); } // copy the deleted items to the result array for (ulong i = 0; i < (ulong)deleteCount; i++) { result.SetValueAtIndex((uint)i, LateBinding.GetValueAtIndex(thisob, i + (ulong)startIndex)); } // shift the remaining elements left or right long n = oldLength - startIndex - deleteCount; if (newLength < oldLength) { for (long i = 0; i < n; i++) { LateBinding.SetValueAtIndex(thisob, (ulong)(i + startIndex + args.Length), LateBinding.GetValueAtIndex(thisob, (ulong)(i + startIndex + deleteCount))); } LateBinding.SetMemberValue(thisob, "length", newLength); } else { LateBinding.SetMemberValue(thisob, "length", newLength); for (long i = n - 1; i >= 0; i--) { LateBinding.SetValueAtIndex(thisob, (ulong)(i + startIndex + args.Length), LateBinding.GetValueAtIndex(thisob, (ulong)(i + startIndex + deleteCount))); } } // splice in the arguments int m = args == null ? 0 : args.Length; for (uint i = 0; i < m; i++) { LateBinding.SetValueAtIndex(thisob, i + (ulong)startIndex, args[i]); } return(result); }