Skip to content

Commit

Permalink
Explicit BitCasts
Browse files Browse the repository at this point in the history
  • Loading branch information
SupinePandora43 committed Jul 4, 2024
1 parent 2c37d36 commit b2745c3
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/UltralightNet.AppCore/ULSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void FromManaged(ULSettings settings)
DeveloperName = new(settings.DeveloperName.AsSpan());
AppName = new(settings.AppName.AsSpan());
FileSystemPath = new(settings.FileSystemPath.AsSpan());
LoadShadersFromFileSystem = Unsafe.As<bool, byte>(ref settings.LoadShadersFromFileSystem);
ForceCPURenderer = Unsafe.As<bool, byte>(ref settings.ForceCPURenderer);
LoadShadersFromFileSystem = Methods.BitCast<bool, byte>(settings.LoadShadersFromFileSystem);
ForceCPURenderer = Methods.BitCast<bool, byte>(settings.ForceCPURenderer);
}

public readonly Marshaller ToUnmanaged() => this;
Expand Down
11 changes: 2 additions & 9 deletions src/UltralightNet/JavaScript/JSBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ public static unsafe partial class JavaScriptMethods
internal static void ThrowUnsupportedConstructor() => throw new NotSupportedException("Constructor is unsupported");
internal static Exception UnsupportedMethodException => new NotSupportedException("Method is unsupported");

// backported from net8.0 for compatibility
internal static TTo BitCast<TFrom, TTo>(TFrom from) where TFrom : unmanaged where TTo : unmanaged
{
Debug.Assert(sizeof(TFrom) == sizeof(TTo));
return Unsafe.As<TFrom, TTo>(ref Unsafe.AsRef(from));
}

[LibraryImport(LibWebCore)]
public static partial JSValueRef JSEvaluateScript(JSContextRef context, JSStringRef script, JSObjectRef thisObject, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception = null);

Expand All @@ -46,8 +39,8 @@ public abstract unsafe class JSNativeContainer<NativeHandle> : NativeContainer w
{
public NativeHandle JSHandle
{
get => JavaScriptMethods.BitCast<nuint, NativeHandle>((nuint)Handle);
protected init => Handle = (void*)JavaScriptMethods.BitCast<NativeHandle, nuint>(value);
get => Methods.BitCast<nuint, NativeHandle>((nuint)Handle);
protected init => Handle = (void*)Methods.BitCast<NativeHandle, nuint>(value);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/UltralightNet/JavaScript/JSContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public readonly struct JSGlobalContextRef
public static bool operator ==(JSGlobalContextRef left, JSGlobalContextRef right) => left._handle == right._handle;
public static bool operator !=(JSGlobalContextRef left, JSGlobalContextRef right) => left._handle != right._handle;

public static implicit operator JSContextRef(JSGlobalContextRef globalContextRef) => JavaScriptMethods.BitCast<JSGlobalContextRef, JSContextRef>(globalContextRef);
public static explicit operator JSGlobalContextRef(JSContextRef contextRef) => JavaScriptMethods.BitCast<JSContextRef, JSGlobalContextRef>(contextRef);
public static implicit operator JSContextRef(JSGlobalContextRef globalContextRef) => Methods.BitCast<JSGlobalContextRef, JSContextRef>(globalContextRef);
public static explicit operator JSGlobalContextRef(JSContextRef contextRef) => Methods.BitCast<JSContextRef, JSGlobalContextRef>(contextRef);
}
public readonly struct JSContextRef
{
Expand Down
4 changes: 2 additions & 2 deletions src/UltralightNet/JavaScript/JSObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public readonly struct JSObjectRef
public static bool operator ==(JSObjectRef left, JSObjectRef right) => left._handle == right._handle;
public static bool operator !=(JSObjectRef left, JSObjectRef right) => left._handle != right._handle;

public static implicit operator JSValueRef(JSObjectRef jsObject) => JavaScriptMethods.BitCast<JSObjectRef, JSValueRef>(jsObject);
public static explicit operator JSObjectRef(JSValueRef jsValue) => JavaScriptMethods.BitCast<JSValueRef, JSObjectRef>(jsValue);
public static implicit operator JSValueRef(JSObjectRef jsObject) => Methods.BitCast<JSObjectRef, JSValueRef>(jsObject);
public static explicit operator JSObjectRef(JSValueRef jsValue) => Methods.BitCast<JSValueRef, JSObjectRef>(jsValue);
}
public readonly struct JSClassRef
{
Expand Down
4 changes: 2 additions & 2 deletions src/UltralightNet/JavaScript/Structs/JSStaticValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public unsafe struct JSStaticValue
public delegate* unmanaged[Cdecl]<void*, void*, void*, void**, void*> getProperty;
public delegate* unmanaged[Cdecl]<void*, void*, void*, void*, void**, bool> setProperty;
private uint _Attributes;
public JSPropertyAttributes Attributes { get => Unsafe.As<uint, JSPropertyAttributes>(ref _Attributes); set => _Attributes = Unsafe.As<JSPropertyAttributes, uint>(ref value); }
public JSPropertyAttributes Attributes { readonly get => Methods.BitCast<uint, JSPropertyAttributes>(_Attributes); set => _Attributes = Methods.BitCast<JSPropertyAttributes, uint>(value); }
}
public unsafe struct JSStaticValueEx
{
public byte* name;
public delegate* unmanaged[Cdecl]<void*, void*, void*, void*, void**, void*> getPropertyEx;
public delegate* unmanaged[Cdecl]<void*, void*, void*, void*, void*, void**, bool> setPropertyEx;
private uint _Attributes;
public JSPropertyAttributes Attributes { get => Unsafe.As<uint, JSPropertyAttributes>(ref _Attributes); set => _Attributes = Unsafe.As<JSPropertyAttributes, uint>(ref value); }
public JSPropertyAttributes Attributes { readonly get => Methods.BitCast<uint, JSPropertyAttributes>(_Attributes); set => _Attributes = Methods.BitCast<JSPropertyAttributes, uint>(value); }
}
12 changes: 12 additions & 0 deletions src/UltralightNet/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,16 @@ public static void Preload()
}
#endif
}

// backported from net8.0 for compatibility
internal static TTo BitCast<TFrom, TTo>(TFrom from) where TFrom : unmanaged where TTo : unmanaged
#if !NET8_0_OR_GREATER
{
System.Diagnostics.Debug.Assert(sizeof(TFrom) == sizeof(TTo));
return Unsafe.As<TFrom, TTo>(ref from);
}
#else
=> Unsafe.BitCast<TFrom, TTo>(from);
#endif

}
4 changes: 2 additions & 2 deletions src/UltralightNet/Structs/RenderTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public struct RenderTarget : IEquatable<RenderTarget>
{
private byte _IsEmpty;
/// <summary>Whether this target is empty (null texture)</summary>
public bool IsEmpty { readonly get => Unsafe.As<byte, bool>(ref Unsafe.AsRef(_IsEmpty)); set => _IsEmpty = Unsafe.As<bool, byte>(ref value); }
public bool IsEmpty { readonly get => Methods.BitCast<byte, bool>(_IsEmpty); set => _IsEmpty = Methods.BitCast<bool, byte>(value); }

/// <summary>The viewport width (in device coordinates).</summary>
public uint Width;
Expand All @@ -26,7 +26,7 @@ public struct RenderTarget : IEquatable<RenderTarget>

private byte _TextureFormat;
/// <summary>The pixel format of the texture.</summary>
public ULBitmapFormat TextureFormat { readonly get => Unsafe.As<byte, ULBitmapFormat>(ref Unsafe.AsRef(_TextureFormat)); set => _TextureFormat = Unsafe.As<ULBitmapFormat, byte>(ref value); }
public ULBitmapFormat TextureFormat { readonly get => Methods.BitCast<byte, ULBitmapFormat>(_TextureFormat); set => _TextureFormat = Methods.BitCast<ULBitmapFormat, byte>(value); }

/// <summary>UV coordinates of the texture (this is needed because the texture may be padded).</summary>
public ULRect UV;
Expand Down
2 changes: 1 addition & 1 deletion src/UltralightNet/Structs/ULCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace UltralightNet;
public struct ULCommand : IEquatable<ULCommand>
{
private byte _CommandType;
public ULCommandType CommandType { readonly get => Unsafe.As<byte, ULCommandType>(ref Unsafe.AsRef(_CommandType)); set => _CommandType = Unsafe.As<ULCommandType, byte>(ref value); }
public ULCommandType CommandType { readonly get => Methods.BitCast<byte, ULCommandType>(_CommandType); set => _CommandType = Methods.BitCast<ULCommandType, byte>(value); }
public ULGPUState GPUState;

/// <remarks>Only used when <see cref="CommandType"/> is <see cref="ULCommandType.DrawGeometry"/></remarks>
Expand Down
8 changes: 4 additions & 4 deletions src/UltralightNet/Structs/ULGPUState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public unsafe struct ULGPUState : IEquatable<ULGPUState>
public Matrix4x4 Transform;

private byte _EnableTexturing;
public bool EnableTexturing { readonly get => Unsafe.As<byte, bool>(ref Unsafe.AsRef(_EnableTexturing)); set => _EnableTexturing = Unsafe.As<bool, byte>(ref value); }
public bool EnableTexturing { readonly get => Methods.BitCast<byte, bool>(_EnableTexturing); set => _EnableTexturing = Methods.BitCast<bool, byte>(value); }

private byte _EnableBlend;
public bool EnableBlend { readonly get => Unsafe.As<byte, bool>(ref Unsafe.AsRef(_EnableBlend)); set => _EnableBlend = Unsafe.As<bool, byte>(ref value); }
public bool EnableBlend { readonly get => Methods.BitCast<byte, bool>(_EnableBlend); set => _EnableBlend = Methods.BitCast<bool, byte>(value); }

private byte _ShaderType;
public ULShaderType ShaderType { readonly get => Unsafe.As<byte, ULShaderType>(ref Unsafe.AsRef(_ShaderType)); set => _ShaderType = Unsafe.As<ULShaderType, byte>(ref value); }
public ULShaderType ShaderType { readonly get => Methods.BitCast<byte, ULShaderType>(_ShaderType); set => _ShaderType = Methods.BitCast<ULShaderType, byte>(value); }

public uint RenderBufferId;

Expand Down Expand Up @@ -92,7 +92,7 @@ public byte ClipSize
#endif

private byte _EnableScissor;
public bool EnableScissor { readonly get => Unsafe.As<byte, bool>(ref Unsafe.AsRef(_EnableScissor)); set => _EnableScissor = Unsafe.As<bool, byte>(ref value); }
public bool EnableScissor { readonly get => Methods.BitCast<byte, bool>(_EnableScissor); set => _EnableScissor = Methods.BitCast<bool, byte>(value); }

public ULIntRect ScissorRect;

Expand Down
4 changes: 2 additions & 2 deletions src/UltralightNet/Structs/ULMouseEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public static unsafe partial class Methods
public struct ULMouseEvent : IEquatable<ULMouseEvent>
{
private int _Type;
public ULMouseEventType Type { readonly get => Unsafe.As<int, ULMouseEventType>(ref Unsafe.AsRef(_Type)); set => _Type = Unsafe.As<ULMouseEventType, int>(ref value); }
public ULMouseEventType Type { readonly get => Methods.BitCast<int, ULMouseEventType>(_Type); set => _Type = Methods.BitCast<ULMouseEventType, int>(value); }
public int X;
public int Y;
private int _Button;
public ULMouseEventButton Button { readonly get => Unsafe.As<int, ULMouseEventButton>(ref Unsafe.AsRef(_Button)); set => _Button = Unsafe.As<ULMouseEventButton, int>(ref value); }
public ULMouseEventButton Button { readonly get => Methods.BitCast<int, ULMouseEventButton>(_Button); set => _Button = Methods.BitCast<ULMouseEventButton, int>(value); }

public readonly bool Equals(ULMouseEvent other) => Type == other.Type && X == other.X && Y == other.Y && Button == other.Button;
}
8 changes: 4 additions & 4 deletions src/UltralightNet/Structs/ULRenderBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public struct ULRenderBuffer
private byte _HasStencilBuffer;
public bool HasStencilBuffer
{
readonly get => Unsafe.As<byte, bool>(ref Unsafe.AsRef(_HasStencilBuffer));
set => _HasStencilBuffer = Unsafe.As<bool, byte>(ref Unsafe.AsRef(value));
readonly get => Methods.BitCast<byte, bool>(_HasStencilBuffer);
set => _HasStencilBuffer = Methods.BitCast<bool, byte>(value);
}
private byte _HasDepthBuffer;
public bool HasDepthBuffer
{
readonly get => Unsafe.As<byte, bool>(ref Unsafe.AsRef(_HasDepthBuffer));
set => _HasDepthBuffer = Unsafe.As<bool, byte>(ref Unsafe.AsRef(value));
readonly get => Methods.BitCast<byte, bool>(_HasDepthBuffer);
set => _HasDepthBuffer = Methods.BitCast<bool, byte>(value);
}
}
2 changes: 1 addition & 1 deletion src/UltralightNet/Structs/ULScrollEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct ULScrollEvent : IEquatable<ULScrollEvent>
/// <summary>
/// Type of event
/// </summary>
public ULScrollEventType Type { readonly get => Unsafe.As<int, ULScrollEventType>(ref Unsafe.AsRef(_Type)); set => _Type = Unsafe.As<ULScrollEventType, int>(ref value); }
public ULScrollEventType Type { readonly get => Methods.BitCast<int, ULScrollEventType>(_Type); set => _Type = Methods.BitCast<ULScrollEventType, int>(value); }

/// <summary>
/// horizontal scroll
Expand Down
4 changes: 2 additions & 2 deletions src/UltralightNet/Structs/ULVertexBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public unsafe ref struct ULVertexBuffer

public ULVertexBufferFormat Format
{
readonly get => Unsafe.As<byte, ULVertexBufferFormat>(ref Unsafe.AsRef(format));
set => format = Unsafe.As<ULVertexBufferFormat, byte>(ref value);
readonly get => Methods.BitCast<byte, ULVertexBufferFormat>(format);
set => format = Methods.BitCast<ULVertexBufferFormat, byte>(value);
}
}
6 changes: 3 additions & 3 deletions src/UltralightNet/ULConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ public void FromManaged(ULConfig config)
{
CachePath = new(config.CachePath.AsSpan());
ResourcePathPrefix = new ULString(config.ResourcePathPrefix.AsSpan());
FaceWinding = Unsafe.As<ULFaceWinding, byte>(ref Unsafe.AsRef(config.FaceWinding));
FontHinting = Unsafe.As<ULFontHinting, byte>(ref Unsafe.AsRef(config.FontHinting));
FaceWinding = Methods.BitCast<ULFaceWinding, byte>(config.FaceWinding);
FontHinting = Methods.BitCast<ULFontHinting, byte>(config.FontHinting);
FontGamma = config.FontGamma;
UserStylesheet = new(config.UserStylesheet.AsSpan());
ForceRepaint = Unsafe.As<bool, byte>(ref Unsafe.AsRef(config.ForceRepaint));
ForceRepaint = Methods.BitCast<bool, byte>(config.ForceRepaint);
AnimationTimerDelay = config.AnimationTimerDelay;
ScrollTimerDelay = config.ScrollTimerDelay;
RecycleDelay = config.RecycleDelay;
Expand Down
10 changes: 5 additions & 5 deletions src/UltralightNet/ULViewConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ internal ref struct Marshaller

public void FromManaged(ULViewConfig config)
{
IsAccelerated = Unsafe.As<bool, byte>(ref config.IsAccelerated);
IsTransparent = Unsafe.As<bool, byte>(ref config.IsTransparent);
IsAccelerated = Methods.BitCast<bool, byte>(config.IsAccelerated);
IsTransparent = Methods.BitCast<bool, byte>(config.IsTransparent);
InitialDeviceScale = config.InitialDeviceScale;
InitialFocus = Unsafe.As<bool, byte>(ref config.InitialFocus);
EnableImages = Unsafe.As<bool, byte>(ref config.EnableImages);
EnableJavaScript = Unsafe.As<bool, byte>(ref config.EnableJavaScript);
InitialFocus = Methods.BitCast<bool, byte>(config.InitialFocus);
EnableImages = Methods.BitCast<bool, byte>(config.EnableImages);
EnableJavaScript = Methods.BitCast<bool, byte>(config.EnableJavaScript);
FontFamilyStandard = new(config.FontFamilyStandard.AsSpan());
FontFamilyFixed = new(config.FontFamilyFixed.AsSpan());
FontFamilySerif = new(config.FontFamilySerif.AsSpan());
Expand Down

0 comments on commit b2745c3

Please sign in to comment.