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