Skip to content

Commit

Permalink
Use Span.Reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
lewing committed Nov 10, 2021
1 parent 7e05e8e commit aac76ed
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,6 @@ public MonoBinaryReader(Stream stream, bool hasError = false) : base(stream)
HasError = hasError;
}

internal static void SwapForBE(Span<byte> dest, Span<byte> src)
{
// SDB is big endian
if (BitConverter.IsLittleEndian)
{
for (int i = 0; i < src.Length; i++)
dest [src.Length - i - 1] = src [i];
}
else
{
src.CopyTo(dest);
}
}

public override string ReadString()
{
var valueLen = ReadInt32();
Expand All @@ -413,20 +399,24 @@ public override string ReadString()
public override sbyte ReadSByte() => (sbyte)ReadInt32();
public byte ReadUByte() => (byte)ReadUInt32();
public ushort ReadUShort() => (ushort)ReadUInt32();
public override unsafe int ReadInt32() => Read<int>();
public override int ReadInt32() => ReadBigEndian<int>();

public override unsafe double ReadDouble() => Read<double>();
public override unsafe uint ReadUInt32() => Read<uint>();
public override unsafe float ReadSingle() => Read<float>();
public override unsafe ulong ReadUInt64() => Read<ulong>();
public override unsafe long ReadInt64() => Read<long>();
public override double ReadDouble() => ReadBigEndian<double>();
public override uint ReadUInt32() => ReadBigEndian<uint>();
public override float ReadSingle() => ReadBigEndian<float>();
public override ulong ReadUInt64() => ReadBigEndian<ulong>();
public override long ReadInt64() => ReadBigEndian<long>();

protected unsafe T Read<T>() where T : struct
protected unsafe T ReadBigEndian<T>() where T : struct
{
Span<byte> data = stackalloc byte[Unsafe.SizeOf<T>()];
T ret = default;
Read(data);
SwapForBE(new Span<byte>(Unsafe.AsPointer(ref ret), data.Length), data);
if (BitConverter.IsLittleEndian)
{
data.Reverse();
}
data.CopyTo(new Span<byte>(Unsafe.AsPointer(ref ret), data.Length));
return ret;
}
}
Expand All @@ -435,20 +425,24 @@ internal class MonoBinaryWriter : BinaryWriter
{
public MonoBinaryWriter(Stream stream) : base(stream) {}

public void WriteString(string val)
public override void Write(string val)
{
var bytes = Encoding.UTF8.GetBytes(val);
Write(bytes.Length);
Write(bytes);
}

public override unsafe void Write(long val) => WriteType<long>(val);
public override unsafe void Write(int val) => WriteType<int>(val);
public override void Write(long val) => WriteBigEndian<long>(val);
public override void Write(int val) => WriteBigEndian<int>(val);

protected unsafe void WriteType<T>(T val) where T : struct
protected unsafe void WriteBigEndian<T>(T val) where T : struct
{
Span<byte> data = stackalloc byte[Unsafe.SizeOf<T>()];
MonoBinaryReader.SwapForBE(data, new Span<byte>(Unsafe.AsPointer(ref val), data.Length));
new Span<byte>(Unsafe.AsPointer(ref val), data.Length).CopyTo(data);
if (BitConverter.IsLittleEndian)
{
data.Reverse();
}
base.Write(data);
}

Expand Down Expand Up @@ -821,7 +815,7 @@ public async Task<int> CreateString(SessionId sessionId, string value, Cancellat
var retDebuggerCmdReader = await SendDebuggerAgentCommand<CmdAppDomain>(sessionId, CmdAppDomain.GetRootDomain, commandParams, token);
var root_domain = retDebuggerCmdReader.ReadInt32();
commandParamsWriter.Write(root_domain);
commandParamsWriter.WriteString(value);
commandParamsWriter.Write(value);
retDebuggerCmdReader = await SendDebuggerAgentCommand<CmdAppDomain>(sessionId, CmdAppDomain.CreateString, commandParams, token);
return retDebuggerCmdReader.ReadInt32();
}
Expand Down Expand Up @@ -932,7 +926,7 @@ public async Task<int> GetAssemblyId(SessionId sessionId, string asm_name, Cance
{
var commandParams = new MemoryStream();
var commandParamsWriter = new MonoBinaryWriter(commandParams);
commandParamsWriter.WriteString(asm_name);
commandParamsWriter.Write(asm_name);

var retDebuggerCmdReader = await SendDebuggerAgentCommand<CmdVM>(sessionId, CmdVM.GetAssemblyByName, commandParams, token);
return retDebuggerCmdReader.ReadInt32();
Expand Down Expand Up @@ -1452,7 +1446,7 @@ public async Task<int> GetMethodIdByName(SessionId sessionId, int type_id, strin
var commandParams = new MemoryStream();
var commandParamsWriter = new MonoBinaryWriter(commandParams);
commandParamsWriter.Write((int)type_id);
commandParamsWriter.WriteString(method_name);
commandParamsWriter.Write(method_name);
commandParamsWriter.Write((int)(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static));
commandParamsWriter.Write((int)1); //case sensitive
var retDebuggerCmdReader = await SendDebuggerAgentCommand<CmdType>(sessionId, CmdType.GetMethodsByNameFlags, commandParams, token);
Expand Down Expand Up @@ -2198,7 +2192,7 @@ public async Task<int> GetTypeByName(SessionId sessionId, string typeToSearch, C
{
var commandParams = new MemoryStream();
var commandParamsWriter = new MonoBinaryWriter(commandParams);
commandParamsWriter.WriteString(typeToSearch);
commandParamsWriter.Write(typeToSearch);
var retDebuggerCmdReader = await SendDebuggerAgentCommand<CmdVM>(sessionId, CmdVM.GetTypes, commandParams, token);
var count = retDebuggerCmdReader.ReadInt32(); //count ret
return retDebuggerCmdReader.ReadInt32();
Expand Down

0 comments on commit aac76ed

Please sign in to comment.