Skip to content

Commit

Permalink
rune: Use a single RTTI
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 3, 2024
1 parent 276bfb6 commit e884877
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 172 deletions.
8 changes: 4 additions & 4 deletions crates/rune/src/compile/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::module::{
};
use crate::runtime::{
AnyTypeInfo, ConstConstruct, ConstContext, ConstValue, FunctionHandler, InstAddress, Memory,
Output, Protocol, RuntimeContext, TypeCheck, TypeInfo, VariantRtti, VmResult,
Output, Protocol, Rtti, RuntimeContext, TypeCheck, TypeInfo, VmResult,
};
use crate::{Hash, Item, ItemBuf};

Expand Down Expand Up @@ -771,9 +771,9 @@ impl Context {
item: item.try_clone()?,
hash,
type_check: None,
type_info: TypeInfo::variant(Arc::new(VariantRtti {
enum_hash: ty.hash,
hash,
type_info: TypeInfo::typed(Arc::new(Rtti {
hash: ty.hash,
variant_hash: hash,
item: item.try_clone()?,
fields: fields.to_fields()?,
})),
Expand Down
4 changes: 2 additions & 2 deletions crates/rune/src/compile/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ pub(crate) enum ErrorKind {
path: Vec<ImportStep>,
},
LastUseComponent,
VariantRttiConflict {
RttiConflict {
hash: Hash,
},
TypeRttiConflict {
Expand Down Expand Up @@ -1113,7 +1113,7 @@ impl fmt::Display for ErrorKind {
ErrorKind::LastUseComponent => {
write!(f, "Missing last use component")?;
}
ErrorKind::VariantRttiConflict { hash } => {
ErrorKind::RttiConflict { hash } => {
write!(f,"Tried to insert variant runtime type information, but conflicted with hash `{hash}`")?;
}
ErrorKind::TypeRttiConflict { hash } => {
Expand Down
48 changes: 20 additions & 28 deletions crates/rune/src/compile/unit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use crate::query::QueryInner;
use crate::runtime::debug::{DebugArgs, DebugSignature};
use crate::runtime::unit::UnitEncoder;
use crate::runtime::{
Call, ConstValue, DebugInfo, DebugInst, Inst, Label, Protocol, Rtti, StaticString, Unit,
UnitFn, VariantRtti,
Call, ConstValue, DebugInfo, DebugInst, Inst, Label, Protocol, Rtti, StaticString, Unit, UnitFn,
};
use crate::{Context, Diagnostics, Hash, Item, SourceId};

Expand Down Expand Up @@ -74,8 +73,6 @@ pub(crate) struct UnitBuilder {
static_object_keys_rev: HashMap<Hash, usize>,
/// Runtime type information for types.
rtti: hash::Map<Arc<Rtti>>,
/// Runtime type information for variants.
variant_rtti: hash::Map<Arc<VariantRtti>>,
/// The current label count.
label_count: usize,
/// A collection of required function hashes.
Expand Down Expand Up @@ -153,7 +150,6 @@ impl UnitBuilder {
self.static_bytes,
self.static_object_keys,
self.rtti,
self.variant_rtti,
self.debug,
self.constants,
))
Expand Down Expand Up @@ -317,6 +313,7 @@ impl UnitBuilder {

let rtti = Arc::new(Rtti {
hash,
variant_hash: Hash::EMPTY,
item: pool.item(meta.item_meta.item).try_to_owned()?,
fields: HashMap::new(),
});
Expand Down Expand Up @@ -348,6 +345,7 @@ impl UnitBuilder {

let rtti = Arc::new(Rtti {
hash: meta.hash,
variant_hash: Hash::EMPTY,
item: pool.item(meta.item_meta.item).try_to_owned()?,
fields: HashMap::new(),
});
Expand Down Expand Up @@ -405,6 +403,7 @@ impl UnitBuilder {

let rtti = Arc::new(Rtti {
hash: meta.hash,
variant_hash: Hash::EMPTY,
item: pool.item(meta.item_meta.item).try_to_owned()?,
fields: HashMap::new(),
});
Expand Down Expand Up @@ -454,6 +453,7 @@ impl UnitBuilder {

let rtti = Arc::new(Rtti {
hash,
variant_hash: Hash::EMPTY,
item: pool.item(meta.item_meta.item).try_to_owned()?,
fields: named.to_fields()?,
});
Expand All @@ -477,22 +477,22 @@ impl UnitBuilder {
fields: meta::Fields::Empty,
..
} => {
let rtti = Arc::new(VariantRtti {
enum_hash,
hash: meta.hash,
let rtti = Arc::new(Rtti {
hash: enum_hash,
variant_hash: meta.hash,
item: pool.item(meta.item_meta.item).try_to_owned()?,
fields: HashMap::new(),
});

if self
.variant_rtti
.rtti
.try_insert(meta.hash, rtti)
.with_span(span)?
.is_some()
{
return Err(compile::Error::new(
span,
ErrorKind::VariantRttiConflict { hash: meta.hash },
ErrorKind::RttiConflict { hash: meta.hash },
));
}

Expand Down Expand Up @@ -526,22 +526,22 @@ impl UnitBuilder {
fields: meta::Fields::Unnamed(args),
..
} => {
let rtti = Arc::new(VariantRtti {
enum_hash,
hash: meta.hash,
let rtti = Arc::new(Rtti {
hash: enum_hash,
variant_hash: meta.hash,
item: pool.item(meta.item_meta.item).try_to_owned()?,
fields: HashMap::new(),
});

if self
.variant_rtti
.rtti
.try_insert(meta.hash, rtti)
.with_span(span)?
.is_some()
{
return Err(compile::Error::new(
span,
ErrorKind::VariantRttiConflict { hash: meta.hash },
ErrorKind::RttiConflict { hash: meta.hash },
));
}

Expand Down Expand Up @@ -580,23 +580,15 @@ impl UnitBuilder {
} => {
let hash = pool.item_type_hash(meta.item_meta.item);

let rtti = Arc::new(VariantRtti {
enum_hash,
hash,
let rtti = Arc::new(Rtti {
hash: enum_hash,
variant_hash: hash,
item: pool.item(meta.item_meta.item).try_to_owned()?,
fields: named.to_fields()?,
});

if self
.variant_rtti
.try_insert(hash, rtti)
.with_span(span)?
.is_some()
{
return Err(compile::Error::new(
span,
ErrorKind::VariantRttiConflict { hash },
));
if self.rtti.try_insert(hash, rtti).with_span(span)?.is_some() {
return Err(compile::Error::new(span, ErrorKind::RttiConflict { hash }));
}
}
meta::Kind::Enum { .. } => {
Expand Down
16 changes: 8 additions & 8 deletions crates/rune/src/runtime/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::Hash;

use super::{
Args, Call, ConstValue, Formatter, FromValue, FunctionHandler, GuardedArgs, InstAddress,
Output, OwnedTuple, Rtti, RuntimeContext, RuntimeError, Stack, Unit, Value, VariantRtti, Vm,
VmCall, VmErrorKind, VmHalt, VmResult,
Output, OwnedTuple, Rtti, RuntimeContext, RuntimeError, Stack, Unit, Value, Vm, VmCall,
VmErrorKind, VmHalt, VmResult,
};

/// The type of a function in Rune.
Expand Down Expand Up @@ -256,12 +256,12 @@ impl Function {
}

/// Create a function pointer that constructs a empty variant.
pub(crate) fn from_unit_variant(rtti: Arc<VariantRtti>) -> Self {
pub(crate) fn from_unit_variant(rtti: Arc<Rtti>) -> Self {
Self(FunctionImpl::from_unit_variant(rtti))
}

/// Create a function pointer that constructs a tuple variant.
pub(crate) fn from_tuple_variant(rtti: Arc<VariantRtti>, args: usize) -> Self {
pub(crate) fn from_tuple_variant(rtti: Arc<Rtti>, args: usize) -> Self {
Self(FunctionImpl::from_tuple_variant(rtti, args))
}

Expand Down Expand Up @@ -766,14 +766,14 @@ where
}

/// Create a function pointer that constructs a empty variant.
pub(crate) fn from_unit_variant(rtti: Arc<VariantRtti>) -> Self {
pub(crate) fn from_unit_variant(rtti: Arc<Rtti>) -> Self {
Self {
inner: Inner::FnUnitVariant(FnUnitVariant { rtti }),
}
}

/// Create a function pointer that constructs a tuple variant.
pub(crate) fn from_tuple_variant(rtti: Arc<VariantRtti>, args: usize) -> Self {
pub(crate) fn from_tuple_variant(rtti: Arc<Rtti>, args: usize) -> Self {
Self {
inner: Inner::FnTupleVariant(FnTupleVariant { rtti, args }),
}
Expand Down Expand Up @@ -1032,13 +1032,13 @@ struct FnTupleStruct {
#[derive(Debug, Clone, TryClone)]
struct FnUnitVariant {
/// Runtime information fo variant.
rtti: Arc<VariantRtti>,
rtti: Arc<Rtti>,
}

#[derive(Debug, Clone, TryClone)]
struct FnTupleVariant {
/// Runtime information fo variant.
rtti: Arc<VariantRtti>,
rtti: Arc<Rtti>,
/// The number of arguments the tuple takes.
args: usize,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub use self::unit::{Unit, UnitStorage};
mod value;
pub use self::value::{
Accessor, EmptyStruct, Inline, RawValueGuard, Rtti, Struct, TupleStruct, TypeValue, Value,
ValueMutGuard, ValueRefGuard, VariantRtti,
ValueMutGuard, ValueRefGuard,
};
pub(crate) use self::value::{Mutable, ReprMut, ReprOwned, ReprRef};

Expand Down
21 changes: 5 additions & 16 deletions crates/rune/src/runtime/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ use crate::{Any, TypeHash};

use ::rust_alloc::sync::Arc;

use super::{Rtti, VariantRtti};
use super::Rtti;

#[derive(Debug, TryClone, PartialEq, Eq)]
enum TypeInfoKind {
/// Reference to an external type.
Any(AnyTypeInfo),
/// A named type.
Typed(Arc<Rtti>),
/// A variant.
Variant(Arc<VariantRtti>),
Runtime(Arc<Rtti>),
}

/// Diagnostical type information for a given type.
Expand Down Expand Up @@ -67,20 +65,14 @@ impl TypeInfo {

#[inline]
pub(crate) const fn typed(rtti: Arc<Rtti>) -> Self {
Self::new(TypeInfoKind::Typed(rtti))
}

#[inline]
pub(crate) const fn variant(rtti: Arc<VariantRtti>) -> Self {
Self::new(TypeInfoKind::Variant(rtti))
Self::new(TypeInfoKind::Runtime(rtti))
}

#[cfg(feature = "emit")]
pub(crate) fn type_hash(&self) -> Hash {
match &self.kind {
TypeInfoKind::Typed(ty) => ty.hash,
TypeInfoKind::Variant(ty) => ty.hash,
TypeInfoKind::Any(ty) => ty.hash,
TypeInfoKind::Runtime(ty) => ty.hash,
}
}
}
Expand All @@ -95,10 +87,7 @@ impl fmt::Debug for TypeInfo {
impl fmt::Display for TypeInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.kind {
TypeInfoKind::Typed(rtti) => {
write!(f, "{}", rtti.item)?;
}
TypeInfoKind::Variant(rtti) => {
TypeInfoKind::Runtime(rtti) => {
write!(f, "{}", rtti.item)?;
}
TypeInfoKind::Any(info) => {
Expand Down
13 changes: 1 addition & 12 deletions crates/rune/src/runtime/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ use crate as rune;
use crate::alloc::prelude::*;
use crate::alloc::{self, Box, String, Vec};
use crate::hash;
use crate::runtime::{
Call, ConstValue, DebugInfo, Inst, Rtti, StaticString, VariantRtti, VmError, VmErrorKind,
};
use crate::runtime::{Call, ConstValue, DebugInfo, Inst, Rtti, StaticString, VmError, VmErrorKind};
use crate::Hash;

pub use self::storage::{ArrayUnit, EncodeError, UnitEncoder, UnitStorage};
Expand Down Expand Up @@ -72,8 +70,6 @@ pub struct Logic<S = DefaultStorage> {
static_object_keys: Vec<Box<[String]>>,
/// Runtime information for types.
rtti: hash::Map<Arc<Rtti>>,
/// Runtime information for variants.
variant_rtti: hash::Map<Arc<VariantRtti>>,
/// Named constants
constants: hash::Map<ConstValue>,
}
Expand All @@ -96,7 +92,6 @@ impl<S> Unit<S> {
static_bytes: Vec<Vec<u8>>,
static_object_keys: Vec<Box<[String]>>,
rtti: hash::Map<Arc<Rtti>>,
variant_rtti: hash::Map<Arc<VariantRtti>>,
debug: Option<Box<DebugInfo>>,
constants: hash::Map<ConstValue>,
) -> Self {
Expand All @@ -108,7 +103,6 @@ impl<S> Unit<S> {
static_bytes,
static_object_keys,
rtti,
variant_rtti,
constants,
},
debug,
Expand Down Expand Up @@ -194,11 +188,6 @@ impl<S> Unit<S> {
self.logic.rtti.get(&hash)
}

/// Lookup variant run-time information for the given variant hash.
pub(crate) fn lookup_variant_rtti(&self, hash: Hash) -> Option<&Arc<VariantRtti>> {
self.logic.variant_rtti.get(&hash)
}

/// Lookup a function in the unit.
pub(crate) fn function(&self, hash: Hash) -> Option<UnitFn> {
self.logic.functions.get(&hash).copied()
Expand Down
Loading

0 comments on commit e884877

Please sign in to comment.