Skip to content

Commit

Permalink
Merge pull request #13 from KristofferStrube/feature/in-process-typed…
Browse files Browse the repository at this point in the history
…-arrays-and-buffers

Added InProcess variants of typed array and buffer types.
  • Loading branch information
KristofferStrube authored Mar 9, 2024
2 parents 9f951ee + ee7a621 commit 830e08f
Show file tree
Hide file tree
Showing 17 changed files with 641 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.JSInterop;

namespace KristofferStrube.Blazor.WebIDL;

/// <summary>
/// An object that holds a pointer (which can be null) to a buffer of a fixed number of bytes.
/// </summary>
/// <remarks><see href="https://tc39.es/ecma262/multipage/structured-data.html#sec-arraybuffer-objects">See the API definition here</see>.</remarks>
public class ArrayBufferInProcess : ArrayBuffer, IArrayBufferInProcess, IJSInProcessCreatable<ArrayBufferInProcess, ArrayBuffer>
{
/// <summary>
/// A lazily loaded task that evaluates to a helper module instance from the Blazor.WebIDL library.
/// </summary>
protected readonly IJSInProcessObjectReference inProcessHelper;

/// <inheritdoc/>
public new IJSInProcessObjectReference JSReference { get; }

/// <inheritdoc/>
public static async Task<ArrayBufferInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInProcessObjectReference jSReference)
{
return await CreateAsync(jSRuntime, jSReference, new());
}

/// <inheritdoc/>
public static async Task<ArrayBufferInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInProcessObjectReference jSReference, CreationOptions options)
{
IJSInProcessObjectReference inProcessHelper = await jSRuntime.GetInProcessHelperAsync();
return new(jSRuntime, inProcessHelper, jSReference, options);
}

/// <inheritdoc cref="ArrayBuffer(IJSRuntime, IJSObjectReference, CreationOptions)"/>
protected ArrayBufferInProcess(IJSRuntime jSRuntime, IJSInProcessObjectReference inProcessHelper, IJSInProcessObjectReference jSReference, CreationOptions options) : base(jSRuntime, jSReference, options)
{
this.inProcessHelper = inProcessHelper;
JSReference = jSReference;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace KristofferStrube.Blazor.WebIDL;

/// <summary>
/// A common interface for <see cref="ArrayBufferInProcess"/> and <see cref="SharedArrayBufferInProcess"/>.
/// </summary>
public interface IArrayBufferInProcess : IArrayBuffer
{
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace KristofferStrube.Blazor.WebIDL.Buffers;
namespace KristofferStrube.Blazor.WebIDL;

/// <summary>
/// A common interface for objects that provide a view on to an <see cref="ArrayBuffer"/> or <see cref="SharedArrayBuffer"/>.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.JSInterop;

namespace KristofferStrube.Blazor.WebIDL;

/// <summary>
/// An object that holds a pointer (which cannot be null) to a shared buffer of a fixed number of bytes
/// </summary>
/// <remarks><see href="https://tc39.es/ecma262/multipage/structured-data.html#sec-sharedarraybuffer-objects">See the API definition here</see>.</remarks>
[IJSWrapperConverter]
public class SharedArrayBufferInProcess : SharedArrayBuffer, IArrayBufferInProcess, IJSInProcessCreatable<SharedArrayBufferInProcess, SharedArrayBuffer>
{
/// <summary>
/// A lazily loaded task that evaluates to a helper module instance from the Blazor.WebIDL library.
/// </summary>
protected IJSInProcessObjectReference inProcessHelper;

/// <inheritdoc/>
public new IJSInProcessObjectReference JSReference { get; }

/// <inheritdoc/>
public static async Task<SharedArrayBufferInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInProcessObjectReference jSReference)
{
return await CreateAsync(jSRuntime, jSReference, new());
}

/// <inheritdoc/>
public static async Task<SharedArrayBufferInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInProcessObjectReference jSReference, CreationOptions options)
{
IJSInProcessObjectReference inProcessHelper = await jSRuntime.GetInProcessHelperAsync();
return new SharedArrayBufferInProcess(jSRuntime, inProcessHelper, jSReference, options);
}

/// <inheritdoc/>
protected SharedArrayBufferInProcess(IJSRuntime jSRuntime, IJSInProcessObjectReference inProcessHelper, IJSInProcessObjectReference jSReference, CreationOptions options) : base(jSRuntime, jSReference, options)
{
this.inProcessHelper = inProcessHelper;
JSReference = jSReference;
}
}
4 changes: 2 additions & 2 deletions src/KristofferStrube.Blazor.WebIDL/IJSCreatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace KristofferStrube.Blazor.WebIDL;
public interface IJSCreatable<T> : IJSWrapper where T : IJSCreatable<T>
{
/// <summary>
/// Constructs a wrapper instance for an equivalent JS instance.
/// Constructs a wrapper instance for an equivalent JS instance of a <typeparamref name="T"/>.
/// </summary>
/// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param>
/// <param name="jSReference">A JS reference to an existing JS instance that should be wrapped.</param>
public static abstract Task<T> CreateAsync(IJSRuntime jSRuntime, IJSObjectReference jSReference);

/// <summary>
/// Constructs a wrapper instance for an equivalent JS instance with the option for configuring how the wrapper is constructed.
/// Constructs a wrapper instance for an equivalent JS instance of a <typeparamref name="T"/> with the option for configuring how the wrapper is constructed.
/// </summary>
/// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param>
/// <param name="jSReference">A JS reference to an existing JS instance that should be wrapped.</param>
Expand Down
4 changes: 2 additions & 2 deletions src/KristofferStrube.Blazor.WebIDL/IJSInProcessCreatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public interface IJSInProcessCreatable<TInProcess, T> : IJSCreatable<T> where TI
public new IJSInProcessObjectReference JSReference { get; }

/// <summary>
/// Constructs an in-process wrapper instance for an equivalent JS instance.
/// Constructs an in-process wrapper instance for an equivalent JS instance of a <typeparamref name="TInProcess"/>.
/// </summary>
/// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param>
/// <param name="jSReference">A JS reference to an existing JS instance that should be wrapped.</param>
public static abstract Task<TInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInProcessObjectReference jSReference);

/// <summary>
/// Constructs an in-process wrapper instance for an equivalent JS instance with the option for configuring how the wrapper is constructed.
/// Constructs an in-process wrapper instance for an equivalent JS instance of a <typeparamref name="TInProcess"/> with the option for configuring how the wrapper is constructed.
/// </summary>
/// <param name="jSRuntime">An <see cref="IJSRuntime"/> instance.</param>
/// <param name="jSReference">A JS reference to an existing JS instance that should be wrapped.</param>
Expand Down
2 changes: 1 addition & 1 deletion src/KristofferStrube.Blazor.WebIDL/IJSWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface IJSWrapper : IAsyncDisposable
/// <summary>
/// Disposes the wrapper.
/// </summary>
public abstract new ValueTask DisposeAsync();
public new abstract ValueTask DisposeAsync();

/// <summary>
/// Disposes the underlying JSReference if <see cref="DisposesJSReference"/> is set to <see langword="true"/>.
Expand Down
12 changes: 6 additions & 6 deletions src/KristofferStrube.Blazor.WebIDL/IReadonlySetlike.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ public static async Task ForEachAsync<TSet>(this TSet set, Func<Task> function)
await helper.InvokeVoidAsync("forEachWithNoArguments", set, callbackObjRef);
}

public static async Task ForEachAsync<TSet, T>(this TSet set, Func<T, Task> function) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>
public static async Task ForEachAsync<TSet, T>(this TSet set, Func<T, Task> function) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>

Check warning on line 22 in src/KristofferStrube.Blazor.WebIDL/IReadonlySetlike.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'IReadonlySetlikeExtensions.ForEachAsync<TSet, T>(TSet, Func<T, Task>)'
{
Callback<T> callback = new(set.JSRuntime, function);
using DotNetObjectReference<Callback<T>> callbackObjRef = DotNetObjectReference.Create(callback);
IJSObjectReference helper = await set.JSRuntime.GetHelperAsync();
await helper.InvokeVoidAsync("forEachWithOneArgument", set, callbackObjRef);
}

public static async Task ForEachAsync<TSet, T>(this TSet set, Func<T, T, Task> function) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>
public static async Task ForEachAsync<TSet, T>(this TSet set, Func<T, T, Task> function) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>

Check warning on line 30 in src/KristofferStrube.Blazor.WebIDL/IReadonlySetlike.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'IReadonlySetlikeExtensions.ForEachAsync<TSet, T>(TSet, Func<T, T, Task>)'
{
Callback<T, T> callback = new(set.JSRuntime, function);
using DotNetObjectReference<Callback<T, T>> callbackObjRef = DotNetObjectReference.Create(callback);
IJSObjectReference helper = await set.JSRuntime.GetHelperAsync();
await helper.InvokeVoidAsync("forEachWithTwoArguments", set, callbackObjRef);
}

public static async Task ForEachAsync<TSet, T>(this TSet set, Func<T, T, TSet, Task> function) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>
public static async Task ForEachAsync<TSet, T>(this TSet set, Func<T, T, TSet, Task> function) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>

Check warning on line 38 in src/KristofferStrube.Blazor.WebIDL/IReadonlySetlike.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'IReadonlySetlikeExtensions.ForEachAsync<TSet, T>(TSet, Func<T, T, TSet, Task>)'
{
await set.ForEachAsync<TSet, T>(async (value, key) => await function(value, key, set));
}
Expand All @@ -45,12 +45,12 @@ public static async Task<bool> HasAsync<TSet, T>(this TSet set, T element) where
return await set.JSReference.InvokeAsync<bool>("has", element);
}

public static async Task<Iterator<T>> ValuesAsync<TSet, T>(this TSet set) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>
public static async Task<Iterator<T>> ValuesAsync<TSet, T>(this TSet set) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>

Check warning on line 48 in src/KristofferStrube.Blazor.WebIDL/IReadonlySetlike.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'IReadonlySetlikeExtensions.ValuesAsync<TSet, T>(TSet)'
{
return await Iterator<T>.CreateAsync(set.JSRuntime, await set.JSReference.InvokeAsync<IJSObjectReference>("values"), new() { DisposesJSReference = true});
return await Iterator<T>.CreateAsync(set.JSRuntime, await set.JSReference.InvokeAsync<IJSObjectReference>("values"), new() { DisposesJSReference = true });
}

public static async Task<Iterator<T>> KeysAsync<TSet, T>(this TSet set) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>
public static async Task<Iterator<T>> KeysAsync<TSet, T>(this TSet set) where TSet : IReadonlySetlike<TSet> where T : IJSCreatable<T>

Check warning on line 53 in src/KristofferStrube.Blazor.WebIDL/IReadonlySetlike.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'IReadonlySetlikeExtensions.KeysAsync<TSet, T>(TSet)'
{
return await set.ValuesAsync<TSet, T>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Microsoft.JSInterop;

namespace KristofferStrube.Blazor.WebIDL;

/// <summary>
/// A view on to a buffer type instance that exposes it as an array of IEEE 754 floating point numbers of 4 bytes.
/// </summary>
/// <remarks><see href="https://webidl.spec.whatwg.org/#idl-Float32Array">See the API definition here</see>.</remarks>
[IJSWrapperConverter]
public class Float32ArrayInProcess : Float32Array, ITypedArrayInProcess<float, Float32ArrayInProcess, Float32Array>
{
/// <summary>
/// A lazily loaded task that evaluates to a helper module instance from the Blazor.WebIDL library.
/// </summary>
public IJSInProcessObjectReference InProcessHelper { get; }

/// <inheritdoc/>
public new IJSInProcessObjectReference JSReference { get; }

/// <inheritdoc/>
public static async Task<Float32ArrayInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInProcessObjectReference jSReference)
{
return await CreateAsync(jSRuntime, jSReference, new());
}

/// <inheritdoc/>
public static async Task<Float32ArrayInProcess> CreateAsync(IJSRuntime jSRuntime, IJSInProcessObjectReference jSReference, CreationOptions options)
{
IJSInProcessObjectReference inProcessHelper = await jSRuntime.GetInProcessHelperAsync();
return new(jSRuntime, inProcessHelper, jSReference, options);
}

/// <inheritdoc/>
public static new async Task<Float32ArrayInProcess> CreateAsync(IJSRuntime jSRuntime)
{
return await ITypedArrayInProcess<float, Float32ArrayInProcess, Float32Array>.CreateInternalAsync(jSRuntime);
}

/// <inheritdoc/>
public static new async Task<Float32ArrayInProcess> CreateAsync<TFromElement, TFromTypedArray>(IJSRuntime jSRuntime, TypedArray<TFromElement, TFromTypedArray> typedArray) where TFromTypedArray : IJSCreatable<TFromTypedArray>
{
return await ITypedArrayInProcess<float, Float32ArrayInProcess, Float32Array>.CreateInternalAsync(jSRuntime, typedArray);
}

/// <inheritdoc/>
public static new async Task<Float32ArrayInProcess> CreateAsync(IJSRuntime jSRuntime, IArrayBuffer buffer, long? byteOffset, long? length)
{
return await ITypedArrayInProcess<float, Float32ArrayInProcess, Float32Array>.CreateInternalAsync(jSRuntime, buffer, byteOffset, length);
}

/// <inheritdoc/>
public static new async Task<Float32ArrayInProcess> CreateAsync(IJSRuntime jSRuntime, long length)
{
return await ITypedArrayInProcess<float, Float32ArrayInProcess, Float32Array>.CreateInternalAsync(jSRuntime, length);
}

/// <inheritdoc cref="IJSInProcessCreatable{TInProcess, T}.CreateAsync(IJSRuntime, IJSInProcessObjectReference, CreationOptions)"/>
protected Float32ArrayInProcess(IJSRuntime jSRuntime, IJSInProcessObjectReference inProcessHelper, IJSInProcessObjectReference jSReference, CreationOptions options) : base(jSRuntime, jSReference, options)
{
this.InProcessHelper = inProcessHelper;
JSReference = jSReference;
}

/// <inheritdoc/>
public new async Task<IArrayBufferInProcess> GetBufferAsync()
{
return await ITypedArrayInProcess<float, Float32ArrayInProcess, Float32Array>.GetBufferAsync(this);
}

/// <inheritdoc/>
public float At(long index)
{
return ITypedArrayInProcess<float, Float32ArrayInProcess, Float32Array>.At(this, index);
}

/// <inheritdoc/>
public long Length => ITypedArrayInProcess<float, Float32ArrayInProcess, Float32Array>.GetLength(this);
}
Loading

0 comments on commit 830e08f

Please sign in to comment.