#![cfg(feature = "coreclr")]
use hao::{
dotnet::{
entries::Method,
il::{Emitter, OpCode},
module::{ModuleKind, TargetRuntime},
signature::{MethodSig, TypeSig},
},
Module,
};
use hao_runtime::Runtime;
fn plain_module() -> Module {
let mut module = Module::new(
"Plain",
ModuleKind::Dll,
TargetRuntime::NetCoreImplementation(9),
);
let math = module.add_static_class("Plain", "Math").unwrap();
let signature = |params| MethodSig::new_static(TypeSig::INT32, vec![TypeSig::INT32; params]);
math.add_method(
Method::public_static("Add", signature(2)).with_body(Emitter::build(|e| {
e.ldarg(0).ldarg(1).op(OpCode::Add).ret();
})),
);
math.add_method(
Method::public_static("Triple", signature(1)).with_body(Emitter::build(|e| {
e.ldarg(0).ldc_i4(2).op(OpCode::Mul).ret();
})),
);
module
}
#[test]
fn calls_methods_without_unmanaged_callers_only() {
if hao_runtime::find_dotnet_root().is_none() {
eprintln!("skipping: .NET is not installed");
return;
}
let runtime = Runtime::coreclr().unwrap();
let module = plain_module();
module
.find_method("Plain.Math", "Triple")
.unwrap()
.edit_body(|body| {
let index = body.position(|i| i.opcode == OpCode::Ldc_I4_2).unwrap();
body.instructions[index] = hao::dotnet::il::Instruction::ldc_i4(3);
});
runtime.load_module(&module).unwrap();
assert!(runtime
.function_pointer("Plain", "Plain.Math", "Add")
.is_err());
let add: extern "C" fn(i32, i32) -> i32 = unsafe {
std::mem::transmute(
runtime
.method_pointer("Plain", "Plain.Math", "Add")
.unwrap(),
)
};
assert_eq!(add(40, 2), 42);
let triple: extern "C" fn(i32) -> i32 = unsafe {
std::mem::transmute(
runtime
.method_pointer("Plain", "Plain.Math", "Triple")
.unwrap(),
)
};
assert_eq!(triple(10), 30, "the patched IL should run");
assert!(runtime
.method_pointer("Plain", "Plain.Math", "Missing")
.is_err());
}