@@ -517,6 +517,60 @@ private static void ArrayTypeCheck_Helper(object obj, void* elementType)
517517            } 
518518        } 
519519
520+         // Helpers for boxing 
521+         [ DebuggerHidden ] 
522+         private  static   object ?  Box_Nullable ( IntPtr  handle ,  ref  byte  nullableData ) 
523+         { 
524+             MethodTable *  srcMT  =  new  TypeHandle ( ( void * ) handle ) . AsMethodTable ( ) ; 
525+             return  BoxNullableImpl ( srcMT ,  ref  nullableData ) ; 
526+         } 
527+ 
528+         [ DebuggerHidden ] 
529+         internal  static   object ?  BoxNullableImpl ( MethodTable *  srcMT ,  ref  byte  nullableData ) 
530+         { 
531+             Debug . Assert ( srcMT ->IsNullable ) ; 
532+ 
533+             // If 'hasValue' is false, return null. 
534+             if  ( ! Unsafe . As < byte ,  bool > ( ref  nullableData ) ) 
535+                 return  null ; 
536+ 
537+             // Allocate a new instance of the T in Nullable<T>. 
538+             MethodTable *  dstMT  =  srcMT ->InstantiationArg0 ( ) ; 
539+             ref  byte  srcValue  =  ref  Unsafe . Add ( ref  nullableData ,  srcMT ->NullableValueAddrOffset ) ; 
540+ 
541+             // Delegate to non-nullable boxing implementation 
542+             return  BoxImpl ( dstMT ,  ref  srcValue ) ; 
543+         } 
544+ 
545+         [ DebuggerHidden ] 
546+         internal  static   object  Box ( IntPtr  handle ,  ref  byte  unboxedData ) 
547+         { 
548+             MethodTable *  typeMT  =  new  TypeHandle ( ( void * ) handle ) . AsMethodTable ( ) ; 
549+             Debug . Assert ( typeMT ->IsValueType  &&  ! typeMT ->IsNullable ) ; 
550+ 
551+             return  BoxImpl ( typeMT ,  ref  unboxedData ) ; 
552+         } 
553+ 
554+         [ DebuggerHidden ] 
555+         internal  static   object  BoxImpl ( MethodTable *  typeMT ,  ref  byte  unboxedData ) 
556+         { 
557+             Debug . Assert ( typeMT  !=  null ) ; 
558+             Debug . Assert ( typeMT ->IsValueType ) ; 
559+ 
560+             object  boxed  =  RuntimeTypeHandle . InternalAllocNoChecks ( typeMT ) ; 
561+             if  ( typeMT ->ContainsGCPointers ) 
562+             { 
563+                 Buffer . BulkMoveWithWriteBarrier ( ref  boxed . GetRawData ( ) ,  ref  unboxedData , 
564+                     typeMT ->GetNumInstanceFieldBytesIfContainsGCPointers ( ) ) ; 
565+             } 
566+             else 
567+             { 
568+                 SpanHelpers . Memmove ( ref  boxed . GetRawData ( ) ,  ref  unboxedData ,  typeMT ->GetNumInstanceFieldBytes ( ) ) ; 
569+             } 
570+ 
571+             return  boxed ; 
572+         } 
573+ 
520574        // Helpers for Unboxing 
521575#if FEATURE_TYPEEQUIVALENCE 
522576        [ DebuggerHidden ] 
@@ -615,27 +669,8 @@ internal static void Unbox_Nullable(ref byte destPtr, MethodTable* typeMT, objec
615669        [ DebuggerHidden ] 
616670        internal  static   object ?  ReboxFromNullable ( MethodTable *  srcMT ,  object  src ) 
617671        { 
618-             Debug . Assert ( srcMT ->IsNullable ) ; 
619- 
620672            ref  byte  nullableData  =  ref  src . GetRawData ( ) ; 
621- 
622-             // If 'hasValue' is false, return null. 
623-             if  ( ! Unsafe . As < byte ,  bool > ( ref  nullableData ) ) 
624-                 return  null ; 
625- 
626-             // Allocate a new instance of the T in Nullable<T>. 
627-             MethodTable *  dstMT  =  srcMT ->InstantiationArg0 ( ) ; 
628-             object  dst  =  RuntimeTypeHandle . InternalAlloc ( dstMT ) ; 
629- 
630-             // Copy data from the Nullable<T>. 
631-             ref  byte  srcData  =  ref  Unsafe . Add ( ref  nullableData ,  srcMT ->NullableValueAddrOffset ) ; 
632-             ref  byte  dstData  =  ref  RuntimeHelpers . GetRawData ( dst ) ; 
633-             if  ( dstMT ->ContainsGCPointers ) 
634-                 Buffer . BulkMoveWithWriteBarrier ( ref  dstData ,  ref  srcData ,  dstMT ->GetNumInstanceFieldBytesIfContainsGCPointers ( ) ) ; 
635-             else 
636-                 SpanHelpers . Memmove ( ref  dstData ,  ref  srcData ,  dstMT ->GetNumInstanceFieldBytes ( ) ) ; 
637- 
638-             return  dst ; 
673+             return  BoxNullableImpl ( srcMT ,  ref  nullableData ) ; 
639674        } 
640675
641676        [ DebuggerHidden ] 
0 commit comments