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