diff --git a/crates/rune/src/cli/tests.rs b/crates/rune/src/cli/tests.rs index a2e67e576..179552ac1 100644 --- a/crates/rune/src/cli/tests.rs +++ b/crates/rune/src/cli/tests.rs @@ -18,7 +18,7 @@ use crate::cli::{ use crate::compile::FileSourceLoader; use crate::doc::{TestKind, TestParams}; use crate::modules::capture_io::CaptureIo; -use crate::runtime::{RefRepr, Value, Vm, VmError, VmResult}; +use crate::runtime::{ReprRef, Value, Vm, VmError, VmResult}; use crate::{Diagnostics, Hash, Item, ItemBuf, Source, Sources, TypeHash, Unit}; mod cli { @@ -534,8 +534,8 @@ impl TestCase { capture_io.drain_into(&mut self.output)?; self.outcome = match result { - VmResult::Ok(v) => match v.as_ref_repr()? { - RefRepr::Any(value) => match value.type_hash() { + VmResult::Ok(v) => match v.as_ref()? { + ReprRef::Any(value) => match value.type_hash() { Result::::HASH => { let result = value.borrow_ref::>()?; diff --git a/crates/rune/src/compile/ir/eval.rs b/crates/rune/src/compile/ir/eval.rs index 2ddd91f07..30664b073 100644 --- a/crates/rune/src/compile/ir/eval.rs +++ b/crates/rune/src/compile/ir/eval.rs @@ -7,7 +7,7 @@ use crate::ast::{Span, Spanned}; use crate::compile::ir::{self}; use crate::compile::{self, WithSpan}; use crate::query::Used; -use crate::runtime::{BorrowRefRepr, Inline, Object, OwnedTuple, Value}; +use crate::runtime::{Inline, Object, OwnedTuple, ReprRef, Value}; use crate::TypeHash; /// The outcome of a constant evaluation. @@ -72,11 +72,11 @@ fn eval_ir_binary( let a = eval_ir(&ir.lhs, interp, used)?; let b = eval_ir(&ir.rhs, interp, used)?; - let a = a.borrow_ref_repr().with_span(ir)?; - let b = b.borrow_ref_repr().with_span(ir)?; + let a = a.as_ref().with_span(ir)?; + let b = b.as_ref().with_span(ir)?; match (a, b) { - (BorrowRefRepr::Inline(a), BorrowRefRepr::Inline(b)) => { + (ReprRef::Inline(a), ReprRef::Inline(b)) => { let out = 'out: { match (a, b) { (Inline::Signed(a), Inline::Signed(b)) => match ir.op { @@ -140,7 +140,7 @@ fn eval_ir_binary( return Ok(Value::from(out)); } - (BorrowRefRepr::Any(a), BorrowRefRepr::Any(b)) => { + (ReprRef::Any(a), ReprRef::Any(b)) => { let value = 'out: { if let (String::HASH, String::HASH) = (a.type_hash(), b.type_hash()) { let a = a.borrow_ref::().with_span(span)?; @@ -353,10 +353,10 @@ fn eval_ir_template( } ir::IrTemplateComponent::Ir(ir) => { let const_value = eval_ir(ir, interp, used)?; - let value = const_value.borrow_ref_repr().with_span(ir)?; + let value = const_value.as_ref().with_span(ir)?; match value { - BorrowRefRepr::Inline(value) => match value { + ReprRef::Inline(value) => match value { Inline::Signed(integer) => { write!(buf, "{integer}")?; } @@ -371,7 +371,7 @@ fn eval_ir_template( return Err(EvalOutcome::not_const(ir)); } }, - BorrowRefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { String::HASH => { let s = value.borrow_ref::().with_span(ir)?; buf.try_push_str(&s)?; @@ -380,7 +380,7 @@ fn eval_ir_template( return Err(EvalOutcome::not_const(ir)); } }, - BorrowRefRepr::Mutable(..) => { + ReprRef::Mutable(..) => { return Err(EvalOutcome::not_const(ir)); } } diff --git a/crates/rune/src/compile/ir/interpreter.rs b/crates/rune/src/compile/ir/interpreter.rs index d5ab55064..c9a67c8e1 100644 --- a/crates/rune/src/compile/ir/interpreter.rs +++ b/crates/rune/src/compile/ir/interpreter.rs @@ -7,7 +7,7 @@ use crate::compile::meta; use crate::compile::{self, IrErrorKind, ItemId, ModId, WithSpan}; use crate::hir; use crate::query::{Query, Used}; -use crate::runtime::{self, ConstValue, Object, OwnedTuple, RefRepr, Value}; +use crate::runtime::{self, ConstValue, Object, OwnedTuple, ReprRef, Value}; use crate::TypeHash; /// The interpreter that executed [Ir][crate::ir::Ir]. @@ -200,22 +200,22 @@ impl ir::Scopes { } ir::IrTargetKind::Index(target, index) => { let value = self.get_target(target)?; - let target = value.as_ref_repr().with_span(ir_target)?; + let target = value.as_ref().with_span(ir_target)?; match target { - RefRepr::Inline(value) => { + ReprRef::Inline(value) => { return Err(compile::Error::expected_type::( ir_target, value.type_info(), )); } - RefRepr::Mutable(value) => { + ReprRef::Mutable(value) => { return Err(compile::Error::expected_type::( ir_target, value.borrow_ref().with_span(ir_target)?.type_info(), )); } - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { runtime::Vec::HASH => { let vec = value.borrow_ref::().with_span(ir_target)?; @@ -270,14 +270,14 @@ impl ir::Scopes { ir::IrTargetKind::Index(target, index) => { let target = self.get_target(target)?; - match target.as_ref_repr().with_span(ir_target)? { - RefRepr::Inline(value) => { + match target.as_ref().with_span(ir_target)? { + ReprRef::Inline(value) => { return Err(compile::Error::expected_type::( ir_target, value.type_info(), )); } - RefRepr::Mutable(current) => { + ReprRef::Mutable(current) => { let mutable = current.borrow_mut().with_span(ir_target)?; return Err(compile::Error::expected_type::( @@ -285,7 +285,7 @@ impl ir::Scopes { mutable.type_info(), )); } - RefRepr::Any(any) => match any.type_hash() { + ReprRef::Any(any) => match any.type_hash() { runtime::Vec::HASH => { let mut vec = any.borrow_mut::().with_span(ir_target)?; @@ -347,8 +347,8 @@ impl ir::Scopes { ir::IrTargetKind::Index(target, index) => { let current = self.get_target(target)?; - match current.as_ref_repr().with_span(ir_target)? { - RefRepr::Mutable(value) => { + match current.as_ref().with_span(ir_target)? { + ReprRef::Mutable(value) => { let value = value.borrow_ref().with_span(ir_target)?; Err(compile::Error::expected_type::( @@ -356,7 +356,7 @@ impl ir::Scopes { value.type_info(), )) } - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { runtime::Vec::HASH => { let mut vec = value.borrow_mut::().with_span(ir_target)?; diff --git a/crates/rune/src/modules/future.rs b/crates/rune/src/modules/future.rs index 8809370f4..ae2ab0495 100644 --- a/crates/rune/src/modules/future.rs +++ b/crates/rune/src/modules/future.rs @@ -2,7 +2,7 @@ use crate as rune; use crate::alloc::Vec; -use crate::runtime::{self, Future, Inline, RefRepr, SelectFuture, Value, VmErrorKind, VmResult}; +use crate::runtime::{self, Future, Inline, ReprRef, SelectFuture, Value, VmErrorKind, VmResult}; use crate::{ContextError, Module, TypeHash}; /// Asynchronous computations. @@ -25,20 +25,20 @@ where let mut results = vm_try!(Vec::try_with_capacity(len)); for (index, value) in values.into_iter().enumerate() { - match vm_try!(value.as_ref_repr()) { - RefRepr::Inline(value) => { + match vm_try!(value.as_ref()) { + ReprRef::Inline(value) => { return VmResult::err([ VmErrorKind::expected::(value.type_info()), VmErrorKind::bad_argument(index), ]); } - RefRepr::Mutable(value) => { + ReprRef::Mutable(value) => { return VmResult::err([ VmErrorKind::expected::(vm_try!(value.borrow_ref()).type_info()), VmErrorKind::bad_argument(index), ]); } - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { Future::HASH => { let future = vm_try!(Value::from(value.clone()).into_future()); futures.push(SelectFuture::new(index, future)); @@ -98,19 +98,19 @@ where /// ``` #[rune::function(keep)] async fn join(value: Value) -> VmResult { - match vm_try!(value.as_ref_repr()) { - RefRepr::Inline(value) => match value { + match vm_try!(value.as_ref()) { + ReprRef::Inline(value) => match value { Inline::Unit => VmResult::Ok(Value::unit()), value => VmResult::err([ VmErrorKind::bad_argument(0), VmErrorKind::expected::(value.type_info()), ]), }, - RefRepr::Mutable(value) => VmResult::err([ + ReprRef::Mutable(value) => VmResult::err([ VmErrorKind::bad_argument(0), VmErrorKind::expected::(vm_try!(value.borrow_ref()).type_info()), ]), - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { runtime::Vec::HASH => { let vec = vm_try!(value.borrow_ref::()); let result = try_join_impl(vec.iter(), vec.len(), |vec| { diff --git a/crates/rune/src/modules/iter.rs b/crates/rune/src/modules/iter.rs index fa93b4f36..d164ef4a9 100644 --- a/crates/rune/src/modules/iter.rs +++ b/crates/rune/src/modules/iter.rs @@ -8,7 +8,7 @@ use crate::modules::collections::VecDeque; use crate::modules::collections::{HashMap, HashSet}; use crate::runtime::range::RangeIter; use crate::runtime::{ - BorrowRefRepr, FromValue, Function, Inline, InstAddress, Object, Output, OwnedTuple, Protocol, + FromValue, Function, Inline, InstAddress, Object, Output, OwnedTuple, Protocol, ReprRef, TypeHash, Value, Vec, VmErrorKind, VmResult, }; use crate::shared::Caller; @@ -473,17 +473,19 @@ pub fn module() -> Result { let mut string = String::new(); while let Some(value) = vm_try!(next.call((iter.clone(),))) { - match vm_try!(value.borrow_ref_repr()) { - BorrowRefRepr::Inline(Inline::Char(c)) => { + match vm_try!(value.as_ref()) { + ReprRef::Inline(Inline::Char(c)) => { vm_try!(string.try_push(*c)); } - BorrowRefRepr::Inline(value) => { + ReprRef::Inline(value) => { return VmResult::expected::(value.type_info()); } - BorrowRefRepr::Mutable(value) => { - return VmResult::expected::(value.type_info()); + ReprRef::Mutable(value) => { + return VmResult::expected::( + vm_try!(value.borrow_ref()).type_info(), + ); } - BorrowRefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { String::HASH => { let s = vm_try!(value.borrow_ref::()); vm_try!(string.try_push_str(&s)); diff --git a/crates/rune/src/modules/string.rs b/crates/rune/src/modules/string.rs index af2643f93..953620ba0 100644 --- a/crates/rune/src/modules/string.rs +++ b/crates/rune/src/modules/string.rs @@ -12,7 +12,7 @@ use crate::alloc::{String, Vec}; use crate::compile::Named; use crate::runtime::{ Bytes, Formatter, FromValue, Function, Hasher, Inline, MaybeTypeOf, Panic, Range, RangeFrom, - RangeFull, RangeInclusive, RangeTo, RangeToInclusive, Ref, RefRepr, ToValue, TypeOf, Value, + RangeFull, RangeInclusive, RangeTo, RangeToInclusive, Ref, ReprRef, ToValue, TypeOf, Value, VmErrorKind, VmResult, }; use crate::{Any, ContextError, Module, TypeHash}; @@ -888,19 +888,19 @@ fn shrink_to_fit(s: &mut String) -> VmResult<()> { /// [`split_whitespace`]: str::split_whitespace #[rune::function(instance, deprecated = "Use String::split instead")] fn split(this: Ref, value: Value) -> VmResult { - match vm_try!(value.as_ref_repr()) { - RefRepr::Inline(Inline::Char(c)) => { + match vm_try!(value.as_ref()) { + ReprRef::Inline(Inline::Char(c)) => { VmResult::Ok(vm_try!(rune::to_value(Split::new(this, *c)))) } - RefRepr::Inline(value) => VmResult::err([ + ReprRef::Inline(value) => VmResult::err([ VmErrorKind::expected::(value.type_info()), VmErrorKind::bad_argument(0), ]), - RefRepr::Mutable(value) => VmResult::err([ + ReprRef::Mutable(value) => VmResult::err([ VmErrorKind::expected::(vm_try!(value.borrow_ref()).type_info()), VmErrorKind::bad_argument(0), ]), - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { String::HASH => { let s = vm_try!(value.borrow_ref::()); @@ -937,21 +937,21 @@ fn split(this: Ref, value: Value) -> VmResult { /// ``` #[rune::function(instance)] fn split_once(this: &str, value: Value) -> VmResult> { - let outcome = match vm_try!(value.as_ref_repr()) { - RefRepr::Inline(Inline::Char(pat)) => this.split_once(*pat), - RefRepr::Inline(value) => { + let outcome = match vm_try!(value.as_ref()) { + ReprRef::Inline(Inline::Char(pat)) => this.split_once(*pat), + ReprRef::Inline(value) => { return VmResult::err([ VmErrorKind::expected::(value.type_info()), VmErrorKind::bad_argument(0), ]); } - RefRepr::Mutable(value) => { + ReprRef::Mutable(value) => { return VmResult::err([ VmErrorKind::expected::(vm_try!(value.borrow_ref()).type_info()), VmErrorKind::bad_argument(0), ]); } - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { String::HASH => { let s = vm_try!(value.borrow_ref::()); this.split_once(s.as_str()) diff --git a/crates/rune/src/runtime/const_value.rs b/crates/rune/src/runtime/const_value.rs index 8fcea0ce0..26e9d5c14 100644 --- a/crates/rune/src/runtime/const_value.rs +++ b/crates/rune/src/runtime/const_value.rs @@ -16,8 +16,8 @@ use crate::runtime; use crate::{Hash, TypeHash}; use super::{ - BorrowRefRepr, Bytes, FromValue, Inline, Object, OwnedTuple, ToValue, Tuple, Type, TypeInfo, - Value, VmErrorKind, + Bytes, FromValue, Inline, Object, OwnedTuple, ReprRef, ToValue, Tuple, Type, TypeInfo, Value, + VmErrorKind, }; /// Derive for the [`ToConstValue`](trait@ToConstValue) trait. @@ -234,16 +234,14 @@ impl ConstValue { /// Construct a constant value from a reference to a value.. pub(crate) fn from_value_ref(value: &Value) -> Result { - let inner = match value.borrow_ref_repr()? { - BorrowRefRepr::Inline(value) => ConstValueKind::Inline(*value), - BorrowRefRepr::Mutable(value) => match &*value { - value => { - return Err(RuntimeError::from(VmErrorKind::ConstNotSupported { - actual: value.type_info(), - })) - } - }, - BorrowRefRepr::Any(value) => match value.type_hash() { + let inner = match value.as_ref()? { + ReprRef::Inline(value) => ConstValueKind::Inline(*value), + ReprRef::Mutable(value) => { + return Err(RuntimeError::from(VmErrorKind::ConstNotSupported { + actual: value.borrow_ref()?.type_info(), + })); + } + ReprRef::Any(value) => match value.type_hash() { Option::::HASH => { let option = value.borrow_ref::>()?; diff --git a/crates/rune/src/runtime/format.rs b/crates/rune/src/runtime/format.rs index c35943afa..a71839da2 100644 --- a/crates/rune/src/runtime/format.rs +++ b/crates/rune/src/runtime/format.rs @@ -13,7 +13,7 @@ use crate as rune; use crate::alloc::clone::TryClone; use crate::alloc::fmt::TryWrite; use crate::alloc::String; -use crate::runtime::{Formatter, Inline, ProtocolCaller, RefRepr, Value, VmErrorKind, VmResult}; +use crate::runtime::{Formatter, Inline, ProtocolCaller, ReprRef, Value, VmErrorKind, VmResult}; use crate::{Any, TypeHash}; /// Error raised when trying to parse a type string and it fails. @@ -208,8 +208,8 @@ impl FormatSpec { caller: &mut dyn ProtocolCaller, ) -> VmResult<()> { 'fallback: { - match vm_try!(value.as_ref_repr()) { - RefRepr::Inline(value) => match value { + match vm_try!(value.as_ref()) { + ReprRef::Inline(value) => match value { Inline::Char(c) => { vm_try!(f.buf_mut().try_push(*c)); vm_try!(self.format_fill(f, self.align, self.fill, None)); @@ -228,10 +228,10 @@ impl FormatSpec { break 'fallback; } }, - RefRepr::Mutable(..) => { + ReprRef::Mutable(..) => { break 'fallback; } - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { String::HASH => { let s = vm_try!(value.borrow_ref::()); vm_try!(f.buf_mut().try_push_str(&s)); @@ -256,8 +256,8 @@ impl FormatSpec { caller: &mut dyn ProtocolCaller, ) -> VmResult<()> { 'fallback: { - match vm_try!(value.as_ref_repr()) { - RefRepr::Inline(value) => match value { + match vm_try!(value.as_ref()) { + ReprRef::Inline(value) => match value { Inline::Signed(n) => { let (n, align, fill, sign) = self.int_traits(*n); vm_try!(self.format_number(f.buf_mut(), n)); @@ -272,10 +272,10 @@ impl FormatSpec { break 'fallback; } }, - RefRepr::Mutable(..) => { + ReprRef::Mutable(..) => { break 'fallback; } - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { String::HASH => { let s = vm_try!(value.borrow_ref::()); vm_try!(vm_write!(f, "{s:?}")); diff --git a/crates/rune/src/runtime/mod.rs b/crates/rune/src/runtime/mod.rs index e0fef4304..bf8c8d6a9 100644 --- a/crates/rune/src/runtime/mod.rs +++ b/crates/rune/src/runtime/mod.rs @@ -165,7 +165,7 @@ pub use self::value::{ Accessor, EmptyStruct, Inline, RawValueGuard, Rtti, Struct, TupleStruct, TypeValue, Value, ValueMutGuard, ValueRefGuard, VariantRtti, }; -pub(crate) use self::value::{BorrowRefRepr, MutRepr, Mutable, OwnedRepr, RefRepr}; +pub(crate) use self::value::{Mutable, ReprMut, ReprOwned, ReprRef}; mod variant; pub use self::variant::{Variant, VariantData}; diff --git a/crates/rune/src/runtime/range.rs b/crates/rune/src/runtime/range.rs index e2f6f59e5..5f00573aa 100644 --- a/crates/rune/src/runtime/range.rs +++ b/crates/rune/src/runtime/range.rs @@ -5,7 +5,7 @@ use core::ops; use crate as rune; use crate::alloc::clone::TryClone; use crate::runtime::{ - EnvProtocolCaller, FromValue, Inline, ProtocolCaller, RefRepr, RuntimeError, ToValue, Value, + EnvProtocolCaller, FromValue, Inline, ProtocolCaller, ReprRef, RuntimeError, ToValue, Value, VmErrorKind, VmResult, }; use crate::Any; @@ -94,19 +94,16 @@ impl Range { /// ``` #[rune::function(keep)] pub fn iter(&self) -> VmResult { - let value = match ( - &vm_try!(self.start.as_ref_repr()), - vm_try!(self.end.as_ref_repr()), - ) { - (RefRepr::Inline(Inline::Unsigned(start)), RefRepr::Inline(end)) => { + let value = match (&vm_try!(self.start.as_ref()), vm_try!(self.end.as_ref())) { + (ReprRef::Inline(Inline::Unsigned(start)), ReprRef::Inline(end)) => { let end = vm_try!(end.as_integer::()); vm_try!(rune::to_value(RangeIter::new(*start..end))) } - (RefRepr::Inline(Inline::Signed(start)), RefRepr::Inline(end)) => { + (ReprRef::Inline(Inline::Signed(start)), ReprRef::Inline(end)) => { let end = vm_try!(end.as_integer::()); vm_try!(rune::to_value(RangeIter::new(*start..end))) } - (RefRepr::Inline(Inline::Char(start)), RefRepr::Inline(Inline::Char(end))) => { + (ReprRef::Inline(Inline::Char(start)), ReprRef::Inline(Inline::Char(end))) => { vm_try!(rune::to_value(RangeIter::new(*start..*end))) } (start, end) => { diff --git a/crates/rune/src/runtime/range_from.rs b/crates/rune/src/runtime/range_from.rs index a58b7c13a..8f8b7b516 100644 --- a/crates/rune/src/runtime/range_from.rs +++ b/crates/rune/src/runtime/range_from.rs @@ -5,7 +5,7 @@ use core::ops; use crate as rune; use crate::alloc::clone::TryClone; use crate::runtime::{ - EnvProtocolCaller, FromValue, Inline, ProtocolCaller, RefRepr, RuntimeError, ToValue, Value, + EnvProtocolCaller, FromValue, Inline, ProtocolCaller, ReprRef, RuntimeError, ToValue, Value, VmErrorKind, VmResult, }; use crate::Any; @@ -88,14 +88,14 @@ impl RangeFrom { /// ``` #[rune::function(keep)] pub fn iter(&self) -> VmResult { - let value = match vm_try!(self.start.as_ref_repr()) { - RefRepr::Inline(Inline::Unsigned(start)) => { + let value = match vm_try!(self.start.as_ref()) { + ReprRef::Inline(Inline::Unsigned(start)) => { vm_try!(crate::to_value(RangeFromIter::new(*start..))) } - RefRepr::Inline(Inline::Signed(start)) => { + ReprRef::Inline(Inline::Signed(start)) => { vm_try!(crate::to_value(RangeFromIter::new(*start..))) } - RefRepr::Inline(Inline::Char(start)) => { + ReprRef::Inline(Inline::Char(start)) => { vm_try!(crate::to_value(RangeFromIter::new(*start..))) } start => { diff --git a/crates/rune/src/runtime/range_inclusive.rs b/crates/rune/src/runtime/range_inclusive.rs index 26695a109..ea15e818a 100644 --- a/crates/rune/src/runtime/range_inclusive.rs +++ b/crates/rune/src/runtime/range_inclusive.rs @@ -5,7 +5,7 @@ use core::ops; use crate as rune; use crate::alloc::clone::TryClone; use crate::runtime::{ - EnvProtocolCaller, FromValue, Inline, ProtocolCaller, RefRepr, RuntimeError, ToValue, Value, + EnvProtocolCaller, FromValue, Inline, ProtocolCaller, ReprRef, RuntimeError, ToValue, Value, VmErrorKind, VmResult, }; use crate::Any; @@ -95,19 +95,16 @@ impl RangeInclusive { /// ``` #[rune::function(keep)] pub fn iter(&self) -> VmResult { - let value = match ( - vm_try!(self.start.as_ref_repr()), - vm_try!(self.end.as_ref_repr()), - ) { - (RefRepr::Inline(Inline::Unsigned(start)), RefRepr::Inline(end)) => { + let value = match (vm_try!(self.start.as_ref()), vm_try!(self.end.as_ref())) { + (ReprRef::Inline(Inline::Unsigned(start)), ReprRef::Inline(end)) => { let end = vm_try!(end.as_integer::()); vm_try!(rune::to_value(RangeInclusiveIter::new(*start..=end))) } - (RefRepr::Inline(Inline::Signed(start)), RefRepr::Inline(end)) => { + (ReprRef::Inline(Inline::Signed(start)), ReprRef::Inline(end)) => { let end = vm_try!(end.as_integer::()); vm_try!(rune::to_value(RangeInclusiveIter::new(*start..=end))) } - (RefRepr::Inline(Inline::Char(start)), RefRepr::Inline(Inline::Char(end))) => { + (ReprRef::Inline(Inline::Char(start)), ReprRef::Inline(Inline::Char(end))) => { vm_try!(rune::to_value(RangeInclusiveIter::new(*start..=*end))) } (start, end) => { diff --git a/crates/rune/src/runtime/value.rs b/crates/rune/src/runtime/value.rs index 3cc3ae3bc..cb97d1d8f 100644 --- a/crates/rune/src/runtime/value.rs +++ b/crates/rune/src/runtime/value.rs @@ -77,64 +77,47 @@ enum Repr { Any(AnyObj), } -pub(crate) enum OwnedRepr { +pub(crate) enum ReprOwned { Inline(Inline), Mutable(Mutable), Any(AnyObj), } -impl OwnedRepr { +impl ReprOwned { #[inline] pub(crate) fn type_info(&self) -> TypeInfo { match self { - OwnedRepr::Inline(value) => value.type_info(), - OwnedRepr::Mutable(value) => value.type_info(), - OwnedRepr::Any(value) => value.type_info(), + ReprOwned::Inline(value) => value.type_info(), + ReprOwned::Mutable(value) => value.type_info(), + ReprOwned::Any(value) => value.type_info(), } } } -pub(crate) enum RefRepr<'a> { +pub(crate) enum ReprRef<'a> { Inline(&'a Inline), Mutable(&'a Shared), Any(&'a AnyObj), } -impl RefRepr<'_> { +impl ReprRef<'_> { #[inline] pub(crate) fn type_info(&self) -> Result { match self { - RefRepr::Inline(value) => Ok(value.type_info()), - RefRepr::Mutable(value) => Ok(value.borrow_ref()?.type_info()), - RefRepr::Any(value) => Ok(value.type_info()), + ReprRef::Inline(value) => Ok(value.type_info()), + ReprRef::Mutable(value) => Ok(value.borrow_ref()?.type_info()), + ReprRef::Any(value) => Ok(value.type_info()), } } } /// Access the internals of a value mutably. -pub(crate) enum MutRepr<'a> { +pub(crate) enum ReprMut<'a> { Inline(&'a mut Inline), Mutable(#[allow(unused)] &'a mut Shared), Any(#[allow(unused)] &'a mut AnyObj), } -pub(crate) enum BorrowRefRepr<'a> { - Inline(&'a Inline), - Mutable(BorrowRef<'a, Mutable>), - Any(&'a AnyObj), -} - -impl<'a> BorrowRefRepr<'a> { - #[inline] - pub(crate) fn type_info(&self) -> TypeInfo { - match self { - BorrowRefRepr::Inline(value) => value.type_info(), - BorrowRefRepr::Mutable(value) => value.type_info(), - BorrowRefRepr::Any(value) => value.type_info(), - } - } -} - /// An entry on the stack. pub struct Value { repr: Repr, @@ -354,8 +337,8 @@ impl Value { caller: &mut dyn ProtocolCaller, ) -> VmResult<()> { 'fallback: { - match vm_try!(self.borrow_ref_repr()) { - BorrowRefRepr::Inline(value) => match value { + match vm_try!(self.as_ref()) { + ReprRef::Inline(value) => match value { Inline::Char(c) => { vm_try!(f.try_write_char(*c)); } @@ -414,19 +397,19 @@ impl Value { pub(crate) fn clone_with(&self, caller: &mut dyn ProtocolCaller) -> VmResult { 'fallback: { - let value = match vm_try!(self.as_ref_repr()) { - RefRepr::Inline(value) => { + let value = match vm_try!(self.as_ref()) { + ReprRef::Inline(value) => { return VmResult::Ok(Self { repr: Repr::Inline(*value), }); } - RefRepr::Mutable(value) => match &*vm_try!(value.borrow_ref()) { + ReprRef::Mutable(value) => match &*vm_try!(value.borrow_ref()) { Mutable::EmptyStruct(value) => Mutable::EmptyStruct(vm_try!(value.try_clone())), Mutable::TupleStruct(value) => Mutable::TupleStruct(vm_try!(value.try_clone())), Mutable::Struct(value) => Mutable::Struct(vm_try!(value.try_clone())), Mutable::Variant(value) => Mutable::Variant(vm_try!(value.try_clone())), }, - RefRepr::Any(..) => { + ReprRef::Any(..) => { break 'fallback; } }; @@ -673,7 +656,7 @@ impl Value { #[inline] pub fn into_string(self) -> Result { match self.take_repr()? { - OwnedRepr::Any(value) => Ok(value.downcast()?), + ReprOwned::Any(value) => Ok(value.downcast()?), actual => Err(RuntimeError::expected::(actual.type_info())), } } @@ -683,17 +666,17 @@ impl Value { #[inline] pub fn into_type_value(self) -> Result { match self.take_repr()? { - OwnedRepr::Inline(value) => match value { + ReprOwned::Inline(value) => match value { Inline::Unit => Ok(TypeValue::Unit), value => Ok(TypeValue::NotTypedInline(NotTypedInlineValue(value))), }, - OwnedRepr::Mutable(value) => match value { + ReprOwned::Mutable(value) => match value { Mutable::EmptyStruct(empty) => Ok(TypeValue::EmptyStruct(empty)), Mutable::TupleStruct(tuple) => Ok(TypeValue::TupleStruct(tuple)), Mutable::Struct(object) => Ok(TypeValue::Struct(object)), Mutable::Variant(object) => Ok(TypeValue::Variant(object)), }, - OwnedRepr::Any(value) => match value.type_hash() { + ReprOwned::Any(value) => match value.type_hash() { OwnedTuple::HASH => Ok(TypeValue::Tuple(value.downcast()?)), Object::HASH => Ok(TypeValue::Object(value.downcast()?)), _ => Ok(TypeValue::NotTypedAnyObj(NotTypedAnyObj(value))), @@ -704,9 +687,9 @@ impl Value { /// Coerce into a unit. #[inline] pub fn into_unit(&self) -> Result<(), RuntimeError> { - match self.borrow_ref_repr()? { - BorrowRefRepr::Inline(Inline::Unit) => Ok(()), - value => Err(RuntimeError::expected::<()>(value.type_info())), + match self.as_ref()? { + ReprRef::Inline(Inline::Unit) => Ok(()), + value => Err(RuntimeError::expected::<()>(value.type_info()?)), } } @@ -775,13 +758,13 @@ impl Value { /// and does not consume it. #[inline] pub fn borrow_tuple_ref(&self) -> Result, RuntimeError> { - match self.as_ref_repr()? { - RefRepr::Inline(Inline::Unit) => Ok(BorrowRef::from_static(Tuple::new(&[]))), - RefRepr::Inline(value) => Err(RuntimeError::expected::(value.type_info())), - RefRepr::Mutable(value) => Err(RuntimeError::expected::( + match self.as_ref()? { + ReprRef::Inline(Inline::Unit) => Ok(BorrowRef::from_static(Tuple::new(&[]))), + ReprRef::Inline(value) => Err(RuntimeError::expected::(value.type_info())), + ReprRef::Mutable(value) => Err(RuntimeError::expected::( value.borrow_ref()?.type_info(), )), - RefRepr::Any(value) => { + ReprRef::Any(value) => { let value = value.borrow_ref::()?; let value = BorrowRef::map(value, OwnedTuple::as_ref); Ok(value) @@ -795,13 +778,13 @@ impl Value { /// does not consume it. #[inline] pub fn borrow_tuple_mut(&self) -> Result, RuntimeError> { - match self.as_ref_repr()? { - RefRepr::Inline(Inline::Unit) => Ok(BorrowMut::from_static(Tuple::new_mut(&mut []))), - RefRepr::Inline(value) => Err(RuntimeError::expected::(value.type_info())), - RefRepr::Mutable(value) => Err(RuntimeError::expected::( + match self.as_ref()? { + ReprRef::Inline(Inline::Unit) => Ok(BorrowMut::from_static(Tuple::new_mut(&mut []))), + ReprRef::Inline(value) => Err(RuntimeError::expected::(value.type_info())), + ReprRef::Mutable(value) => Err(RuntimeError::expected::( value.borrow_ref()?.type_info(), )), - RefRepr::Any(value) => { + ReprRef::Any(value) => { let value = value.borrow_mut::()?; let value = BorrowMut::map(value, OwnedTuple::as_mut); Ok(value) @@ -815,13 +798,13 @@ impl Value { /// does not consume it. #[inline] pub fn into_tuple(&self) -> Result, RuntimeError> { - match self.as_ref_repr()? { - RefRepr::Inline(Inline::Unit) => Ok(Tuple::from_boxed(Box::default())), - RefRepr::Inline(value) => Err(RuntimeError::expected::(value.type_info())), - RefRepr::Mutable(value) => Err(RuntimeError::expected::( + match self.as_ref()? { + ReprRef::Inline(Inline::Unit) => Ok(Tuple::from_boxed(Box::default())), + ReprRef::Inline(value) => Err(RuntimeError::expected::(value.type_info())), + ReprRef::Mutable(value) => Err(RuntimeError::expected::( value.borrow_ref()?.type_info(), )), - RefRepr::Any(value) => Ok(value.clone().downcast::()?.into_boxed_tuple()), + ReprRef::Any(value) => Ok(value.clone().downcast::()?.into_boxed_tuple()), } } @@ -831,13 +814,13 @@ impl Value { /// does not consume it. #[inline] pub fn into_tuple_ref(&self) -> Result, RuntimeError> { - match self.as_ref_repr()? { - RefRepr::Inline(Inline::Unit) => Ok(Ref::from_static(Tuple::new(&[]))), - RefRepr::Inline(value) => Err(RuntimeError::expected::(value.type_info())), - RefRepr::Mutable(value) => Err(RuntimeError::expected::( + match self.as_ref()? { + ReprRef::Inline(Inline::Unit) => Ok(Ref::from_static(Tuple::new(&[]))), + ReprRef::Inline(value) => Err(RuntimeError::expected::(value.type_info())), + ReprRef::Mutable(value) => Err(RuntimeError::expected::( value.borrow_ref()?.type_info(), )), - RefRepr::Any(value) => { + ReprRef::Any(value) => { let value = value.clone().into_ref::()?; let value = Ref::map(value, OwnedTuple::as_ref); Ok(value) @@ -851,13 +834,13 @@ impl Value { /// does not consume it. #[inline] pub fn into_tuple_mut(&self) -> Result, RuntimeError> { - match self.as_ref_repr()? { - RefRepr::Inline(Inline::Unit) => Ok(Mut::from_static(Tuple::new_mut(&mut []))), - RefRepr::Inline(value) => Err(RuntimeError::expected::(value.type_info())), - RefRepr::Mutable(value) => Err(RuntimeError::expected::( + match self.as_ref()? { + ReprRef::Inline(Inline::Unit) => Ok(Mut::from_static(Tuple::new_mut(&mut []))), + ReprRef::Inline(value) => Err(RuntimeError::expected::(value.type_info())), + ReprRef::Mutable(value) => Err(RuntimeError::expected::( value.borrow_ref()?.type_info(), )), - RefRepr::Any(value) => { + ReprRef::Any(value) => { let value = value.clone().into_mut::()?; let value = Mut::map(value, OwnedTuple::as_mut); Ok(value) @@ -1091,19 +1074,20 @@ impl Value { b: &Value, caller: &mut dyn ProtocolCaller, ) -> VmResult { - match (vm_try!(self.as_ref_repr()), vm_try!(b.borrow_ref_repr())) { - (RefRepr::Inline(a), BorrowRefRepr::Inline(b)) => { + match (vm_try!(self.as_ref()), vm_try!(b.as_ref())) { + (ReprRef::Inline(a), ReprRef::Inline(b)) => { return VmResult::Ok(vm_try!(a.partial_eq(b))); } - (RefRepr::Inline(a), b) => { + (ReprRef::Inline(a), b) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: Protocol::PARTIAL_EQ.name, lhs: a.type_info(), - rhs: b.type_info(), + rhs: vm_try!(b.type_info()), }); } - (RefRepr::Mutable(a), BorrowRefRepr::Mutable(b)) => { + (ReprRef::Mutable(a), ReprRef::Mutable(b)) => { let a = vm_try!(a.borrow_ref()); + let b = vm_try!(b.borrow_ref()); match (&*a, &*b) { (Mutable::EmptyStruct(a), Mutable::EmptyStruct(b)) => { @@ -1134,7 +1118,7 @@ impl Value { _ => {} } } - (RefRepr::Any(value), _) => match value.type_hash() { + (ReprRef::Any(value), _) => match value.type_hash() { runtime::Vec::HASH => { let vec = vm_try!(value.borrow_ref::()); return Vec::partial_eq_with(&vec, b.clone(), caller); @@ -1176,8 +1160,8 @@ impl Value { hasher: &mut Hasher, caller: &mut dyn ProtocolCaller, ) -> VmResult<()> { - match vm_try!(self.as_ref_repr()) { - RefRepr::Inline(value) => match value { + match vm_try!(self.as_ref()) { + ReprRef::Inline(value) => match value { Inline::Unsigned(value) => { hasher.write_u64(*value); return VmResult::Ok(()); @@ -1205,7 +1189,7 @@ impl Value { }); } }, - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { Vec::HASH => { let vec = vm_try!(value.borrow_ref::()); return Vec::hash_with(&vec, hasher, caller); @@ -1252,48 +1236,50 @@ impl Value { /// This is the basis for the eq operation (`==`). #[cfg_attr(feature = "bench", inline(never))] pub(crate) fn eq_with(&self, b: &Value, caller: &mut dyn ProtocolCaller) -> VmResult { - match ( - vm_try!(self.borrow_ref_repr()), - vm_try!(b.borrow_ref_repr()), - ) { - (BorrowRefRepr::Inline(a), BorrowRefRepr::Inline(b)) => { + match (vm_try!(self.as_ref()), vm_try!(b.as_ref())) { + (ReprRef::Inline(a), ReprRef::Inline(b)) => { return a.eq(b); } - (BorrowRefRepr::Inline(lhs), rhs) => { + (ReprRef::Inline(lhs), rhs) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: Protocol::EQ.name, lhs: lhs.type_info(), - rhs: rhs.type_info(), + rhs: vm_try!(rhs.type_info()), }); } - (BorrowRefRepr::Mutable(a), BorrowRefRepr::Mutable(b)) => match (&*a, &*b) { - (Mutable::EmptyStruct(a), Mutable::EmptyStruct(b)) => { - if a.rtti.hash == b.rtti.hash { - // NB: don't get any future ideas, this must fall through to - // the VmError below since it's otherwise a comparison - // between two incompatible types. - // - // Other than that, all units are equal. - return VmResult::Ok(true); + (ReprRef::Mutable(a), ReprRef::Mutable(b)) => { + let a = vm_try!(a.borrow_ref()); + let b = vm_try!(b.borrow_ref()); + + match (&*a, &*b) { + (Mutable::EmptyStruct(a), Mutable::EmptyStruct(b)) => { + if a.rtti.hash == b.rtti.hash { + // NB: don't get any future ideas, this must fall through to + // the VmError below since it's otherwise a comparison + // between two incompatible types. + // + // Other than that, all units are equal. + return VmResult::Ok(true); + } } - } - (Mutable::TupleStruct(a), Mutable::TupleStruct(b)) => { - if a.rtti.hash == b.rtti.hash { - return Vec::eq_with(&a.data, &b.data, Value::eq_with, caller); + (Mutable::TupleStruct(a), Mutable::TupleStruct(b)) => { + if a.rtti.hash == b.rtti.hash { + return Vec::eq_with(&a.data, &b.data, Value::eq_with, caller); + } } - } - (Mutable::Struct(a), Mutable::Struct(b)) => { - if a.rtti.hash == b.rtti.hash { - return Vec::eq_with(&a.data, &b.data, Value::eq_with, caller); + (Mutable::Struct(a), Mutable::Struct(b)) => { + if a.rtti.hash == b.rtti.hash { + return Vec::eq_with(&a.data, &b.data, Value::eq_with, caller); + } } - } - (Mutable::Variant(a), Mutable::Variant(b)) => { - if a.rtti().enum_hash == b.rtti().enum_hash { - return Variant::eq_with(a, b, caller); + (Mutable::Variant(a), Mutable::Variant(b)) => { + if a.rtti().enum_hash == b.rtti().enum_hash { + return Variant::eq_with(a, b, caller); + } } + _ => {} } - _ => {} - }, + } _ => {} } @@ -1335,48 +1321,50 @@ impl Value { b: &Value, caller: &mut dyn ProtocolCaller, ) -> VmResult> { - match ( - vm_try!(self.borrow_ref_repr()), - vm_try!(b.borrow_ref_repr()), - ) { - (BorrowRefRepr::Inline(a), BorrowRefRepr::Inline(b)) => { + match (vm_try!(self.as_ref()), vm_try!(b.as_ref())) { + (ReprRef::Inline(a), ReprRef::Inline(b)) => { return VmResult::Ok(vm_try!(a.partial_cmp(b))) } - (BorrowRefRepr::Inline(lhs), rhs) => { + (ReprRef::Inline(lhs), rhs) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: Protocol::PARTIAL_CMP.name, lhs: lhs.type_info(), - rhs: rhs.type_info(), + rhs: vm_try!(rhs.type_info()), }) } - (BorrowRefRepr::Mutable(a), BorrowRefRepr::Mutable(b)) => match (&*a, &*b) { - (Mutable::EmptyStruct(a), Mutable::EmptyStruct(b)) => { - if a.rtti.hash == b.rtti.hash { - // NB: don't get any future ideas, this must fall through to - // the VmError below since it's otherwise a comparison - // between two incompatible types. - // - // Other than that, all units are equal. - return VmResult::Ok(Some(Ordering::Equal)); + (ReprRef::Mutable(a), ReprRef::Mutable(b)) => { + let a = vm_try!(a.borrow_ref()); + let b = vm_try!(b.borrow_ref()); + + match (&*a, &*b) { + (Mutable::EmptyStruct(a), Mutable::EmptyStruct(b)) => { + if a.rtti.hash == b.rtti.hash { + // NB: don't get any future ideas, this must fall through to + // the VmError below since it's otherwise a comparison + // between two incompatible types. + // + // Other than that, all units are equal. + return VmResult::Ok(Some(Ordering::Equal)); + } } - } - (Mutable::TupleStruct(a), Mutable::TupleStruct(b)) => { - if a.rtti.hash == b.rtti.hash { - return Vec::partial_cmp_with(&a.data, &b.data, caller); + (Mutable::TupleStruct(a), Mutable::TupleStruct(b)) => { + if a.rtti.hash == b.rtti.hash { + return Vec::partial_cmp_with(&a.data, &b.data, caller); + } } - } - (Mutable::Struct(a), Mutable::Struct(b)) => { - if a.rtti.hash == b.rtti.hash { - return Vec::partial_cmp_with(&a.data, &b.data, caller); + (Mutable::Struct(a), Mutable::Struct(b)) => { + if a.rtti.hash == b.rtti.hash { + return Vec::partial_cmp_with(&a.data, &b.data, caller); + } } - } - (Mutable::Variant(a), Mutable::Variant(b)) => { - if a.rtti().enum_hash == b.rtti().enum_hash { - return Variant::partial_cmp_with(a, b, caller); + (Mutable::Variant(a), Mutable::Variant(b)) => { + if a.rtti().enum_hash == b.rtti().enum_hash { + return Variant::partial_cmp_with(a, b, caller); + } } + _ => {} } - _ => {} - }, + } _ => {} } @@ -1418,44 +1406,46 @@ impl Value { b: &Value, caller: &mut dyn ProtocolCaller, ) -> VmResult { - match ( - vm_try!(self.borrow_ref_repr()), - vm_try!(b.borrow_ref_repr()), - ) { - (BorrowRefRepr::Inline(a), BorrowRefRepr::Inline(b)) => return a.cmp(b), - (BorrowRefRepr::Mutable(a), BorrowRefRepr::Mutable(b)) => match (&*a, &*b) { - (Mutable::EmptyStruct(a), Mutable::EmptyStruct(b)) => { - if a.rtti.hash == b.rtti.hash { - // NB: don't get any future ideas, this must fall through to - // the VmError below since it's otherwise a comparison - // between two incompatible types. - // - // Other than that, all units are equal. - return VmResult::Ok(Ordering::Equal); + match (vm_try!(self.as_ref()), vm_try!(b.as_ref())) { + (ReprRef::Inline(a), ReprRef::Inline(b)) => return a.cmp(b), + (ReprRef::Mutable(a), ReprRef::Mutable(b)) => { + let a = vm_try!(a.borrow_ref()); + let b = vm_try!(b.borrow_ref()); + + match (&*a, &*b) { + (Mutable::EmptyStruct(a), Mutable::EmptyStruct(b)) => { + if a.rtti.hash == b.rtti.hash { + // NB: don't get any future ideas, this must fall through to + // the VmError below since it's otherwise a comparison + // between two incompatible types. + // + // Other than that, all units are equal. + return VmResult::Ok(Ordering::Equal); + } } - } - (Mutable::TupleStruct(a), Mutable::TupleStruct(b)) => { - if a.rtti.hash == b.rtti.hash { - return Vec::cmp_with(&a.data, &b.data, caller); + (Mutable::TupleStruct(a), Mutable::TupleStruct(b)) => { + if a.rtti.hash == b.rtti.hash { + return Vec::cmp_with(&a.data, &b.data, caller); + } } - } - (Mutable::Struct(a), Mutable::Struct(b)) => { - if a.rtti.hash == b.rtti.hash { - return Vec::cmp_with(&a.data, &b.data, caller); + (Mutable::Struct(a), Mutable::Struct(b)) => { + if a.rtti.hash == b.rtti.hash { + return Vec::cmp_with(&a.data, &b.data, caller); + } } - } - (Mutable::Variant(a), Mutable::Variant(b)) => { - if a.rtti().enum_hash == b.rtti().enum_hash { - return Variant::cmp_with(a, b, caller); + (Mutable::Variant(a), Mutable::Variant(b)) => { + if a.rtti().enum_hash == b.rtti().enum_hash { + return Variant::cmp_with(a, b, caller); + } } + _ => {} } - _ => {} - }, - (BorrowRefRepr::Inline(lhs), rhs) => { + } + (ReprRef::Inline(lhs), rhs) => { return VmResult::err(VmErrorKind::UnsupportedBinaryOperation { op: Protocol::CMP.name, lhs: lhs.type_info(), - rhs: rhs.type_info(), + rhs: vm_try!(rhs.type_info()), }); } _ => {} @@ -1549,39 +1539,30 @@ impl Value { } } - pub(crate) fn take_repr(self) -> Result { + pub(crate) fn take_repr(self) -> Result { match self.repr { Repr::Empty => Err(AccessError::empty()), - Repr::Inline(value) => Ok(OwnedRepr::Inline(value)), - Repr::Mutable(value) => Ok(OwnedRepr::Mutable(value.take()?)), - Repr::Any(value) => Ok(OwnedRepr::Any(value)), - } - } - - pub(crate) fn borrow_ref_repr(&self) -> Result, AccessError> { - match &self.repr { - Repr::Empty => Err(AccessError::empty()), - Repr::Inline(value) => Ok(BorrowRefRepr::Inline(value)), - Repr::Mutable(value) => Ok(BorrowRefRepr::Mutable(value.borrow_ref()?)), - Repr::Any(value) => Ok(BorrowRefRepr::Any(value)), + Repr::Inline(value) => Ok(ReprOwned::Inline(value)), + Repr::Mutable(value) => Ok(ReprOwned::Mutable(value.take()?)), + Repr::Any(value) => Ok(ReprOwned::Any(value)), } } - pub(crate) fn as_ref_repr(&self) -> Result, AccessError> { + pub(crate) fn as_ref(&self) -> Result, AccessError> { match &self.repr { - Repr::Inline(value) => Ok(RefRepr::Inline(value)), - Repr::Mutable(value) => Ok(RefRepr::Mutable(value)), - Repr::Any(value) => Ok(RefRepr::Any(value)), + Repr::Inline(value) => Ok(ReprRef::Inline(value)), + Repr::Mutable(value) => Ok(ReprRef::Mutable(value)), + Repr::Any(value) => Ok(ReprRef::Any(value)), Repr::Empty => Err(AccessError::empty()), } } - pub(crate) fn as_mut_repr(&mut self) -> Result, AccessError> { + pub(crate) fn as_mut(&mut self) -> Result, AccessError> { match &mut self.repr { Repr::Empty => Err(AccessError::empty()), - Repr::Inline(value) => Ok(MutRepr::Inline(value)), - Repr::Mutable(value) => Ok(MutRepr::Mutable(value)), - Repr::Any(value) => Ok(MutRepr::Any(value)), + Repr::Inline(value) => Ok(ReprMut::Inline(value)), + Repr::Mutable(value) => Ok(ReprMut::Mutable(value)), + Repr::Any(value) => Ok(ReprMut::Any(value)), } } diff --git a/crates/rune/src/runtime/value/data.rs b/crates/rune/src/runtime/value/data.rs index 8eefcce1f..474e3d753 100644 --- a/crates/rune/src/runtime/value/data.rs +++ b/crates/rune/src/runtime/value/data.rs @@ -9,7 +9,7 @@ use crate as rune; use crate::alloc::prelude::*; use crate::runtime::{OwnedTuple, TypeInfo}; -use super::{FromValue, Mutable, OwnedRepr, Rtti, RuntimeError, Value}; +use super::{FromValue, Mutable, ReprOwned, Rtti, RuntimeError, Value}; /// A empty with a well-defined type. #[derive(TryClone)] @@ -34,10 +34,10 @@ impl EmptyStruct { impl FromValue for EmptyStruct { fn from_value(value: Value) -> Result { match value.take_repr()? { - OwnedRepr::Inline(value) => Err(RuntimeError::expected_unit_struct(value.type_info())), - OwnedRepr::Mutable(Mutable::EmptyStruct(value)) => Ok(value), - OwnedRepr::Mutable(value) => Err(RuntimeError::expected_unit_struct(value.type_info())), - OwnedRepr::Any(value) => Err(RuntimeError::expected_unit_struct(value.type_info())), + ReprOwned::Inline(value) => Err(RuntimeError::expected_unit_struct(value.type_info())), + ReprOwned::Mutable(Mutable::EmptyStruct(value)) => Ok(value), + ReprOwned::Mutable(value) => Err(RuntimeError::expected_unit_struct(value.type_info())), + ReprOwned::Any(value) => Err(RuntimeError::expected_unit_struct(value.type_info())), } } } @@ -92,12 +92,12 @@ impl TupleStruct { impl FromValue for TupleStruct { fn from_value(value: Value) -> Result { match value.take_repr()? { - OwnedRepr::Inline(value) => Err(RuntimeError::expected_tuple_struct(value.type_info())), - OwnedRepr::Mutable(Mutable::TupleStruct(value)) => Ok(value), - OwnedRepr::Mutable(value) => { + ReprOwned::Inline(value) => Err(RuntimeError::expected_tuple_struct(value.type_info())), + ReprOwned::Mutable(Mutable::TupleStruct(value)) => Ok(value), + ReprOwned::Mutable(value) => { Err(RuntimeError::expected_tuple_struct(value.type_info())) } - OwnedRepr::Any(value) => Err(RuntimeError::expected_tuple_struct(value.type_info())), + ReprOwned::Any(value) => Err(RuntimeError::expected_tuple_struct(value.type_info())), } } } @@ -160,10 +160,10 @@ impl Struct { impl FromValue for Struct { fn from_value(value: Value) -> Result { match value.take_repr()? { - OwnedRepr::Inline(value) => Err(RuntimeError::expected_struct(value.type_info())), - OwnedRepr::Mutable(Mutable::Struct(value)) => Ok(value), - OwnedRepr::Mutable(value) => Err(RuntimeError::expected_struct(value.type_info())), - OwnedRepr::Any(value) => Err(RuntimeError::expected_struct(value.type_info())), + ReprOwned::Inline(value) => Err(RuntimeError::expected_struct(value.type_info())), + ReprOwned::Mutable(Mutable::Struct(value)) => Ok(value), + ReprOwned::Mutable(value) => Err(RuntimeError::expected_struct(value.type_info())), + ReprOwned::Any(value) => Err(RuntimeError::expected_struct(value.type_info())), } } } diff --git a/crates/rune/src/runtime/value/macros.rs b/crates/rune/src/runtime/value/macros.rs index 8c2f88973..00c848c69 100644 --- a/crates/rune/src/runtime/value/macros.rs +++ b/crates/rune/src/runtime/value/macros.rs @@ -80,11 +80,11 @@ macro_rules! into_base { /// and does not consume it. #[inline] pub fn $borrow_ref(&self) -> Result, RuntimeError> { - match self.as_ref_repr()? { - RefRepr::Inline(value) => { + match self.as_ref()? { + ReprRef::Inline(value) => { Err(RuntimeError::expected::<$ty>(value.type_info())) }, - RefRepr::Mutable(value) => { + ReprRef::Mutable(value) => { let result = BorrowRef::try_map(value.borrow_ref()?, |kind| match kind { Mutable::$kind(value) => Some(value), _ => None, @@ -95,7 +95,7 @@ macro_rules! into_base { Err(value) => Err(RuntimeError::expected::<$ty>(value.type_info())), } }, - RefRepr::Any(value) => { + ReprRef::Any(value) => { Err(RuntimeError::expected::<$ty>(value.type_info())) }, } @@ -107,11 +107,11 @@ macro_rules! into_base { /// and does not consume it. #[inline] pub fn $borrow_mut(&self) -> Result, RuntimeError> { - match self.as_ref_repr()? { - RefRepr::Inline(value) => { + match self.as_ref()? { + ReprRef::Inline(value) => { Err(RuntimeError::expected::<$ty>(value.type_info())) } - RefRepr::Mutable(value) => { + ReprRef::Mutable(value) => { let result = BorrowMut::try_map(value.borrow_mut()?, |kind| match kind { Mutable::$kind(value) => Some(value), _ => None, @@ -122,7 +122,7 @@ macro_rules! into_base { Err(value) => Err(RuntimeError::expected::<$ty>(value.type_info())), } }, - RefRepr::Any(value) => { + ReprRef::Any(value) => { Err(RuntimeError::expected::<$ty>(value.type_info())) } } @@ -155,7 +155,7 @@ macro_rules! into { #[inline] pub fn $into(self) -> Result<$ty, RuntimeError> { match self.take_repr()? { - OwnedRepr::Mutable(Mutable::$kind(value)) => Ok(value), + ReprOwned::Mutable(Mutable::$kind(value)) => Ok(value), value => Err(RuntimeError::expected::<$ty>(value.type_info())), } } diff --git a/crates/rune/src/runtime/value/serde.rs b/crates/rune/src/runtime/value/serde.rs index 1888b8c61..6a00f4968 100644 --- a/crates/rune/src/runtime/value/serde.rs +++ b/crates/rune/src/runtime/value/serde.rs @@ -2,7 +2,7 @@ use core::fmt; use crate::alloc; use crate::alloc::prelude::*; -use crate::runtime::{self, BorrowRefRepr, Bytes, Inline, Mutable, Object, OwnedTuple, Vec}; +use crate::runtime::{self, Bytes, Inline, Mutable, Object, OwnedTuple, ReprRef, Vec}; use crate::TypeHash; use serde::de::{self, Deserialize as _, Error as _}; @@ -26,8 +26,8 @@ impl ser::Serialize for Value { where S: ser::Serializer, { - match self.borrow_ref_repr().map_err(S::Error::custom)? { - BorrowRefRepr::Inline(value) => match *value { + match self.as_ref().map_err(S::Error::custom)? { + ReprRef::Inline(value) => match *value { Inline::Unit => serializer.serialize_unit(), Inline::Bool(value) => serializer.serialize_bool(value), Inline::Char(value) => serializer.serialize_char(value), @@ -37,7 +37,7 @@ impl ser::Serialize for Value { Inline::Type(..) => Err(ser::Error::custom("cannot serialize types")), Inline::Ordering(..) => Err(ser::Error::custom("cannot serialize orderings")), }, - BorrowRefRepr::Mutable(value) => match &*value { + ReprRef::Mutable(value) => match &*value.borrow_ref().map_err(S::Error::custom)? { Mutable::EmptyStruct(..) => { Err(ser::Error::custom("cannot serialize empty structs")) } @@ -47,7 +47,7 @@ impl ser::Serialize for Value { Mutable::Struct(..) => Err(ser::Error::custom("cannot serialize objects structs")), Mutable::Variant(..) => Err(ser::Error::custom("cannot serialize variants")), }, - BorrowRefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { Option::::HASH => { let option = value .borrow_ref::>() diff --git a/crates/rune/src/runtime/variant.rs b/crates/rune/src/runtime/variant.rs index 3b0c96b1b..e079d577a 100644 --- a/crates/rune/src/runtime/variant.rs +++ b/crates/rune/src/runtime/variant.rs @@ -8,7 +8,7 @@ use crate::alloc::clone::TryClone; use crate::alloc::Box; use super::{ - Accessor, FromValue, Mutable, OwnedRepr, OwnedTuple, ProtocolCaller, RuntimeError, Tuple, + Accessor, FromValue, Mutable, OwnedTuple, ProtocolCaller, ReprOwned, RuntimeError, Tuple, TypeInfo, Value, VariantRtti, Vec, VmResult, }; @@ -179,10 +179,10 @@ impl Variant { impl FromValue for Variant { fn from_value(value: Value) -> Result { match value.take_repr()? { - OwnedRepr::Inline(value) => Err(RuntimeError::expected_variant(value.type_info())), - OwnedRepr::Mutable(Mutable::Variant(value)) => Ok(value), - OwnedRepr::Mutable(value) => Err(RuntimeError::expected_variant(value.type_info())), - OwnedRepr::Any(value) => Err(RuntimeError::expected_variant(value.type_info())), + ReprOwned::Inline(value) => Err(RuntimeError::expected_variant(value.type_info())), + ReprOwned::Mutable(Mutable::Variant(value)) => Ok(value), + ReprOwned::Mutable(value) => Err(RuntimeError::expected_variant(value.type_info())), + ReprOwned::Any(value) => Err(RuntimeError::expected_variant(value.type_info())), } } } diff --git a/crates/rune/src/runtime/vm.rs b/crates/rune/src/runtime/vm.rs index 3f285b8f8..f430c3c9f 100644 --- a/crates/rune/src/runtime/vm.rs +++ b/crates/rune/src/runtime/vm.rs @@ -15,14 +15,14 @@ use crate::modules::{option, result}; use crate::runtime; use super::{ - budget, Args, Awaited, BorrowMut, BorrowRefRepr, Bytes, Call, ControlFlow, DynArgs, - DynGuardedArgs, EmptyStruct, Format, FormatSpec, Formatter, FromValue, Function, Future, - Generator, GeneratorState, GuardedArgs, Inline, Inst, InstAddress, InstAssignOp, InstOp, - InstRange, InstTarget, InstValue, InstVariant, MutRepr, Mutable, Object, Output, OwnedTuple, - Pair, Panic, Protocol, ProtocolCaller, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, - RangeToInclusive, RefRepr, RuntimeContext, Select, SelectFuture, Stack, Stream, Struct, Type, - TypeCheck, TypeHash, TypeInfo, TypeOf, Unit, UnitFn, UnitStorage, Value, Variant, VariantData, - Vec, VmDiagnostics, VmDiagnosticsObj, VmError, VmErrorKind, VmExecution, VmHalt, VmIntegerRepr, + budget, Args, Awaited, BorrowMut, Bytes, Call, ControlFlow, DynArgs, DynGuardedArgs, + EmptyStruct, Format, FormatSpec, Formatter, FromValue, Function, Future, Generator, + GeneratorState, GuardedArgs, Inline, Inst, InstAddress, InstAssignOp, InstOp, InstRange, + InstTarget, InstValue, InstVariant, Mutable, Object, Output, OwnedTuple, Pair, Panic, Protocol, + ProtocolCaller, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, + ReprMut, ReprRef, RuntimeContext, Select, SelectFuture, Stack, Stream, Struct, Type, TypeCheck, + TypeHash, TypeInfo, TypeOf, Unit, UnitFn, UnitStorage, Value, Variant, VariantData, Vec, + VmDiagnostics, VmDiagnosticsObj, VmError, VmErrorKind, VmExecution, VmHalt, VmIntegerRepr, VmResult, VmSendExecution, }; @@ -823,9 +823,9 @@ impl Vm { /// Implementation of getting a string index on an object-like type. fn try_object_like_index_get(target: &Value, field: &str) -> VmResult> { - match vm_try!(target.as_ref_repr()) { - RefRepr::Inline(..) => VmResult::Ok(None), - RefRepr::Mutable(target) => { + match vm_try!(target.as_ref()) { + ReprRef::Inline(..) => VmResult::Ok(None), + ReprRef::Mutable(target) => { let target = vm_try!(target.borrow_ref()); let value = match &*target { @@ -846,7 +846,7 @@ impl Vm { VmResult::Ok(Some(value.clone())) } - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { Object::HASH => { let target = vm_try!(value.borrow_ref::()); @@ -866,26 +866,30 @@ impl Vm { /// Implementation of getting a string index on an object-like type. fn try_tuple_like_index_get(target: &Value, index: usize) -> VmResult> { - let result = match vm_try!(target.borrow_ref_repr()) { - BorrowRefRepr::Inline(target) => match target { + let result = match vm_try!(target.as_ref()) { + ReprRef::Inline(target) => match target { Inline::Unit => Err(target.type_info()), _ => return VmResult::Ok(None), }, - BorrowRefRepr::Mutable(target) => match &*target { - Mutable::TupleStruct(tuple_struct) => match tuple_struct.data().get(index) { - Some(value) => Ok(value.clone()), - None => Err(target.type_info()), - }, - Mutable::Variant(variant) => match variant.data() { - VariantData::Tuple(tuple) => match tuple.get(index) { + ReprRef::Mutable(target) => { + let target = vm_try!(target.borrow_ref()); + + match &*target { + Mutable::TupleStruct(tuple_struct) => match tuple_struct.data().get(index) { Some(value) => Ok(value.clone()), None => Err(target.type_info()), }, + Mutable::Variant(variant) => match variant.data() { + VariantData::Tuple(tuple) => match tuple.get(index) { + Some(value) => Ok(value.clone()), + None => Err(target.type_info()), + }, + _ => return VmResult::Ok(None), + }, _ => return VmResult::Ok(None), - }, - _ => return VmResult::Ok(None), - }, - BorrowRefRepr::Any(target) => match target.type_hash() { + } + } + ReprRef::Any(target) => match target.type_hash() { Result::::HASH => { match ( index, @@ -943,8 +947,8 @@ impl Vm { target: &Value, index: usize, ) -> VmResult>> { - match vm_try!(target.as_ref_repr()) { - RefRepr::Mutable(value) => { + match vm_try!(target.as_ref()) { + ReprRef::Mutable(value) => { let mut unsupported = false; let result = BorrowMut::try_map(vm_try!(value.borrow_mut()), |kind| { @@ -975,7 +979,7 @@ impl Vm { }), } } - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { Result::::HASH => { let result = BorrowMut::try_map( vm_try!(value.borrow_mut::>()), @@ -1069,12 +1073,12 @@ impl Vm { target: &'a Value, field: &str, ) -> VmResult>> { - match vm_try!(target.as_ref_repr()) { - RefRepr::Inline(actual) => err(VmErrorKind::MissingField { + match vm_try!(target.as_ref()) { + ReprRef::Inline(actual) => err(VmErrorKind::MissingField { target: actual.type_info(), field: vm_try!(field.try_to_owned()), }), - RefRepr::Mutable(target) => { + ReprRef::Mutable(target) => { let target = vm_try!(target.borrow_mut()); let mut unsupported = false; @@ -1110,7 +1114,7 @@ impl Vm { }), } } - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { Object::HASH => { let object = vm_try!(value.borrow_mut::()); @@ -1130,12 +1134,12 @@ impl Vm { /// Implementation of getting a string index on an object-like type. fn try_tuple_like_index_set(target: &Value, index: usize, from: &Value) -> VmResult { - match vm_try!(target.as_ref_repr()) { - RefRepr::Inline(target) => match target { + match vm_try!(target.as_ref()) { + ReprRef::Inline(target) => match target { Inline::Unit => VmResult::Ok(false), _ => VmResult::Ok(false), }, - RefRepr::Mutable(target) => match &mut *vm_try!(target.borrow_mut()) { + ReprRef::Mutable(target) => match &mut *vm_try!(target.borrow_mut()) { Mutable::TupleStruct(tuple_struct) => { if let Some(target) = tuple_struct.get_mut(index) { target.clone_from(from); @@ -1156,7 +1160,7 @@ impl Vm { } _ => VmResult::Ok(false), }, - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { Result::::HASH => { let mut result = vm_try!(value.borrow_mut::>()); @@ -1215,11 +1219,11 @@ impl Vm { let index = vm_try!(self.unit.lookup_string(slot)); 'fallback: { - match vm_try!(target.as_ref_repr()) { - RefRepr::Inline(..) => { + match vm_try!(target.as_ref()) { + ReprRef::Inline(..) => { return VmResult::Ok(CallResult::Unsupported(target.clone())); } - RefRepr::Mutable(target) => match *vm_try!(target.borrow_ref()) { + ReprRef::Mutable(target) => match *vm_try!(target.borrow_ref()) { Mutable::Struct(ref typed_object) => { if let Some(value) = typed_object.get(index.as_str()) { vm_try!(out.store(&mut self.stack, || value.clone())); @@ -1240,7 +1244,7 @@ impl Vm { break 'fallback; } }, - RefRepr::Any(value) => match value.type_hash() { + ReprRef::Any(value) => match value.type_hash() { Object::HASH => { let object = vm_try!(value.borrow_ref::()); @@ -1262,12 +1266,12 @@ impl Vm { } fn try_object_slot_index_set(target: &Value, field: &str, value: &Value) -> VmResult { - match vm_try!(target.as_ref_repr()) { - RefRepr::Inline(target) => err(VmErrorKind::MissingField { + match vm_try!(target.as_ref()) { + ReprRef::Inline(target) => err(VmErrorKind::MissingField { target: target.type_info(), field: vm_try!(field.try_to_owned()), }), - RefRepr::Mutable(target) => { + ReprRef::Mutable(target) => { let mut target = vm_try!(target.borrow_mut()); match &mut *target { @@ -1295,7 +1299,7 @@ impl Vm { field: vm_try!(field.try_to_owned()), }) } - RefRepr::Any(target) => match target.type_hash() { + ReprRef::Any(target) => match target.type_hash() { Object::HASH => { let key = vm_try!(field.try_to_owned()); let mut object = vm_try!(target.borrow_mut::()); @@ -1311,15 +1315,13 @@ impl Vm { where F: FnOnce(&[Value]) -> O, { - let value = match vm_try!(value.borrow_ref_repr()) { - BorrowRefRepr::Inline(value) => match (ty, value) { + let value = match vm_try!(value.as_ref()) { + ReprRef::Inline(value) => match (ty, value) { (TypeCheck::Unit, Inline::Unit) => Some(f(&[])), _ => None, }, - BorrowRefRepr::Mutable(value) => match (ty, &*value) { - _ => None, - }, - BorrowRefRepr::Any(value) => match (ty, value.type_hash()) { + ReprRef::Mutable(..) => None, + ReprRef::Any(value) => match (ty, value.type_hash()) { (TypeCheck::Vec, runtime::Vec::HASH) => { let vec = vm_try!(value.borrow_ref::()); Some(f(&vec)) @@ -1340,7 +1342,7 @@ impl Vm { let b = vm_try!(self.stack.at(rhs)); let a = vm_try!(self.stack.at(lhs)); - let RefRepr::Inline(Inline::Type(ty)) = vm_try!(b.as_ref_repr()) else { + let ReprRef::Inline(Inline::Type(ty)) = vm_try!(b.as_ref()) else { return err(VmErrorKind::UnsupportedIs { value: vm_try!(a.type_info()), test_type: vm_try!(b.type_info()), @@ -1363,10 +1365,10 @@ impl Vm { }; } - let value = match vm_try!(a.as_ref_repr()) { - RefRepr::Inline(Inline::Unsigned(a)) => convert!(u64, *a), - RefRepr::Inline(Inline::Signed(a)) => convert!(i64, *a), - RefRepr::Inline(Inline::Float(a)) => convert!(f64, *a), + let value = match vm_try!(a.as_ref()) { + ReprRef::Inline(Inline::Unsigned(a)) => convert!(u64, *a), + ReprRef::Inline(Inline::Signed(a)) => convert!(i64, *a), + ReprRef::Inline(Inline::Float(a)) => convert!(f64, *a), value => { return err(VmErrorKind::UnsupportedAs { value: vm_try!(value.type_info()), @@ -1404,8 +1406,8 @@ impl Vm { let rhs = vm_try!(self.stack.at(rhs)); let lhs = vm_try!(self.stack.at(lhs)); - let inline = match (vm_try!(lhs.as_ref_repr()), vm_try!(rhs.as_ref_repr())) { - (RefRepr::Inline(lhs), RefRepr::Inline(rhs)) => match (lhs, rhs) { + let inline = match (vm_try!(lhs.as_ref()), vm_try!(rhs.as_ref())) { + (ReprRef::Inline(lhs), ReprRef::Inline(rhs)) => match (lhs, rhs) { (Inline::Bool(lhs), Inline::Bool(rhs)) => Inline::Bool(bool_op(*lhs, *rhs)), (lhs, rhs) => { return err(VmErrorKind::UnsupportedBinaryOperation { @@ -1545,23 +1547,23 @@ impl Vm { let fallback = match target_value!(self, target, guard, lhs, rhs) { TargetValue::Same(value) => { - match vm_try!(value.as_mut_repr()) { - MutRepr::Inline(Inline::Signed(value)) => { + match vm_try!(value.as_mut()) { + ReprMut::Inline(Inline::Signed(value)) => { let out = vm_try!(signed_op(*value, *value).ok_or_else(error)); *value = out; return VmResult::Ok(()); } - MutRepr::Inline(Inline::Unsigned(value)) => { + ReprMut::Inline(Inline::Unsigned(value)) => { let out = vm_try!(unsigned_op(*value, *value).ok_or_else(error)); *value = out; return VmResult::Ok(()); } - MutRepr::Inline(Inline::Float(value)) => { + ReprMut::Inline(Inline::Float(value)) => { let out = float_op(*value, *value); *value = out; return VmResult::Ok(()); } - MutRepr::Inline(value) => { + ReprMut::Inline(value) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: protocol.name, lhs: value.type_info(), @@ -1574,8 +1576,8 @@ impl Vm { TargetFallback::Value(value.clone(), value.clone()) } TargetValue::Pair(lhs, rhs) => { - match (vm_try!(lhs.as_mut_repr()), vm_try!(rhs.as_ref_repr())) { - (MutRepr::Inline(lhs), RefRepr::Inline(rhs)) => match (lhs, rhs) { + match (vm_try!(lhs.as_mut()), vm_try!(rhs.as_ref())) { + (ReprMut::Inline(lhs), ReprRef::Inline(rhs)) => match (lhs, rhs) { (Inline::Signed(lhs), rhs) => { let rhs = vm_try!(rhs.as_integer()); let out = vm_try!(signed_op(*lhs, rhs).ok_or_else(error)); @@ -1601,7 +1603,7 @@ impl Vm { }); } }, - (MutRepr::Inline(lhs), rhs) => { + (ReprMut::Inline(lhs), rhs) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: protocol.name, lhs: lhs.type_info(), @@ -1701,8 +1703,8 @@ impl Vm { let lhs = vm_try!(self.stack.at(lhs)); 'fallback: { - let inline = match (vm_try!(lhs.as_ref_repr()), vm_try!(rhs.as_ref_repr())) { - (RefRepr::Inline(lhs), RefRepr::Inline(rhs)) => match (lhs, rhs) { + let inline = match (vm_try!(lhs.as_ref()), vm_try!(rhs.as_ref())) { + (ReprRef::Inline(lhs), ReprRef::Inline(rhs)) => match (lhs, rhs) { (Inline::Unsigned(lhs), rhs) => { let rhs = vm_try!(rhs.as_integer()); Inline::Unsigned(vm_try!(unsigned_op(*lhs, rhs).ok_or_else(error))) @@ -1720,7 +1722,7 @@ impl Vm { }); } }, - (RefRepr::Inline(lhs), rhs) => { + (ReprRef::Inline(lhs), rhs) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: protocol.name, lhs: lhs.type_info(), @@ -1770,8 +1772,8 @@ impl Vm { let rhs = vm_try!(self.stack.at(rhs)); 'fallback: { - let inline = match (vm_try!(lhs.as_ref_repr()), vm_try!(rhs.as_ref_repr())) { - (RefRepr::Inline(lhs), RefRepr::Inline(rhs)) => match (lhs, rhs) { + let inline = match (vm_try!(lhs.as_ref()), vm_try!(rhs.as_ref())) { + (ReprRef::Inline(lhs), ReprRef::Inline(rhs)) => match (lhs, rhs) { (Inline::Unsigned(lhs), rhs) => { let rhs = vm_try!(rhs.as_integer()); Inline::Unsigned(unsigned_op(*lhs, rhs)) @@ -1789,7 +1791,7 @@ impl Vm { }); } }, - (RefRepr::Inline(lhs), rhs) => { + (ReprRef::Inline(lhs), rhs) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: protocol.name, lhs: lhs.type_info(), @@ -1837,23 +1839,23 @@ impl Vm { let fallback = match target_value!(self, target, guard, lhs, rhs) { TargetValue::Same(value) => { - match vm_try!(value.as_mut_repr()) { - MutRepr::Inline(Inline::Unsigned(value)) => { + match vm_try!(value.as_mut()) { + ReprMut::Inline(Inline::Unsigned(value)) => { let rhs = *value; unsigned_op(value, rhs); return VmResult::Ok(()); } - MutRepr::Inline(Inline::Signed(value)) => { + ReprMut::Inline(Inline::Signed(value)) => { let rhs = *value; signed_op(value, rhs); return VmResult::Ok(()); } - MutRepr::Inline(Inline::Bool(value)) => { + ReprMut::Inline(Inline::Bool(value)) => { let rhs = *value; bool_op(value, rhs); return VmResult::Ok(()); } - MutRepr::Inline(value) => { + ReprMut::Inline(value) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: protocol.name, lhs: value.type_info(), @@ -1866,8 +1868,8 @@ impl Vm { TargetFallback::Value(value.clone(), value.clone()) } TargetValue::Pair(lhs, rhs) => { - match (vm_try!(lhs.as_mut_repr()), vm_try!(rhs.as_ref_repr())) { - (MutRepr::Inline(lhs), RefRepr::Inline(rhs)) => match (lhs, rhs) { + match (vm_try!(lhs.as_mut()), vm_try!(rhs.as_ref())) { + (ReprMut::Inline(lhs), ReprRef::Inline(rhs)) => match (lhs, rhs) { (Inline::Unsigned(lhs), rhs) => { let rhs = vm_try!(rhs.as_integer()); unsigned_op(lhs, rhs); @@ -1890,7 +1892,7 @@ impl Vm { }); } }, - (MutRepr::Inline(lhs), rhs) => { + (ReprMut::Inline(lhs), rhs) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: protocol.name, lhs: lhs.type_info(), @@ -1922,7 +1924,7 @@ impl Vm { let inline = 'inline: { match vm_try!(self.stack.pair(lhs, rhs)) { Pair::Same(value) => { - if let MutRepr::Inline(lhs) = vm_try!(value.as_mut_repr()) { + if let ReprMut::Inline(lhs) = vm_try!(value.as_mut()) { match lhs { Inline::Unsigned(value) => { let shift = @@ -1950,8 +1952,8 @@ impl Vm { break 'fallback (value.clone(), value.clone()); } Pair::Pair(lhs, rhs) => { - match (vm_try!(lhs.as_mut_repr()), vm_try!(rhs.as_ref_repr())) { - (MutRepr::Inline(lhs), RefRepr::Inline(rhs)) => match (lhs, rhs) { + match (vm_try!(lhs.as_mut()), vm_try!(rhs.as_ref())) { + (ReprMut::Inline(lhs), ReprRef::Inline(rhs)) => match (lhs, rhs) { (Inline::Unsigned(lhs), rhs) => { let rhs = vm_try!(rhs.as_integer()); let value = vm_try!(unsigned_op(*lhs, rhs).ok_or_else(error)); @@ -1970,7 +1972,7 @@ impl Vm { }); } }, - (MutRepr::Inline(lhs), rhs) => { + (ReprMut::Inline(lhs), rhs) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: protocol.name, lhs: lhs.type_info(), @@ -2018,20 +2020,20 @@ impl Vm { let fallback = match target_value!(self, target, guard, lhs, rhs) { TargetValue::Same(value) => { - match vm_try!(value.as_mut_repr()) { - MutRepr::Inline(Inline::Unsigned(value)) => { + match vm_try!(value.as_mut()) { + ReprMut::Inline(Inline::Unsigned(value)) => { let shift = vm_try!(u32::try_from(*value).ok().ok_or_else(error)); let out = vm_try!(unsigned_op(*value, shift).ok_or_else(error)); *value = out; return VmResult::Ok(()); } - MutRepr::Inline(Inline::Signed(value)) => { + ReprMut::Inline(Inline::Signed(value)) => { let shift = vm_try!(u32::try_from(*value).ok().ok_or_else(error)); let out = vm_try!(signed_op(*value, shift).ok_or_else(error)); *value = out; return VmResult::Ok(()); } - MutRepr::Inline(value) => { + ReprMut::Inline(value) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: protocol.name, lhs: value.type_info(), @@ -2044,8 +2046,8 @@ impl Vm { TargetFallback::Value(value.clone(), value.clone()) } TargetValue::Pair(lhs, rhs) => { - match (vm_try!(lhs.as_mut_repr()), vm_try!(rhs.as_ref_repr())) { - (MutRepr::Inline(lhs), RefRepr::Inline(rhs)) => match (lhs, rhs) { + match (vm_try!(lhs.as_mut()), vm_try!(rhs.as_ref())) { + (ReprMut::Inline(lhs), ReprRef::Inline(rhs)) => match (lhs, rhs) { (Inline::Unsigned(lhs), rhs) => { let rhs = vm_try!(rhs.as_integer()); let out = vm_try!(unsigned_op(*lhs, rhs).ok_or_else(error)); @@ -2066,7 +2068,7 @@ impl Vm { }); } }, - (MutRepr::Inline(lhs), rhs) => { + (ReprMut::Inline(lhs), rhs) => { return err(VmErrorKind::UnsupportedBinaryOperation { op: protocol.name, lhs: lhs.type_info(), @@ -2256,8 +2258,8 @@ impl Vm { fn op_not(&mut self, operand: InstAddress, out: Output) -> VmResult<()> { let value = vm_try!(self.stack.at(operand)); - let value = match vm_try!(value.borrow_ref_repr()) { - BorrowRefRepr::Inline(value) => match value { + let value = match vm_try!(value.as_ref()) { + ReprRef::Inline(value) => match value { Inline::Bool(value) => Value::from(!value), Inline::Unsigned(value) => Value::from(!value), Inline::Signed(value) => Value::from(!value), @@ -2267,7 +2269,7 @@ impl Vm { } }, actual => { - let operand = actual.type_info(); + let operand = vm_try!(actual.type_info()); return err(VmErrorKind::UnsupportedUnaryOperation { op: "!", operand }); } }; @@ -2280,8 +2282,8 @@ impl Vm { fn op_neg(&mut self, addr: InstAddress, out: Output) -> VmResult<()> { let value = vm_try!(self.stack.at(addr)); - let value = match vm_try!(value.borrow_ref_repr()) { - BorrowRefRepr::Inline(value) => match value { + let value = match vm_try!(value.as_ref()) { + ReprRef::Inline(value) => match value { Inline::Float(value) => Value::from(-value), Inline::Signed(value) => Value::from(-value), actual => { @@ -2290,7 +2292,7 @@ impl Vm { } }, actual => { - let operand = actual.type_info(); + let operand = vm_try!(actual.type_info()); return err(VmErrorKind::UnsupportedUnaryOperation { op: "-", operand }); } }; @@ -2784,15 +2786,15 @@ impl Vm { let index = vm_try!(self.stack.at(index)); let target = vm_try!(self.stack.at(target)); - match vm_try!(index.as_ref_repr()) { - RefRepr::Inline(inline) => { + match vm_try!(index.as_ref()) { + ReprRef::Inline(inline) => { let index = vm_try!(inline.as_integer::()); if let Some(value) = vm_try!(Self::try_tuple_like_index_get(target, index)) { break 'store value; } } - RefRepr::Any(value) => { + ReprRef::Any(value) => { if let Some(index) = vm_try!(value.try_borrow_ref::()) { if let Some(value) = vm_try!(Self::try_object_like_index_get(target, index.as_str())) @@ -3138,7 +3140,7 @@ impl Vm { let value = { let value = vm_try!(self.stack.at(addr)); - if let RefRepr::Any(value) = vm_try!(value.as_ref_repr()) { + if let ReprRef::Any(value) = vm_try!(value.as_ref()) { match value.type_hash() { Result::::HASH => { let result = vm_try!(value.borrow_ref::>()); @@ -3307,8 +3309,8 @@ impl Vm { let value = vm_try!(self.stack.at(addr)); let is_match = 'out: { - match vm_try!(value.as_ref_repr()) { - RefRepr::Mutable(value) => match &*vm_try!(value.borrow_ref()) { + match vm_try!(value.as_ref()) { + ReprRef::Mutable(value) => match &*vm_try!(value.borrow_ref()) { Mutable::Variant(variant) => { let rtti = variant.rtti(); break 'out rtti.enum_hash == enum_hash && rtti.hash == variant_hash; @@ -3317,7 +3319,7 @@ impl Vm { break 'out false; } }, - RefRepr::Any(any) => match enum_hash { + ReprRef::Any(any) => match enum_hash { Result::::HASH => { let result = vm_try!(any.borrow_ref::>()); @@ -3372,15 +3374,13 @@ impl Vm { ) -> VmResult<()> { let value = vm_try!(self.stack.at(addr)); - let is_match = match vm_try!(value.borrow_ref_repr()) { - BorrowRefRepr::Inline(value) => match (type_check, value) { + let is_match = match vm_try!(value.as_ref()) { + ReprRef::Inline(value) => match (type_check, value) { (TypeCheck::Unit, Inline::Unit) => true, _ => false, }, - BorrowRefRepr::Mutable(value) => match (type_check, &*value) { - _ => false, - }, - BorrowRefRepr::Any(value) => match (type_check, value.type_hash()) { + ReprRef::Mutable(..) => false, + ReprRef::Any(value) => match (type_check, value.type_hash()) { (TypeCheck::Vec, runtime::Vec::HASH) => true, (TypeCheck::Tuple, runtime::OwnedTuple::HASH) => true, _ => false, @@ -3680,14 +3680,14 @@ impl Vm { return VmResult::Ok(None); } - match vm_try!(function.as_ref_repr()) { - RefRepr::Inline(value) => err(VmErrorKind::UnsupportedCallFn { + match vm_try!(function.as_ref()) { + ReprRef::Inline(value) => err(VmErrorKind::UnsupportedCallFn { actual: value.type_info(), }), - RefRepr::Mutable(value) => err(VmErrorKind::UnsupportedCallFn { + ReprRef::Mutable(value) => err(VmErrorKind::UnsupportedCallFn { actual: vm_try!(value.borrow_ref()).type_info(), }), - RefRepr::Any(value) => { + ReprRef::Any(value) => { let value = value.clone(); let f = vm_try!(value.borrow_ref::()); f.call_with_vm(self, addr, args, out) @@ -3699,8 +3699,8 @@ impl Vm { fn op_iter_next(&mut self, addr: InstAddress, jump: usize, out: Output) -> VmResult<()> { let value = vm_try!(self.stack.at(addr)); - let some = match vm_try!(value.as_ref_repr()) { - RefRepr::Any(value) => match value.type_hash() { + let some = match vm_try!(value.as_ref()) { + ReprRef::Any(value) => match value.type_hash() { Option::::HASH => { let option = vm_try!(value.borrow_ref::>()); diff --git a/crates/rune/src/tests.rs b/crates/rune/src/tests.rs index 734065fd1..d165aa647 100644 --- a/crates/rune/src/tests.rs +++ b/crates/rune/src/tests.rs @@ -17,7 +17,7 @@ pub(crate) mod prelude { pub(crate) use crate::parse; pub(crate) use crate::runtime::{ self, Bytes, Formatter, Function, InstAddress, MaybeTypeOf, Mutable, Object, Output, - OwnedRepr, OwnedTuple, Protocol, RawAnyGuard, Ref, Stack, Tuple, TupleStruct, TypeHash, + OwnedTuple, Protocol, RawAnyGuard, Ref, ReprOwned, Stack, Tuple, TupleStruct, TypeHash, TypeInfo, TypeOf, UnsafeToRef, Variant, VecTuple, VmErrorKind, VmResult, }; pub(crate) use crate::support::Result; diff --git a/crates/rune/src/tests/vm_function.rs b/crates/rune/src/tests/vm_function.rs index c93376aec..3bf06d34f 100644 --- a/crates/rune/src/tests/vm_function.rs +++ b/crates/rune/src/tests/vm_function.rs @@ -36,7 +36,7 @@ fn test_function() { let value: Value = function.call((1i64,)).unwrap(); assert!(matches!( value.take_repr().unwrap(), - OwnedRepr::Mutable(Mutable::Variant(..)) + ReprOwned::Mutable(Mutable::Variant(..)) )); // ptr to dynamic function. @@ -49,7 +49,7 @@ fn test_function() { let value: Value = function.call((1i64,)).unwrap(); assert!(matches!( value.take_repr().unwrap(), - OwnedRepr::Mutable(Mutable::TupleStruct(..)) + ReprOwned::Mutable(Mutable::TupleStruct(..)) )); // non-capturing closure == free function