77using System ;
88using System . Collections . Generic ;
99using System . Numerics ;
10+ using System . Reflection ;
1011using System . Reflection . Emit ;
1112using System . Runtime . CompilerServices ;
1213using System . Runtime . InteropServices ;
1314
14- using Microsoft . Scripting ;
15- using Microsoft . Scripting . Runtime ;
16-
1715using IronPython . Runtime ;
1816using IronPython . Runtime . Operations ;
1917using IronPython . Runtime . Types ;
2018
19+ using Microsoft . Scripting . Runtime ;
20+
2121namespace IronPython . Modules {
2222 /// <summary>
2323 /// Provides support for interop with native code from Python code.
@@ -344,6 +344,8 @@ object INativeType.SetValue(MemoryHolder/*!*/ owner, int offset, object value) {
344344 throw new InvalidOperationException ( ) ;
345345 }
346346
347+ private static readonly MethodInfo StringGetPinnableReference = typeof ( string ) . GetMethod ( "GetPinnableReference" ) ;
348+
347349 MarshalCleanup INativeType . EmitMarshalling ( ILGenerator /*!*/ method , LocalOrArg argIndex , List < object > /*!*/ constantPool , int constantPoolArgument ) {
348350 MarshalCleanup cleanup = null ;
349351 Label marshalled = method . DefineLabel ( ) ;
@@ -415,12 +417,18 @@ MarshalCleanup INativeType.EmitMarshalling(ILGenerator/*!*/ method, LocalOrArg a
415417 method . Emit ( OpCodes . Dup ) ;
416418 method . Emit ( OpCodes . Brfalse , nextTry ) ;
417419
418- LocalBuilder lb = method . DeclareLocal ( typeof ( string ) , true ) ;
419- method . Emit ( OpCodes . Stloc , lb ) ;
420- method . Emit ( OpCodes . Ldloc , lb ) ;
421- method . Emit ( OpCodes . Conv_I ) ;
422- method . Emit ( OpCodes . Ldc_I4 , RuntimeHelpers . OffsetToStringData ) ;
423- method . Emit ( OpCodes . Add ) ;
420+ if ( StringGetPinnableReference is null ) {
421+ LocalBuilder lb = method . DeclareLocal ( typeof ( string ) , true ) ;
422+ method . Emit ( OpCodes . Stloc , lb ) ;
423+ method . Emit ( OpCodes . Ldloc , lb ) ;
424+ method . Emit ( OpCodes . Conv_I ) ;
425+ #pragma warning disable CS0618 // Type or member is obsolete
426+ method . Emit ( OpCodes . Ldc_I4 , RuntimeHelpers . OffsetToStringData ) ;
427+ #pragma warning restore CS0618 // Type or member is obsolete
428+ method . Emit ( OpCodes . Add ) ;
429+ } else {
430+ method . Emit ( OpCodes . Call , StringGetPinnableReference ) ;
431+ }
424432 method . Emit ( OpCodes . Br , done ) ;
425433
426434 method . MarkLabel ( nextTry ) ;
@@ -518,7 +526,6 @@ internal static void MarshalWCharPointer(ILGenerator method, LocalOrArg argIndex
518526 Type argumentType = argIndex . Type ;
519527 Label isNull ;
520528 Label done ;
521- LocalBuilder lb ;
522529 isNull = method . DefineLabel ( ) ;
523530 done = method . DefineLabel ( ) ;
524531 method . Emit ( OpCodes . Brfalse , isNull ) ;
@@ -527,12 +534,19 @@ internal static void MarshalWCharPointer(ILGenerator method, LocalOrArg argIndex
527534 method . Emit ( OpCodes . Box , argumentType ) ;
528535 }
529536
530- lb = method . DeclareLocal ( typeof ( string ) , true ) ;
531- method . Emit ( OpCodes . Stloc , lb ) ;
532- method . Emit ( OpCodes . Ldloc , lb ) ;
533- method . Emit ( OpCodes . Conv_I ) ;
534- method . Emit ( OpCodes . Ldc_I4 , RuntimeHelpers . OffsetToStringData ) ;
535- method . Emit ( OpCodes . Add ) ;
537+ if ( StringGetPinnableReference is null ) {
538+ LocalBuilder lb = method . DeclareLocal ( typeof ( string ) , true ) ;
539+ method . Emit ( OpCodes . Stloc , lb ) ;
540+ method . Emit ( OpCodes . Ldloc , lb ) ;
541+ method . Emit ( OpCodes . Conv_I ) ;
542+ #pragma warning disable CS0618 // Type or member is obsolete
543+ method . Emit ( OpCodes . Ldc_I4 , RuntimeHelpers . OffsetToStringData ) ;
544+ #pragma warning restore CS0618 // Type or member is obsolete
545+ method . Emit ( OpCodes . Add ) ;
546+ }
547+ else {
548+ method . Emit ( OpCodes . Call , StringGetPinnableReference ) ;
549+ }
536550 method . Emit ( OpCodes . Br , done ) ;
537551
538552 method . MarkLabel ( isNull ) ;
0 commit comments