Skip to main content

Runtime

Struct Runtime 

Source
pub struct Runtime(/* private fields */);
Expand description

A handle to a running .NET runtime. Cheap to clone.

Implementations§

Source§

impl Runtime

Source

pub fn coreclr() -> Result<Self>

Available on crate feature coreclr only.

Starts CoreCLR with the newest installed framework, or returns the already running instance. Only one CoreCLR can run per process.

Source

pub fn coreclr_with(options: &CoreClrOptions) -> Result<Self>

Available on crate feature coreclr only.

Starts CoreCLR with options. The options are ignored if it is already running.

Source

pub 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.

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.

Source

pub fn mono(options: &MonoOptions) -> Result<Self>

Available on crate feature mono only.

Starts Mono, or returns the already running instance.

Source

pub fn mono_attach(options: &MonoOptions) -> Result<Self>

Available on crate feature 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.

Source

pub fn backend(&self) -> Backend

Which runtime this is.

Source

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.

Source

pub fn load_assembly(&self, path: impl AsRef<Path>) -> Result<()>

Loads an assembly and makes it available to later loads by name.

Source

pub fn load_assembly_bytes(&self, name: &str, bytes: &[u8]) -> Result<()>

Loads an assembly from memory. name is its assembly name.

Source

pub fn load_module(&self, module: &Module) -> Result<()>

Available on crate feature hao only.

Loads a module built or edited with hao, without writing it to disk.

Source

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.

Source

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.

Source

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.

Source

pub fn corlib_method_pointer( &self, method: WellKnownMethod, ) -> Result<*const c_void>

Available on crate feature 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).

Source

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.

Source

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.

Source

pub fn invoke(&self, ptr: usize, args: &[u64]) -> Result<u64>

Available on x86-64 only.

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).

Source

pub fn invoke_float(&self, ptr: usize, args: &[u64]) -> Result<f64>

Available on x86-64 only.

Like invoke but returns f64.

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn type_names(&self, assembly: &str) -> Result<Vec<String>>

Returns all type full names from a loaded assembly.

Source

pub fn method_names( &self, assembly: &str, type_name: &str, ) -> Result<Vec<String>>

Returns all method names from a type in a loaded assembly.

Trait Implementations§

Source§

impl Clone for Runtime

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Runtime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Runtime

§

impl Freeze for Runtime
where *const ArcInner<Inner> is !null: Freeze, PhantomData<ArcInner<Inner>>: Freeze,

§

impl Move for Runtime

§

impl RefUnwindSafe for Runtime

§

impl Send for Runtime
where Arc<Inner>: Send,

§

impl Sync for Runtime
where Arc<Inner>: Sync,

§

impl Unpin for Runtime
where Arc<Inner>: Unpin,

§

impl UnsafeUnpin for Runtime
where *const ArcInner<Inner> is !null: UnsafeUnpin, PhantomData<ArcInner<Inner>>: UnsafeUnpin,

§

impl UnwindSafe for Runtime
where Arc<Inner>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more