From b2745c3125cbd3d8d1a2611416787bdc63ed3154 Mon Sep 17 00:00:00 2001 From: SupinePandora43 Date: Thu, 4 Jul 2024 12:20:15 +0500 Subject: [PATCH] Explicit BitCasts --- src/UltralightNet.AppCore/ULSettings.cs | 4 ++-- src/UltralightNet/JavaScript/JSBase.cs | 11 ++--------- src/UltralightNet/JavaScript/JSContext.cs | 4 ++-- src/UltralightNet/JavaScript/JSObject.cs | 4 ++-- .../JavaScript/Structs/JSStaticValue.cs | 4 ++-- src/UltralightNet/Methods.cs | 12 ++++++++++++ src/UltralightNet/Structs/RenderTarget.cs | 4 ++-- src/UltralightNet/Structs/ULCommand.cs | 2 +- src/UltralightNet/Structs/ULGPUState.cs | 8 ++++---- src/UltralightNet/Structs/ULMouseEvent.cs | 4 ++-- src/UltralightNet/Structs/ULRenderBuffer.cs | 8 ++++---- src/UltralightNet/Structs/ULScrollEvent.cs | 2 +- src/UltralightNet/Structs/ULVertexBuffer.cs | 4 ++-- src/UltralightNet/ULConfig.cs | 6 +++--- src/UltralightNet/ULViewConfig.cs | 10 +++++----- 15 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/UltralightNet.AppCore/ULSettings.cs b/src/UltralightNet.AppCore/ULSettings.cs index 86237ed09..f3fd72dcd 100755 --- a/src/UltralightNet.AppCore/ULSettings.cs +++ b/src/UltralightNet.AppCore/ULSettings.cs @@ -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(ref settings.LoadShadersFromFileSystem); - ForceCPURenderer = Unsafe.As(ref settings.ForceCPURenderer); + LoadShadersFromFileSystem = Methods.BitCast(settings.LoadShadersFromFileSystem); + ForceCPURenderer = Methods.BitCast(settings.ForceCPURenderer); } public readonly Marshaller ToUnmanaged() => this; diff --git a/src/UltralightNet/JavaScript/JSBase.cs b/src/UltralightNet/JavaScript/JSBase.cs index a44ef00e5..5d18edfff 100644 --- a/src/UltralightNet/JavaScript/JSBase.cs +++ b/src/UltralightNet/JavaScript/JSBase.cs @@ -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 from) where TFrom : unmanaged where TTo : unmanaged - { - Debug.Assert(sizeof(TFrom) == sizeof(TTo)); - return Unsafe.As(ref Unsafe.AsRef(from)); - } - [LibraryImport(LibWebCore)] public static partial JSValueRef JSEvaluateScript(JSContextRef context, JSStringRef script, JSObjectRef thisObject, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception = null); @@ -46,8 +39,8 @@ public abstract unsafe class JSNativeContainer : NativeContainer w { public NativeHandle JSHandle { - get => JavaScriptMethods.BitCast((nuint)Handle); - protected init => Handle = (void*)JavaScriptMethods.BitCast(value); + get => Methods.BitCast((nuint)Handle); + protected init => Handle = (void*)Methods.BitCast(value); } } } diff --git a/src/UltralightNet/JavaScript/JSContext.cs b/src/UltralightNet/JavaScript/JSContext.cs index 020a7eaac..c7665b32c 100644 --- a/src/UltralightNet/JavaScript/JSContext.cs +++ b/src/UltralightNet/JavaScript/JSContext.cs @@ -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(globalContextRef); - public static explicit operator JSGlobalContextRef(JSContextRef contextRef) => JavaScriptMethods.BitCast(contextRef); + public static implicit operator JSContextRef(JSGlobalContextRef globalContextRef) => Methods.BitCast(globalContextRef); + public static explicit operator JSGlobalContextRef(JSContextRef contextRef) => Methods.BitCast(contextRef); } public readonly struct JSContextRef { diff --git a/src/UltralightNet/JavaScript/JSObject.cs b/src/UltralightNet/JavaScript/JSObject.cs index c810d85d1..b72214e71 100644 --- a/src/UltralightNet/JavaScript/JSObject.cs +++ b/src/UltralightNet/JavaScript/JSObject.cs @@ -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(jsObject); - public static explicit operator JSObjectRef(JSValueRef jsValue) => JavaScriptMethods.BitCast(jsValue); + public static implicit operator JSValueRef(JSObjectRef jsObject) => Methods.BitCast(jsObject); + public static explicit operator JSObjectRef(JSValueRef jsValue) => Methods.BitCast(jsValue); } public readonly struct JSClassRef { diff --git a/src/UltralightNet/JavaScript/Structs/JSStaticValue.cs b/src/UltralightNet/JavaScript/Structs/JSStaticValue.cs index 8c847ce53..262da80b3 100644 --- a/src/UltralightNet/JavaScript/Structs/JSStaticValue.cs +++ b/src/UltralightNet/JavaScript/Structs/JSStaticValue.cs @@ -9,7 +9,7 @@ public unsafe struct JSStaticValue public delegate* unmanaged[Cdecl] getProperty; public delegate* unmanaged[Cdecl] setProperty; private uint _Attributes; - public JSPropertyAttributes Attributes { get => Unsafe.As(ref _Attributes); set => _Attributes = Unsafe.As(ref value); } + public JSPropertyAttributes Attributes { readonly get => Methods.BitCast(_Attributes); set => _Attributes = Methods.BitCast(value); } } public unsafe struct JSStaticValueEx { @@ -17,5 +17,5 @@ public unsafe struct JSStaticValueEx public delegate* unmanaged[Cdecl] getPropertyEx; public delegate* unmanaged[Cdecl] setPropertyEx; private uint _Attributes; - public JSPropertyAttributes Attributes { get => Unsafe.As(ref _Attributes); set => _Attributes = Unsafe.As(ref value); } + public JSPropertyAttributes Attributes { readonly get => Methods.BitCast(_Attributes); set => _Attributes = Methods.BitCast(value); } } diff --git a/src/UltralightNet/Methods.cs b/src/UltralightNet/Methods.cs index fcdb047b3..10dc0e8d0 100755 --- a/src/UltralightNet/Methods.cs +++ b/src/UltralightNet/Methods.cs @@ -63,4 +63,16 @@ public static void Preload() } #endif } + + // backported from net8.0 for compatibility + internal static TTo BitCast(TFrom from) where TFrom : unmanaged where TTo : unmanaged +#if !NET8_0_OR_GREATER + { + System.Diagnostics.Debug.Assert(sizeof(TFrom) == sizeof(TTo)); + return Unsafe.As(ref from); + } +#else + => Unsafe.BitCast(from); +#endif + } diff --git a/src/UltralightNet/Structs/RenderTarget.cs b/src/UltralightNet/Structs/RenderTarget.cs index bbdc5be06..76ad472e6 100644 --- a/src/UltralightNet/Structs/RenderTarget.cs +++ b/src/UltralightNet/Structs/RenderTarget.cs @@ -7,7 +7,7 @@ public struct RenderTarget : IEquatable { private byte _IsEmpty; /// Whether this target is empty (null texture) - public bool IsEmpty { readonly get => Unsafe.As(ref Unsafe.AsRef(_IsEmpty)); set => _IsEmpty = Unsafe.As(ref value); } + public bool IsEmpty { readonly get => Methods.BitCast(_IsEmpty); set => _IsEmpty = Methods.BitCast(value); } /// The viewport width (in device coordinates). public uint Width; @@ -26,7 +26,7 @@ public struct RenderTarget : IEquatable private byte _TextureFormat; /// The pixel format of the texture. - public ULBitmapFormat TextureFormat { readonly get => Unsafe.As(ref Unsafe.AsRef(_TextureFormat)); set => _TextureFormat = Unsafe.As(ref value); } + public ULBitmapFormat TextureFormat { readonly get => Methods.BitCast(_TextureFormat); set => _TextureFormat = Methods.BitCast(value); } /// UV coordinates of the texture (this is needed because the texture may be padded). public ULRect UV; diff --git a/src/UltralightNet/Structs/ULCommand.cs b/src/UltralightNet/Structs/ULCommand.cs index 1664cf0db..0aa482ef7 100755 --- a/src/UltralightNet/Structs/ULCommand.cs +++ b/src/UltralightNet/Structs/ULCommand.cs @@ -5,7 +5,7 @@ namespace UltralightNet; public struct ULCommand : IEquatable { private byte _CommandType; - public ULCommandType CommandType { readonly get => Unsafe.As(ref Unsafe.AsRef(_CommandType)); set => _CommandType = Unsafe.As(ref value); } + public ULCommandType CommandType { readonly get => Methods.BitCast(_CommandType); set => _CommandType = Methods.BitCast(value); } public ULGPUState GPUState; /// Only used when is diff --git a/src/UltralightNet/Structs/ULGPUState.cs b/src/UltralightNet/Structs/ULGPUState.cs index bceb81cdb..21a23018a 100755 --- a/src/UltralightNet/Structs/ULGPUState.cs +++ b/src/UltralightNet/Structs/ULGPUState.cs @@ -18,13 +18,13 @@ public unsafe struct ULGPUState : IEquatable public Matrix4x4 Transform; private byte _EnableTexturing; - public bool EnableTexturing { readonly get => Unsafe.As(ref Unsafe.AsRef(_EnableTexturing)); set => _EnableTexturing = Unsafe.As(ref value); } + public bool EnableTexturing { readonly get => Methods.BitCast(_EnableTexturing); set => _EnableTexturing = Methods.BitCast(value); } private byte _EnableBlend; - public bool EnableBlend { readonly get => Unsafe.As(ref Unsafe.AsRef(_EnableBlend)); set => _EnableBlend = Unsafe.As(ref value); } + public bool EnableBlend { readonly get => Methods.BitCast(_EnableBlend); set => _EnableBlend = Methods.BitCast(value); } private byte _ShaderType; - public ULShaderType ShaderType { readonly get => Unsafe.As(ref Unsafe.AsRef(_ShaderType)); set => _ShaderType = Unsafe.As(ref value); } + public ULShaderType ShaderType { readonly get => Methods.BitCast(_ShaderType); set => _ShaderType = Methods.BitCast(value); } public uint RenderBufferId; @@ -92,7 +92,7 @@ public byte ClipSize #endif private byte _EnableScissor; - public bool EnableScissor { readonly get => Unsafe.As(ref Unsafe.AsRef(_EnableScissor)); set => _EnableScissor = Unsafe.As(ref value); } + public bool EnableScissor { readonly get => Methods.BitCast(_EnableScissor); set => _EnableScissor = Methods.BitCast(value); } public ULIntRect ScissorRect; diff --git a/src/UltralightNet/Structs/ULMouseEvent.cs b/src/UltralightNet/Structs/ULMouseEvent.cs index c6d869402..c000f35b2 100755 --- a/src/UltralightNet/Structs/ULMouseEvent.cs +++ b/src/UltralightNet/Structs/ULMouseEvent.cs @@ -19,11 +19,11 @@ public static unsafe partial class Methods public struct ULMouseEvent : IEquatable { private int _Type; - public ULMouseEventType Type { readonly get => Unsafe.As(ref Unsafe.AsRef(_Type)); set => _Type = Unsafe.As(ref value); } + public ULMouseEventType Type { readonly get => Methods.BitCast(_Type); set => _Type = Methods.BitCast(value); } public int X; public int Y; private int _Button; - public ULMouseEventButton Button { readonly get => Unsafe.As(ref Unsafe.AsRef(_Button)); set => _Button = Unsafe.As(ref value); } + public ULMouseEventButton Button { readonly get => Methods.BitCast(_Button); set => _Button = Methods.BitCast(value); } public readonly bool Equals(ULMouseEvent other) => Type == other.Type && X == other.X && Y == other.Y && Button == other.Button; } diff --git a/src/UltralightNet/Structs/ULRenderBuffer.cs b/src/UltralightNet/Structs/ULRenderBuffer.cs index eb7e2c9fe..d67723b2c 100755 --- a/src/UltralightNet/Structs/ULRenderBuffer.cs +++ b/src/UltralightNet/Structs/ULRenderBuffer.cs @@ -11,13 +11,13 @@ public struct ULRenderBuffer private byte _HasStencilBuffer; public bool HasStencilBuffer { - readonly get => Unsafe.As(ref Unsafe.AsRef(_HasStencilBuffer)); - set => _HasStencilBuffer = Unsafe.As(ref Unsafe.AsRef(value)); + readonly get => Methods.BitCast(_HasStencilBuffer); + set => _HasStencilBuffer = Methods.BitCast(value); } private byte _HasDepthBuffer; public bool HasDepthBuffer { - readonly get => Unsafe.As(ref Unsafe.AsRef(_HasDepthBuffer)); - set => _HasDepthBuffer = Unsafe.As(ref Unsafe.AsRef(value)); + readonly get => Methods.BitCast(_HasDepthBuffer); + set => _HasDepthBuffer = Methods.BitCast(value); } } diff --git a/src/UltralightNet/Structs/ULScrollEvent.cs b/src/UltralightNet/Structs/ULScrollEvent.cs index f3e2ce75d..78d937112 100755 --- a/src/UltralightNet/Structs/ULScrollEvent.cs +++ b/src/UltralightNet/Structs/ULScrollEvent.cs @@ -11,7 +11,7 @@ public struct ULScrollEvent : IEquatable /// /// Type of event /// - public ULScrollEventType Type { readonly get => Unsafe.As(ref Unsafe.AsRef(_Type)); set => _Type = Unsafe.As(ref value); } + public ULScrollEventType Type { readonly get => Methods.BitCast(_Type); set => _Type = Methods.BitCast(value); } /// /// horizontal scroll diff --git a/src/UltralightNet/Structs/ULVertexBuffer.cs b/src/UltralightNet/Structs/ULVertexBuffer.cs index 938f3db74..c7a30c23d 100755 --- a/src/UltralightNet/Structs/ULVertexBuffer.cs +++ b/src/UltralightNet/Structs/ULVertexBuffer.cs @@ -10,7 +10,7 @@ public unsafe ref struct ULVertexBuffer public ULVertexBufferFormat Format { - readonly get => Unsafe.As(ref Unsafe.AsRef(format)); - set => format = Unsafe.As(ref value); + readonly get => Methods.BitCast(format); + set => format = Methods.BitCast(value); } } diff --git a/src/UltralightNet/ULConfig.cs b/src/UltralightNet/ULConfig.cs index 1ae7c01d0..36a844825 100755 --- a/src/UltralightNet/ULConfig.cs +++ b/src/UltralightNet/ULConfig.cs @@ -129,11 +129,11 @@ public void FromManaged(ULConfig config) { CachePath = new(config.CachePath.AsSpan()); ResourcePathPrefix = new ULString(config.ResourcePathPrefix.AsSpan()); - FaceWinding = Unsafe.As(ref Unsafe.AsRef(config.FaceWinding)); - FontHinting = Unsafe.As(ref Unsafe.AsRef(config.FontHinting)); + FaceWinding = Methods.BitCast(config.FaceWinding); + FontHinting = Methods.BitCast(config.FontHinting); FontGamma = config.FontGamma; UserStylesheet = new(config.UserStylesheet.AsSpan()); - ForceRepaint = Unsafe.As(ref Unsafe.AsRef(config.ForceRepaint)); + ForceRepaint = Methods.BitCast(config.ForceRepaint); AnimationTimerDelay = config.AnimationTimerDelay; ScrollTimerDelay = config.ScrollTimerDelay; RecycleDelay = config.RecycleDelay; diff --git a/src/UltralightNet/ULViewConfig.cs b/src/UltralightNet/ULViewConfig.cs index 8dd046922..75ba6b920 100755 --- a/src/UltralightNet/ULViewConfig.cs +++ b/src/UltralightNet/ULViewConfig.cs @@ -76,12 +76,12 @@ internal ref struct Marshaller public void FromManaged(ULViewConfig config) { - IsAccelerated = Unsafe.As(ref config.IsAccelerated); - IsTransparent = Unsafe.As(ref config.IsTransparent); + IsAccelerated = Methods.BitCast(config.IsAccelerated); + IsTransparent = Methods.BitCast(config.IsTransparent); InitialDeviceScale = config.InitialDeviceScale; - InitialFocus = Unsafe.As(ref config.InitialFocus); - EnableImages = Unsafe.As(ref config.EnableImages); - EnableJavaScript = Unsafe.As(ref config.EnableJavaScript); + InitialFocus = Methods.BitCast(config.InitialFocus); + EnableImages = Methods.BitCast(config.EnableImages); + EnableJavaScript = Methods.BitCast(config.EnableJavaScript); FontFamilyStandard = new(config.FontFamilyStandard.AsSpan()); FontFamilyFixed = new(config.FontFamilyFixed.AsSpan()); FontFamilySerif = new(config.FontFamilySerif.AsSpan());