Skip to content

Commit

Permalink
rune: Store OwnedTuple in AnyObj instead of Mutable (relates #844)
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 1, 2024
1 parent 70cec66 commit a044a51
Show file tree
Hide file tree
Showing 22 changed files with 665 additions and 449 deletions.
2 changes: 1 addition & 1 deletion crates/rune-modules/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,7 @@ mod const_status_code {

#[inline]
pub(super) fn to_const_value(status: reqwest::StatusCode) -> Result<ConstValue, RuntimeError> {
ConstValue::try_from(status.as_u16())
Ok(ConstValue::from(status.as_u16()))
}

#[inline]
Expand Down
114 changes: 58 additions & 56 deletions crates/rune/src/compile/ir/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,45 +222,45 @@ impl ir::Scopes {
}
ir::IrTargetKind::Index(target, index) => {
let value = self.get_target(target)?;
let target = value.borrow_ref_repr().with_span(ir_target)?;
let target = value.as_ref_repr().with_span(ir_target)?;

match target {
BorrowRefRepr::Mutable(value) => {
match &*value {
Mutable::Tuple(tuple) => {
if let Some(value) = tuple.get(*index) {
return Ok(value.clone());
}
}
actual => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
actual.type_info(),
))
}
};
RefRepr::Inline(value) => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
}
RefRepr::Mutable(value) => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.borrow_ref().with_span(ir_target)?.type_info(),
));
}
BorrowRefRepr::Any(value) => match value.type_hash() {
RefRepr::Any(value) => match value.type_hash() {
runtime::Vec::HASH => {
let vec = value.borrow_ref::<runtime::Vec>().with_span(ir_target)?;

if let Some(value) = vec.get(*index) {
return Ok(value.clone());
}
}
runtime::OwnedTuple::HASH => {
let tuple = value
.borrow_ref::<runtime::OwnedTuple>()
.with_span(ir_target)?;

if let Some(value) = tuple.get(*index) {
return Ok(value.clone());
}
}
_ => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
}
},
value => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
}
}

Err(compile::Error::new(
Expand Down Expand Up @@ -321,22 +321,12 @@ impl ir::Scopes {
));
}
RefRepr::Mutable(current) => {
let mut mutable = current.borrow_mut().with_span(ir_target)?;

match &mut *mutable {
Mutable::Tuple(tuple) => {
if let Some(current) = tuple.get_mut(*index) {
*current = value;
return Ok(());
}
}
value => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
}
};
let mutable = current.borrow_mut().with_span(ir_target)?;

return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
mutable.type_info(),
));
}
RefRepr::Any(any) => match any.type_hash() {
runtime::Vec::HASH => {
Expand All @@ -347,6 +337,16 @@ impl ir::Scopes {
return Ok(());
}
}
runtime::OwnedTuple::HASH => {
let mut tuple = any
.borrow_mut::<runtime::OwnedTuple>()
.with_span(ir_target)?;

if let Some(current) = tuple.get_mut(*index) {
*current = value;
return Ok(());
}
}
_ => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
Expand Down Expand Up @@ -409,24 +409,12 @@ impl ir::Scopes {

match current.as_ref_repr().with_span(ir_target)? {
RefRepr::Mutable(value) => {
let mut value = value.borrow_mut().with_span(ir_target)?;

match &mut *value {
Mutable::Tuple(tuple) => {
let value = tuple.get_mut(*index).ok_or_else(|| {
compile::Error::new(
ir_target,
IrErrorKind::MissingIndex { index: *index },
)
})?;

op(value)
}
actual => Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
actual.type_info(),
)),
}
let value = value.borrow_ref().with_span(ir_target)?;

Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
))
}
RefRepr::Any(value) => match value.type_hash() {
runtime::Vec::HASH => {
Expand All @@ -442,6 +430,20 @@ impl ir::Scopes {

op(value)
}
runtime::OwnedTuple::HASH => {
let mut tuple = value
.borrow_mut::<runtime::OwnedTuple>()
.with_span(ir_target)?;

let value = tuple.get_mut(*index).ok_or_else(|| {
compile::Error::new(
ir_target,
IrErrorKind::MissingIndex { index: *index },
)
})?;

op(value)
}
_ => Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
Expand Down
8 changes: 4 additions & 4 deletions crates/rune/src/doc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use crate::doc::context::{Function, Kind, Meta, Signature};
use crate::doc::templating;
use crate::doc::{Artifacts, Context, Visitor};
use crate::item::ComponentRef;
use crate::runtime::static_type;
use crate::runtime::OwnedTuple;
use crate::std::borrow::ToOwned;
use crate::{Hash, Item};
use crate::{Hash, Item, TypeHash};

use super::markdown;

Expand Down Expand Up @@ -440,7 +440,7 @@ impl<'m> Ctxt<'_, 'm> {
match *ty {
meta::DocType {
base, ref generics, ..
} if static_type::TUPLE == base && generics.is_empty() => Ok(None),
} if OwnedTuple::HASH == base && generics.is_empty() => Ok(None),
meta::DocType {
base, ref generics, ..
} => Ok(Some(self.link(base, None, generics)?)),
Expand Down Expand Up @@ -619,7 +619,7 @@ impl<'m> Ctxt<'_, 'm> {
return Ok(());
};

if static_type::TUPLE == hash && text.is_none() {
if OwnedTuple::HASH == hash && text.is_none() {
write!(o, "(")?;
self.write_generics(o, generics)?;
write!(o, ")")?;
Expand Down
36 changes: 36 additions & 0 deletions crates/rune/src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@ macro_rules! resolve_context {
};
}

macro_rules! impl_any_type {
(impl $(<$($p:ident),*>)? for $ty:ty, $path:path) => {
impl $(<$($p,)*>)* $crate::TypeHash for $ty {
const HASH: $crate::Hash = ::rune_macros::hash!($path);
}

impl $(<$($p,)*>)* $crate::runtime::TypeOf for $ty
where
$($($p: $crate::runtime::MaybeTypeOf,)*)*
{
const STATIC_TYPE_INFO: $crate::runtime::StaticTypeInfo = $crate::runtime::StaticTypeInfo::any_type_info(
$crate::runtime::AnyTypeInfo::new(
{
fn full_name(f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "{}", ::rune_macros::item!($path))
}

full_name
},
<Self as $crate::TypeHash>::HASH,
)
);
}

impl $(<$($p,)*>)* $crate::runtime::MaybeTypeOf for $ty
where
$($($p: $crate::runtime::MaybeTypeOf,)*)*
{
#[inline]
fn maybe_type_of() -> $crate::alloc::Result<$crate::compile::meta::DocType> {
Ok($crate::compile::meta::DocType::new(<$ty as $crate::TypeHash>::HASH))
}
}
}
}

/// Build an implementation of `TypeOf` basic of a static type.
macro_rules! impl_static_type {
(impl $(<$($p:ident),*>)? for $ty:ty, $name:ident, $hash:ident) => {
Expand Down
37 changes: 18 additions & 19 deletions crates/rune/src/modules/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use crate as rune;
use crate::alloc::Vec;
use crate::runtime::{
self, BorrowRefRepr, Future, Inline, Mut, Mutable, RefRepr, SelectFuture, Value, VmErrorKind,
VmResult,
self, Future, Inline, Mut, Mutable, RefRepr, SelectFuture, Value, VmErrorKind, VmResult,
};
use crate::{ContextError, Module, TypeHash};

Expand Down Expand Up @@ -101,29 +100,19 @@ where
/// ```
#[rune::function]
async fn join(value: Value) -> VmResult<Value> {
match vm_try!(value.borrow_ref_repr()) {
BorrowRefRepr::Inline(value) => match value {
match vm_try!(value.as_ref_repr()) {
RefRepr::Inline(value) => match value {
Inline::Unit => VmResult::Ok(Value::unit()),
value => VmResult::err([
VmErrorKind::bad_argument(0),
VmErrorKind::expected::<runtime::Vec>(value.type_info()),
]),
},
BorrowRefRepr::Mutable(value) => match *value {
Mutable::Tuple(ref tuple) => {
let result = try_join_impl(tuple.iter(), tuple.len(), |vec| {
VmResult::Ok(vm_try!(Value::tuple(vec)))
})
.await;

VmResult::Ok(vm_try!(result))
}
ref value => VmResult::err([
VmErrorKind::bad_argument(0),
VmErrorKind::expected::<runtime::Vec>(value.type_info()),
]),
},
BorrowRefRepr::Any(value) => match value.type_hash() {
RefRepr::Mutable(value) => VmResult::err([
VmErrorKind::bad_argument(0),
VmErrorKind::expected::<runtime::Vec>(vm_try!(value.borrow_ref()).type_info()),
]),
RefRepr::Any(value) => match value.type_hash() {
runtime::Vec::HASH => {
let vec = vm_try!(value.borrow_ref::<runtime::Vec>());
let result = try_join_impl(vec.iter(), vec.len(), |vec| {
Expand All @@ -132,6 +121,16 @@ async fn join(value: Value) -> VmResult<Value> {
.await;
VmResult::Ok(vm_try!(result))
}
runtime::OwnedTuple::HASH => {
let tuple = vm_try!(value.borrow_ref::<runtime::OwnedTuple>());

let result = try_join_impl(tuple.iter(), tuple.len(), |vec| {
VmResult::Ok(vm_try!(Value::tuple(vec)))
})
.await;

VmResult::Ok(vm_try!(result))
}
_ => VmResult::err([
VmErrorKind::bad_argument(0),
VmErrorKind::expected::<runtime::Vec>(value.type_info()),
Expand Down
Loading

0 comments on commit a044a51

Please sign in to comment.