Skip to content

Commit

Permalink
Merge pull request #632 from MihaZupan/wasm-mt-ptr
Browse files Browse the repository at this point in the history
Don't try to use the MT pointer on unsupported platforms
  • Loading branch information
xoofx committed Apr 23, 2022
2 parents b32e71a + e8f9274 commit 263041e
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/Markdig/Renderers/RendererBase.cs
Expand Up @@ -4,7 +4,9 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Markdig.Helpers;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
Expand Down Expand Up @@ -180,17 +182,50 @@ public void Write(MarkdownObject obj)
private static KeyWrapper GetKeyForType(MarkdownObject obj)
{
#if NET
IntPtr methodTablePtr = Unsafe.Add(ref Unsafe.As<RawData>(obj).Data, -1);
return new KeyWrapper(methodTablePtr);
#else
if (s_canUseMethodTablePointer)
{
IntPtr methodTablePtr = Unsafe.Add(ref Unsafe.As<RawData>(obj).Data, -1);
return new KeyWrapper(methodTablePtr);
}
#endif

IntPtr typeHandle = Type.GetTypeHandle(obj).Value;
return new KeyWrapper(typeHandle);
#endif
}

#if NET
private sealed class RawData
{
public IntPtr Data;
}

private static readonly bool s_canUseMethodTablePointer = GetCanUseMethodTablePointer();

private static bool GetCanUseMethodTablePointer()
{
if (RuntimeInformation.OSArchitecture == Architecture.Wasm)
{
return false;
}

var obj1 = new LiteralInline();
var obj2 = new ParagraphBlock();
var obj3 = new LiteralInline();
IntPtr ptr1 = Unsafe.Add(ref Unsafe.As<RawData>(obj1).Data, -1);
IntPtr ptr2 = Unsafe.Add(ref Unsafe.As<RawData>(obj2).Data, -1);
IntPtr ptr3 = Unsafe.Add(ref Unsafe.As<RawData>(obj3).Data, -1);
GC.KeepAlive(obj1);
GC.KeepAlive(obj2);
GC.KeepAlive(obj3);

if (ptr1 == ptr2 || ptr1 != ptr3)
{
Debug.Fail("This platform doesn't support the MT pointer optimization");
return false;
}

return true;
}
#endif
}
}

0 comments on commit 263041e

Please sign in to comment.