Skip to main content

Crate hao

Crate hao 

Source
Expand description

hao reads, edits, creates and writes .NET assemblies.

The entry point is Module:

use hao::Module;

let module = Module::from_path("Example.dll").unwrap();

for ty in module.all_types() {
    let ty = ty.get();
    println!("{}", ty.full_name());
    for method in &ty.methods {
        println!("\t{}", method.get().name);
    }
}

Creating a module from scratch: a static class with one method, its body built with the Emitter:

use hao::prelude::{well_known::SystemType, *};

let mut module = Module::new("Demo", ModuleKind::Dll, TargetRuntime::NetCore(9));
let object = module.corlib_type(SystemType::Object)?;
let math = module.add_type(TypeDef::new(
    TypeAttributes::Public | TypeAttributes::Abstract | TypeAttributes::Sealed,
    "Demo",
    "MathUtil",
    Some(object),
));
let add = math.add_method(Method::new(
    MethodFlags::Public | MethodFlags::Static,
    "Add",
    MethodSig::new_static(TypeSig::INT32, vec![TypeSig::INT32, TypeSig::INT32]),
));
add.get_mut().body = Some(Emitter::build(|e| {
    e.ldarg(0).ldarg(1).op(OpCode::Add).ret();
}));

let bytes = module.write()?; // or module.write_to_path("Demo.dll")
assert!(Module::from_bytes(&bytes)?.find_method("Demo.MathUtil", "Add").is_some());

Modules are written with Module::write, which rebuilds the metadata from the entries.

Entries are backed by Rc<RefCell<_>>, or Arc<RwLock<_>> with the sync feature.

Loading is strict: malformed metadata is an error rather than being skipped.

Re-exports§

pub use dotnet::module::resolver;
pub use dotnet::Module;

Modules§

dotnet
Everything .NET: the Module model and its entries, signatures, IL, the raw metadata, and C# output. The crate root lists what each module is for.
error
The HaoError type returned by every fallible operation, and its Result alias.
io
Low-level reading of PE images: PEImage finds the .NET metadata in a PE file and DataReader parses bytes from it. Only needed to work below Module.
prelude
The common imports for building and inspecting a Module:
reflectreflect
Compile-time reflection, via facet, for registering hao’s types with an external consumer such as a scripting runtime. Enabled by the reflect feature (which also turns on sync).
sync
The pointer and lock types behind every Entry.

Macros§

callchain
Emits a MethodBody for an [UnmanagedCallersOnly] native-call chain — see the module docs for the pattern.
dotnet_class
Declares a static class with methods in module and returns hao::error::Result<Entry<TypeDef>>.
managed_body
Emits a MethodBody of straight-line managed calls — see the module docs.