Add native call implementations (#18)

This commit is contained in:
Patrick Stevens
2025-05-26 19:24:28 +01:00
committed by GitHub
parent 70f78f9729
commit 7599dd05c9
17 changed files with 329 additions and 63 deletions

View File

@@ -21,11 +21,13 @@ module TestBasicLock =
let dotnetRuntimes =
DotnetRuntime.SelectForDll assy.Location |> ImmutableArray.CreateRange
let impls = NativeImpls.Mock ()
use peImage = new MemoryStream (image)
try
let terminalState, terminatingThread =
Program.run loggerFactory (Some "BasicLock.cs") peImage dotnetRuntimes []
Program.run loggerFactory (Some "BasicLock.cs") peImage dotnetRuntimes impls []
let exitCode =
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with

View File

@@ -6,6 +6,7 @@ open FsUnitTyped
open NUnit.Framework
open WoofWare.DotnetRuntimeLocator
open WoofWare.PawPrint
open WoofWare.PawPrint.ExternImplementations
open WoofWare.PawPrint.Test
[<TestFixture>]
@@ -17,14 +18,17 @@ module TestCases =
{
FileName = "BasicException.cs"
ExpectedReturnCode = 10
NativeImpls = NativeImpls.Mock ()
}
{
FileName = "BasicLock.cs"
ExpectedReturnCode = 10
NativeImpls = NativeImpls.Mock ()
}
{
FileName = "WriteLine.cs"
ExpectedReturnCode = 1
NativeImpls = NativeImpls.Mock ()
}
]
@@ -33,10 +37,34 @@ module TestCases =
{
FileName = "NoOp.cs"
ExpectedReturnCode = 1
NativeImpls = NativeImpls.Mock ()
}
{
FileName = "TriangleNumber.cs"
ExpectedReturnCode = 10
NativeImpls = NativeImpls.Mock ()
}
{
FileName = "InstaQuit.cs"
ExpectedReturnCode = 1
NativeImpls =
let mock = NativeImpls.Mock ()
{ mock with
System_Env =
{ System_EnvironmentMock.Empty with
GetProcessorCount =
fun thread state ->
let state =
state |> IlMachineState.pushToEvalStack' (EvalStackValue.Int32 1) thread
(state, WhatWeDid.Executed) |> ExecutionResult.Stepped
_Exit =
fun thread state ->
let state = state |> IlMachineState.loadArgument thread 0
ExecutionResult.Terminated (state, thread)
}
}
}
]
@@ -53,7 +81,7 @@ module TestCases =
try
let terminalState, terminatingThread =
Program.run loggerFactory (Some case.FileName) peImage dotnetRuntimes []
Program.run loggerFactory (Some case.FileName) peImage dotnetRuntimes case.NativeImpls []
let exitCode =
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with
@@ -81,11 +109,13 @@ module TestCases =
let dotnetRuntimes =
DotnetRuntime.SelectForDll assy.Location |> ImmutableArray.CreateRange
let impls = NativeImpls.Mock ()
use peImage = new MemoryStream (image)
try
let terminalState, terminatingThread =
Program.run loggerFactory (Some case.FileName) peImage dotnetRuntimes []
Program.run loggerFactory (Some case.FileName) peImage dotnetRuntimes impls []
let exitCode =
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with

View File

@@ -1,6 +1,7 @@
namespace WoofWare.PawPrint.Test
open WoofWare.PawPrint
open WoofWare.PawPrint.ExternImplementations
/// Result of executing (some steps of) the program under PawPrint.
type RunResult =
@@ -16,8 +17,22 @@ type RunResult =
FinalState : IlMachineState
}
type NativeImpls =
{
System_Env : ISystem_Environment
}
interface ISystem_Environment_Env with
member this.System_Environment = this.System_Env
static member Mock () =
{
System_Env = System_EnvironmentMock.Empty
}
type TestCase =
{
FileName : string
ExpectedReturnCode : int
NativeImpls : NativeImpls
}

View File

@@ -22,11 +22,13 @@ module TestHelloWorld =
let dotnetRuntimes =
DotnetRuntime.SelectForDll assy.Location |> ImmutableArray.CreateRange
let impls = NativeImpls.Mock ()
try
use peImage = new MemoryStream (image)
let terminalState, terminatingThread =
Program.run loggerFactory (Some "HelloWorld.cs") peImage dotnetRuntimes []
Program.run loggerFactory (Some "HelloWorld.cs") peImage dotnetRuntimes impls []
let exitCode =
match terminalState.ThreadState.[terminatingThread].MethodState.EvaluationStack.Values with

View File

@@ -22,6 +22,7 @@
<EmbeddedResource Include="sources\BasicException.cs" />
<EmbeddedResource Include="sources\TriangleNumber.cs" />
<EmbeddedResource Include="sources\WriteLine.cs" />
<EmbeddedResource Include="sources\InstaQuit.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,13 @@
using System;
namespace HelloWorldApp
{
class Program
{
static int Main(string[] args)
{
Environment.Exit(1);
return 100;
}
}
}