Skip to content

Commit 919d71d

Browse files
committed
Unify handling of CallResult::Unsupported
1 parent 0956586 commit 919d71d

File tree

1 file changed

+59
-58
lines changed
  • crates/rune/src/runtime

1 file changed

+59
-58
lines changed

crates/rune/src/runtime/vm.rs

+59-58
Original file line numberDiff line numberDiff line change
@@ -568,19 +568,17 @@ impl Vm {
568568
(Value::Integer(lhs), Value::Integer(rhs)) => int_op(lhs, rhs),
569569
(Value::Float(lhs), Value::Float(rhs)) => float_op(lhs, rhs),
570570
(lhs, rhs) => {
571-
let ordering = match vm_try!(self.call_instance_fn(lhs, Protocol::CMP, (&rhs,))) {
572-
CallResult::Ok(()) => {
573-
vm_try!(<Ordering>::from_value(vm_try!(self.stack.pop())))
574-
}
575-
CallResult::Unsupported(lhs) => {
576-
return err(VmErrorKind::UnsupportedBinaryOperation {
577-
op,
578-
lhs: vm_try!(lhs.type_info()),
579-
rhs: vm_try!(rhs.type_info()),
580-
});
581-
}
582-
};
571+
if let CallResult::Unsupported(lhs) =
572+
vm_try!(self.call_instance_fn(lhs, Protocol::CMP, (&rhs,)))
573+
{
574+
return err(VmErrorKind::UnsupportedBinaryOperation {
575+
op,
576+
lhs: vm_try!(lhs.type_info()),
577+
rhs: vm_try!(rhs.type_info()),
578+
});
579+
}
583580

581+
let ordering = vm_try!(<Ordering>::from_value(vm_try!(self.stack.pop())));
584582
match_ordering(ordering)
585583
}
586584
};
@@ -983,7 +981,7 @@ impl Vm {
983981

984982
match vm_try!(self.call_field_fn(Protocol::SET, target, hash, (value,))) {
985983
CallResult::Ok(()) => {
986-
vm_try!(self.stack.pop());
984+
vm_try!(<()>::from_value(vm_try!(self.stack.pop())));
987985
CallResult::Ok(())
988986
}
989987
result => result,
@@ -1205,28 +1203,28 @@ impl Vm {
12051203
TargetValue::Fallback(fallback) => fallback,
12061204
};
12071205

1208-
self.target_fallback(fallback, protocol)
1206+
self.target_fallback_assign(fallback, protocol)
12091207
}
12101208

12111209
/// Execute a fallback operation.
1212-
fn target_fallback(
1210+
fn target_fallback_assign(
12131211
&mut self,
12141212
fallback: TargetFallback<'_>,
12151213
protocol: Protocol,
12161214
) -> VmResult<()> {
12171215
match fallback {
12181216
TargetFallback::Value(lhs, rhs) => {
1219-
match vm_try!(self.call_instance_fn(lhs, protocol, (&rhs,))) {
1220-
CallResult::Ok(()) => vm_try!(<()>::from_value(vm_try!(self.stack.pop()))),
1221-
CallResult::Unsupported(lhs) => {
1222-
return err(VmErrorKind::UnsupportedBinaryOperation {
1223-
op: protocol.name,
1224-
lhs: vm_try!(lhs.type_info()),
1225-
rhs: vm_try!(rhs.type_info()),
1226-
});
1227-
}
1217+
if let CallResult::Unsupported(lhs) =
1218+
vm_try!(self.call_instance_fn(lhs, protocol, (&rhs,)))
1219+
{
1220+
return err(VmErrorKind::UnsupportedBinaryOperation {
1221+
op: protocol.name,
1222+
lhs: vm_try!(lhs.type_info()),
1223+
rhs: vm_try!(rhs.type_info()),
1224+
});
12281225
};
12291226

1227+
vm_try!(<()>::from_value(vm_try!(self.stack.pop())));
12301228
VmResult::Ok(())
12311229
}
12321230
TargetFallback::Field(lhs, hash, rhs) => {
@@ -1287,14 +1285,14 @@ impl Vm {
12871285

12881286
if let CallResult::Unsupported(lhs) = vm_try!(self.call_instance_fn(lhs, protocol, (&rhs,)))
12891287
{
1290-
err(VmErrorKind::UnsupportedBinaryOperation {
1288+
return err(VmErrorKind::UnsupportedBinaryOperation {
12911289
op: protocol.name,
12921290
lhs: vm_try!(lhs.type_info()),
12931291
rhs: vm_try!(rhs.type_info()),
1294-
})
1295-
} else {
1296-
VmResult::Ok(())
1292+
});
12971293
}
1294+
1295+
VmResult::Ok(())
12981296
}
12991297

13001298
/// Internal impl of a numeric operation.
@@ -1318,14 +1316,14 @@ impl Vm {
13181316

13191317
if let CallResult::Unsupported(lhs) = vm_try!(self.call_instance_fn(lhs, protocol, (&rhs,)))
13201318
{
1321-
err(VmErrorKind::UnsupportedBinaryOperation {
1319+
return err(VmErrorKind::UnsupportedBinaryOperation {
13221320
op: protocol.name,
13231321
lhs: vm_try!(lhs.type_info()),
13241322
rhs: vm_try!(rhs.type_info()),
1325-
})
1326-
} else {
1327-
VmResult::Ok(())
1323+
});
13281324
}
1325+
1326+
VmResult::Ok(())
13291327
}
13301328

13311329
/// Internal impl of a numeric operation.
@@ -1354,14 +1352,14 @@ impl Vm {
13541352

13551353
if let CallResult::Unsupported(lhs) = vm_try!(self.call_instance_fn(lhs, protocol, (&rhs,)))
13561354
{
1357-
err(VmErrorKind::UnsupportedBinaryOperation {
1355+
return err(VmErrorKind::UnsupportedBinaryOperation {
13581356
op: protocol.name,
13591357
lhs: vm_try!(lhs.type_info()),
13601358
rhs: vm_try!(rhs.type_info()),
1361-
})
1362-
} else {
1363-
VmResult::Ok(())
1359+
});
13641360
}
1361+
1362+
VmResult::Ok(())
13651363
}
13661364

13671365
fn internal_infallible_bitwise_assign(
@@ -1384,7 +1382,7 @@ impl Vm {
13841382
TargetValue::Fallback(fallback) => fallback,
13851383
};
13861384

1387-
self.target_fallback(fallback, protocol)
1385+
self.target_fallback_assign(fallback, protocol)
13881386
}
13891387

13901388
fn internal_bitwise(
@@ -1409,14 +1407,14 @@ impl Vm {
14091407

14101408
if let CallResult::Unsupported(lhs) = vm_try!(self.call_instance_fn(lhs, protocol, (&rhs,)))
14111409
{
1412-
err(VmErrorKind::UnsupportedBinaryOperation {
1410+
return err(VmErrorKind::UnsupportedBinaryOperation {
14131411
op: protocol.name,
14141412
lhs: vm_try!(lhs.type_info()),
14151413
rhs: vm_try!(rhs.type_info()),
1416-
})
1417-
} else {
1418-
VmResult::Ok(())
1414+
});
14191415
}
1416+
1417+
VmResult::Ok(())
14201418
}
14211419

14221420
fn internal_bitwise_assign(
@@ -1441,7 +1439,7 @@ impl Vm {
14411439
TargetValue::Fallback(fallback) => fallback,
14421440
};
14431441

1444-
self.target_fallback(fallback, protocol)
1442+
self.target_fallback_assign(fallback, protocol)
14451443
}
14461444

14471445
#[cfg_attr(feature = "bench", inline(never))]
@@ -2005,17 +2003,17 @@ impl Vm {
20052003
if let CallResult::Unsupported(target) =
20062004
vm_try!(self.call_instance_fn(target, Protocol::INDEX_SET, (&index, &value)))
20072005
{
2008-
err(VmErrorKind::UnsupportedIndexSet {
2006+
return err(VmErrorKind::UnsupportedIndexSet {
20092007
target: vm_try!(target.type_info()),
20102008
index: vm_try!(index.type_info()),
20112009
value: vm_try!(value.type_info()),
2012-
})
2013-
} else {
2014-
// Calling index set should not produce a value on the stack, but all
2015-
// handler functions to produce a value. So pop it here.
2016-
vm_try!(<()>::from_value(vm_try!(self.stack.pop())));
2017-
VmResult::Ok(())
2010+
});
20182011
}
2012+
2013+
// Calling index set should not produce a value on the stack, but all
2014+
// handler functions to produce a value. So pop it here.
2015+
vm_try!(<()>::from_value(vm_try!(self.stack.pop())));
2016+
VmResult::Ok(())
20192017
}
20202018

20212019
#[inline]
@@ -2160,13 +2158,14 @@ impl Vm {
21602158
if let CallResult::Unsupported(target) =
21612159
vm_try!(self.call_instance_fn(target, Protocol::INDEX_GET, (&index,)))
21622160
{
2163-
err(VmErrorKind::UnsupportedIndexGet {
2161+
return err(VmErrorKind::UnsupportedIndexGet {
21642162
target: vm_try!(target.type_info()),
21652163
index: vm_try!(index.type_info()),
2166-
})
2167-
} else {
2168-
VmResult::Ok(())
2164+
});
21692165
}
2166+
2167+
// NB: Should leave a value on the stack.
2168+
VmResult::Ok(())
21702169
}
21712170

21722171
/// Perform an index get operation specialized for tuples.
@@ -2188,7 +2187,7 @@ impl Vm {
21882187
});
21892188
}
21902189

2191-
// NB: should leave a value on the stack.
2190+
// NB: Should leave a value on the stack.
21922191
VmResult::Ok(())
21932192
}
21942193

@@ -2228,6 +2227,7 @@ impl Vm {
22282227
});
22292228
}
22302229

2230+
// NB: Should leave a value on the stack.
22312231
VmResult::Ok(())
22322232
}
22332233

@@ -2268,12 +2268,12 @@ impl Vm {
22682268
if let CallResult::Unsupported(target) =
22692269
vm_try!(self.try_object_slot_index_set(target, string_slot, value))
22702270
{
2271-
err(VmErrorKind::UnsupportedObjectSlotIndexSet {
2271+
return err(VmErrorKind::UnsupportedObjectSlotIndexSet {
22722272
target: vm_try!(target.type_info()),
2273-
})
2274-
} else {
2275-
VmResult::Ok(())
2273+
});
22762274
}
2275+
2276+
VmResult::Ok(())
22772277
}
22782278

22792279
/// Perform a specialized index get operation on an object.
@@ -2479,6 +2479,7 @@ impl Vm {
24792479
actual: vm_try!(target.type_info()),
24802480
});
24812481
}
2482+
24822483
vm_try!(<Result<Value, Value>>::from_value(vm_try!(self
24832484
.stack
24842485
.pop())))

0 commit comments

Comments
 (0)