-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Platforms/Echo.Platforms.AsmResolver/Emulation/Invocation/StringInvoker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |