Skip to content

Commit

Permalink
Add default string invoker shim.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed Apr 1, 2024
1 parent 9637373 commit 694ee61
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public static class DefaultInvokers
/// Gets the method invoker that steps over any method by invoking it via System.Reflection.
/// </summary>
public static ReflectionInvoker ReflectionInvoke => ReflectionInvoker.Instance;

/// <summary>
/// Gets the default shim for the <see cref="System.String"/> type.
/// </summary>
public static StringInvoker StringShim => StringInvoker.Instance;

/// <summary>
/// Gets the method invoker that forwards any method that is not within the resolution scope of the current
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using AsmResolver.DotNet;
using Echo.Memory;
using Echo.Platforms.AsmResolver.Emulation.Dispatch;

namespace Echo.Platforms.AsmResolver.Emulation.Invocation;

/// <summary>
/// Implements a method invoker that shims methods from the <see cref="System.String"/> class.
/// </summary>
public class StringInvoker : IMethodInvoker
{
/// <summary>
/// Gets the singleton instance of the <see cref="StringInvoker"/> class.
/// </summary>
public static StringInvoker Instance { get; } = new();

/// <inheritdoc />
public InvocationResult Invoke(CilExecutionContext context, IMethodDescriptor method, IList<BitVector> arguments)
{
if ((!method.DeclaringType?.IsTypeOf("System", "String") ?? true) || method.Signature is null)
return InvocationResult.Inconclusive();

switch (method.Name?.Value)
{
case "FastAllocateString":
return InvokeFastAllocateString(context, arguments);

default:
return InvocationResult.Inconclusive();
}
}

private static InvocationResult InvokeFastAllocateString(CilExecutionContext context, IList<BitVector> arguments)
{
var length = arguments[0];
if (!length.IsFullyKnown)
throw new CilEmulatorException("Cannot allocate a string with an unknown length.");

long address = context.Machine.Heap.AllocateString(length.AsSpan().I32, true);
var result = context.Machine.ValueFactory.RentNativeInteger(address);

return InvocationResult.StepOver(result);
}
}

0 comments on commit 694ee61

Please sign in to comment.