From 5173805562b0863008ec170579c222e88c2adb3f Mon Sep 17 00:00:00 2001 From: Patrick Stevens <3138005+Smaug123@users.noreply.github.com> Date: Fri, 29 Aug 2025 18:57:19 +0100 Subject: [PATCH] Concretize all base class types eagerly (#122) --- WoofWare.PawPrint/BasicCliType.fs | 5 +- WoofWare.PawPrint/Corelib.fs | 81 +++++++++++++++++++++++++++++++ WoofWare.PawPrint/Program.fs | 5 ++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/WoofWare.PawPrint/BasicCliType.fs b/WoofWare.PawPrint/BasicCliType.fs index 2ccda9b..bb951dd 100644 --- a/WoofWare.PawPrint/BasicCliType.fs +++ b/WoofWare.PawPrint/BasicCliType.fs @@ -550,7 +550,10 @@ module CliType = let typeDef = assembly.TypeDefs.[concreteType.Definition.Get] // Check if it's a primitive type by comparing with corelib types FIRST - if concreteType.Assembly = corelib.Corelib.Name && concreteType.Generics.IsEmpty then + if + concreteType.Assembly.FullName = corelib.Corelib.Name.FullName + && concreteType.Generics.IsEmpty + then // Check against known primitive types if TypeInfo.NominallyEqual typeDef corelib.Boolean then zeroOfPrimitive PrimitiveType.Boolean, concreteTypes diff --git a/WoofWare.PawPrint/Corelib.fs b/WoofWare.PawPrint/Corelib.fs index 62cf81f..ab5e393 100644 --- a/WoofWare.PawPrint/Corelib.fs +++ b/WoofWare.PawPrint/Corelib.fs @@ -1,5 +1,8 @@ namespace WoofWare.PawPrint +open System.Collections.Immutable +open System.Reflection.Metadata + [] module Corelib = @@ -180,3 +183,81 @@ module Corelib = IntPtr = intPtrType UIntPtr = uintPtrType } + + let concretizeAll + (loaded : ImmutableDictionary) + (bct : BaseClassTypes) + (t : AllConcreteTypes) + : AllConcreteTypes + = + let ctx = + { + TypeConcretization.ConcretizationContext.InProgress = ImmutableDictionary.Empty + TypeConcretization.ConcretizationContext.ConcreteTypes = t + TypeConcretization.ConcretizationContext.LoadedAssemblies = loaded + TypeConcretization.ConcretizationContext.BaseTypes = bct + } + + let loader = + { new IAssemblyLoad with + member _.LoadAssembly _ _ _ = + failwith "should have already loaded this assembly" + } + + let tys = + [ + bct.String + bct.Boolean + bct.Char + bct.SByte + bct.Byte + bct.Int16 + bct.UInt16 + bct.Int32 + bct.UInt32 + bct.Int64 + bct.UInt64 + bct.Single + bct.Double + bct.Array + bct.Enum + bct.ValueType + bct.DelegateType + bct.Object + bct.RuntimeTypeHandle + bct.RuntimeMethodHandle + bct.RuntimeFieldHandle + bct.RuntimeFieldInfoStub + bct.RuntimeFieldHandleInternal + bct.RuntimeType + bct.Void + bct.TypedReference + bct.IntPtr + bct.UIntPtr + ] + + (ctx, tys) + ||> List.fold (fun ctx ty -> + let stk = + match DumpedAssembly.resolveBaseType ctx.BaseTypes ctx.LoadedAssemblies ty.Assembly ty.BaseType with + | ResolvedBaseType.Enum + | ResolvedBaseType.ValueType -> SignatureTypeKind.ValueType + | ResolvedBaseType.Object + | ResolvedBaseType.Delegate -> SignatureTypeKind.Class + + let _handle, ctx = + TypeConcretization.concretizeType + ctx + loader + ty.Assembly + ImmutableArray.Empty + ImmutableArray.Empty + (TypeDefn.FromDefinition ( + ComparableTypeDefinitionHandle.Make ty.TypeDefHandle, + ty.Assembly.FullName, + stk + )) + + ctx + ) + |> _.ConcreteTypes diff --git a/WoofWare.PawPrint/Program.fs b/WoofWare.PawPrint/Program.fs index 958daf6..250cec0 100644 --- a/WoofWare.PawPrint/Program.fs +++ b/WoofWare.PawPrint/Program.fs @@ -302,6 +302,11 @@ module Program = logger.LogInformation "Main method class now initialised" + let state = + { state with + ConcreteTypes = Corelib.concretizeAll state._LoadedAssemblies baseClassTypes state.ConcreteTypes + } + // Now that BCL initialisation has taken place and the user-code classes are constructed, // overwrite the main thread completely using the already-concretized method. let methodState =