Skip to content

Commit

Permalink
rune: Simplify the empty sentinel value
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 4, 2024
1 parent d2dd714 commit aa45a38
Show file tree
Hide file tree
Showing 26 changed files with 393 additions and 504 deletions.
6 changes: 3 additions & 3 deletions crates/rune/src/cli/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{ReprRef, Value, Vm, VmError, VmResult};
use crate::runtime::{Repr, Value, Vm, VmError, VmResult};
use crate::{Diagnostics, Hash, Item, ItemBuf, Source, Sources, TypeHash, Unit};

mod cli {
Expand Down Expand Up @@ -534,8 +534,8 @@ impl TestCase {
capture_io.drain_into(&mut self.output)?;

self.outcome = match result {
VmResult::Ok(v) => match v.as_ref()? {
ReprRef::Any(value) => match value.type_hash() {
VmResult::Ok(v) => match v.as_ref() {
Repr::Any(value) => match value.type_hash() {
Result::<Value, Value>::HASH => {
let result = value.borrow_ref::<Result<Value, Value>>()?;

Expand Down
4 changes: 2 additions & 2 deletions crates/rune/src/compile/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ impl IrAssignOp {
where
S: Copy + Spanned,
{
if let Some(Inline::Signed(target)) = target.as_inline_mut().with_span(spanned)? {
if let Some(Inline::Signed(operand)) = operand.as_inline().with_span(spanned)? {
if let Some(Inline::Signed(target)) = target.as_inline_mut() {
if let Some(Inline::Signed(operand)) = operand.as_inline() {
return self.assign_int(spanned, target, *operand);
}
}
Expand Down
53 changes: 21 additions & 32 deletions crates/rune/src/compile/ir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{Inline, Object, OwnedTuple, ReprRef, Value};
use crate::runtime::{Inline, Object, OwnedTuple, Repr, Value};
use crate::TypeHash;

/// The outcome of a constant evaluation.
Expand Down Expand Up @@ -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.as_ref().with_span(ir)?;
let b = b.as_ref().with_span(ir)?;
let a = a.as_ref();
let b = b.as_ref();

match (a, b) {
(ReprRef::Inline(a), ReprRef::Inline(b)) => {
(Repr::Inline(a), Repr::Inline(b)) => {
let out = 'out: {
match (a, b) {
(Inline::Signed(a), Inline::Signed(b)) => match ir.op {
Expand Down Expand Up @@ -140,7 +140,7 @@ fn eval_ir_binary(

return Ok(Value::from(out));
}
(ReprRef::Any(a), ReprRef::Any(b)) => {
(Repr::Any(a), Repr::Any(b)) => {
let value = 'out: {
if let (String::HASH, String::HASH) = (a.type_hash(), b.type_hash()) {
let a = a.borrow_ref::<String>().with_span(span)?;
Expand Down Expand Up @@ -353,34 +353,23 @@ fn eval_ir_template(
}
ir::IrTemplateComponent::Ir(ir) => {
let const_value = eval_ir(ir, interp, used)?;
let value = const_value.as_ref().with_span(ir)?;

match value {
ReprRef::Inline(value) => match value {
Inline::Signed(integer) => {
write!(buf, "{integer}")?;
}
Inline::Float(float) => {
let mut buffer = ryu::Buffer::new();
buf.try_push_str(buffer.format(*float))?;
}
Inline::Bool(b) => {
write!(buf, "{b}")?;
}
_ => {
return Err(EvalOutcome::not_const(ir));
}
},
ReprRef::Any(value) => match value.type_hash() {
String::HASH => {
let s = value.borrow_ref::<String>().with_span(ir)?;
buf.try_push_str(&s)?;
}
_ => {
return Err(EvalOutcome::not_const(ir));
}
},
ReprRef::Dynamic(..) => {
match const_value.as_ref() {
Repr::Inline(Inline::Signed(integer)) => {
write!(buf, "{integer}")?;
}
Repr::Inline(Inline::Float(float)) => {
let mut buffer = ryu::Buffer::new();
buf.try_push_str(buffer.format(*float))?;
}
Repr::Inline(Inline::Bool(b)) => {
write!(buf, "{b}")?;
}
Repr::Any(value) if value.type_hash() == String::HASH => {
let s = value.borrow_ref::<String>().with_span(ir)?;
buf.try_push_str(&s)?;
}
_ => {
return Err(EvalOutcome::not_const(ir));
}
}
Expand Down
65 changes: 25 additions & 40 deletions crates/rune/src/compile/ir/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ReprRef, Value};
use crate::runtime::{self, ConstValue, Object, OwnedTuple, Repr, Value};
use crate::TypeHash;

/// The interpreter that executed [Ir][crate::ir::Ir].
Expand Down Expand Up @@ -200,45 +200,30 @@ impl ir::Scopes {
}
ir::IrTargetKind::Index(target, index) => {
let value = self.get_target(target)?;
let target = value.as_ref().with_span(ir_target)?;

match target {
ReprRef::Inline(value) => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
match value.type_hash() {
runtime::Vec::HASH => {
let vec = value.borrow_ref::<runtime::Vec>().with_span(ir_target)?;

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

if let Some(value) = tuple.get(*index) {
return Ok(value.clone());
}
}
_ => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
}
ReprRef::Any(value) => match value.type_hash() {
runtime::Vec::HASH => {
let vec = value.borrow_ref::<runtime::Vec>().with_span(ir_target)?;

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

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

Err(compile::Error::new(
Expand Down Expand Up @@ -270,20 +255,20 @@ impl ir::Scopes {
ir::IrTargetKind::Index(target, index) => {
let target = self.get_target(target)?;

match target.as_ref().with_span(ir_target)? {
ReprRef::Inline(value) => {
match target.as_ref() {
Repr::Inline(value) => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
}
ReprRef::Dynamic(value) => {
Repr::Dynamic(value) => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
}
ReprRef::Any(any) => match any.type_hash() {
Repr::Any(any) => match any.type_hash() {
runtime::Vec::HASH => {
let mut vec = any.borrow_mut::<runtime::Vec>().with_span(ir_target)?;

Expand Down Expand Up @@ -345,12 +330,12 @@ impl ir::Scopes {
ir::IrTargetKind::Index(target, index) => {
let current = self.get_target(target)?;

match current.as_ref().with_span(ir_target)? {
ReprRef::Dynamic(value) => Err(compile::Error::expected_type::<OwnedTuple>(
match current.as_ref() {
Repr::Dynamic(value) => Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
)),
ReprRef::Any(value) => match value.type_hash() {
Repr::Any(value) => match value.type_hash() {
runtime::Vec::HASH => {
let mut vec =
value.borrow_mut::<runtime::Vec>().with_span(ir_target)?;
Expand Down
6 changes: 6 additions & 0 deletions crates/rune/src/compile/v1/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,12 @@ fn const_<'a, 'hir>(

match *value.as_kind() {
ConstValueKind::Inline(value) => match value {
Inline::Empty => {
return Err(compile::Error::msg(
span,
"Empty inline constant value is not supported",
));
}
Inline::Unit => {
cx.asm.push(Inst::unit(out), span)?;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rune/src/macros/format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ fn expand_format_spec<'a>(

let value = cx.eval(expr)?;

let number = match value.as_inline().with_span(expr)? {
let number = match value.as_inline() {
Some(Inline::Signed(n)) => n.to_usize(),
_ => None,
};
Expand All @@ -589,7 +589,7 @@ fn expand_format_spec<'a>(
to be a positive number in use as precision, \
but got `{}`",
count,
value.type_info().with_span(span)?
value.type_info()
),
));
};
Expand Down
4 changes: 2 additions & 2 deletions crates/rune/src/modules/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub fn module() -> Result<Module, ContextError> {
/// ```
#[rune::function(free, path = Type::of_val)]
#[inline]
fn type_of_val(value: Value) -> VmResult<Type> {
VmResult::Ok(Type::new(vm_try!(value.type_hash())))
fn type_of_val(value: Value) -> Type {
Type::new(value.type_hash())
}

/// Formatting a type.
Expand Down
18 changes: 9 additions & 9 deletions crates/rune/src/modules/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate as rune;
use crate::alloc::Vec;
use crate::runtime::{self, Future, Inline, ReprRef, SelectFuture, Value, VmErrorKind, VmResult};
use crate::runtime::{self, Future, Inline, Repr, SelectFuture, Value, VmErrorKind, VmResult};
use crate::{ContextError, Module, TypeHash};

/// Asynchronous computations.
Expand All @@ -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()) {
ReprRef::Inline(value) => {
match value.as_ref() {
Repr::Inline(value) => {
return VmResult::err([
VmErrorKind::expected::<Future>(value.type_info()),
VmErrorKind::bad_argument(index),
]);
}
ReprRef::Dynamic(value) => {
Repr::Dynamic(value) => {
return VmResult::err([
VmErrorKind::expected::<Future>(value.type_info()),
VmErrorKind::bad_argument(index),
]);
}
ReprRef::Any(value) => match value.type_hash() {
Repr::Any(value) => match value.type_hash() {
Future::HASH => {
let future = vm_try!(Value::from(value.clone()).into_future());
futures.push(SelectFuture::new(index, future));
Expand Down Expand Up @@ -98,19 +98,19 @@ where
/// ```
#[rune::function(keep)]
async fn join(value: Value) -> VmResult<Value> {
match vm_try!(value.as_ref()) {
ReprRef::Inline(value) => match value {
match value.as_ref() {
Repr::Inline(value) => match value {
Inline::Unit => VmResult::Ok(Value::unit()),
value => VmResult::err([
VmErrorKind::bad_argument(0),
VmErrorKind::expected::<runtime::Vec>(value.type_info()),
]),
},
ReprRef::Dynamic(value) => VmResult::err([
Repr::Dynamic(value) => VmResult::err([
VmErrorKind::bad_argument(0),
VmErrorKind::expected::<runtime::Vec>(value.type_info()),
]),
ReprRef::Any(value) => match value.type_hash() {
Repr::Any(value) => match value.type_hash() {
runtime::Vec::HASH => {
let vec = vm_try!(value.borrow_ref::<runtime::Vec>());
let result = try_join_impl(vec.iter(), vec.len(), |vec| {
Expand Down
14 changes: 7 additions & 7 deletions crates/rune/src/modules/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::modules::collections::VecDeque;
use crate::modules::collections::{HashMap, HashSet};
use crate::runtime::range::RangeIter;
use crate::runtime::{
FromValue, Function, Inline, InstAddress, Object, Output, OwnedTuple, Protocol, ReprRef,
TypeHash, Value, Vec, VmErrorKind, VmResult,
FromValue, Function, Inline, InstAddress, Object, Output, OwnedTuple, Protocol, Repr, TypeHash,
Value, Vec, VmErrorKind, VmResult,
};
use crate::shared::Caller;
use crate::{Any, ContextError, Module, Params};
Expand Down Expand Up @@ -473,17 +473,17 @@ pub fn module() -> Result<Module, ContextError> {
let mut string = String::new();

while let Some(value) = vm_try!(next.call((iter.clone(),))) {
match vm_try!(value.as_ref()) {
ReprRef::Inline(Inline::Char(c)) => {
match value.as_ref() {
Repr::Inline(Inline::Char(c)) => {
vm_try!(string.try_push(*c));
}
ReprRef::Inline(value) => {
Repr::Inline(value) => {
return VmResult::expected::<String>(value.type_info());
}
ReprRef::Dynamic(value) => {
Repr::Dynamic(value) => {
return VmResult::expected::<String>(value.type_info());
}
ReprRef::Any(value) => match value.type_hash() {
Repr::Any(value) => match value.type_hash() {
String::HASH => {
let s = vm_try!(value.borrow_ref::<String>());
vm_try!(string.try_push_str(&s));
Expand Down
Loading

0 comments on commit aa45a38

Please sign in to comment.