Skip to main content

xur_std/re/dotnet/
types.rs

1//! The opaque values scripts get from `std::re::dotnet`: handles on a
2//! module, its entries (types, methods, fields, ...), signatures and IL.
3//!
4//! A host can make them too, around a module or entry it keeps itself:
5//! the handles share state (hao's entries are `Arc`s, and a module is behind
6//! an `Arc<Mutex<..>>`), so what a script changes through one is what the
7//! host sees, and the other way round.
8
9use std::sync::{Arc, Mutex};
10
11use facet::Facet;
12use hao::prelude::*;
13
14#[derive(Facet, Clone)]
15#[facet(opaque)]
16pub struct DotnetModule(pub(crate) Arc<Mutex<hao::Module>>);
17
18impl std::fmt::Display for DotnetModule {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        write!(f, "Module({})", self.0.lock().unwrap().name())
21    }
22}
23
24#[derive(Facet, Clone)]
25#[facet(opaque)]
26pub struct DotnetEmitter(pub(crate) Arc<Mutex<Option<hao::dotnet::il::Emitter>>>);
27
28impl std::fmt::Display for DotnetEmitter {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        write!(f, "Emitter")
31    }
32}
33
34#[derive(Facet, Clone)]
35#[facet(opaque)]
36pub struct DotnetToken(pub(crate) MemberToken);
37
38impl std::fmt::Display for DotnetToken {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        write!(f, "Token")
41    }
42}
43
44#[derive(Facet, Clone)]
45#[facet(opaque)]
46pub struct DotnetTypeSig(pub(crate) TypeSig);
47
48impl std::fmt::Display for DotnetTypeSig {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "{:?}", self.0)
51    }
52}
53
54#[derive(Facet, Clone)]
55#[facet(opaque)]
56pub struct DotnetMethodSig(pub(crate) MethodSig);
57
58impl std::fmt::Display for DotnetMethodSig {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        write!(f, "(")?;
61        for (i, p) in self.0.params.iter().enumerate() {
62            if i > 0 {
63                write!(f, ", ")?;
64            }
65            write!(f, "{}", DotnetTypeSig(p.clone()))?;
66        }
67        write!(f, ") -> {}", DotnetTypeSig(self.0.ret.clone()))
68    }
69}
70
71#[derive(Facet, Clone)]
72#[facet(opaque)]
73pub struct DotnetTypeDef(pub(crate) Entry<TypeDef>);
74
75impl std::fmt::Display for DotnetTypeDef {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        write!(f, "TypeDef({})", self.0.get().full_name())
78    }
79}
80
81#[derive(Facet, Clone)]
82#[facet(opaque)]
83pub struct DotnetMethodEntry(pub(crate) Entry<Method>);
84
85impl std::fmt::Display for DotnetMethodEntry {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        write!(f, "Method({})", self.0.get().name)
88    }
89}
90
91#[derive(Facet, Clone)]
92#[facet(opaque)]
93pub struct DotnetMethodBody(pub(crate) hao::dotnet::il::MethodBody);
94
95impl std::fmt::Display for DotnetMethodBody {
96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97        write!(f, "MethodBody({} instructions)", self.0.instructions.len())
98    }
99}
100
101#[derive(Facet, Clone)]
102#[facet(opaque)]
103pub struct DotnetTypeRef(pub(crate) hao::dotnet::entries::TypeDefOrRef);
104
105impl std::fmt::Display for DotnetTypeRef {
106    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107        write!(f, "TypeRef({})", self.0.full_name())
108    }
109}
110
111#[derive(Facet, Clone)]
112#[facet(opaque)]
113pub struct DotnetField(pub(crate) Entry<Field>);
114
115impl std::fmt::Display for DotnetField {
116    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        write!(f, "Field({})", self.0.get().name)
118    }
119}
120
121#[derive(Facet, Clone)]
122#[facet(opaque)]
123pub struct DotnetProperty(pub(crate) Entry<Property>);
124
125impl std::fmt::Display for DotnetProperty {
126    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127        write!(f, "Property({})", self.0.get().name)
128    }
129}
130
131#[derive(Facet, Clone)]
132#[facet(opaque)]
133pub struct DotnetEvent(pub(crate) Entry<Event>);
134
135impl std::fmt::Display for DotnetEvent {
136    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137        write!(f, "Event({})", self.0.get().name)
138    }
139}
140
141#[derive(Facet, Clone)]
142#[facet(opaque)]
143pub struct DotnetParam(pub(crate) Entry<Param>);
144
145impl std::fmt::Display for DotnetParam {
146    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147        write!(f, "Param({})", self.0.get().name)
148    }
149}
150
151impl DotnetModule {
152    /// A script handle on a module the host keeps too.
153    pub fn from_shared(module: Arc<Mutex<hao::Module>>) -> Self {
154        Self(module)
155    }
156
157    /// The module this handle shares.
158    pub fn shared(&self) -> Arc<Mutex<hao::Module>> {
159        Arc::clone(&self.0)
160    }
161}
162
163impl DotnetTypeDef {
164    /// A script handle on a type the host has.
165    pub fn from_entry(entry: Entry<TypeDef>) -> Self {
166        Self(entry)
167    }
168
169    pub fn entry(&self) -> &Entry<TypeDef> {
170        &self.0
171    }
172}
173
174impl DotnetMethodEntry {
175    /// A script handle on a method the host has.
176    pub fn from_entry(entry: Entry<Method>) -> Self {
177        Self(entry)
178    }
179
180    pub fn entry(&self) -> &Entry<Method> {
181        &self.0
182    }
183}