/// <summary> /// Creates a new <see cref="T:WebAssembly.Core.TypedArray`2"/> from the ReadOnlySpan. /// </summary> /// <returns>The new TypedArray/</returns> /// <param name="span">ReadOnlySpan.</param> public unsafe static T From(ReadOnlySpan <U> span) { // source has to be instantiated. ValidateFromSource(span); TypedArrayTypeCode type = (TypedArrayTypeCode)Type.GetTypeCode(typeof(U)); // Special case for Uint8ClampedArray, a clamped array which represents an array of 8-bit unsigned integers clamped to 0-255; if (type == TypedArrayTypeCode.Uint8Array && typeof(T) == typeof(Uint8ClampedArray)) { type = TypedArrayTypeCode.Uint8ClampedArray; // This is only passed to the JavaScript side so it knows it will be a Uint8ClampedArray } var bytes = MemoryMarshal.AsBytes(span); fixed(byte *ptr = bytes) { var res = Runtime.TypedArrayFrom((int)ptr, 0, span.Length, Marshal.SizeOf <U> (), (int)type, out int exception); if (exception != 0) { throw new JSException((string)res); } return((T)res); } }
public static unsafe T From(ReadOnlySpan <U> span) { // source has to be instantiated. if (span == null) { throw new System.ArgumentException($"Invalid argument: {nameof(span)} can not be null."); } TypedArrayTypeCode type = (TypedArrayTypeCode)Type.GetTypeCode(typeof(U)); // Special case for Uint8ClampedArray, a clamped array which represents an array of 8-bit unsigned integers clamped to 0-255; if (type == TypedArrayTypeCode.Uint8Array && typeof(T) == typeof(Uint8ClampedArray)) { type = TypedArrayTypeCode.Uint8ClampedArray; // This is only passed to the JavaScript side so it knows it will be a Uint8ClampedArray } ReadOnlySpan <byte> bytes = MemoryMarshal.AsBytes(span); fixed(byte *ptr = bytes) { object res = Interop.Runtime.TypedArrayFrom((int)ptr, 0, span.Length, Unsafe.SizeOf <U>(), (int)type, out int exception); if (exception != 0) { throw new JSException((string)res); } return((T)res); } }