Skip to content

Commit 0956586

Browse files
committed
Improve call index support
1 parent abfba1c commit 0956586

File tree

1 file changed

+21
-34
lines changed
  • crates/rune/src/runtime

1 file changed

+21
-34
lines changed

crates/rune/src/runtime/vm.rs

+21-34
Original file line numberDiff line numberDiff line change
@@ -472,14 +472,8 @@ impl Vm {
472472
H: ToTypeHash,
473473
A: GuardedArgs,
474474
{
475-
let count = args.count();
476-
let full_count = args.count() + 1;
475+
let count = args.count().wrapping_add(1);
477476
let type_hash = vm_try!(target.type_hash());
478-
self.stack.push(target);
479-
480-
// Safety: We hold onto the guard for the duration of this call.
481-
let _guard = unsafe { vm_try!(args.unsafe_into_stack(&mut self.stack)) };
482-
483477
let hash = Hash::associated_function(type_hash, hash.to_type_hash());
484478

485479
if let Some(UnitFn::Offset {
@@ -488,19 +482,22 @@ impl Vm {
488482
args: expected,
489483
}) = self.unit.function(hash)
490484
{
491-
vm_try!(check_args(full_count, expected));
492-
vm_try!(self.call_offset_fn(offset, call, full_count));
485+
self.stack.push(target);
486+
// Safety: We hold onto the guard for the duration of this call.
487+
let _guard = unsafe { vm_try!(args.unsafe_into_stack(&mut self.stack)) };
488+
vm_try!(check_args(count, expected));
489+
vm_try!(self.call_offset_fn(offset, call, count));
493490
return VmResult::Ok(CallResult::Ok(()));
494491
}
495492

496493
if let Some(handler) = self.context.function(hash) {
497-
vm_try!(handler(&mut self.stack, full_count));
494+
self.stack.push(target);
495+
// Safety: We hold onto the guard for the duration of this call.
496+
let _guard = unsafe { vm_try!(args.unsafe_into_stack(&mut self.stack)) };
497+
vm_try!(handler(&mut self.stack, count));
498498
return VmResult::Ok(CallResult::Ok(()));
499499
}
500500

501-
// Restore the stack!
502-
vm_try!(self.stack.popn(count));
503-
let target = vm_try!(self.stack.pop());
504501
VmResult::Ok(CallResult::Unsupported(target))
505502
}
506503

@@ -515,23 +512,18 @@ impl Vm {
515512
) -> VmResult<CallResult<()>>
516513
where
517514
N: IntoHash,
518-
A: Args,
515+
A: GuardedArgs,
519516
{
520-
let count = args.count();
521-
let full_count = count + 1;
517+
let count = args.count().wrapping_add(1);
522518
let hash = Hash::field_function(protocol, vm_try!(target.type_hash()), name);
523519

524-
self.stack.push(target);
525-
vm_try!(args.into_stack(&mut self.stack));
526-
527520
if let Some(handler) = self.context.function(hash) {
528-
vm_try!(handler(&mut self.stack, full_count));
521+
self.stack.push(target);
522+
let _guard = unsafe { vm_try!(args.unsafe_into_stack(&mut self.stack)) };
523+
vm_try!(handler(&mut self.stack, count));
529524
return VmResult::Ok(CallResult::Ok(()));
530525
}
531526

532-
// Restore the stack!
533-
vm_try!(self.stack.popn(count));
534-
let target = vm_try!(self.stack.pop());
535527
VmResult::Ok(CallResult::Unsupported(target))
536528
}
537529

@@ -545,23 +537,18 @@ impl Vm {
545537
args: A,
546538
) -> VmResult<CallResult<()>>
547539
where
548-
A: Args,
540+
A: GuardedArgs,
549541
{
550-
let count = args.count();
551-
let full_count = count + 1;
542+
let count = args.count().wrapping_add(1);
552543
let hash = Hash::index_function(protocol, vm_try!(target.type_hash()), Hash::index(index));
553544

554-
self.stack.push(target);
555-
vm_try!(args.into_stack(&mut self.stack));
556-
557545
if let Some(handler) = self.context.function(hash) {
558-
vm_try!(handler(&mut self.stack, full_count));
546+
self.stack.push(target);
547+
let _guard = unsafe { vm_try!(args.unsafe_into_stack(&mut self.stack)) };
548+
vm_try!(handler(&mut self.stack, count));
559549
return VmResult::Ok(CallResult::Ok(()));
560550
}
561551

562-
// Restore the stack!
563-
vm_try!(self.stack.popn(count));
564-
let target = vm_try!(self.stack.pop());
565552
VmResult::Ok(CallResult::Unsupported(target))
566553
}
567554

@@ -1257,7 +1244,7 @@ impl Vm {
12571244
}
12581245
TargetFallback::Index(lhs, index, rhs) => {
12591246
if let CallResult::Unsupported(lhs) =
1260-
vm_try!(self.call_index_fn(protocol, lhs.clone(), index, (rhs,)))
1247+
vm_try!(self.call_index_fn(protocol, lhs.clone(), index, (&rhs,)))
12611248
{
12621249
return err(VmErrorKind::UnsupportedTupleIndexGet {
12631250
target: vm_try!(lhs.type_info()),

0 commit comments

Comments
 (0)