Skip to content

Commit

Permalink
rune: Move ToValue to RuntimeError
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Oct 29, 2024
1 parent 571bbc0 commit b02e5e2
Show file tree
Hide file tree
Showing 23 changed files with 237 additions and 196 deletions.
9 changes: 5 additions & 4 deletions crates/rune-macros/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ fn expand_enum_install_with(
to_value,
type_of,
vm_result,
vm_try,
..
} = tokens;

Expand Down Expand Up @@ -337,9 +338,9 @@ fn expand_enum_install_with(
let fields = field_fns.entry(f_name).or_default();

let value = if attrs.copy {
quote!(#to_value::to_value(*#f_ident))
quote!(#vm_result::Ok(#vm_try!(#to_value::to_value(*#f_ident))))
} else {
quote!(#to_value::to_value(#f_ident.clone()))
quote!(#vm_result::Ok(#vm_try!(#to_value::to_value(#f_ident.clone()))))
};

fields.push(quote!(#ident::#variant_ident { #f_ident, .. } => #value));
Expand All @@ -365,9 +366,9 @@ fn expand_enum_install_with(
let n = syn::LitInt::new(&n.to_string(), span);

let value = if attrs.copy {
quote!(#to_value::to_value(*value))
quote!(#vm_result::Ok(#vm_try!(#to_value::to_value(*value))))
} else {
quote!(#to_value::to_value(value.clone()))
quote!(#vm_result::Ok(#vm_try!(#to_value::to_value(value.clone()))))
};

fields.push(quote!(#ident::#variant_ident { #n: value, .. } => #value));
Expand Down
6 changes: 3 additions & 3 deletions crates/rune-macros/src/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Derive {
});

from_value_fields.push(quote! {
<#ty as #from_value>::from_value(#value::take(#var)).into_result()?
<#ty as #from_value>::from_value(#value::take(#var))?
});
}

Expand All @@ -98,7 +98,7 @@ impl Derive {
}

body = quote! {
#const_value::for_struct(<Self as #type_hash_t>::HASH, [#(#fields),*])?
#const_value::for_struct(<Self as #type_hash_t>::HASH, [#(#fields),*])
};
}
syn::Data::Enum(..) => {
Expand Down Expand Up @@ -161,7 +161,7 @@ where
impl #to_const_value_t for #ident {
#[inline]
fn to_const_value(self) -> #result<#const_value, #runtime_error> {
#result::Ok(#body)
#body
}

#[inline]
Expand Down
23 changes: 11 additions & 12 deletions crates/rune-macros/src/to_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ impl Expander {

let Tokens {
value,
vm_result,
to_value,
result,
runtime_error,
..
} = &self.tokens;

Ok(quote! {
#[automatically_derived]
impl #to_value for #ident {
fn to_value(self) -> #vm_result<#value> {
fn to_value(self) -> #result<#value, #runtime_error> {
#inner
}
}
Expand Down Expand Up @@ -58,8 +59,7 @@ impl Expander {
to_value,
value,
owned_tuple,
vm_result,
vm_try,
result,
try_from,
vec,
..
Expand All @@ -68,16 +68,16 @@ impl Expander {
for (index, f) in unnamed.unnamed.iter().enumerate() {
let _ = self.cx.field_attrs(&f.attrs)?;
let index = syn::Index::from(index);
to_values.push(quote!(#vm_try!(#vec::try_push(&mut tuple, #vm_try!(#to_value::to_value(self.#index))))));
to_values.push(quote!(#vec::try_push(&mut tuple, #to_value::to_value(self.#index)?)?));
}

let cap = unnamed.unnamed.len();

Ok(quote! {
let mut tuple = #vm_try!(#vec::try_with_capacity(#cap));
let mut tuple = #vec::try_with_capacity(#cap)?;
#(#to_values;)*
let tuple = #vm_try!(<#owned_tuple as #try_from<_>>::try_from(tuple));
#vm_result::Ok(#vm_try!(<#value as #try_from<_>>::try_from(tuple)))
let tuple = <#owned_tuple as #try_from<_>>::try_from(tuple)?;
#result::Ok(<#value as #try_from<_>>::try_from(tuple)?)
})
}

Expand All @@ -87,8 +87,7 @@ impl Expander {
to_value,
value,
object,
vm_result,
vm_try,
result,
string,
try_from,
..
Expand All @@ -103,14 +102,14 @@ impl Expander {
let name = &syn::LitStr::new(&ident.to_string(), ident.span());

to_values.push(quote! {
object.insert(#vm_try!(<#string as #try_from<_>>::try_from(#name)), #vm_try!(#to_value::to_value(self.#ident)))
object.insert(<#string as #try_from<_>>::try_from(#name)?, #to_value::to_value(self.#ident)?)
});
}

Ok(quote! {
let mut object = <#object>::new();
#(#to_values;)*
#vm_result::Ok(#vm_try!(<#value as #try_from<_>>::try_from(object)))
#result::Ok(<#value as #try_from<_>>::try_from(object)?)
})
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/rune/src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::alloc;
use crate::compile::meta;
use crate::hash::Hash;
use crate::runtime::{
self, FromValue, InstAddress, MaybeTypeOf, Memory, Output, ToValue, TypeHash, TypeInfo, TypeOf,
UnsafeToMut, UnsafeToRef, Value, VmErrorKind, VmResult,
self, FromValue, InstAddress, MaybeTypeOf, Memory, Output, ToReturn, TypeHash, TypeInfo,
TypeOf, UnsafeToMut, UnsafeToRef, Value, VmErrorKind, VmResult,
};

// Expand to function variable bindings.
Expand Down Expand Up @@ -242,7 +242,7 @@ macro_rules! impl_function_traits {
impl<T, U, $($ty,)*> Function<($($place,)*), Plain> for T
where
T: 'static + Send + Sync + Fn($($($mut)* $ty),*) -> U,
U: ToValue,
U: ToReturn,
$($ty: $($trait)*,)*
{
type Return = U;
Expand All @@ -260,7 +260,7 @@ macro_rules! impl_function_traits {
let ret = self($($var.0),*);
$(drop($var.1);)*

let value = vm_try!(ToValue::to_value(ret));
let value = vm_try!(ToReturn::to_return(ret));
vm_try!(out.store(memory, value));
VmResult::Ok(())
}
Expand All @@ -270,7 +270,7 @@ macro_rules! impl_function_traits {
where
T: 'static + Send + Sync + Fn($($($mut)* $ty),*) -> U,
U: 'static + Future,
U::Output: ToValue,
U::Output: ToReturn,
$($ty: $($trait)*,)*
{
type Return = U::Output;
Expand All @@ -292,7 +292,7 @@ macro_rules! impl_function_traits {

let ret = vm_try!(runtime::Future::new(async move {
let output = fut.await;
VmResult::Ok(vm_try!(output.to_value()))
VmResult::Ok(vm_try!(ToReturn::to_return(output)))
}));

let value = vm_try!(Value::try_from(ret));
Expand Down
24 changes: 15 additions & 9 deletions crates/rune/src/modules/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,21 @@ async fn join(value: Value) -> VmResult<Value> {
]),
},
BorrowRefRepr::Mutable(value) => match *value {
Mutable::Tuple(ref tuple) => VmResult::Ok(vm_try!(
try_join_impl(tuple.iter(), tuple.len(), |vec| VmResult::Ok(vm_try!(
Value::tuple(vec)
)))
.await
)),
Mutable::Vec(ref vec) => VmResult::Ok(vm_try!(
try_join_impl(vec.iter(), vec.len(), Value::vec).await
)),
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))
}
Mutable::Vec(ref vec) => {
let result = try_join_impl(vec.iter(), vec.len(), |vec| {
VmResult::Ok(vm_try!(Value::vec(vec)))
})
.await;
VmResult::Ok(vm_try!(result))
}
ref value => VmResult::err([
VmErrorKind::bad_argument(0),
VmErrorKind::expected::<crate::runtime::Vec>(value.type_info()),
Expand Down
9 changes: 3 additions & 6 deletions crates/rune/src/runtime/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::alloc::prelude::*;
use crate::alloc::{self, HashMap};
use crate::runtime::{
self, BorrowRefRepr, Bytes, FromValue, Inline, Mutable, Object, OwnedTuple, RawStr, ToValue,
TypeInfo, Value, VmErrorKind, VmResult,
TypeInfo, Value, VmErrorKind,
};
use crate::{Hash, TypeHash};

Expand Down Expand Up @@ -295,11 +295,8 @@ impl FromValue for ConstValue {

impl ToValue for ConstValue {
#[inline]
fn to_value(self) -> VmResult<Value> {
VmResult::Ok(vm_try!(ConstValue::to_value_with(
&self,
&EmptyConstContext
)))
fn to_value(self) -> Result<Value, RuntimeError> {
ConstValue::to_value_with(&self, &EmptyConstContext)
}
}

Expand Down
10 changes: 4 additions & 6 deletions crates/rune/src/runtime/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,13 @@ where
C: ToValue,
{
#[inline]
fn to_value(self) -> VmResult<Value> {
fn to_value(self) -> Result<Value, RuntimeError> {
let value = match self {
ops::ControlFlow::Continue(value) => {
ControlFlow::Continue(vm_try!(ToValue::to_value(value)))
}
ops::ControlFlow::Break(value) => ControlFlow::Break(vm_try!(ToValue::to_value(value))),
ops::ControlFlow::Continue(value) => ControlFlow::Continue(C::to_value(value)?),
ops::ControlFlow::Break(value) => ControlFlow::Break(B::to_value(value)?),
};

VmResult::Ok(vm_try!(Value::try_from(value)))
Ok(Value::try_from(value)?)
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/rune/src/runtime/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ impl Future {
poll: |future, cx| unsafe {
match Pin::new_unchecked(&mut *future.cast::<T>()).poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(VmResult::Ok(result)) => Poll::Ready(result.to_value()),
Poll::Ready(VmResult::Ok(result)) => match result.to_value() {
Ok(value) => Poll::Ready(VmResult::Ok(value)),
Err(err) => Poll::Ready(VmResult::Err(err.into())),
},
Poll::Ready(VmResult::Err(err)) => Poll::Ready(VmResult::Err(err)),
}
},
Expand Down
22 changes: 11 additions & 11 deletions crates/rune/src/runtime/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::iter;

use crate as rune;
use crate::alloc::clone::TryClone;
use crate::runtime::{GeneratorState, Value, Vm, VmErrorKind, VmExecution, VmResult};
use crate::runtime::{GeneratorState, Value, Vm, VmError, VmErrorKind, VmExecution, VmResult};
use crate::Any;

/// The return value of a function producing a generator.
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Generator<Vm> {
}

impl IntoIterator for Generator<Vm> {
type Item = VmResult<Value>;
type Item = Result<Value, VmError>;
type IntoIter = Iter;

#[inline]
Expand All @@ -129,21 +129,21 @@ pub struct Iter {

impl Iter {
#[rune::function(instance, keep, protocol = NEXT)]
pub(crate) fn next(&mut self) -> Option<VmResult<Value>> {
match self.generator.next() {
VmResult::Ok(Some(value)) => Some(VmResult::Ok(value)),
VmResult::Ok(None) => None,
VmResult::Err(error) => Some(VmResult::Err(error)),
}
pub(crate) fn next(&mut self) -> VmResult<Option<Value>> {
self.generator.next()
}
}

impl iter::Iterator for Iter {
type Item = VmResult<Value>;
type Item = Result<Value, VmError>;

#[inline]
fn next(&mut self) -> Option<VmResult<Value>> {
Iter::next(self)
fn next(&mut self) -> Option<Result<Value, VmError>> {
match Iter::next(self) {
VmResult::Ok(Some(value)) => Some(Ok(value)),
VmResult::Ok(None) => None,
VmResult::Err(error) => Some(Err(error)),
}
}
}

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 @@ -149,7 +149,7 @@ mod stream;
pub use self::stream::Stream;

mod to_value;
pub use self::to_value::{to_value, ToValue, UnsafeToValue};
pub use self::to_value::{to_value, ToReturn, ToValue, UnsafeToValue};

mod tuple;
pub use self::tuple::{OwnedTuple, Tuple};
Expand Down
Loading

0 comments on commit b02e5e2

Please sign in to comment.