Introduce UnsafeIL for IL2CPP compatibility.

This commit is contained in:
2023-01-31 22:49:04 +08:00
parent 8ab0c2698b
commit 5727fcf177
3 changed files with 69 additions and 40 deletions

View File

@@ -18,20 +18,16 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using UnsafeIL;
using System.Threading.Tasks;
namespace System.Text.Formatting { namespace System.Text.Formatting {
/// <summary> /// <summary>
/// Specifies an interface for types that act as a set of formatting arguments. /// Specifies an interface for types that act as a set of formatting arguments.
/// </summary> /// </summary>
public interface IArgSet { public interface IArgSet {
/// <summary> /// <summary>
/// The number of arguments in the set. /// The number of arguments in the set.
/// </summary> /// </summary>
@@ -547,37 +543,37 @@ namespace System.Text.Formatting {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void AppendGeneric<T>(T value, StringView format) { internal void AppendGeneric<T>(T value, StringView format) {
// this looks gross, but T is known at JIT-time so this call tree // this looks gross, but T is known at JIT-time so this call tree
// gets compiled down to a direct call with no branching // gets compiled down to a direct call with no branching
if (typeof(T) == typeof(sbyte)) if (typeof(T) == typeof(sbyte))
Append(__refvalue(__makeref(value), sbyte), format); Append(*(sbyte*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(byte)) else if (typeof(T) == typeof(byte))
Append(__refvalue(__makeref(value), byte), format); Append(*(byte*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(short)) else if (typeof(T) == typeof(short))
Append(__refvalue(__makeref(value), short), format); Append(*(short*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(ushort)) else if (typeof(T) == typeof(ushort))
Append(__refvalue(__makeref(value), ushort), format); Append(*(ushort*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(int)) else if (typeof(T) == typeof(int))
Append(__refvalue(__makeref(value), int), format); Append(*(int*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(uint)) else if (typeof(T) == typeof(uint))
Append(__refvalue(__makeref(value), uint), format); Append(*(uint*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(long)) else if (typeof(T) == typeof(long))
Append(__refvalue(__makeref(value), long), format); Append(*(long*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(ulong)) else if (typeof(T) == typeof(ulong))
Append(__refvalue(__makeref(value), ulong), format); Append(*(ulong*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(float)) else if (typeof(T) == typeof(float))
Append(__refvalue(__makeref(value), float), format); Append(*(float*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(double)) else if (typeof(T) == typeof(double))
Append(__refvalue(__makeref(value), double), format); Append(*(double*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(decimal)) else if (typeof(T) == typeof(decimal))
Append(__refvalue(__makeref(value), decimal), format); Append(*(decimal*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(bool)) else if (typeof(T) == typeof(bool))
Append(__refvalue(__makeref(value), bool)); Append(*(bool*)Unsafe.AsPointer(ref value));
else if (typeof(T) == typeof(char)) else if (typeof(T) == typeof(char))
Append(__refvalue(__makeref(value), char), format); Append(*(char*)Unsafe.AsPointer(ref value), format);
else if (typeof(T) == typeof(string)) else if (typeof(T) == typeof(string))
Append((string)(object)value); Append(Unsafe.As<string>(value));
else { else {
// first, check to see if it's a value type implementing IStringFormattable // first, check to see if it's a value type implementing IStringFormattable
var formatter = ValueHelper<T>.Formatter; var formatter = ValueHelper<T>.Formatter;
if (formatter != null) if (formatter != null)

BIN
Assets/Plugins/UnsafeIL.dll Normal file

Binary file not shown.

View File

@@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: d927992f95b82f740a0a95bb6d617d73
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant: