pub fn call_method<T>(
      &mut self,
      receiver: Value,
      signature: impl MethodSignature,
      arguments: impl IntoIterator<Item = Value>,
   ) -> Result<T, Error>
   where
      T: TryFromValue,
   {
      let method_id = signature.to_method_id(&mut self.env)?;
      // Unwrapping here is fine because `to_method_id` ensures that a method with a given ID
      // exists.
      let signature = self.env.get_function_signature(method_id.0).unwrap();
      let stack: Vec<_> =
         Some(receiver).into_iter().chain(arguments).map(|x| x.to_raw(&mut self.gc)).collect();
      let argument_count = u8::try_from(stack.len()).map_err(|_| Error::TooManyArguments)?;
      if Some(argument_count as u16) != signature.arity {
         return Err(Error::ArgumentCount {
            // Unwrapping here is fine because signatures of methods created by `to_method_id`
            // always have a static arity.
            expected: signature.arity.unwrap() as usize - 1,
            got: argument_count as usize - 1,
         });
      }
      let mut chunk = Chunk::new(Rc::from("(call)"));
      chunk.emit((
         Opcode::CallMethod,
         Opr24::pack((method_id.0, argument_count)),
      ));
      chunk.emit(Opcode::Halt);
      let chunk = Rc::new(chunk);
      let fiber = Fiber {
         engine: self,
         inner: vm::Fiber::new(chunk, stack),
      };
      fiber.trampoline()
   }
/// Calls a method on a receiver with the given arguments.
///
/// Note that if you're calling a method often, it's cheaper to precompute the method signature
/// into a [`MethodId`] by using the [`method_id`][`Self::method_id`] function, compared to
/// passing a name+arity pair every single time.
///
/// # Errors
///
/// - [`Error::Runtime`] - if a runtime error occurs - `function` isn't callable or an error is
///   raised during execution
/// - [`Error::TooManyArguments`] - if more arguments than the implementation can support is
///   passed to the function
/// - [`Error::TooManyMethods`] - if too many methods with different signatures exist at the same
///   time
/// - [`Error::ArgumentCount`] - if `arguments.count()` does not match the argument count of the
///   signature
Calls a method on a receiver with the given arguments.
Calls a method on a receiver with the given arguments .
pub fn call_method<T>(
      &mut self,
      receiver: Value,
      signature: impl MethodSignature,
      arguments: impl IntoIterator<Item = Value>,
   ) -> Result<T, Error>
   where
      T: TryFromValue,
   {
      let method_id = signature.to_method_id(&mut self.env)?;
      // Unwrapping here is fine because `to_method_id` ensures that a method with a given ID
      // exists.
      let signature = self.env.get_function_signature(method_id.0).unwrap();
      let stack: Vec<_> =
         Some(receiver).into_iter().chain(arguments).map(|x| x.to_raw(&mut self.gc)).collect();
      let argument_count = u8::try_from(stack.len()).map_err(|_| Error::TooManyArguments)?;
      if Some(argument_count as u16) != signature.arity {
         return Err(Error::ArgumentCount {
            // Unwrapping here is fine because signatures of methods created by `to_method_id`
            // always have a static arity.
            expected: signature.arity.unwrap() as usize - 1,
            got: argument_count as usize - 1,
         });
      }
      let mut chunk = Chunk::new(Rc::from("(call)"));
      chunk.emit((
         Opcode::CallMethod,
         Opr24::pack((method_id.0, argument_count)),
      ));
      chunk.emit(Opcode::Halt);
      let chunk = Rc::new(chunk);
      let fiber = Fiber {
         engine: self,
         inner: vm::Fiber::new(chunk, stack),
      };
      fiber.trampoline()
   }
pub fn call_method < T > ( & mut self , receiver : Value , signature : impl MethodSignature , arguments : impl IntoIterator < Item = Value > , ) -> Result < T , Error > where T : TryFromValue , { let method_id = signature . to_method_id ( & mut self . env ) ? ; // Unwrapping here is fine because `to_method_id` ensures that a method with a given ID // exists. let signature = self . env . get_function_signature ( method_id . 0 ) . unwrap ( ) ; let stack : Vec < _ > = Some ( receiver ) . into_iter ( ) . chain ( arguments ) . map ( | x | x . to_raw ( & mut self . gc ) ) . collect ( ) ; let argument_count = u8 :: try_from ( stack . len ( ) ) . map_err ( | _ | Error :: TooManyArguments ) ? ; if Some ( argument_count as u16 ) != signature . arity { return Err ( Error :: ArgumentCount { // Unwrapping here is fine because signatures of methods created by `to_method_id` // always have a static arity. expected : signature . arity . unwrap ( ) as usize - 1 , got : argument_count as usize - 1 , } ) ; } let mut chunk = Chunk :: new ( Rc :: from ( " " ) ) ; chunk . emit ( ( Opcode :: CallMethod , Opr24 :: pack ( ( method_id . 0 , argument_count ) ) , ) ) ; chunk . emit ( Opcode :: Halt ) ; let chunk = Rc :: new ( chunk ) ; let fiber = Fiber { engine : self , inner : vm :: Fiber :: new ( chunk , stack ) , } ; fiber . trampoline ( ) }
Calls a method on a receiver with the given arguments.
Calls a method on a receiver with the given arguments .
&mut self
receiver
Value
signature
impl MethodSignature
arguments
impl IntoIterator<Item = Value>
errors
[`Error::Runtime`] - if a runtime error occurs - `function` isn't callable or an error is
raised during execution
[`Error::TooManyArguments`] - if more arguments than the implementation can support is
passed to the function
[`Error::TooManyMethods`] - if too many methods with different signatures exist at the same
time
[`Error::ArgumentCount`] - if `arguments.count()` does not match the argument count of the
signature
[ ` Error :: Runtime ` ] - if a runtime error occurs - ` function ` isn ' t callable or an error is raised during execution [ ` Error :: TooManyArguments ` ] - if more arguments than the implementation can support is passed to the function [ ` Error :: TooManyMethods ` ] - if too many methods with different signatures exist at the same time [ ` Error :: ArgumentCount ` ] - if ` arguments . count () ` does not match the argument count of the signature