Repository navigation
Use extension-member polyfills across libraries #136221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eiriktsarpalis
merged 10 commits into
dotnet:main
from
eiriktsarpalis:eiriktsarpalis-stj-extension-polyfills
Oct 22, 2026
+569
−605
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d5a2d8e
Use extension-member polyfills in System.Text.Json
eiriktsarpalis f7b01ab
Merge main to resolve STJ polyfill conflict
eiriktsarpalis 864ecfb
Remove unused assignment exposed by STJ polyfill cleanup
eiriktsarpalis ffc8253
Use ThrowIfNull for remaining STJ argument checks
eiriktsarpalis b05a955
Consolidate compatibility polyfills across libraries
eiriktsarpalis c630e46
Remove redundant array-reverse range delegation
eiriktsarpalis 4661e53
Drop the generic Array.Reverse polyfill
eiriktsarpalis 5d4a3dc
Restore array reversal polyfill with direct swaps
eiriktsarpalis ff704ea
Keep array reversal polyfill faithful to the original loop
eiriktsarpalis e3bd647
Validate array reversal polyfill arguments directly
eiriktsarpalis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/libraries/Common/src/Polyfills/BinaryPrimitivesPolyfills.ReverseEndianness.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Buffers.Binary; | ||
|
|
||
| /// <summary>Provides downlevel polyfills for reversing the endianness of spans.</summary> | ||
| internal static partial class BinaryPrimitivesPolyfills | ||
| { | ||
| extension(BinaryPrimitives) | ||
| { | ||
| public static void ReverseEndianness(ReadOnlySpan<short> source, Span<short> destination) | ||
| { | ||
| source.CopyTo(destination); | ||
| for (int i = 0; i < source.Length; i++) | ||
| { | ||
| destination[i] = BinaryPrimitives.ReverseEndianness(destination[i]); | ||
| } | ||
| } | ||
|
|
||
| public static void ReverseEndianness(ReadOnlySpan<int> source, Span<int> destination) | ||
| { | ||
| source.CopyTo(destination); | ||
| for (int i = 0; i < source.Length; i++) | ||
| { | ||
| destination[i] = BinaryPrimitives.ReverseEndianness(destination[i]); | ||
| } | ||
| } | ||
|
|
||
| public static void ReverseEndianness(ReadOnlySpan<long> source, Span<long> destination) | ||
| { | ||
| source.CopyTo(destination); | ||
| for (int i = 0; i < source.Length; i++) | ||
| { | ||
| destination[i] = BinaryPrimitives.ReverseEndianness(destination[i]); | ||
| } | ||
| } | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/libraries/Common/src/Polyfills/BinaryPrimitivesPolyfills.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Buffers.Binary; | ||
|
|
||
| /// <summary>Provides downlevel polyfills for static methods on <see cref="BinaryPrimitives"/>.</summary> | ||
| internal static partial class BinaryPrimitivesPolyfills | ||
| { | ||
| extension(BinaryPrimitives) | ||
| { | ||
| public static float ReadSingleBigEndian(ReadOnlySpan<byte> source) => | ||
| BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32BigEndian(source)); | ||
|
|
||
| public static double ReadDoubleBigEndian(ReadOnlySpan<byte> source) => | ||
| BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64BigEndian(source)); | ||
|
|
||
| public static void WriteSingleBigEndian(Span<byte> destination, float value) => | ||
| BinaryPrimitives.WriteInt32BigEndian(destination, BitConverter.SingleToInt32Bits(value)); | ||
|
|
||
| public static void WriteDoubleBigEndian(Span<byte> destination, double value) => | ||
| BinaryPrimitives.WriteInt64BigEndian(destination, BitConverter.DoubleToInt64Bits(value)); | ||
|
|
||
| public static void WriteSingleLittleEndian(Span<byte> destination, float value) => | ||
| BinaryPrimitives.WriteInt32LittleEndian(destination, BitConverter.SingleToInt32Bits(value)); | ||
|
|
||
| public static void WriteDoubleLittleEndian(Span<byte> destination, double value) => | ||
| BinaryPrimitives.WriteInt64LittleEndian(destination, BitConverter.DoubleToInt64Bits(value)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace System; | ||
|
|
||
| /// <summary>Provides downlevel polyfills for static methods on <see cref="Math"/>.</summary> | ||
| internal static class MathPolyfills | ||
| { | ||
| extension(Math) | ||
| { | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static ulong BigMul(ulong a, ulong b, out ulong low) | ||
| { | ||
| ulong lowerLow = (ulong)(uint)a * (uint)b; | ||
| ulong higherLow = (ulong)(uint)(a >> 32) * (uint)b; | ||
| ulong lowerHigh = (ulong)(uint)a * (uint)(b >> 32); | ||
| ulong higherHigh = (ulong)(uint)(a >> 32) * (uint)(b >> 32); | ||
|
|
||
| ulong cross = (lowerLow >> 32) + (higherLow & uint.MaxValue) + lowerHigh; | ||
| ulong upper = (higherLow >> 32) + (cross >> 32) + higherHigh; | ||
| low = (cross << 32) | (lowerLow & uint.MaxValue); | ||
| return upper; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/libraries/Common/src/Polyfills/OperatingSystemPolyfills.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #if !NETFRAMEWORK | ||
| using System.Runtime.InteropServices; | ||
| #endif | ||
|
|
||
| namespace System; | ||
|
|
||
| /// <summary>Provides downlevel polyfills for static methods on <see cref="OperatingSystem"/>.</summary> | ||
| internal static class OperatingSystemPolyfills | ||
| { | ||
| extension(OperatingSystem) | ||
| { | ||
| public static bool IsWindows() => | ||
| #if NETFRAMEWORK | ||
| Environment.OSVersion.Platform == PlatformID.Win32NT; | ||
| #else | ||
| RuntimeInformation.IsOSPlatform(OSPlatform.Windows); | ||
| #endif | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/libraries/Common/src/Polyfills/ReadOnlySequencePolyfills.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Buffers; | ||
|
|
||
| /// <summary>Provides downlevel polyfills for instance members on <see cref="ReadOnlySequence{T}"/>.</summary> | ||
| internal static class ReadOnlySequencePolyfills | ||
| { | ||
| extension<T>(ReadOnlySequence<T> sequence) | ||
| { | ||
| public ReadOnlySpan<T> FirstSpan => sequence.First.Span; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
|
|
||
| namespace System; | ||
|
|
||
| /// <summary>Provides a downlevel polyfill for creating strings with a span callback.</summary> | ||
| internal static partial class StringPolyfills | ||
| { | ||
| extension(string) | ||
| { | ||
| public static string Create<TState>(int length, TState state, SpanAction<char, TState> action) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(action); | ||
| ArgumentOutOfRangeException.ThrowIfNegative(length); | ||
|
|
||
| if (length == 0) | ||
| { | ||
| return string.Empty; | ||
| } | ||
|
|
||
| char[] chars = new char[length]; | ||
| action(chars, state); | ||
| return new string(chars); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System; | ||
|
|
||
| /// <summary>Provides downlevel polyfills for instance members on <see cref="Type"/>.</summary> | ||
| internal static class TypePolyfills | ||
| { | ||
| extension(Type type) | ||
| { | ||
| public bool IsByRefLike | ||
| { | ||
| get | ||
| { | ||
| if (!type.IsValueType) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| object[] attributes = type.GetCustomAttributes(inherit: false); | ||
|
|
||
| for (int i = 0; i < attributes.Length; i++) | ||
| { | ||
| if (attributes[i].GetType().FullName == "System.Runtime.CompilerServices.IsByRefLikeAttribute") | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System; | ||
|
|
||
| /// <summary>Provides downlevel polyfills for static methods on <see cref="ulong"/>.</summary> | ||
| internal static class UInt64Polyfills | ||
| { | ||
| extension(ulong) | ||
| { | ||
| public static ulong PopCount(ulong value) | ||
| { | ||
| ulong count = 0; | ||
| while (value != 0) | ||
| { | ||
| value &= value - 1; | ||
| count++; | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Buffers; | ||
|
|
||
| internal delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.