2022-08-29 17:07:58 +02:00
|
|
|
//! This module encapsulates some logic for upvalue handling, which is
|
|
|
|
//! relevant to both thunks (delayed computations for lazy-evaluation)
|
|
|
|
//! as well as closures (lambdas that capture variables from the
|
|
|
|
//! surrounding scope).
|
|
|
|
|
2022-09-06 21:08:25 +02:00
|
|
|
use std::{
|
|
|
|
cell::{Ref, RefMut},
|
|
|
|
ops::Index,
|
|
|
|
};
|
2022-08-29 17:07:58 +02:00
|
|
|
|
|
|
|
use crate::{opcode::UpvalueIdx, Value};
|
|
|
|
|
2022-09-06 21:08:25 +02:00
|
|
|
/// Structure for carrying upvalues inside of thunks & closures. The
|
|
|
|
/// implementation of this struct encapsulates the logic for capturing
|
|
|
|
/// and accessing upvalues.
|
2022-09-18 20:04:40 +02:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2022-09-06 21:08:25 +02:00
|
|
|
pub struct Upvalues {
|
|
|
|
upvalues: Vec<Value>,
|
2022-09-06 22:13:48 +02:00
|
|
|
with_stack: Option<Vec<Value>>,
|
2022-09-06 21:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Upvalues {
|
|
|
|
pub fn with_capacity(count: usize) -> Self {
|
|
|
|
Upvalues {
|
|
|
|
upvalues: Vec::with_capacity(count),
|
2022-09-06 22:13:48 +02:00
|
|
|
with_stack: None,
|
2022-09-06 21:08:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Push an upvalue at the end of the upvalue list.
|
|
|
|
pub fn push(&mut self, value: Value) {
|
|
|
|
self.upvalues.push(value);
|
|
|
|
}
|
2022-09-06 22:13:48 +02:00
|
|
|
|
|
|
|
/// Set the captured with stack.
|
|
|
|
pub fn set_with_stack(&mut self, with_stack: Vec<Value>) {
|
|
|
|
self.with_stack = Some(with_stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_stack(&self) -> Option<&Vec<Value>> {
|
|
|
|
self.with_stack.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_stack_len(&self) -> usize {
|
|
|
|
match &self.with_stack {
|
|
|
|
None => 0,
|
|
|
|
Some(stack) => stack.len(),
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 21:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<UpvalueIdx> for Upvalues {
|
|
|
|
type Output = Value;
|
|
|
|
|
|
|
|
fn index(&self, index: UpvalueIdx) -> &Self::Output {
|
|
|
|
&self.upvalues[index.0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 17:07:58 +02:00
|
|
|
/// `UpvalueCarrier` is implemented by all types that carry upvalues.
|
|
|
|
pub trait UpvalueCarrier {
|
|
|
|
fn upvalue_count(&self) -> usize;
|
|
|
|
|
|
|
|
/// Read-only accessor for the stored upvalues.
|
2022-09-06 21:08:25 +02:00
|
|
|
fn upvalues(&self) -> Ref<'_, Upvalues>;
|
2022-08-29 17:07:58 +02:00
|
|
|
|
|
|
|
/// Mutable accessor for stored upvalues.
|
2022-09-06 21:08:25 +02:00
|
|
|
fn upvalues_mut(&self) -> RefMut<'_, Upvalues>;
|
2022-08-29 17:07:58 +02:00
|
|
|
|
|
|
|
/// Read an upvalue at the given index.
|
|
|
|
fn upvalue(&self, idx: UpvalueIdx) -> Ref<'_, Value> {
|
2022-09-06 21:08:25 +02:00
|
|
|
Ref::map(self.upvalues(), |v| &v.upvalues[idx.0])
|
2022-08-29 17:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve deferred upvalues from the provided stack slice,
|
|
|
|
/// mutating them in the internal upvalue slots.
|
|
|
|
fn resolve_deferred_upvalues(&self, stack: &[Value]) {
|
2022-09-06 21:08:25 +02:00
|
|
|
for upvalue in self.upvalues_mut().upvalues.iter_mut() {
|
2022-08-29 17:07:58 +02:00
|
|
|
if let Value::DeferredUpvalue(idx) = upvalue {
|
|
|
|
*upvalue = stack[idx.0].clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|