Skip to content

Commit

Permalink
rune: Move FromValue to RuntimeError
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Oct 29, 2024
1 parent 74a144d commit 571bbc0
Show file tree
Hide file tree
Showing 29 changed files with 325 additions and 320 deletions.
22 changes: 11 additions & 11 deletions crates/rune-macros/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ fn expand_enum_install_with(
) -> Result<(), ()> {
let Tokens {
protocol,
runtime_error,
to_value,
type_of,
vm_result,
Expand Down Expand Up @@ -422,7 +423,7 @@ fn expand_enum_install_with(
module.field_function(#protocol::GET, #field, |this: &Self| {
match this {
#(#matches,)*
_ => return #vm_result::__rune_macros__unsupported_object_field_get(<Self as #type_of>::type_info()),
_ => return #vm_result::err(#runtime_error::__rune_macros__unsupported_object_field_get(<Self as #type_of>::type_info())),
}
})?;
});
Expand All @@ -433,7 +434,7 @@ fn expand_enum_install_with(
module.index_function(#protocol::GET, #index, |this: &Self| {
match this {
#(#matches,)*
_ => return #vm_result::__rune_macros__unsupported_tuple_index_get(<Self as #type_of>::type_info(), #index),
_ => return #vm_result::err(#runtime_error::__rune_macros__unsupported_tuple_index_get(<Self as #type_of>::type_info(), #index)),
}
})?;
});
Expand Down Expand Up @@ -533,6 +534,8 @@ where
value,
vm_result,
vm_try,
runtime_error,
result,
..
} = &tokens;

Expand Down Expand Up @@ -711,9 +714,8 @@ where

Some(quote! {
impl #from_value for #ty {
fn from_value(value: Value) -> #vm_result<Self> {
let value = #vm_try!(#path(value));
#vm_result::Ok(value)
fn from_value(value: Value) -> #result<Self, #runtime_error> {
#path(value)
}
}
})
Expand Down Expand Up @@ -742,9 +744,8 @@ where
}

impl #from_value for #ref_<#ty> {
fn from_value(value: Value) -> #vm_result<Self> {
let value = #vm_try!(#path(value));
#vm_result::Ok(value)
fn from_value(value: #value) -> #result<Self, #runtime_error> {
#path(value)
}
}
})
Expand Down Expand Up @@ -773,9 +774,8 @@ where
}

impl #from_value for #mut_<#ty> {
fn from_value(value: Value) -> #vm_result<Self> {
let value = #vm_try!(#path(value));
#vm_result::Ok(value)
fn from_value(value: #value) -> #result<Self, #runtime_error> {
#path(value)
}
}
})
Expand Down
8 changes: 4 additions & 4 deletions crates/rune-macros/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ impl Context {
return Ok(());
}

return Err(syn::Error::new_spanned(
Err(syn::Error::new_spanned(
&meta.path,
"Unsupported field attribute",
));
))
});

if let Err(e) = result {
Expand Down Expand Up @@ -493,10 +493,10 @@ impl Context {
return Ok(());
}

return Err(syn::Error::new_spanned(
Err(syn::Error::new_spanned(
&meta.path,
"Unsupported type attribute",
));
))
});

if let Err(e) = result {
Expand Down
69 changes: 35 additions & 34 deletions crates/rune-macros/src/from_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ impl Expander {
value,
type_value,
from_value,
vm_result,
result,
tuple,
vm_try,
runtime_error,
..
} = &self.tokens;

let (expanded, expected) = match &st.fields {
syn::Fields::Unit => {
let expanded = quote! {
#type_value::Unit => {
#vm_result::Ok(Self)
#result::Ok(Self)
}
#type_value::EmptyStruct(..) => {
#vm_result::Ok(Self)
#result::Ok(Self)
}
};

Expand All @@ -46,13 +46,13 @@ impl Expander {
let expanded = quote! {
#type_value::Unit => {
let tuple = #tuple::new(&[]);
#vm_result::Ok(Self(#expanded))
#result::Ok(Self(#expanded))
}
#type_value::Tuple(tuple) => {
#vm_result::Ok(Self(#expanded))
#result::Ok(Self(#expanded))
}
#type_value::TupleStruct(tuple) => {
#vm_result::Ok(Self(#expanded))
#result::Ok(Self(#expanded))
}
};

Expand All @@ -63,10 +63,10 @@ impl Expander {

let expanded = quote! {
#type_value::Object(object) => {
#vm_result::Ok(Self { #expanded })
#result::Ok(Self { #expanded })
}
#type_value::Struct(object) => {
#vm_result::Ok(Self { #expanded })
#result::Ok(Self { #expanded })
}
};

Expand All @@ -77,11 +77,11 @@ impl Expander {
Ok(quote! {
#[automatically_derived]
impl #from_value for #ident {
fn from_value(value: #value) -> #vm_result<Self> {
match #vm_try!(#value::into_type_value(value)) {
fn from_value(value: #value) -> #result<Self, #runtime_error> {
match #value::into_type_value(value)? {
#expanded
actual => {
#vm_result::expected::<#expected>(#type_value::type_info(&actual))
#result::Err(#runtime_error::expected::<#expected>(#type_value::type_info(&actual)))
}
}
}
Expand All @@ -106,8 +106,8 @@ impl Expander {
from_value,
variant_data,
value,
vm_result,
vm_try,
result,
runtime_error,
..
} = &self.tokens;

Expand All @@ -118,37 +118,38 @@ impl Expander {
match &variant.fields {
syn::Fields::Unit => {
unit_matches.push(quote! {
#lit_str => #vm_result::Ok(Self::#ident)
#lit_str => #result::Ok(Self::#ident)
});
}
syn::Fields::Unnamed(named) => {
let expanded = self.expand_unnamed(named)?;

unnamed_matches.push(quote! {
#lit_str => #vm_result::Ok(Self::#ident ( #expanded ))
#lit_str => #result::Ok(Self::#ident ( #expanded ))
});
}
syn::Fields::Named(named) => {
let expanded = self.expand_named(named)?;

named_matches.push(quote! {
#lit_str => #vm_result::Ok(Self::#ident { #expanded })
#lit_str => #result::Ok(Self::#ident { #expanded })
});
}
}
}

let missing = quote! {
name => #vm_try!(#vm_result::__rune_macros__missing_variant(name))
name => {
return #result::Err(#runtime_error::__rune_macros__missing_variant(name)?);
}
};

let variant = quote! {
#type_value::Variant(variant) => {
let mut it = variant.rtti().item.iter();

let name = match it.next_back_str() {
Some(name) => name,
None => return #vm_result::__rune_macros__missing_variant_name(),
let Some(name) = it.next_back_str() else {
return #result::Err(#runtime_error::__rune_macros__missing_variant_name());
};

match variant.data() {
Expand All @@ -168,11 +169,11 @@ impl Expander {
Ok(quote! {
#[automatically_derived]
impl #from_value for #ident {
fn from_value(value: #value) -> #vm_result<Self> {
match #vm_try!(#value::into_type_value(value)) {
fn from_value(value: #value) -> #result<Self, #runtime_error> {
match #value::into_type_value(value)? {
#variant,
actual => {
#vm_result::__rune_macros__expected_variant(#type_value::type_info(&actual))
#result::Err(#runtime_error::__rune_macros__expected_variant(#type_value::type_info(&actual)))
}
}
}
Expand Down Expand Up @@ -200,10 +201,10 @@ impl Expander {

let Tokens {
from_value,
vm_result,
result,
type_name,
vm_try,
try_clone,
runtime_error,
..
} = &self.tokens;

Expand All @@ -213,11 +214,11 @@ impl Expander {
from_values.push(quote! {
match tuple.get(#index) {
Some(value) => {
let value = #vm_try!(#try_clone::try_clone(value));
#vm_try!(#from_value::from_value(value))
let value = #try_clone::try_clone(value)?;
#from_value::from_value(value)?
}
None => {
return #vm_result::__rune_macros__missing_tuple_index(#type_name::<Self>(), #index);
return #result::Err(#runtime_error::__rune_macros__missing_tuple_index(#type_name::<Self>(), #index));
}
}
});
Expand All @@ -238,24 +239,24 @@ impl Expander {

let Tokens {
from_value,
vm_result,
result,
runtime_error,
type_name,
vm_try,
..
} = &self.tokens;

from_values.push(quote_spanned! {
field.span() =>
#ident: match object.get(#name) {
Some(value) => #vm_try!(#from_value::from_value(value.clone())),
Some(value) => #from_value::from_value(value.clone())?,
None => {
return #vm_result::__rune_macros__missing_struct_field(#type_name::<Self>(), #name);
return #result::Err(#runtime_error::__rune_macros__missing_struct_field(#type_name::<Self>(), #name));
}
}
});
}

Ok(quote_spanned!(named.span() => #(#from_values),* ))
Ok(quote!(#(#from_values),*))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/compile/ir/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) fn expr(hir: &hir::Expr<'_>, c: &mut Ctxt<'_, '_>) -> compile::Result
));
};

let value = value.to_value(c.q.context).with_span(span)?;
let value = value.to_value_with(c.q.context).with_span(span)?;
ir::Ir::new(span, value)
}
hir::ExprKind::Variable(name) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/rune/src/compile/ir/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Interpreter<'_, '_> {
.alloc_item(base.extended(name.try_to_string()?)?)?;

if let Some(const_value) = self.q.consts.get(item) {
return Ok(const_value.to_value(self.q.context).with_span(span)?);
return Ok(const_value.to_value_with(self.q.context).with_span(span)?);
}

if let Some(meta) = self.q.query_meta(span, item, used)? {
Expand All @@ -118,7 +118,7 @@ impl Interpreter<'_, '_> {
));
};

return Ok(const_value.to_value(self.q.context).with_span(span)?);
return Ok(const_value.to_value_with(self.q.context).with_span(span)?);
}
_ => {
return Err(compile::Error::new(
Expand Down
18 changes: 9 additions & 9 deletions crates/rune/src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ macro_rules! from_value_ref {
}

impl $crate::runtime::FromValue for $crate::runtime::Ref<$ty> {
fn from_value(value: Value) -> $crate::runtime::VmResult<Self> {
let value = vm_try!(value.$into_ref());
$crate::runtime::VmResult::Ok(value)
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
let value = value.$into_ref()?;
Ok(value)
}
}

impl $crate::runtime::FromValue for $crate::runtime::Mut<$ty> {
fn from_value(value: Value) -> VmResult<Self> {
let value = vm_try!(value.$into_mut());
$crate::runtime::VmResult::Ok(value)
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
let value = value.$into_mut()?;
Ok(value)
}
}
};
Expand All @@ -135,9 +135,9 @@ macro_rules! from_value_ref {
macro_rules! from_value2 {
($ty:ty, $into_ref:ident, $into_mut:ident, $into:ident) => {
impl $crate::runtime::FromValue for $ty {
fn from_value(value: Value) -> $crate::runtime::VmResult<Self> {
let value = vm_try!(value.$into());
$crate::runtime::VmResult::Ok(value)
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
let value = value.$into()?;
Ok(value)
}
}

Expand Down
Loading

0 comments on commit 571bbc0

Please sign in to comment.