mirascript-vm 0.1.88

Native Rust virtual machine for MiraScript
Documentation
use super::*;
use crate::{FunctionName, MiraManageable, bytecode::Program};

pub(crate) struct ScriptFunction {
    pub(crate) execution: ExecutionId,
    pub(crate) program: Rc<Program>,
    pub(crate) function: usize,
    pub(crate) frame: FrameId,
    pub(crate) name: FunctionName,
}

impl MiraFunction for ScriptFunction {
    fn call(&self, runtime: &mut Runtime, args: &[MiraValue]) -> Result<MiraManageable> {
        if self.execution != runtime.execution {
            return Err(MiraError::runtime(RuntimeErrorKind::ExecutionEnded));
        }
        let definition = &self.program.functions[self.function];
        let frame = runtime.create_frame(definition.register_count, Some(self.frame));
        if definition.variadic {
            let fixed = definition.arg_count.saturating_sub(1);
            for index in 0..fixed {
                runtime.write_register(
                    frame,
                    RegisterId::new(index + 1),
                    args.get(index).copied().unwrap_or(MiraValue::Nil),
                );
            }
            let rest = args
                .iter()
                .skip(fixed)
                .copied()
                .map(operations::into_element)
                .collect::<Vec<_>>();
            if definition.arg_count > 0 {
                let rest = runtime.insert(rest)?;
                runtime.write_register(frame, RegisterId::new(definition.arg_count), rest);
            }
        } else {
            for index in 0..definition.arg_count {
                runtime.write_register(
                    frame,
                    RegisterId::new(index + 1),
                    args.get(index).copied().unwrap_or(MiraValue::Nil),
                );
            }
        }
        match runtime.execute_block(&definition.body, frame)? {
            Flow::Return(value) => Ok(value.into()),
            Flow::Continue => Ok(MiraValue::nil().into()),
            Flow::Break | Flow::LoopContinue => {
                Err(MiraError::runtime(RuntimeErrorKind::InvalidControlFlow {
                    context: "function",
                }))
            }
        }
    }

    fn name(&self) -> FunctionName {
        self.name.clone()
    }
}

impl Runtime {
    pub(super) fn call_registers(
        &mut self,
        target: MiraValue,
        registers: &[usize],
        spreads: &[usize],
        frame: FrameId,
    ) -> Result<MiraValue> {
        if spreads.is_empty() {
            let arguments = registers
                .iter()
                .map(|register| self.read_register(frame, *register))
                .collect::<Result<Vec<_>>>()?;
            return self.call(target, &arguments);
        }

        let mut arguments = Vec::with_capacity(registers.len());
        for (index, register) in registers.iter().enumerate() {
            let value = self.read_register(frame, *register)?;
            if spreads.contains(&index) {
                arguments.extend(
                    operations::array_spread(self, value)?
                        .into_iter()
                        .map(operations::into_element),
                );
            } else {
                arguments.push(value);
            }
        }
        self.call(target, &arguments)
    }

    pub(super) fn get_global_slot(&self, slot: usize) -> Result<MiraValue> {
        let (name, std_slot) = &self.active_program().global_names[slot];

        self.globals.get_hint(name, *std_slot).ok_or_else(|| {
            MiraError::runtime(RuntimeErrorKind::UndefinedGlobal { name: name.clone() })
        })
    }

    pub(super) fn get_global_name(&self, key: &str) -> Result<MiraValue> {
        self.globals.get(key).ok_or_else(|| {
            MiraError::runtime(RuntimeErrorKind::UndefinedGlobal {
                name: key.to_owned(),
            })
        })
    }

    pub(super) fn has_value(&mut self, value: MiraValue, key: MiraValue) -> Result<bool> {
        operations::has(self, value, key, None)
    }

    pub(super) fn get_value(&mut self, value: MiraValue, key: MiraValue) -> Result<MiraValue> {
        operations::get_value(self, value, key, None)
    }

    /// Call a function value owned by this Runtime.
    pub fn call(&mut self, function: MiraValue, args: &[MiraValue]) -> Result<MiraValue> {
        self.checkpoint()?;
        if self.call_stack.depth() >= self.options.max_call_depth as usize {
            return Err(MiraError::runtime(RuntimeErrorKind::MaxCallDepth {
                max: self.options.max_call_depth,
            }));
        }
        let MiraValue::Function(handle) = function else {
            return Err(MiraError::runtime(RuntimeErrorKind::NotCallable {
                actual: function.value_type(),
            }));
        };

        let callable = self.get_function_dyn(handle)?;
        self.call_stack.push(callable.name());
        let result = callable
            .call(self, args)
            .and_then(|value| self.insert(value))
            .and_then(|value| {
                self.checkpoint()?;
                Ok(value)
            });
        self.call_stack.pop();
        result
    }

    pub(crate) fn checkpoint(&mut self) -> Result<()> {
        let remaining = self.checkpoint_remaining;
        if remaining > 1 {
            self.checkpoint_remaining = remaining - 1;
            return Ok(());
        }
        self.checkpoint_remaining = self.options.checkpoint_interval.max(1);
        if self.started.elapsed() >= self.options.timeout {
            return Err(MiraError::runtime(RuntimeErrorKind::Timeout));
        }
        Ok(())
    }
}