private DUint16Array createArrayFromObject(mdr.DObject array, int byteoffset = 0, int bytelength = 0) { var buffer = array as DArrayBuffer; if (buffer != null) { bytelength = (bytelength > 0) ? bytelength : buffer.ByteLength - byteoffset; checkOffsetCompatibility(byteoffset, bytelength); return(new DUint16Array(TargetPrototype, buffer, byteoffset, bytelength, TypeSize)); } var darray = array as DTypedArray; if (darray != null) { bytelength = (bytelength > 0) ? bytelength : darray.ByteLength / darray.TypeSize * TypeSize; checkOffsetCompatibility(byteoffset, bytelength); DUint16Array tarray = new DUint16Array(TargetPrototype, bytelength, TypeSize); fillArray(tarray, darray); return(tarray); } Trace.Fail("invalid Arguments"); return(null); }
private void fillArray(DUint16Array dst, mdr.DArrayBase src) { for (int i = 0; i < dst.ByteLength / dst.TypeSize; ++i) { UInt16 value = src.GetPropertyDescriptor(i).Get(src).AsUInt16(); dst.AddPropertyDescriptor(i).Set(dst, value); } }
public JSUint16Array() : base(new mdr.DObject(), "Uint16Array", TypeSize) { JittedCode = (ref mdr.CallFrame callFrame) => { DUint16Array uint16array = new DUint16Array(TargetPrototype, 0, TypeSize); var argsCount = callFrame.PassedArgsCount; if (argsCount == 1) { switch (callFrame.Arg0.ValueType) { case mdr.ValueTypes.Int32: uint16array = new DUint16Array(TargetPrototype, callFrame.Arg0.AsInt32() * TypeSize, TypeSize); break; case mdr.ValueTypes.Object: uint16array = createArrayFromObject(callFrame.Arg0.AsDObject()); break; case mdr.ValueTypes.Array: mdr.DArray array = callFrame.Arg0.AsDArray(); uint16array = new DUint16Array(TargetPrototype, array.Length * TypeSize, TypeSize); fillArray(uint16array, array); break; default: Trace.Fail("invalid Arguments"); break; } } if (argsCount == 2) { int byteoffset = callFrame.Arg1.AsInt32(); uint16array = createArrayFromObject(callFrame.Arg0.AsDObject(), byteoffset); } if (argsCount == 3) { var byteoffset = callFrame.Arg1.AsInt32(); var length = callFrame.Arg2.AsInt32(); checkOffsetMemBoundary(byteoffset); uint16array = createArrayFromObject(callFrame.Arg0.AsDObject(), byteoffset, length * TypeSize); } if (IsConstrutor) { callFrame.This = (uint16array); } else { callFrame.Return.Set(uint16array); } }; TargetPrototype.DefineOwnProperty("subarray", new mdr.DFunction((ref mdr.CallFrame callFrame) => { DUint16Array array = (callFrame.This as DUint16Array); var argsCount = callFrame.PassedArgsCount; var begin = (argsCount >= 1) ? callFrame.Arg0.AsInt32() : 0; var end = array.ByteLength / array.TypeSize; end = (argsCount >= 2) ? callFrame.Arg1.AsInt32() : end; begin = begin < 0 ? begin += array.ByteLength / array.TypeSize : begin; end = end < 0 ? end += array.ByteLength / array.TypeSize : end; var bytelength = Math.Max(0, (end - begin)) * array.TypeSize; end = Math.Max(array.ByteLength, bytelength); var uint16array = new DUint16Array(TargetPrototype, bytelength, array.TypeSize); int offset = (begin <= 0) ? 0 : begin * array.TypeSize; for (int i = 0; i < uint16array.ByteLength; ++i) { uint16array.Elements_[i] = array.Elements_[i + offset]; } callFrame.Return.Set(uint16array); }), mdr.PropertyDescriptor.Attributes.NotEnumerable | mdr.PropertyDescriptor.Attributes.Data); }