xur-std 0.1.11

The standard library of the Xur language
Documentation
//! The opaque values scripts get from `std::re::dotnet`: handles on a
//! module, its entries (types, methods, fields, ...), signatures and IL.
//!
//! A host can make them too, around a module or entry it keeps itself:
//! the handles share state (hao's entries are `Arc`s, and a module is behind
//! an `Arc<Mutex<..>>`), so what a script changes through one is what the
//! host sees, and the other way round.

use std::sync::{Arc, Mutex};

use facet::Facet;
use hao::prelude::*;

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetModule(pub(crate) Arc<Mutex<hao::Module>>);

impl std::fmt::Display for DotnetModule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Module({})", self.0.lock().unwrap().name())
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetEmitter(pub(crate) Arc<Mutex<Option<hao::dotnet::il::Emitter>>>);

impl std::fmt::Display for DotnetEmitter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Emitter")
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetToken(pub(crate) MemberToken);

impl std::fmt::Display for DotnetToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Token")
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetTypeSig(pub(crate) TypeSig);

impl std::fmt::Display for DotnetTypeSig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetMethodSig(pub(crate) MethodSig);

impl std::fmt::Display for DotnetMethodSig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "(")?;
        for (i, p) in self.0.params.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            write!(f, "{}", DotnetTypeSig(p.clone()))?;
        }
        write!(f, ") -> {}", DotnetTypeSig(self.0.ret.clone()))
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetTypeDef(pub(crate) Entry<TypeDef>);

impl std::fmt::Display for DotnetTypeDef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "TypeDef({})", self.0.get().full_name())
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetMethodEntry(pub(crate) Entry<Method>);

impl std::fmt::Display for DotnetMethodEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Method({})", self.0.get().name)
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetMethodBody(pub(crate) hao::dotnet::il::MethodBody);

impl std::fmt::Display for DotnetMethodBody {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MethodBody({} instructions)", self.0.instructions.len())
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetTypeRef(pub(crate) hao::dotnet::entries::TypeDefOrRef);

impl std::fmt::Display for DotnetTypeRef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "TypeRef({})", self.0.full_name())
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetField(pub(crate) Entry<Field>);

impl std::fmt::Display for DotnetField {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Field({})", self.0.get().name)
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetProperty(pub(crate) Entry<Property>);

impl std::fmt::Display for DotnetProperty {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Property({})", self.0.get().name)
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetEvent(pub(crate) Entry<Event>);

impl std::fmt::Display for DotnetEvent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Event({})", self.0.get().name)
    }
}

#[derive(Facet, Clone)]
#[facet(opaque)]
pub struct DotnetParam(pub(crate) Entry<Param>);

impl std::fmt::Display for DotnetParam {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Param({})", self.0.get().name)
    }
}

impl DotnetModule {
    /// A script handle on a module the host keeps too.
    pub fn from_shared(module: Arc<Mutex<hao::Module>>) -> Self {
        Self(module)
    }

    /// The module this handle shares.
    pub fn shared(&self) -> Arc<Mutex<hao::Module>> {
        Arc::clone(&self.0)
    }
}

impl DotnetTypeDef {
    /// A script handle on a type the host has.
    pub fn from_entry(entry: Entry<TypeDef>) -> Self {
        Self(entry)
    }

    pub fn entry(&self) -> &Entry<TypeDef> {
        &self.0
    }
}

impl DotnetMethodEntry {
    /// A script handle on a method the host has.
    pub fn from_entry(entry: Entry<Method>) -> Self {
        Self(entry)
    }

    pub fn entry(&self) -> &Entry<Method> {
        &self.0
    }
}