pub struct Runtime(/* private fields */);Expand description
A handle to a running .NET runtime. Cheap to clone.
Implementations§
Source§impl Runtime
impl Runtime
Sourcepub fn coreclr() -> Result<Self>
Available on crate feature coreclr only.
pub fn coreclr() -> Result<Self>
coreclr only.Starts CoreCLR with the newest installed framework, or returns the already running instance. Only one CoreCLR can run per process.
Sourcepub fn coreclr_with(options: &CoreClrOptions) -> Result<Self>
Available on crate feature coreclr only.
pub fn coreclr_with(options: &CoreClrOptions) -> Result<Self>
coreclr only.Starts CoreCLR with options. The options are ignored if it is already running.
Sourcepub fn coreclr_attach(
host_handle: *mut c_void,
domain_id: u32,
create_delegate: *const c_void,
major: u16,
) -> Result<Self>
Available on crate feature coreclr only.
pub fn coreclr_attach( host_handle: *mut c_void, domain_id: u32, create_delegate: *const c_void, major: u16, ) -> Result<Self>
coreclr only.Attaches to a CoreCLR already running in this process, for a native mod
loaded into a .NET game (e.g. Godot on CoreCLR). host_handle, domain_id
and create_delegate (coreclr_create_delegate) are captured as the
game’s runtime initialises — see the hao-godot crate; major is the
.NET major version. After this, assemblies can be loaded and methods
called as usual. Returns the existing handle if hao already has one.
Sourcepub fn mono(options: &MonoOptions) -> Result<Self>
Available on crate feature mono only.
pub fn mono(options: &MonoOptions) -> Result<Self>
mono only.Starts Mono, or returns the already running instance.
Sourcepub fn mono_attach(options: &MonoOptions) -> Result<Self>
Available on crate feature mono only.
pub fn mono_attach(options: &MonoOptions) -> Result<Self>
mono only.Attaches to a Mono runtime already running in this process, for a mod
loaded into a Unity Mono game. options.library should point at the
game’s own Mono. After this, assemblies can be loaded and methods called
as usual. Returns the existing handle if hao already has one.
Sourcepub fn attach_thread(&self)
pub fn attach_thread(&self)
Lets the calling thread run managed code. Needed for Mono on threads it didn’t start, a no-op on CoreCLR. Binding calls do this automatically.
Sourcepub fn load_assembly(&self, path: impl AsRef<Path>) -> Result<()>
pub fn load_assembly(&self, path: impl AsRef<Path>) -> Result<()>
Loads an assembly and makes it available to later loads by name.
Sourcepub fn load_assembly_bytes(&self, name: &str, bytes: &[u8]) -> Result<()>
pub fn load_assembly_bytes(&self, name: &str, bytes: &[u8]) -> Result<()>
Loads an assembly from memory. name is its assembly name.
Sourcepub fn load_module(&self, module: &Module) -> Result<()>
Available on crate feature hao only.
pub fn load_module(&self, module: &Module) -> Result<()>
hao only.Loads a module built or edited with hao, without writing it to disk.
Sourcepub fn function_pointer(
&self,
assembly: &str,
type_name: &str,
method: &str,
) -> Result<*const c_void>
pub fn function_pointer( &self, assembly: &str, type_name: &str, method: &str, ) -> Result<*const c_void>
A native pointer to the static method type_name::method in assembly.
On CoreCLR the method must be [UnmanagedCallersOnly]; for a method that
isn’t, use method_pointer instead, which calls
any method by JIT-compiling it. On Mono the returned thunk takes an extra
trailing *mut *mut c_void exception argument.
Sourcepub fn method_pointer(
&self,
assembly: &str,
type_name: &str,
method: &str,
) -> Result<*const c_void>
pub fn method_pointer( &self, assembly: &str, type_name: &str, method: &str, ) -> Result<*const c_void>
A native-callable pointer to a static method, whether or not it is
[UnmanagedCallersOnly]. The method is JIT-compiled first, so IL patched
before the assembly was loaded takes effect.
The pointer uses the managed calling convention, which matches a plain
extern "C" fn(args) -> ret for static methods with primitive and
pointer arguments. Unlike function_pointer,
it takes no trailing exception argument on either backend, so a managed
exception crosses it as an unhandled error.
Sourcepub fn method_pointer_from_loaded(
&self,
assembly: &str,
type_name: &str,
method: &str,
) -> Result<*const c_void>
pub fn method_pointer_from_loaded( &self, assembly: &str, type_name: &str, method: &str, ) -> Result<*const c_void>
Like method_pointer, but resolves the type
from whichever AssemblyLoadContext actually loaded the assembly.
Mono has no AssemblyLoadContext, so there it is identical to
method_pointer.
Sourcepub fn corlib_method_pointer(
&self,
method: WellKnownMethod,
) -> Result<*const c_void>
Available on crate feature hao only.
pub fn corlib_method_pointer( &self, method: WellKnownMethod, ) -> Result<*const c_void>
hao only.Resolves a well-known corlib method to a native-callable pointer,
deriving the type name, method name and overload arity from the
WellKnownMethod
vocabulary instead of raw strings.
On CoreCLR the method is found in the loaded System.Private.CoreLib;
on Mono it is found in mono_get_corlib (mscorlib).
Sourcepub fn find_method_from_loaded(
&self,
assembly: &str,
type_name: &str,
method: &str,
param_count: i32,
) -> Result<*const c_void>
pub fn find_method_from_loaded( &self, assembly: &str, type_name: &str, method: &str, param_count: i32, ) -> Result<*const c_void>
Like method_pointer_from_loaded
but matches by parameter count, so it works with overloaded methods.
Sourcepub fn find_method_from_loaded_typed(
&self,
assembly: &str,
type_name: &str,
method: &str,
param_count: i32,
first_param: &str,
) -> Result<*const c_void>
pub fn find_method_from_loaded_typed( &self, assembly: &str, type_name: &str, method: &str, param_count: i32, first_param: &str, ) -> Result<*const c_void>
Like find_method_from_loaded, but
also requires the first parameter’s type, by full name (e.g.
"System.String"), to tell apart overloads with the same parameter
count, like GD.Print(string) and GD.Print(params object[]).
CoreCLR only: on Mono it returns Error::Other.
Sourcepub fn invoke(&self, ptr: usize, args: &[u64]) -> Result<u64>
Available on x86-64 only.
pub fn invoke(&self, ptr: usize, args: &[u64]) -> Result<u64>
Calls a managed method pointer through a P/Invoke wrapper that
ensures the calling thread is registered with the CLR’s GC.
Falls back to jit::call_native on Mono (which handles thread
attach separately).
Sourcepub fn invoke_float(&self, ptr: usize, args: &[u64]) -> Result<f64>
Available on x86-64 only.
pub fn invoke_float(&self, ptr: usize, args: &[u64]) -> Result<f64>
Like invoke but returns f64.
Sourcepub fn suppress_gc(&self, budget: i64) -> Result<bool>
pub fn suppress_gc(&self, budget: i64) -> Result<bool>
Requests a no-GC region for the next budget bytes of managed
allocation. Returns true if the runtime agreed to suppress GC.
Mono (SGen) has no GC.TryStartNoGCRegion equivalent, so this is an
honest no-op there that always returns false.
Sourcepub fn resume_gc(&self) -> Result<()>
pub fn resume_gc(&self) -> Result<()>
Ends a no-GC region started by suppress_gc.
A no-op on Mono, which has no no-GC-region to end (see
suppress_gc).
Sourcepub fn warm_enumerate(&self) -> Result<()>
pub fn warm_enumerate(&self) -> Result<()>
Warms enumerate so it does no lazy setup on the
game thread.
On CoreCLR this JIT-compiles and caches the Hao.Loader::Enumerate
helper, and MUST be called off a cooperative-mode thread (e.g. at init).
On Mono it resolves and caches the corlib enumerator methods; enumerate
resolves them lazily too, so warming is only an optimisation there.
Sourcepub fn enumerate(&self, obj: usize) -> Result<Vec<usize>>
pub fn enumerate(&self, obj: usize) -> Result<Vec<usize>>
Materializes any managed IEnumerable into the addresses of its
elements, by calling a JIT-compiled foreach helper and reading the
resulting object[]. This handles concrete types native code cannot
read by hand — lazy iterators, LINQ chains, dictionary views.
Call warm_enumerate once off a cooperative-mode
thread first; this call must run ON the cooperative (game) thread, like
any method_pointer call.
On Mono the returned addresses are live MonoObject*s that SGen may move
at the next GC (each step of the walk is a safepoint), so read or pin
them before continuing — the same raw-pointer contract as the rest of the
Mono backend.
Sourcepub fn dotnet_major(&self) -> Option<u16>
pub fn dotnet_major(&self) -> Option<u16>
The .NET major version (e.g. 9) the runtime was started or attached
with. None on Mono, where no CoreCLR framework version applies.